Search in sources :

Example 1 with User

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

the class UserShiroRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    User user = (User) principals.getPrimaryPrincipal();
    List<Group> groupList = groupService.selectByUserId(user.getUserId());
    Set<String> groupSet = groupList.stream().map(Group::getGroupName).collect(Collectors.toSet());
    Set<String> permissionSet = groupSet.stream().flatMap(group -> permissionService.selectByGroupName(group).stream().map(Permission::getPermissionName)).collect(Collectors.toSet());
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    simpleAuthorizationInfo.setStringPermissions(permissionSet);
    simpleAuthorizationInfo.setRoles(groupSet);
    return simpleAuthorizationInfo;
}
Also used : PermissionService(com.ganster.cms.core.service.PermissionService) Logger(org.slf4j.Logger) org.apache.shiro.authc(org.apache.shiro.authc) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) LoggerFactory(org.slf4j.LoggerFactory) Resource(javax.annotation.Resource) Permission(com.ganster.cms.core.pojo.Permission) Set(java.util.Set) Collectors(java.util.stream.Collectors) UserService(com.ganster.cms.core.service.UserService) List(java.util.List) Subject(org.apache.shiro.subject.Subject) UserExample(com.ganster.cms.core.pojo.UserExample) User(com.ganster.cms.core.pojo.User) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Group(com.ganster.cms.core.pojo.Group) GroupService(com.ganster.cms.core.service.GroupService) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) SecurityUtils(org.apache.shiro.SecurityUtils) Group(com.ganster.cms.core.pojo.Group) User(com.ganster.cms.core.pojo.User) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) Permission(com.ganster.cms.core.pojo.Permission)

Example 2 with User

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

the class PermissionServiceImplTest method interTest.

@Test
public void interTest() {
    final String userName = "@#$%^&";
    final String siteName = "$%^&&";
    User user = new User();
    user.setUserName(userName);
    userService.insert(user);
    Site site = new Site();
    site.setSiteName(siteName);
    siteService.insert(site);
    Group group = new Group();
    group.setGroupName(userName);
    groupService.insert(group);
    System.out.println(group.getGroupId());
    try {
        groupService.addUserToGroup(user.getUserId(), group.getGroupId());
    } catch (UserNotFoundException | GroupNotFountException e) {
        e.printStackTrace();
    }
    try {
        permissionService.addUserToSite(user.getUserId(), site.getSiteId());
    } catch (UserNotFoundException e) {
        e.printStackTrace();
    }
    List<Site> siteList = permissionService.findAllUserSite(user.getUserId());
    System.out.println(siteList);
    try {
        permissionService.addCategoryPermissionToUser(user.getUserId(), site.getSiteId(), 3, CmsConst.PERMISSION_READ);
    } catch (UserNotFoundException e) {
        e.printStackTrace();
    }
    boolean hasP = permissionService.hasCategoryPermission(user.getUserId(), site.getSiteId(), 3, CmsConst.PERMISSION_READ);
    Assert.assertTrue(hasP);
}
Also used : Site(com.ganster.cms.core.pojo.Site) UserNotFoundException(com.ganster.cms.core.exception.UserNotFoundException) Group(com.ganster.cms.core.pojo.Group) User(com.ganster.cms.core.pojo.User) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with User

use of com.ganster.cms.core.pojo.User 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 4 with User

use of com.ganster.cms.core.pojo.User 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 5 with User

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

the class SiteController method add.

@PostMapping("/add")
public Message add(@RequestBody Site site) {
    Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
    User user = userService.selectByPrimaryKey(userId);
    if (site == null) {
        return super.buildMessage(1, "no data", null);
    }
    site.setSiteCreateTime(new Date());
    site.setSiteStatus(0);
    int count = siteService.insert(site);
    if (count == 0) {
        return super.buildMessage(1, "add site failed", null);
    }
    try {
        if (!user.getUserName().equals("admin")) {
            permissionService.addUserToSite(1, site.getSiteId());
        }
        permissionService.addUserToSite(userId, site.getSiteId());
    } catch (UserNotFoundException e) {
        e.printStackTrace();
        return super.buildMessage(1, "用户未找到", null);
    }
    PermissionUtil.flush(userId);
    return super.buildMessage(0, "success", count);
}
Also used : UserNotFoundException(com.ganster.cms.core.exception.UserNotFoundException) User(com.ganster.cms.core.pojo.User) Date(java.util.Date)

Aggregations

User (com.ganster.cms.core.pojo.User)14 UserExample (com.ganster.cms.core.pojo.UserExample)7 Group (com.ganster.cms.core.pojo.Group)4 List (java.util.List)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 Message (com.ganster.cms.admin.dto.Message)2 GroupNotFountException (com.ganster.cms.core.exception.GroupNotFountException)2 UserNotFoundException (com.ganster.cms.core.exception.UserNotFoundException)2 Permission (com.ganster.cms.core.pojo.Permission)2 GroupService (com.ganster.cms.core.service.GroupService)2 UserService (com.ganster.cms.core.service.UserService)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Collectors (java.util.stream.Collectors)2 SecurityUtils (org.apache.shiro.SecurityUtils)2 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)2 Subject (org.apache.shiro.subject.Subject)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 InformationObject (com.ganster.cms.admin.dto.InformationObject)1