Search in sources :

Example 1 with UserExample

use of com.ganster.cms.core.pojo.UserExample in project Ganster-CMS by Gangster-trio.

the class UserShiroRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    logger.info("进入权限配置");
    String username = (String) principals.getPrimaryPrincipal();
    UserExample userExample = new UserExample();
    userExample.createCriteria().andUserNameEqualTo(username);
    List<User> users = userService.selectByExample(userExample);
    Integer j = 0;
    for (User i : users) {
        userId = i.getUserId();
        j++;
    }
    if (j >= 2) {
        return null;
    }
    User user = userService.selectByPrimaryKey(userId);
    List<Group> groupList = groupService.selectByUserId(user.getUserId());
    Set<String> groupSet = new HashSet<>();
    for (Group i : groupList) {
        if (!StringUtil.isNullOrEmpty(user.getUserName())) {
            groupSet.add(i.getGroupName());
        }
    }
    Set<String> permissionSet = new HashSet<>();
    for (Group i : groupList) {
        if (!StringUtil.isNullOrEmpty(i.getGroupName())) {
            try {
                List<Permission> permissions = permissionService.selectByGroupId(i.getGroupId());
                for (Permission permission : permissions) {
                    permissionSet.add(permission.getPermissionName());
                }
            } catch (GroupNotFountException e) {
                logger.info("角色未找到");
            }
        }
    }
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    simpleAuthorizationInfo.setStringPermissions(permissionSet);
    simpleAuthorizationInfo.setRoles(groupSet);
    return simpleAuthorizationInfo;
}
Also used : Group(com.ganster.cms.core.pojo.Group) User(com.ganster.cms.core.pojo.User) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) UserExample(com.ganster.cms.core.pojo.UserExample) Permission(com.ganster.cms.core.pojo.Permission)

Example 2 with UserExample

use of com.ganster.cms.core.pojo.UserExample in project Ganster-CMS by Gangster-trio.

the class UserShiroRealm method doGetAuthenticationInfo.

/**
 * 认证信息.(身份验证)
 * :
 * Authentication 是用来验证用户身份
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    String username = (String) token.getPrincipal();
    String password = new String((char[]) token.getCredentials());
    logger.info("-----------------------------" + password + "--------------------------------------");
    UserExample userExample = new UserExample();
    userExample.createCriteria().andUserNameEqualTo(username);
    List<User> users = userService.selectByExample(userExample);
    Integer j = 0;
    for (User i : users) {
        userId = i.getUserId();
        j++;
    }
    if (j >= 2) {
        throw new AuthenticationException();
    }
    User user = userService.selectByPrimaryKey(userId);
    if (!user.getUserName().equals(username)) {
        SecurityUtils.getSubject().logout();
        throw new AuthenticationException();
    }
    if (user == null) {
        throw new AuthenticationException();
    }
    SecurityUtils.getSubject().getSession().setAttribute("id", user.getUserId());
    logger.info("用户" + user.getUserName() + "进行认证");
    if (!Objects.equals(password, user.getUserPassword())) {
        throw new IncorrectCredentialsException();
    }
    return new SimpleAuthenticationInfo(username, password, getName());
}
Also used : User(com.ganster.cms.core.pojo.User) UserExample(com.ganster.cms.core.pojo.UserExample)

Example 3 with UserExample

use of com.ganster.cms.core.pojo.UserExample in project Ganster-CMS by Gangster-trio.

the class UserController method addUser.

/**
 * 添加用户
 * @param user
 * @return   Message 添加用户是否成功
 */
@PostMapping("/add")
@ResponseBody
public Message addUser(@RequestBody User user) {
    Message message = new Message();
    if (!this.index()) {
        message.setMsg("添加权限失败");
        return message;
    }
    user.setUserCreateTime(new Date());
    UserExample userExample = new UserExample();
    userExample.createCriteria().andUserNameEqualTo(user.getUserName());
    List<User> userList = userService.selectByExample(userExample);
    if (userList != null && !userList.isEmpty()) {
        message.setMsg("用户已存在");
        message.setCode(1);
    } else {
        userService.createUser(user);
        message.setCode(0);
        message.setMsg("成功添加");
    }
    return message;
}
Also used : User(com.ganster.cms.core.pojo.User) Message(com.ganster.cms.admin.dto.Message) Date(java.util.Date) UserExample(com.ganster.cms.core.pojo.UserExample)

Example 4 with UserExample

use of com.ganster.cms.core.pojo.UserExample in project Ganster-CMS by Gangster-trio.

the class UserController method findUserById.

/**
 *  通过用户Id查找用户
 * @param userId
 * @return  User 查找到的用户
 */
@GetMapping("/find/{UserId}")
@ResponseBody
public User findUserById(@PathVariable("UserId") Integer userId) {
    UserExample userExample = new UserExample();
    userExample.createCriteria().andUserIdEqualTo(userId);
    List<User> userList = userService.selectByExample(userExample);
    return userList.get(0);
}
Also used : User(com.ganster.cms.core.pojo.User) UserExample(com.ganster.cms.core.pojo.UserExample)

Example 5 with UserExample

use of com.ganster.cms.core.pojo.UserExample in project Ganster-CMS by Gangster-trio.

the class UserController method findUser.

/**
 *  查找所有的用户
 * @param page
 * @param limit
 * @return  AjaxData 查找到的所有用户
 */
@GetMapping("/find")
@ResponseBody
public AjaxData findUser(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
    UserExample userExample = new UserExample();
    PageInfo<User> pageInfo = PageHelper.startPage(page, limit).doSelectPageInfo(() -> userService.selectByExample(userExample));
    List<User> list = pageInfo.getList();
    if (list == null || list.isEmpty()) {
        return super.buildAjaxData(1, "false", 0, null);
    } else {
        return super.buildAjaxData(0, "true", pageInfo.getTotal(), list);
    }
}
Also used : User(com.ganster.cms.core.pojo.User) UserExample(com.ganster.cms.core.pojo.UserExample)

Aggregations

User (com.ganster.cms.core.pojo.User)6 UserExample (com.ganster.cms.core.pojo.UserExample)6 Message (com.ganster.cms.admin.dto.Message)1 GroupNotFountException (com.ganster.cms.core.exception.GroupNotFountException)1 Group (com.ganster.cms.core.pojo.Group)1 Permission (com.ganster.cms.core.pojo.Permission)1 Date (java.util.Date)1 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)1