Search in sources :

Example 6 with Role

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

the class SearchPermissionCheckerImpl method doAddPermissionFields_6.

protected void doAddPermissionFields_6(long companyId, long groupId, String className, String classPK, Document doc) throws Exception {
    Group group = null;
    if (groupId > 0) {
        group = GroupLocalServiceUtil.getGroup(groupId);
    }
    List<Role> roles = ListUtil.copy(ResourceActionsUtil.getRoles(companyId, group, className, null));
    if (groupId > 0) {
        List<Role> teamRoles = RoleLocalServiceUtil.getTeamRoles(groupId);
        roles.addAll(teamRoles);
    }
    long[] roleIdsArray = new long[roles.size()];
    for (int i = 0; i < roleIdsArray.length; i++) {
        Role role = roles.get(i);
        roleIdsArray[i] = role.getRoleId();
    }
    boolean[] hasResourcePermissions = null;
    if (ResourceBlockLocalServiceUtil.isSupported(className)) {
        ResourceBlockIdsBag resourceBlockIdsBag = ResourceBlockLocalServiceUtil.getResourceBlockIdsBag(companyId, groupId, className, roleIdsArray);
        long actionId = ResourceBlockLocalServiceUtil.getActionId(className, ActionKeys.VIEW);
        List<Long> resourceBlockIds = resourceBlockIdsBag.getResourceBlockIds(actionId);
        hasResourcePermissions = new boolean[roleIdsArray.length];
        for (long resourceBlockId : resourceBlockIds) {
            for (int i = 0; i < roleIdsArray.length; i++) {
                int count = ResourceBlockPermissionLocalServiceUtil.getResourceBlockPermissionsCount(resourceBlockId, roleIdsArray[i]);
                hasResourcePermissions[i] = (count > 0);
            }
        }
    } else {
        hasResourcePermissions = ResourcePermissionLocalServiceUtil.hasResourcePermissions(companyId, className, ResourceConstants.SCOPE_INDIVIDUAL, classPK, roleIdsArray, ActionKeys.VIEW);
    }
    List<Long> roleIds = new ArrayList<Long>();
    List<String> groupRoleIds = new ArrayList<String>();
    for (int i = 0; i < hasResourcePermissions.length; i++) {
        if (!hasResourcePermissions[i]) {
            continue;
        }
        Role role = roles.get(i);
        if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) || (role.getType() == RoleConstants.TYPE_SITE)) {
            groupRoleIds.add(groupId + StringPool.DASH + role.getRoleId());
        } else {
            roleIds.add(role.getRoleId());
        }
    }
    doc.addKeyword(Field.ROLE_ID, roleIds.toArray(new Long[roleIds.size()]));
    doc.addKeyword(Field.GROUP_ROLE_ID, groupRoleIds.toArray(new String[groupRoleIds.size()]));
}
Also used : Group(com.liferay.portal.model.Group) ArrayList(java.util.ArrayList) UserGroupRole(com.liferay.portal.model.UserGroupRole) Role(com.liferay.portal.model.Role) ResourceBlockIdsBag(com.liferay.portal.security.permission.ResourceBlockIdsBag)

Example 7 with Role

use of com.liferay.portal.model.Role in project sw360portal by sw360.

the class UserPortletUtils method addLiferayUser.

public static User addLiferayUser(PortletRequest request, String firstName, String lastName, String emailAddress, String organizationName, String roleName, boolean male, String externalId, String password, boolean passwordEncrypted, boolean activateImmediately) throws SystemException, PortalException {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    long companyId = themeDisplay.getCompanyId();
    long organizationId = OrganizationLocalServiceUtil.getOrganizationId(companyId, organizationName);
    final Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
    long roleId = role.getRoleId();
    return addLiferayUser(request, firstName, lastName, emailAddress, organizationId, roleId, male, externalId, password, passwordEncrypted, activateImmediately);
}
Also used : Role(com.liferay.portal.model.Role) ThemeDisplay(com.liferay.portal.theme.ThemeDisplay)

Example 8 with Role

use of com.liferay.portal.model.Role in project sw360portal by sw360.

the class UserPortletUtils method addLiferayUser.

