Revize afdd306f
Přidáno uživatelem Jakub Šmíd před asi 2 roky(ů)
backend/src/main/java/cz/zcu/kiv/backendapi/user/UserServiceImpl.java | ||
---|---|---|
25 | 25 |
@RequiredArgsConstructor |
26 | 26 |
@Slf4j |
27 | 27 |
public class UserServiceImpl implements IUserService, UserDetailsService { |
28 |
/** |
|
29 |
* Super ADMIN name |
|
30 |
*/ |
|
31 |
private static final String SUPER_ADMIN_NAME = "admin@admin.com"; |
|
32 |
|
|
28 | 33 |
/** |
29 | 34 |
* Message for exception when user is not found by username |
30 | 35 |
*/ |
... | ... | |
76 | 81 |
log.error(String.format(USER_NOT_FOUND, username)); |
77 | 82 |
throw new UsernameNotFoundException(String.format(USER_NOT_FOUND, username)); |
78 | 83 |
}); |
84 |
if (userEntity.isAdmin()) { |
|
85 |
log.error("Permissions for user with ADMIN rights can not be changed"); |
|
86 |
throw new ApiRequestException("Permissions for user with ADMIN rights can not be changed", HttpStatus.BAD_REQUEST); |
|
87 |
} |
|
79 | 88 |
userEntity.setPermissions(getPermissionsFromDto(permissionDto)); |
80 | 89 |
userRepository.save(userEntity); |
81 | 90 |
} |
82 | 91 |
|
83 | 92 |
@Override |
84 | 93 |
public void resetPassword(String username, String newPassword) { |
94 |
if (username.equals(SUPER_ADMIN_NAME)) { |
|
95 |
log.error("Password for SUPER ADMIN can not be changed"); |
|
96 |
throw new ApiRequestException("Password for SUPER ADMIN can not be changed", HttpStatus.BAD_REQUEST); |
|
97 |
} |
|
85 | 98 |
UserEntity userEntity = userRepository.findByEmail(username).orElseThrow(() -> { |
86 | 99 |
log.error(String.format(USER_NOT_FOUND, username)); |
87 | 100 |
throw new UsernameNotFoundException(String.format(USER_NOT_FOUND, username)); |
... | ... | |
106 | 119 |
|
107 | 120 |
@Override |
108 | 121 |
public List<UserDto> getAllUsers() { |
109 |
return userRepository.findAll().stream().map(this::convertEntityToDto).collect(Collectors.toList()); |
|
122 |
return userRepository.findAll().stream().filter(u -> !u.getEmail().equals(SUPER_ADMIN_NAME)).map(this::convertEntityToDto).collect(Collectors.toList());
|
|
110 | 123 |
} |
111 | 124 |
|
112 | 125 |
@Override |
113 | 126 |
public void changePassword(String oldPassword, String newPassword) { |
114 | 127 |
UserEntity loggedUser = getUserByName((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal()); |
128 |
if (loggedUser.getEmail().equals(SUPER_ADMIN_NAME)) { |
|
129 |
throw new ApiRequestException("Can not change password for SUPER ADMIN", HttpStatus.BAD_REQUEST); |
|
130 |
} |
|
115 | 131 |
if (!bCryptPasswordEncoder.matches(oldPassword, loggedUser.getPassword())) { |
116 | 132 |
throw new ApiRequestException("Old password does not match", HttpStatus.BAD_REQUEST); |
117 | 133 |
} |
Také k dispozici: Unified diff
Added Super Admin user
re #9790