Search in sources :

Example 1 with Group

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

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

the class AllotGroupController method updateGroup.

@RequestMapping("/update")
public int updateGroup(@RequestParam(value = "Group") Group group) {
    this.index();
    List<String> permissionName = this.getPermissionName("group", "updategroup");
    Subject subject = SecurityUtils.getSubject();
    for (String i : permissionName) {
        if (subject.isPermitted(i)) {
            PInformationUtil pInformationUtil = new PInformationUtil();
            try {
                pInformationUtil.dealInfromation(i);
                Group needUpdateGroup = groupService.selectByPrimaryKey(Integer.parseInt(pInformationUtil.getId()));
                if (needUpdateGroup.getGroupId().equals(group.getGroupId())) {
                    return groupService.updateByPrimaryKey(group);
                } else
                    continue;
            } catch (InformationException e) {
                logger.info("权限信息异常");
            }
        }
    }
    return 0;
}
Also used : InformationException(com.ganster.cms.auth.Exception.InformationException) Group(com.ganster.cms.core.pojo.Group) Subject(org.apache.shiro.subject.Subject) PInformationUtil(com.ganster.cms.auth.util.PInformationUtil) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Group

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

the class AllotGroupController method getPermissionName.

public List<String> getPermissionName(String groupName, String permission) {
    List<String> permissionName = new ArrayList<>();
    PInformationUtil pInformationUtil = new PInformationUtil();
    Subject subject = SecurityUtils.getSubject();
    Integer id = (Integer) subject.getSession().getAttribute("id");
    GroupExample groupExample = new GroupExample();
    groupExample.createCriteria().andGroupNameEqualTo(groupName + ":" + id);
    List<Group> groupList = groupService.selectByExample(groupExample);
    for (Group group : groupList) {
        try {
            List<Permission> permissionList = permissionService.selectByGroupId(group.getGroupId());
            for (Permission i : permissionList) {
                int j = 0;
                try {
                    pInformationUtil.dealInfromation(i.getPermissionName());
                    String name = permission + ":" + pInformationUtil.getId();
                    permissionName.add(name);
                    j++;
                } catch (Exception e) {
                    break;
                }
            }
        } catch (GroupNotFountException e) {
            e.printStackTrace();
        }
    }
    return permissionName;
}
Also used : Group(com.ganster.cms.core.pojo.Group) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) ArrayList(java.util.ArrayList) PInformationUtil(com.ganster.cms.auth.util.PInformationUtil) Subject(org.apache.shiro.subject.Subject) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) PermissionNotFoundException(com.ganster.cms.core.exception.PermissionNotFoundException) UserNotFoundException(com.ganster.cms.core.exception.UserNotFoundException) InformationException(com.ganster.cms.auth.Exception.InformationException) GroupExample(com.ganster.cms.core.pojo.GroupExample) Permission(com.ganster.cms.core.pojo.Permission)

Example 4 with Group

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

the class AllotGroupController method addGroup.

@RequestMapping("/add")
public void addGroup(@RequestParam(value = "UserId") Integer userId, @RequestParam(value = "GroupName") String groupName) {
    this.index();
    GroupExample groupExample = new GroupExample();
    groupExample.createCriteria().andGroupNameEqualTo(groupName);
    List<Group> groupList = groupService.selectByExample(groupExample);
    if (userId != null) {
        try {
            if (groupList == null) {
                RInformationUtil rInformationUtil = new RInformationUtil();
                rInformationUtil.dealInfromation(groupName);
                Group group = new Group();
                group.setGroupName(groupName);
                groupService.insert(group);
                groupService.addUserToGroup(userId, groupName);
            // groupService.addCategoryPermissionToGroup(groupName,"update:"+group.getGroupId());
            // groupService.addCategoryPermissionToGroup(groupName, "delete:" + group.getGroupId());
            // groupService.addCategoryPermissionToGroup(groupName,"find:"+group.getGroupId());
            } else {
                for (Group i : groupList) {
                    groupService.addUserToGroup(userId, i.getGroupName());
                }
            }
        } catch (UserNotFoundException e) {
            logger.info("用户未找到");
        } catch (GroupNotFountException e) {
            logger.info("用户组未找到");
        } catch (InformationException e) {
            logger.info("用户组信息不正确");
        } catch (PermissionNotFoundException e) {
            logger.info("用户组未找到");
        } catch (Exception e) {
            logger.info("信息错误");
        }
    }
}
Also used : UserNotFoundException(com.ganster.cms.core.exception.UserNotFoundException) InformationException(com.ganster.cms.auth.Exception.InformationException) Group(com.ganster.cms.core.pojo.Group) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) GroupExample(com.ganster.cms.core.pojo.GroupExample) RInformationUtil(com.ganster.cms.auth.util.RInformationUtil) PermissionNotFoundException(com.ganster.cms.core.exception.PermissionNotFoundException) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) PermissionNotFoundException(com.ganster.cms.core.exception.PermissionNotFoundException) UserNotFoundException(com.ganster.cms.core.exception.UserNotFoundException) InformationException(com.ganster.cms.auth.Exception.InformationException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Group

use of com.ganster.cms.core.pojo.Group 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)

Aggregations

Group (com.ganster.cms.core.pojo.Group)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 GroupNotFountException (com.ganster.cms.core.exception.GroupNotFountException)7 InformationException (com.ganster.cms.auth.Exception.InformationException)6 GroupExample (com.ganster.cms.core.pojo.GroupExample)6 ArrayList (java.util.ArrayList)6 Subject (org.apache.shiro.subject.Subject)6 UserNotFoundException (com.ganster.cms.core.exception.UserNotFoundException)5 Permission (com.ganster.cms.core.pojo.Permission)5 PInformationUtil (com.ganster.cms.auth.util.PInformationUtil)4 User (com.ganster.cms.core.pojo.User)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 AjaxData (com.ganster.cms.admin.dto.AjaxData)3 PermissionNotFoundException (com.ganster.cms.core.exception.PermissionNotFoundException)3 RInformationUtil (com.ganster.cms.auth.util.RInformationUtil)2 UserExample (com.ganster.cms.core.pojo.UserExample)2 GroupService (com.ganster.cms.core.service.GroupService)2 UserService (com.ganster.cms.core.service.UserService)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2