| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.PatternMatchUtils; |
| | | import org.springframework.util.StringUtils; |
| | | import com.ruoyi.common.core.exception.PreAuthorizeException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.security.annotation.PreAuthorize; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.system.api.model.LoginUser; |
| | |
| | | return point.proceed(); |
| | | } |
| | | |
| | | if (!StringUtils.isEmpty(annotation.hasPermi())) |
| | | if (StringUtils.isNotEmpty(annotation.hasPermi())) |
| | | { |
| | | if (hasPermi(annotation.hasPermi())) |
| | | { |
| | |
| | | } |
| | | throw new PreAuthorizeException(); |
| | | } |
| | | else if (!StringUtils.isEmpty(annotation.lacksPermi())) |
| | | else if (StringUtils.isNotEmpty(annotation.lacksPermi())) |
| | | { |
| | | if (lacksPermi(annotation.lacksPermi())) |
| | | { |
| | |
| | | } |
| | | throw new PreAuthorizeException(); |
| | | } |
| | | else if (!StringUtils.isEmpty(annotation.hasRole())) |
| | | else if (StringUtils.isNotEmpty(annotation.hasRole())) |
| | | { |
| | | if (hasRole(annotation.hasRole())) |
| | | { |
| | |
| | | } |
| | | throw new PreAuthorizeException(); |
| | | } |
| | | else if (!StringUtils.isEmpty(annotation.lacksRole())) |
| | | else if (StringUtils.isNotEmpty(annotation.lacksRole())) |
| | | { |
| | | if (lacksRole(annotation.lacksRole())) |
| | | { |
| | |
| | | public boolean hasPermi(String permission) |
| | | { |
| | | LoginUser userInfo = tokenService.getLoginUser(); |
| | | if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) |
| | | if (StringUtils.isNull(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) |
| | | { |
| | | return false; |
| | | } |
| | |
| | | public boolean hasAnyPermi(String[] permissions) |
| | | { |
| | | LoginUser userInfo = tokenService.getLoginUser(); |
| | | if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) |
| | | if (StringUtils.isNull(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) |
| | | { |
| | | return false; |
| | | } |
| | |
| | | public boolean hasRole(String role) |
| | | { |
| | | LoginUser userInfo = tokenService.getLoginUser(); |
| | | if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) |
| | | if (StringUtils.isNull(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) |
| | | { |
| | | return false; |
| | | } |
| | | for (String roleKey : userInfo.getRoles()) |
| | | { |
| | | if (SUPER_ADMIN.contains(roleKey) || roleKey.contains(role)) |
| | | if (SUPER_ADMIN.equals(roleKey) || roleKey.equals(role)) |
| | | { |
| | | return true; |
| | | } |
| | |
| | | public boolean hasAnyRoles(String[] roles) |
| | | { |
| | | LoginUser userInfo = tokenService.getLoginUser(); |
| | | if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) |
| | | if (StringUtils.isNull(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) |
| | | { |
| | | return false; |
| | | } |
| | |
| | | private boolean hasPermissions(Collection<String> authorities, String permission) |
| | | { |
| | | return authorities.stream().filter(StringUtils::hasText) |
| | | .anyMatch(x -> ALL_PERMISSION.contains(x) || PatternMatchUtils.simpleMatch(permission, x)); |
| | | .anyMatch(x -> ALL_PERMISSION.contains(x) || PatternMatchUtils.simpleMatch(x, permission)); |
| | | } |
| | | } |