Search in sources :

Example 1 with NoSuchRoleException

use of com.liferay.portal.NoSuchRoleException 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)

Aggregations

NoSuchRoleException (com.liferay.portal.NoSuchRoleException)1 Role (com.liferay.portal.model.Role)1 User (com.liferay.portal.model.User)1 UserGroupRole (com.liferay.portal.model.UserGroupRole)1 HashSet (java.util.HashSet)1