如何将方法中的条件拆分为两个具有单独条件的方法
How to split condition in method into two methods with separate condition
我有这个代码来检查用户是管理员还是消息的所有者。我必须将此方法分为两种方法:首先 - 检查用户是否是管理员,其次 - 如果用户是所有者。但是,如果我只是将条件一分为二,它将无法正常工作。
public static void checkIfTheUserIsAdminOrTheOwnerOfTheComment(Comment commentFound, SecurityUser user){
if (!(commentFound.getAuthor().getId().equals(user.getUserId())
||(user.getAuthorities().contains(new SimpleGrantedAuthority(Authorities.ADMIN_WRITE.getPermission()))))) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
我试过了
public static void checkIfTheUserIsTheOwnerOfTheComment(Comment commentFound, SecurityUser user){
if (!commentFound.getAuthor().getId().equals(user.getUserId())) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
public static void checkIfTheUserIsAdmin(Comment commentFound, SecurityUser user){
if (!user.getAuthorities().contains(new SimpleGrantedAuthority(Authorities.ADMIN_WRITE.getPermission())))) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
但它不会正常工作,因为如果我以管理员身份登录,我将有一个例外,即我不是所有者,但我必须将其分为两个单独的方法。有什么建议吗?
重构的一种方法是提取执行每个检查的单独方法。也许仍然不完美,但它看起来像这样:
- 第一次检查:
private boolean checkIsUserOwnerOfComment(Comment commentFound, SecurityUser user) {
return commentFound.getAuthor().getId().equals(user.getUserId());
}
- 第二次检查:
private boolean checkIsUserAdmin(Comment commentFound, SecurityUser user) {
return user.getAuthorities().contains(new SimpleGrantedAuthority(Authorities.ADMIN_WRITE.getPermission()));
}
最后,根据与您的用例相关的任何逻辑执行两项检查并引发异常。
public static void validateUser(Comment commentFound, SecurityUser user){
boolean userIsAdminOrOwnerOfComment = this.checkIsUserAdmin(commentFound, user) || this.checkIsUserOwnerOfComment(commentFound, user);
if (!userIsAdminOrOwnerOfComment) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
我有这个代码来检查用户是管理员还是消息的所有者。我必须将此方法分为两种方法:首先 - 检查用户是否是管理员,其次 - 如果用户是所有者。但是,如果我只是将条件一分为二,它将无法正常工作。
public static void checkIfTheUserIsAdminOrTheOwnerOfTheComment(Comment commentFound, SecurityUser user){
if (!(commentFound.getAuthor().getId().equals(user.getUserId())
||(user.getAuthorities().contains(new SimpleGrantedAuthority(Authorities.ADMIN_WRITE.getPermission()))))) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
我试过了
public static void checkIfTheUserIsTheOwnerOfTheComment(Comment commentFound, SecurityUser user){
if (!commentFound.getAuthor().getId().equals(user.getUserId())) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
public static void checkIfTheUserIsAdmin(Comment commentFound, SecurityUser user){
if (!user.getAuthorities().contains(new SimpleGrantedAuthority(Authorities.ADMIN_WRITE.getPermission())))) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}
但它不会正常工作,因为如果我以管理员身份登录,我将有一个例外,即我不是所有者,但我必须将其分为两个单独的方法。有什么建议吗?
重构的一种方法是提取执行每个检查的单独方法。也许仍然不完美,但它看起来像这样:
- 第一次检查:
private boolean checkIsUserOwnerOfComment(Comment commentFound, SecurityUser user) {
return commentFound.getAuthor().getId().equals(user.getUserId());
}
- 第二次检查:
private boolean checkIsUserAdmin(Comment commentFound, SecurityUser user) {
return user.getAuthorities().contains(new SimpleGrantedAuthority(Authorities.ADMIN_WRITE.getPermission()));
}
最后,根据与您的用例相关的任何逻辑执行两项检查并引发异常。
public static void validateUser(Comment commentFound, SecurityUser user){
boolean userIsAdminOrOwnerOfComment = this.checkIsUserAdmin(commentFound, user) || this.checkIsUserOwnerOfComment(commentFound, user);
if (!userIsAdminOrOwnerOfComment) {
throw new ForbiddenRequestException(Errors.ERROR4.getMessage());
}
}