Search in sources :

Example 11 with Role

use of com.liferay.portal.model.Role in project liferay-ide by liferay.

the class UserLocalServiceImpl method getRoleUsersCount.

/**
 * Returns the number of users with the status belonging to the role.
 *
 * @param  roleId the primary key of the role
 * @param  status the workflow status
 * @return the number of users with the status belonging to the role
 * @throws PortalException if an role with the primary key could not be
 *         found
 * @throws SystemException if a system exception occurred
 */
@Override
public int getRoleUsersCount(long roleId, int status) throws PortalException, SystemException {
    Role role = rolePersistence.findByPrimaryKey(roleId);
    LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
    params.put("usersRoles", new Long(roleId));
    return searchCount(role.getCompanyId(), null, status, params);
}
Also used : Role(com.liferay.portal.model.Role) UserGroupRole(com.liferay.portal.model.UserGroupRole) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with Role

use of com.liferay.portal.model.Role in project liferay-ide by liferay.

the class UserLocalServiceImpl method addDefaultRoles.

/**
 * Adds the user to the default roles, unless the user already has these
 * roles. The default roles can be specified in
 * <code>portal.properties</code> with the key
 * <code>admin.default.role.names</code>.
 *
 * @param  userId the primary key of the user
 * @throws PortalException if a user with the primary key could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public void addDefaultRoles(long userId) throws PortalException, SystemException {
    User user = userPersistence.findByPrimaryKey(userId);
    Set<Long> roleIdSet = new HashSet<Long>();
    String[] defaultRoleNames = PrefsPropsUtil.getStringArray(user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_ROLE_NAMES, StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_ROLE_NAMES);
    for (String defaultRoleName : defaultRoleNames) {
        try {
            Role role = rolePersistence.findByC_N(user.getCompanyId(), defaultRoleName);
            if (!userPersistence.containsRole(userId, role.getRoleId())) {
                roleIdSet.add(role.getRoleId());
            }
        } catch (NoSuchRoleException nsre) {
        }
    }
    long[] roleIds = ArrayUtil.toArray(roleIdSet.toArray(new Long[roleIdSet.size()]));
    roleIds = UsersAdminUtil.addRequiredRoles(user, roleIds);
    userPersistence.addRoles(userId, roleIds);
}
Also used : Role(com.liferay.portal.model.Role) UserGroupRole(com.liferay.portal.model.UserGroupRole) NoSuchRoleException(com.liferay.portal.NoSuchRoleException) User(com.liferay.portal.model.User) HashSet(java.util.HashSet)

Example 13 with Role

use of com.liferay.portal.model.Role in project liferay-ide by liferay.

the class UserLocalServiceImpl method unsetRoleUsers.

/**
 * Removes the users from the role.
 *
 * @param  roleId the primary key of the role
 * @param  userIds the primary keys of the users
 * @throws PortalException if a portal exception occurred
 * @throws SystemException if a system exception occurred
 */
@Override
public void unsetRoleUsers(long roleId, long[] userIds) throws PortalException, SystemException {
    Role role = rolePersistence.findByPrimaryKey(roleId);
    String roleName = role.getName();
    if (roleName.equals(RoleConstants.USER) || (roleName.equals(RoleConstants.ADMINISTRATOR) && getRoleUsersCount(role.getRoleId()) <= 1)) {
        return;
    }
    rolePersistence.removeUsers(roleId, userIds);
    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
    indexer.reindex(userIds);
    PermissionCacheUtil.clearCache();
}
Also used : Role(com.liferay.portal.model.Role) UserGroupRole(com.liferay.portal.model.UserGroupRole) Indexer(com.liferay.portal.kernel.search.Indexer)

Example 14 with Role

use of com.liferay.portal.model.Role in project liferay-ide by liferay.

the class UserLocalServiceImpl method addDefaultRolesAndTeams.

