Search in sources :

Example 1 with DataScope

use of com.albedo.java.plugins.database.mybatis.datascope.DataScope in project albedo by somowhere.

the class DeptResource method findTreeList.

/**
 * 部门树列表信息
 *
 * @return 分页对象
 */
@GetMapping
@PreAuthorize("@pms.hasPermission('sys_dept_view')")
@LogOperate(value = "部门管理查看")
public Result<IPage<DeptVo>> findTreeList(DeptQueryCriteria deptQueryCriteria) {
    DataScope dataScope = SecurityUtil.getDataScope();
    if (!dataScope.isAll()) {
        ArgumentAssert.notEmpty(dataScope.getDeptIds(), "login user deptIds is empty");
        deptQueryCriteria.setDeptIds(dataScope.getDeptIds());
    }
    return Result.buildOkData(deptService.findTreeList(deptQueryCriteria));
}
Also used : DataScope(com.albedo.java.plugins.database.mybatis.datascope.DataScope) LogOperate(com.albedo.java.common.log.annotation.LogOperate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with DataScope

use of com.albedo.java.plugins.database.mybatis.datascope.DataScope in project albedo by somowhere.

the class UserDetailsServiceImpl method getUserDetails.

/**
 * 构建userdetails
 *
 * @param userInfo 用户信息
 * @return
 */
private UserDetails getUserDetails(UserInfo userInfo) {
    ArgumentAssert.notNull(userInfo, () -> new UsernameNotFoundException("用户不存在"));
    Set<String> dbAuthsSet = new HashSet<>();
    if (ArrayUtil.isNotEmpty(userInfo.getRoles())) {
        // 获取角色
        Arrays.stream(userInfo.getRoles()).forEach(role -> dbAuthsSet.add(SecurityConstants.ROLE + role));
        // 获取资源
        dbAuthsSet.addAll(Arrays.asList(userInfo.getPermissions()));
    }
    List<GrantedAuthority> authorities = AuthUtil.createAuthorityList(dbAuthsSet.toArray(new String[0]));
    UserVo userVo = userInfo.getUser();
    DataScope dataScope = new DataScope();
    if (CollUtil.isNotEmpty(userVo.getRoleDoList())) {
        for (RoleDo roleDo : userVo.getRoleDoList()) {
            if (DataScopeType.ALL.eq(roleDo.getDataScope())) {
                dataScope.setAll(true);
                break;
            } else if (DataScopeType.THIS_LEVEL_CHILDREN.eq(roleDo.getDataScope())) {
                dataScope.getDeptIds().addAll(deptService.findDescendantIdList(userVo.getDeptId()));
            } else if (DataScopeType.THIS_LEVEL.eq(roleDo.getDataScope())) {
                dataScope.getDeptIds().add(userVo.getDeptId());
            } else if (DataScopeType.SELF.eq(roleDo.getDataScope())) {
                dataScope.setSelf(true);
                dataScope.setUserId(userVo.getId());
            } else if (DataScopeType.CUSTOMIZE.eq(roleDo.getDataScope())) {
                dataScope.getDeptIds().addAll(roleService.findDeptIdsByRoleId(roleDo.getId()));
            }
        }
    }
    // 构造security用户
    return new UserDetail(userVo.getId(), userVo.getDeptId(), userVo.getDeptName(), userVo.getUsername(), SecurityConstants.BCRYPT + userVo.getPassword(), userVo.isAvailable(), true, true, true, authorities, dataScope);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) DataScope(com.albedo.java.plugins.database.mybatis.datascope.DataScope) UserVo(com.albedo.java.modules.sys.domain.vo.UserVo) GrantedAuthority(org.springframework.security.core.GrantedAuthority) RoleDo(com.albedo.java.modules.sys.domain.RoleDo) HashSet(java.util.HashSet)

Aggregations

DataScope (com.albedo.java.plugins.database.mybatis.datascope.DataScope)2 LogOperate (com.albedo.java.common.log.annotation.LogOperate)1 RoleDo (com.albedo.java.modules.sys.domain.RoleDo)1 UserVo (com.albedo.java.modules.sys.domain.vo.UserVo)1 HashSet (java.util.HashSet)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1