private static User addLiferayUser(PortletRequest request, String firstName, String lastName, String emailAddress, long organizationId, long roleId, boolean isMale, String externalId, String password, boolean passwordEncrypted, boolean activateImmediately) {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    String screenName = firstName + lastName;
    long companyId = themeDisplay.getCompanyId();
    try {
        if (userAlreadyExists(request, emailAddress, externalId, screenName, companyId)) {
            return null;
        }
    } catch (PortalException | SystemException e) {
        log.error(e);
        // won't try to create user if even checking for existing user failed
        return null;
    }
    try {
        ServiceContext serviceContext = ServiceContextFactory.getInstance(request);
        long[] roleIds = roleId == 0 ? new long[] {} : new long[] { roleId };
        long[] organizationIds = organizationId == 0 ? new long[] {} : new long[] { organizationId };
        long[] userGroupIds = null;
        long currentUserId = themeDisplay.getUserId();
        User user = UserLocalServiceUtil.addUser(currentUserId, /*creator*/
        companyId, false, /*autoPassword*/
        password, password, false, /*autoScreenName*/
        screenName, emailAddress, 0, /*facebookId*/
        externalId, /*openId*/
        themeDisplay.getLocale(), firstName, "", /*middleName*/
        lastName, 0, /*prefixId*/
        0, /*suffixId*/
        isMale, 4, /*birthdayMonth*/
        12, /*birthdayDay*/
        1959, /*birthdayYear*/
        "", /*jobTitle*/
        null, /*groupIds*/
        organizationIds, roleIds, userGroupIds, false, /*sendEmail*/
        serviceContext);
        user.setPasswordReset(false);
        if (passwordEncrypted) {
            user.setPassword(password);
            user.setPasswordEncrypted(true);
        }
        Role role = RoleLocalServiceUtil.getRole(roleId);
        RoleUtil.addUser(role.getRoleId(), user.getUserId());
        UserLocalServiceUtil.updateUser(user);
        RoleLocalServiceUtil.updateRole(role);
        UserLocalServiceUtil.updateStatus(user.getUserId(), activateImmediately ? WorkflowConstants.STATUS_APPROVED : WorkflowConstants.STATUS_INACTIVE);
        Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
        indexer.reindex(user);
        return user;
    } catch (PortalException | SystemException e) {
        log.error(e);
        return null;
    }
}
Also used : Role(com.liferay.portal.model.Role) User(com.liferay.portal.model.User) Indexer(com.liferay.portal.kernel.search.Indexer) SystemException(com.liferay.portal.kernel.exception.SystemException) PortalException(com.liferay.portal.kernel.exception.PortalException) ThemeDisplay(com.liferay.portal.theme.ThemeDisplay)

Example 9 with Role

use of com.liferay.portal.model.Role in project sw360portal by sw360.

the class CustomFieldHelper method ensureUserCustomFieldExists.

private static void ensureUserCustomFieldExists(com.liferay.portal.model.User liferayUser, String customFieldName, int customFieldType) throws PortalException, SystemException {
    ExpandoBridge exp = liferayUser.getExpandoBridge();
    if (!exp.hasAttribute(customFieldName)) {
        exp.addAttribute(customFieldName, customFieldType, false);
        long companyId = liferayUser.getCompanyId();
        ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(companyId, exp.getClassName(), ExpandoTableConstants.DEFAULT_TABLE_NAME, customFieldName);
        String[] roleNames = new String[] { RoleConstants.USER, RoleConstants.POWER_USER };
        for (String roleName : roleNames) {
            Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
            if (role != null && column != null) {
                ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(column.getColumnId()), role.getRoleId(), new String[] { ActionKeys.VIEW, ActionKeys.UPDATE });
            }
        }
    }
}
Also used : Role(com.liferay.portal.model.Role) ExpandoColumn(com.liferay.portlet.expando.model.ExpandoColumn) ExpandoBridge(com.liferay.portlet.expando.model.ExpandoBridge)

Example 10 with Role

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

the class UserLocalServiceImpl method addDefaultAdminUser.

/**
 * Adds a default admin user for the company.
 *
 * @param  companyId the primary key of the user's company
 * @param  screenName the user's screen name
 * @param  emailAddress the user's email address
 * @param  locale the user's locale
 * @param  firstName the user's first name
 * @param  middleName the user's middle name
 * @param  lastName the user's last name
 * @return the new default admin user
 * @throws PortalException n if a portal exception occurred
 * @throws SystemException if a system exception occurred
 */
@Override
public User addDefaultAdminUser(long companyId, String screenName, String emailAddress, Locale locale, String firstName, String middleName, String lastName) throws PortalException, SystemException {
    long creatorUserId = 0;
    boolean autoPassword = false;
    String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
    String password2 = password1;
    boolean autoScreenName = false;
    screenName = getLogin(screenName);
    for (int i = 1; ; i++) {
        User screenNameUser = userPersistence.fetchByC_SN(companyId, screenName);
        if (screenNameUser == null) {
            break;
        }
        screenName = screenName + i;
    }
    long facebookId = 0;
    String openId = StringPool.BLANK;
    int prefixId = 0;
    int suffixId = 0;
    boolean male = true;
    int birthdayMonth = Calendar.JANUARY;
    int birthdayDay = 1;
    int birthdayYear = 1970;
    String jobTitle = StringPool.BLANK;
    Group guestGroup = groupLocalService.getGroup(companyId, GroupConstants.GUEST);
    long[] groupIds = { guestGroup.getGroupId() };
    long[] organizationIds = null;
    Role adminRole = roleLocalService.getRole(companyId, RoleConstants.ADMINISTRATOR);
    Role powerUserRole = roleLocalService.getRole(companyId, RoleConstants.POWER_USER);
    long[] roleIds = { adminRole.getRoleId(), powerUserRole.getRoleId() };
    long[] userGroupIds = null;
    boolean sendEmail = false;
    ServiceContext serviceContext = new ServiceContext();
    User defaultAdminUser = addUser(creatorUserId, companyId, autoPassword, password1, password2, autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName, middleName, lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
    updateEmailAddressVerified(defaultAdminUser.getUserId(), true);
    updateLastLogin(defaultAdminUser.getUserId(), defaultAdminUser.getLoginIP());
    updatePasswordReset(defaultAdminUser.getUserId(), false);
    return defaultAdminUser;
}
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) User(com.liferay.portal.model.User) ServiceContext(com.liferay.portal.service.ServiceContext)

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