protected void addDefaultRolesAndTeams(long groupId, long[] userIds) throws PortalException, SystemException {
    List<Role> defaultSiteRoles = new ArrayList<Role>();
    Group group = groupLocalService.getGroup(groupId);
    UnicodeProperties typeSettingsProperties = group.getTypeSettingsProperties();
    long[] defaultSiteRoleIds = StringUtil.split(typeSettingsProperties.getProperty("defaultSiteRoleIds"), 0L);
    for (long defaultSiteRoleId : defaultSiteRoleIds) {
        Role defaultSiteRole = rolePersistence.fetchByPrimaryKey(defaultSiteRoleId);
        if (defaultSiteRole == null) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to find role " + defaultSiteRoleId);
            }
            continue;
        }
        defaultSiteRoles.add(defaultSiteRole);
    }
    List<Team> defaultTeams = new ArrayList<Team>();
    long[] defaultTeamIds = StringUtil.split(typeSettingsProperties.getProperty("defaultTeamIds"), 0L);
    for (long defaultTeamId : defaultTeamIds) {
        Team defaultTeam = teamPersistence.findByPrimaryKey(defaultTeamId);
        if (defaultTeam == null) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to find team " + defaultTeamId);
            }
            continue;
        }
        defaultTeams.add(defaultTeam);
    }
    for (long userId : userIds) {
        Set<Long> userRoleIdsSet = new HashSet<Long>();
        for (Role role : defaultSiteRoles) {
            if (!userPersistence.containsRole(userId, role.getRoleId())) {
                userRoleIdsSet.add(role.getRoleId());
            }
        }
        long[] userRoleIds = ArrayUtil.toArray(userRoleIdsSet.toArray(new Long[userRoleIdsSet.size()]));
        userGroupRoleLocalService.addUserGroupRoles(userId, groupId, userRoleIds);
        Set<Long> userTeamIdsSet = new HashSet<Long>();
        for (Team team : defaultTeams) {
            if (!userPersistence.containsTeam(userId, team.getTeamId())) {
                userTeamIdsSet.add(team.getTeamId());
            }
        }
        long[] userTeamIds = ArrayUtil.toArray(userTeamIdsSet.toArray(new Long[userTeamIdsSet.size()]));
        userPersistence.addTeams(userId, userTeamIds);
    }
}
Also used : Role(com.liferay.portal.model.Role) UserGroupRole(com.liferay.portal.model.UserGroupRole) Group(com.liferay.portal.model.Group) UserGroup(com.liferay.portal.model.UserGroup) UnicodeProperties(com.liferay.portal.kernel.util.UnicodeProperties) ArrayList(java.util.ArrayList) Team(com.liferay.portal.model.Team) HashSet(java.util.HashSet)

Example 15 with Role

use of com.liferay.portal.model.Role in project liferay-ide by liferay.

the class SearchPermissionCheckerImpl method doGetPermissionQuery_6.

