Search in sources :

Example 1 with RoleAndResourcesExample

use of com.itrus.portal.db.RoleAndResourcesExample in project portal by ixinportal.

the class ItrusPortalUserDetailsService method loadUserByUsername.

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    // 资源编号集合
    Collection<Integer> resNums = new HashSet<Integer>();
    // 查询用户信息
    AdminExample adminex = new AdminExample();
    adminex.or().andAccountEqualTo(username.toLowerCase());
    Admin admin = sqlSession.selectOne("com.itrus.portal.db.AdminMapper.selectByExample", adminex);
    boolean isNonLocked = true;
    // 用户授权信息
    Collection authorities = new ArrayList();
    // 用户不存在,异常处理
    if (admin == null) {
        Integer count = sqlSession.selectOne("com.itrus.portal.db.AdminMapper.countByExample", null);
        if (count > 0)
            throw new UsernameNotFoundException(username);
        admin = new Admin();
        admin.setPassword("itrusyes");
        admin.setStatus("valid");
        admin.setCreateTime(new Date());
        InitSystemData license = InitSystemData.getDefault();
        resNums = license.getResNums();
        for (String title : license.getRoleTitle()) authorities.add(new SimpleGrantedAuthority(title));
    } else {
        // 项目管理员
        AdminRoleExample roleex = new AdminRoleExample();
        roleex.or().andIdEqualTo(admin.getAdminRole());
        AdminRole adminRole = sqlSession.selectOne("com.itrus.portal.db.AdminRoleMapper.selectByExample", roleex);
        RoleAndResourcesExample rarEx = new RoleAndResourcesExample();
        rarEx.or().andAdminRoleEqualTo(adminRole.getId());
        List<RoleAndResources> roleAndRes = sqlSession.selectList("com.itrus.portal.db.RoleAndResourcesMapper.selectByExample", rarEx);
        for (RoleAndResources rar : roleAndRes) {
            SysResources res = cacheCustomer.getResById(rar.getSysResources());
            resNums.add(res.getResNum());
            // 不能为null角色名称
            if (res.getResRoleName() != null) {
                authorities.add(new SimpleGrantedAuthority(res.getResRoleName()));
            }
        }
    }
    String pass = admin.getPassword();
    if (pass != null && pass.length() != 40)
        // pass = PassUtil.doDigestSHA1(pass,username);
        pass = passwordEncoder.encodePassword(pass, username);
    isNonLocked = "valid".equalsIgnoreCase(admin.getStatus()) ? true : false;
    return new PortalUser(admin.getId(), username, pass, isNonLocked, admin.getProjects(), admin.getProject(), admin.getCreateTime(), resNums, authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) ArrayList(java.util.ArrayList) AdminRoleExample(com.itrus.portal.db.AdminRoleExample) RoleAndResources(com.itrus.portal.db.RoleAndResources) Admin(com.itrus.portal.db.Admin) InitSystemData(com.itrus.portal.utils.InitSystemData) Date(java.util.Date) PortalUser(com.itrus.portal.utils.PortalUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SysResources(com.itrus.portal.db.SysResources) Collection(java.util.Collection) AdminRole(com.itrus.portal.db.AdminRole) AdminExample(com.itrus.portal.db.AdminExample) RoleAndResourcesExample(com.itrus.portal.db.RoleAndResourcesExample) HashSet(java.util.HashSet)

Aggregations

Admin (com.itrus.portal.db.Admin)1 AdminExample (com.itrus.portal.db.AdminExample)1 AdminRole (com.itrus.portal.db.AdminRole)1 AdminRoleExample (com.itrus.portal.db.AdminRoleExample)1 RoleAndResources (com.itrus.portal.db.RoleAndResources)1 RoleAndResourcesExample (com.itrus.portal.db.RoleAndResourcesExample)1 SysResources (com.itrus.portal.db.SysResources)1 InitSystemData (com.itrus.portal.utils.InitSystemData)1 PortalUser (com.itrus.portal.utils.PortalUser)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1