Search in sources :

Example 16 with User

use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.

the class ShiroRealm method doGetAuthorizationInfo.

/**
 * 权限认证,为当前登录的Subject授予角色和权限(角色的权限信息集合)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    // 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
    // 赋予角色
    List<Role> roleList = roleService.listRolesByUserId(userId);
    if (null != roleList) {
        for (Role role : roleList) {
            info.addRole(role.getName());
        }
    }
    // 赋予权限
    List<Resources> resourcesList = null;
    User user = userService.getByPrimaryKey(userId);
    if (null == user) {
        return info;
    }
    // ROOT用户默认拥有所有权限
    if (UserTypeEnum.ROOT.toString().equalsIgnoreCase(user.getUserType())) {
        resourcesList = resourcesService.listAll();
    } else {
        resourcesList = resourcesService.listByUserId(userId);
    }
    if (!CollectionUtils.isEmpty(resourcesList)) {
        Set<String> permissionSet = new HashSet<>();
        for (Resources resources : resourcesList) {
            String permission = null;
            if (!StringUtils.isEmpty(permission = resources.getPermission())) {
                permissionSet.addAll(Arrays.asList(permission.trim().split(",")));
            }
        }
        info.setStringPermissions(permissionSet);
    }
    return info;
}
Also used : Role(com.zyd.blog.business.entity.Role) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) User(com.zyd.blog.business.entity.User) Resources(com.zyd.blog.business.entity.Resources) HashSet(java.util.HashSet)

Example 17 with User

use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.

the class ShiroRealm method doGetAuthenticationInfo.

/**
 * 提供账户信息返回认证信息(用户的角色信息集合)
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    // 获取用户的输入的账号.
    String username = (String) token.getPrincipal();
    User user = userService.getByUserName(username);
    if (user == null) {
        throw new UnknownAccountException("账号不存在!");
    }
    if (user.getStatus() != null && UserStatusEnum.DISABLE.getCode().equals(user.getStatus())) {
        throw new LockedAccountException("帐号已被锁定,禁止登录!");
    }
    // principal参数使用用户Id,方便动态刷新用户权限
    return new SimpleAuthenticationInfo(user.getId(), user.getPassword(), ByteSource.Util.bytes(username), getName());
}
Also used : User(com.zyd.blog.business.entity.User)

Example 18 with User

use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.

the class RestUserController method add.

@RequiresPermissions("user:add")
@PostMapping(value = "/add")
@BussinessLog("添加用户")
public ResponseVO add(User user) {
    User u = userService.getByUserName(user.getUsername());
    if (u != null) {
        return ResultUtil.error("该用户名[" + user.getUsername() + "]已存在!请更改用户名");
    }
    try {
        user.setPassword(PasswordUtil.encrypt(user.getPassword(), user.getUsername()));
        userService.insert(user);
        return ResultUtil.success("成功");
    } catch (Exception e) {
        e.printStackTrace();
        return ResultUtil.error("error");
    }
}
Also used : User(com.zyd.blog.business.entity.User) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) PostMapping(org.springframework.web.bind.annotation.PostMapping) BussinessLog(com.zyd.blog.business.annotation.BussinessLog)

Example 19 with User

use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.

the class SysUserServiceImpl method getByUserName.

/**
 * 根据用户名查找
 *
 * @param userName
 * @return
 */
@Override
public User getByUserName(String userName) {
    User user = new User(userName, null);
    SysUser sysUser = this.sysUserMapper.selectOne(user.getSysUser());
    return null == sysUser ? null : new User(sysUser);
}
Also used : SysUser(com.zyd.blog.persistence.beans.SysUser) User(com.zyd.blog.business.entity.User) SysUser(com.zyd.blog.persistence.beans.SysUser)

Example 20 with User

use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.

the class SysUserServiceImpl method findPageBreakByCondition.

@Override
public PageInfo<User> findPageBreakByCondition(UserConditionVO vo) {
    PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
    List<SysUser> sysUsers = sysUserMapper.findPageBreakByCondition(vo);
    if (CollectionUtils.isEmpty(sysUsers)) {
        return null;
    }
    List<User> users = new ArrayList<>();
    for (SysUser su : sysUsers) {
        users.add(new User(su));
    }
    PageInfo bean = new PageInfo<SysUser>(sysUsers);
    bean.setList(users);
    return bean;
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SysUser(com.zyd.blog.persistence.beans.SysUser) User(com.zyd.blog.business.entity.User) SysUser(com.zyd.blog.persistence.beans.SysUser) ArrayList(java.util.ArrayList)

Aggregations

User (com.zyd.blog.business.entity.User)24 SysUser (com.zyd.blog.persistence.beans.SysUser)8 ArrayList (java.util.ArrayList)4 BussinessLog (com.zyd.blog.business.annotation.BussinessLog)3 ZhydException (com.zyd.blog.framework.exception.ZhydException)3 JapUser (com.fujieid.jap.core.JapUser)2 RedisCache (com.zyd.blog.business.annotation.RedisCache)2 Article (com.zyd.blog.business.entity.Article)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 JapConfig (com.fujieid.jap.core.config.JapConfig)1 JapResponse (com.fujieid.jap.core.result.JapResponse)1 SocialStrategy (com.fujieid.jap.social.SocialStrategy)1 PageInfo (com.github.pagehelper.PageInfo)1 File (com.zyd.blog.business.entity.File)1 Log (com.zyd.blog.business.entity.Log)1 Resources (com.zyd.blog.business.entity.Resources)1 Role (com.zyd.blog.business.entity.Role)1 SocialConfig (com.zyd.blog.business.entity.SocialConfig)1 Tags (com.zyd.blog.business.entity.Tags)1