protected Query doGetPermissionQuery_6(long companyId, long[] groupIds, long userId, String className, Query query, SearchContext searchContext, AdvancedPermissionChecker advancedPermissionChecker, List<Group> groups, List<Role> roles, List<UserGroupRole> userGroupRoles, Map<Long, List<Role>> groupIdsToRoles) throws Exception {
    BooleanQuery permissionQuery = BooleanQueryFactoryUtil.create(searchContext);
    if (userId > 0) {
        permissionQuery.addTerm(Field.USER_ID, userId);
    }
    BooleanQuery groupsQuery = BooleanQueryFactoryUtil.create(searchContext);
    BooleanQuery rolesQuery = BooleanQueryFactoryUtil.create(searchContext);
    for (Role role : roles) {
        String roleName = role.getName();
        if (roleName.equals(RoleConstants.ADMINISTRATOR)) {
            return query;
        }
        if (ResourcePermissionLocalServiceUtil.hasResourcePermission(companyId, className, ResourceConstants.SCOPE_COMPANY, String.valueOf(companyId), role.getRoleId(), ActionKeys.VIEW)) {
            return query;
        }
        if ((role.getType() == RoleConstants.TYPE_REGULAR) && ResourcePermissionLocalServiceUtil.hasResourcePermission(companyId, className, ResourceConstants.SCOPE_GROUP_TEMPLATE, String.valueOf(GroupConstants.DEFAULT_PARENT_GROUP_ID), role.getRoleId(), ActionKeys.VIEW)) {
            return query;
        }
        for (Group group : groups) {
            if (ResourcePermissionLocalServiceUtil.hasResourcePermission(companyId, className, ResourceConstants.SCOPE_GROUP, String.valueOf(group.getGroupId()), role.getRoleId(), ActionKeys.VIEW)) {
                groupsQuery.addTerm(Field.GROUP_ID, group.getGroupId());
            }
            if ((role.getType() != RoleConstants.TYPE_REGULAR) && ResourcePermissionLocalServiceUtil.hasResourcePermission(companyId, className, ResourceConstants.SCOPE_GROUP_TEMPLATE, String.valueOf(GroupConstants.DEFAULT_PARENT_GROUP_ID), role.getRoleId(), ActionKeys.VIEW)) {
                List<Role> groupRoles = groupIdsToRoles.get(group.getGroupId());
                if (groupRoles.contains(role)) {
                    groupsQuery.addTerm(Field.GROUP_ID, group.getGroupId());
                }
            }
            if (group.isSite() && !roleName.equals(RoleConstants.SITE_MEMBER) && (role.getType() == RoleConstants.TYPE_SITE)) {
                rolesQuery.addTerm(Field.GROUP_ROLE_ID, group.getGroupId() + StringPool.DASH + role.getRoleId());
            }
        }
        rolesQuery.addTerm(Field.ROLE_ID, role.getRoleId());
    }
    for (Group group : groups) {
        addRequiredMemberRole(group, rolesQuery);
    }
    for (UserGroupRole userGroupRole : userGroupRoles) {
        rolesQuery.addTerm(Field.GROUP_ROLE_ID, userGroupRole.getGroupId() + StringPool.DASH + userGroupRole.getRoleId());
    }
    if (groupsQuery.hasClauses()) {
        permissionQuery.add(groupsQuery, BooleanClauseOccur.SHOULD);
    }
    if (rolesQuery.hasClauses()) {
        permissionQuery.add(rolesQuery, BooleanClauseOccur.SHOULD);
    }
    BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
    fullQuery.add(query, BooleanClauseOccur.MUST);
    fullQuery.add(permissionQuery, BooleanClauseOccur.MUST);
    return fullQuery;
}
Also used : UserGroupRole(com.liferay.portal.model.UserGroupRole) Role(com.liferay.portal.model.Role) UserGroupRole(com.liferay.portal.model.UserGroupRole) BooleanQuery(com.liferay.portal.kernel.search.BooleanQuery) Group(com.liferay.portal.model.Group)

Aggregations

Role (com.liferay.portal.model.Role)16 UserGroupRole (com.liferay.portal.model.UserGroupRole)11 Group (com.liferay.portal.model.Group)5 Indexer (com.liferay.portal.kernel.search.Indexer)4 User (com.liferay.portal.model.User)3 ThemeDisplay (com.liferay.portal.theme.ThemeDisplay)3 ArrayList (java.util.ArrayList)3 UserGroup (com.liferay.portal.model.UserGroup)2 ExpandoColumn (com.liferay.portlet.expando.model.ExpandoColumn)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 NoSuchRoleException (com.liferay.portal.NoSuchRoleException)1 PortalException (com.liferay.portal.kernel.exception.PortalException)1 SystemException (com.liferay.portal.kernel.exception.SystemException)1 BooleanQuery (com.liferay.portal.kernel.search.BooleanQuery)1 SearchPermissionChecker (com.liferay.portal.kernel.search.SearchPermissionChecker)1 UnicodeProperties (com.liferay.portal.kernel.util.UnicodeProperties)1 UniqueList (com.liferay.portal.kernel.util.UniqueList)1 Layout (com.liferay.portal.model.Layout)1