Commit a73a94fa by gdj

增加简易权限。

parent bda15104
package com.dji.sample.common.util; package com.dji.sample.common.util;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.manage.model.enums.UserTypeEnum;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import static com.dji.sample.component.AuthInterceptor.PARAM_TOKEN;
/** /**
* @author guan * @author guan
...@@ -30,4 +39,35 @@ public class SecurityUtils { ...@@ -30,4 +39,35 @@ public class SecurityUtils {
return passwordEncoder.matches(rawPassword, encodedPassword); return passwordEncoder.matches(rawPassword, encodedPassword);
} }
/**
* 判断当前用户是否admin
* @return
*/
public static boolean isAdmin() {
DecodedJWT jwt = JwtUtil.verifyToken(getToken());
CustomClaim customClaim = new CustomClaim(jwt.getClaims());
Integer userType = customClaim.getUserType();
if (userType == null) {
return false;
}
return userType == UserTypeEnum.WEB.getVal();
}
public static boolean isNotAdmin() {
return !isAdmin();
}
/**
* 获取token
* @return
*/
public static String getToken() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return request.getHeader(PARAM_TOKEN);
}
} }
package com.dji.sample.manage.model.dto; package com.dji.sample.manage.model.dto;
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -10,6 +11,9 @@ import lombok.NonNull; ...@@ -10,6 +11,9 @@ import lombok.NonNull;
@NoArgsConstructor @NoArgsConstructor
public class UserLoginDTO { public class UserLoginDTO {
@JsonAlias("workspaceId")
private String workspaceId;
@NonNull @NonNull
private String username; private String username;
......
...@@ -38,6 +38,8 @@ import java.util.Optional; ...@@ -38,6 +38,8 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.dji.sample.common.util.SecurityUtils.isNotAdmin;
@Service @Service
@Transactional @Transactional
public class UserServiceImpl implements IUserService { public class UserServiceImpl implements IUserService {
...@@ -76,8 +78,11 @@ public class UserServiceImpl implements IUserService { ...@@ -76,8 +78,11 @@ public class UserServiceImpl implements IUserService {
.setCode(HttpStatus.UNAUTHORIZED.value()) .setCode(HttpStatus.UNAUTHORIZED.value())
.setMessage("invalid username"); .setMessage("invalid username");
} }
if (flag.intValue() != userEntity.getUserType().intValue()) { // 修改逻辑 跳过web判定
return HttpResultResponse.error("The account type does not match."); if (flag != UserTypeEnum.WEB.getVal()) {
if (flag.intValue() != userEntity.getUserType().intValue()) {
return HttpResultResponse.error("The account type does not match.");
}
} }
// 密码加密验证 // 密码加密验证
// if (!password.equals(userEntity.getPassword())) { // if (!password.equals(userEntity.getPassword())) {
...@@ -169,6 +174,9 @@ public class UserServiceImpl implements IUserService { ...@@ -169,6 +174,9 @@ public class UserServiceImpl implements IUserService {
@Override @Override
public Boolean deleteUser(String workspaceId, String userId) { public Boolean deleteUser(String workspaceId, String userId) {
if (isNotAdmin()) {
throw new RuntimeException("The current user is not an admin and has no permissions");
}
LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserEntity::getWorkspaceId, workspaceId); queryWrapper.eq(UserEntity::getWorkspaceId, workspaceId);
queryWrapper.eq(UserEntity::getUserId, userId); queryWrapper.eq(UserEntity::getUserId, userId);
...@@ -192,12 +200,21 @@ public class UserServiceImpl implements IUserService { ...@@ -192,12 +200,21 @@ public class UserServiceImpl implements IUserService {
String username = user.getUsername(); String username = user.getUsername();
LambdaQueryWrapper<UserEntity> userQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<UserEntity> userQueryWrapper = new LambdaQueryWrapper<>();
userQueryWrapper.eq(UserEntity::getUsername, username); userQueryWrapper.eq(UserEntity::getUsername, username);
userQueryWrapper.eq(UserEntity::getWorkspaceId, workspaceId);
List<UserEntity> nameUserList = this.mapper.selectList(userQueryWrapper); List<UserEntity> nameUserList = this.mapper.selectList(userQueryWrapper);
if (!CollectionUtils.isEmpty(nameUserList)) { if (!CollectionUtils.isEmpty(nameUserList)) {
throw new RuntimeException("the username is already existed"); throw new RuntimeException("the username is already existed");
} }
UserEntity userEntity = new UserEntity(); UserEntity userEntity = new UserEntity();
// 普通用户不能创建管理员
if (user.getUserType() == UserTypeEnum.WEB.getVal()) {
if (isNotAdmin()) {
throw new RuntimeException("The current user is not an admin and has no permissions");
}
}
userEntity.setUserType(user.getUserType() != null ? user.getUserType() : UserTypeEnum.PILOT.getVal()); userEntity.setUserType(user.getUserType() != null ? user.getUserType() : UserTypeEnum.PILOT.getVal());
userEntity.setUserId(UUID.randomUUID().toString()); userEntity.setUserId(UUID.randomUUID().toString());
userEntity.setPassword(SecurityUtils.encryptPassword(user.getPassword())); userEntity.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment