refactor: 简化代码

This commit is contained in:
shiziyuan9527 2020-07-24 15:04:38 +08:00
parent be96428a13
commit 56e0415ddb
2 changed files with 11 additions and 13 deletions

View File

@ -117,7 +117,7 @@ public class ShiroDBRealm extends AuthorizingRealm {
UserDTO user = userService.getLoginUser(userId, Arrays.asList(UserSource.LDAP.name(), UserSource.LOCAL.name()));
String msg;
if (user == null) {
user = userService.getLoginUserByEmail(email, Arrays.asList(UserSource.LDAP.name(), UserSource.LOCAL.name()));
user = userService.getUserDTOByEmail(email, UserSource.LDAP.name(), UserSource.LOCAL.name());
if (user == null) {
msg = "The user does not exist: " + userId;
logger.warn(msg);
@ -136,7 +136,7 @@ public class ShiroDBRealm extends AuthorizingRealm {
UserDTO user = userService.getLoginUser(userId, Collections.singletonList(UserSource.LOCAL.name()));
String msg;
if (user == null) {
user = userService.getLoginUserByEmail(userId, Collections.singletonList(UserSource.LOCAL.name()));
user = userService.getUserDTOByEmail(userId, UserSource.LOCAL.name());
if (user == null) {
msg = "The user does not exist: " + userId;
logger.warn(msg);

View File

@ -199,23 +199,21 @@ public class UserService {
return getUserDTO(userId);
}
public UserDTO getUserDTOByEmail(String email) {
public UserDTO getUserDTOByEmail(String email, String... source) {
UserExample example = new UserExample();
example.createCriteria().andEmailEqualTo(email);
List<User> users = userMapper.selectByExample(example);
if (users == null || users.size() <= 0) {
return null;
}
return getUserDTO(users.get(0).getId());
}
UserExample.Criteria criteria = example.createCriteria();
criteria.andEmailEqualTo(email);
if (!CollectionUtils.isEmpty(Arrays.asList(source))) {
criteria.andSourceIn(Arrays.asList(source));
}
public UserDTO getLoginUserByEmail(String email, List<String> list) {
UserExample example = new UserExample();
example.createCriteria().andEmailEqualTo(email).andSourceIn(list);
List<User> users = userMapper.selectByExample(example);
if (users == null || users.size() <= 0) {
return null;
}
return getUserDTO(users.get(0).getId());
}