Skip to content

Commit

Permalink
Merge pull request #71 from NJUPT-SAST/dev-felmose
Browse files Browse the repository at this point in the history
Dev felmose
  • Loading branch information
feellmoose authored Nov 14, 2023
2 parents bb560d6 + 8e8d4d6 commit f63e5ac
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/main/java/sast/evento/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ public class LoginController {
@PostMapping("/login/link")
@DefaultActionState(ActionState.PUBLIC)
public Map<String, Object> linkLogin(@RequestParam String code,
@RequestParam Integer type) {
@RequestParam Integer type,
@RequestParam(required = false,defaultValue = "false") Boolean update) {
if (code == null || code.isEmpty()) {
throw new LocalRunTimeException(ErrorEnum.PARAM_ERROR, "invalid code");
}
try {
return loginService.linkLogin(code,type);
return loginService.linkLogin(code,type,update);
} catch (SastLinkException e) {
throw new LocalRunTimeException(ErrorEnum.SAST_LINK_SERVICE_ERROR, e.getMessage());
}
Expand Down
63 changes: 54 additions & 9 deletions src/main/java/sast/evento/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sast.evento.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -11,11 +12,21 @@
import sast.evento.annotation.OperateLog;
import sast.evento.common.enums.ActionState;
import sast.evento.common.enums.ErrorEnum;
import sast.evento.config.ActionRegister;
import sast.evento.entitiy.Event;
import sast.evento.exception.LocalRunTimeException;
import sast.evento.interceptor.HttpInterceptor;
import sast.evento.mapper.EventMapper;
import sast.evento.model.Action;
import sast.evento.model.UserModel;
import sast.evento.service.PermissionService;
import sast.sastlink.sdk.service.SastLinkService;

import java.util.List;
import java.util.Objects;

@RestController
@ConditionalOnProperty(prefix = "test",name = "challenge")
@ConditionalOnProperty(prefix = "test", name = "challenge")
@RequestMapping("/test")
public class TestController {
@Value("${test.challenge}")
Expand All @@ -25,22 +36,56 @@ public class TestController {

@Resource
private SastLinkService sastLinkService;
@Resource
private PermissionService permissionService;
@Resource
private EventMapper eventMapper;


@OperateLog("获取个人信息")
@OperateLog("link登录")
@DefaultActionState(ActionState.PUBLIC)
@PostMapping("/linklogin")
public String linkLogin(@RequestParam String email,@RequestParam String password){
if(challenge.isEmpty()||method.isEmpty()){
public String linkLogin(@RequestParam String email,
@RequestParam String password) {
if (challenge.isEmpty() || method.isEmpty()) {
throw new LocalRunTimeException(ErrorEnum.COMMON_ERROR);
}
try {
String token = sastLinkService.login(email,password);
return sastLinkService.authorize(token,challenge,method);
}catch (Exception e){
throw new LocalRunTimeException(ErrorEnum.SAST_LINK_SERVICE_ERROR,e.getMessage());
String token = sastLinkService.login(email, password);
return sastLinkService.authorize(token, challenge, method);
} catch (Exception e) {
throw new LocalRunTimeException(ErrorEnum.SAST_LINK_SERVICE_ERROR, e.getMessage());
}

}

@OperateLog("addPermissionForTest")
@DefaultActionState(ActionState.LOGIN)
@PostMapping("/permission")
public String permission(@RequestParam ActionState type,
@RequestParam(required = false) Integer eventId,
@RequestParam(required = false) List<String> permission) {
UserModel userModel = HttpInterceptor.userHolder.get();
final List<String> adminPermission = ActionRegister.actionName2action.values().stream()
.filter(a -> a.getActionState().equals(ActionState.ADMIN))
.map(Action::getMethodName).toList();
final List<String> managerPermission = ActionRegister.actionName2action.values().stream()
.filter(a -> a.getActionState().equals(ActionState.MANAGER))
.map(Action::getMethodName).toList();
switch (type) {
case ADMIN ->
permissionService.addAdmin(Objects.requireNonNullElse(permission, adminPermission), userModel.getId());
case MANAGER -> {
if (eventMapper.exists(Wrappers.lambdaQuery(Event.class).eq(Event::getId, eventId))) {
permissionService.addManager(eventId, Objects.requireNonNullElse(permission, managerPermission), userModel.getId());
} else {
return "no such event";
}
}
default -> {
return "error type";
}
}
return "ok";
}

}
5 changes: 3 additions & 2 deletions src/main/java/sast/evento/entitiy/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sast.evento.entitiy;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.GsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand All @@ -22,7 +23,7 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "user")
@TableName(value = "user",autoResultMap = true)
public class User {
@TableId(value = "id",type = IdType.ASSIGN_ID)
private String id;
Expand All @@ -48,7 +49,7 @@ public class User {
@JsonAlias("bio")
@TableField("bio")
private String biography;
@TableField(typeHandler = JacksonTypeHandler.class)
@TableField(value = "link",typeHandler = JacksonTypeHandler.class)
private List<String> link;

public User(UserInfo userInfo){
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/sast/evento/service/LoginService.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package sast.evento.service;

import sast.evento.common.enums.Platform;
import sast.sastlink.sdk.exception.SastLinkException;

import java.util.Map;

public interface LoginService {
Map<String, Object> linkLogin(String code, Integer type) throws SastLinkException;
Map<String, Object> linkLogin(String code, Integer type, Boolean updateUser) throws SastLinkException;

Map<String, Object> wxLogin(String code);

void logout(String userId) throws SastLinkException;

void checkLoginState(String userId, String token);

Map<String,Object> bindStudentOnWechat(String userId, String studentId, Boolean force);
Map<String, Object> bindStudentOnWechat(String userId, String studentId, Boolean force);

Map<String, Object> getKeyForLogin(String studentId);

Map<String, Object> getLoginTicket(String studentId,String ticket);
Map<String, Object> getLoginTicket(String studentId, String ticket);

void checkTicket(String studentId, String ticket);

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/sast/evento/service/impl/LoginServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public class LoginServiceImpl implements LoginService {
*/

@Override
public Map<String, Object> linkLogin(String code, Integer type) throws SastLinkException {
@Transactional
public Map<String, Object> linkLogin(String code, Integer type, Boolean updateUser) throws SastLinkException {
SastLinkService service = switch (type) {
case 0 -> sastLinkService;
case 1 -> sastLinkServiceWeb;
Expand All @@ -73,28 +74,34 @@ public Map<String, Object> linkLogin(String code, Integer type) throws SastLinkE
UserInfo userInfo = service.userInfo(accessTokenData.getAccess_token());
User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
.eq(User::getLinkId, userInfo.getUserId()));
//查看学号是否冲突,若冲突则绑定至wx账号
if (user == null) {
//查看有对应学号的账号
//若无对应用户,查看是否存在有对应学号的账号
User origin = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
.eq(User::getStudentId, userInfo.getUserId()));
if (origin != null) {
//学号已经存在,直接绑定到该学号账号上
origin.setLinkId(userInfo.getUserId());
origin.setStudentId(userInfo.getUserId());
userMapper.updateById(origin);
user = origin;
} else {
//学号也不存在,创建新的账号
user = new User();
setCommonInfo(user, userInfo);
user.setLinkId(userInfo.getUserId());
user.setStudentId(userInfo.getUserId());//link默认直接绑定学号
userMapper.insert(user);
}
} else if(updateUser){
setCommonInfo(user,userInfo);
userMapper.updateById(user);
}
String token = addTokenInCache(user, false);
return Map.of("token", token, "userInfo", user);
}

@Override
@Transactional
public Map<String, Object> wxLogin(String code) {
//没有学号冲突的风险
JsCodeSessionResponse jsCodeSessionResponse = wxService.login(code);
Expand Down

0 comments on commit f63e5ac

Please sign in to comment.