126 lines
3.5 KiB
Java
126 lines
3.5 KiB
Java
package com.cm.common.component;
|
|
|
|
import com.cm.common.pojo.bos.*;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.swing.text.Position;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @ClassName: SecurityCompontent
|
|
* @Description: 权限工具
|
|
* @Author: WangGeng
|
|
* @Date: 2019/2/26 3:31 PM
|
|
* @Version: 1.0
|
|
**/
|
|
@Component
|
|
public class SecurityComponent {
|
|
|
|
/**
|
|
* 超级管理员
|
|
*/
|
|
public static final String USERNAME_ADMIN = "admin";
|
|
|
|
/**
|
|
* 获取当前用户
|
|
*
|
|
* @return
|
|
*/
|
|
public UserInfoBO getCurrentUser() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
Object user = authentication.getPrincipal();
|
|
UserInfoBO userInfoBO = null;
|
|
if (user instanceof UserBO) {
|
|
userInfoBO = new UserInfoBO();
|
|
UserBO userBO = (UserBO) user;
|
|
userInfoBO.setUserId(userBO.getUserId());
|
|
userInfoBO.setUserUsername(userBO.getUsername());
|
|
userInfoBO.setUserName(userBO.getUserName());
|
|
userInfoBO.setUserPhone(userBO.getUserPhone());
|
|
userInfoBO.setDataAuthority(userBO.getDataAuthority());
|
|
userInfoBO.setDataAuthorityUserIds(userBO.getDataAuthorityUserIds());
|
|
userInfoBO.setBaseDepartmentIds(userBO.getBaseDepartmentIds());
|
|
userInfoBO.setRoles(userBO.getRoles());
|
|
userInfoBO.setDepartments(userBO.getDepartments());
|
|
userInfoBO.setGroups(userBO.getGroups());
|
|
userInfoBO.setPositions(userBO.getPositions());
|
|
}
|
|
if (user instanceof UserInfoBO) {
|
|
userInfoBO = (UserInfoBO) user;
|
|
}
|
|
return userInfoBO;
|
|
}
|
|
|
|
/**
|
|
* 角色ID列表
|
|
*
|
|
* @return
|
|
*/
|
|
public List<String> listRoleIds() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
Collection<? extends GrantedAuthority> grantedAuthorities = authentication.getAuthorities();
|
|
List<String> roleIds = new ArrayList<>();
|
|
for (GrantedAuthority grantedAuthority : grantedAuthorities) {
|
|
RoleGrantedAuthority roleGrantedAuthority = (RoleGrantedAuthority) grantedAuthority;
|
|
roleIds.add(roleGrantedAuthority.getRoleId());
|
|
}
|
|
return roleIds;
|
|
}
|
|
|
|
/**
|
|
* 当前角色列表
|
|
*
|
|
* @return
|
|
*/
|
|
public List<RoleBO> listRole() {
|
|
UserInfoBO userInfoBO = getCurrentUser();
|
|
return userInfoBO.getRoles();
|
|
}
|
|
|
|
/**
|
|
* 部门列表
|
|
*
|
|
* @return
|
|
*/
|
|
public List<DepartmentBO> listDepartment() {
|
|
UserInfoBO userInfoBO = getCurrentUser();
|
|
return userInfoBO.getDepartments();
|
|
}
|
|
|
|
/**
|
|
* 组列表
|
|
*
|
|
* @return
|
|
*/
|
|
public List<GroupBO> listGroup() {
|
|
UserInfoBO userInfoBO = getCurrentUser();
|
|
return userInfoBO.getGroups();
|
|
}
|
|
|
|
/**
|
|
* 职位列白
|
|
*
|
|
* @return
|
|
*/
|
|
public List<PositionBO> listPosition() {
|
|
UserInfoBO userInfoBO = getCurrentUser();
|
|
return userInfoBO.getPositions();
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户名
|
|
*
|
|
* @return
|
|
*/
|
|
public String getCurrentUsername() {
|
|
UserInfoBO userInfoBO = getCurrentUser();
|
|
return userInfoBO.getUserUsername();
|
|
}
|
|
|
|
}
|