Example usage for com.liferay.portal.kernel.security.permission ActionKeys ADD_USER

List of usage examples for com.liferay.portal.kernel.security.permission ActionKeys ADD_USER

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.security.permission ActionKeys ADD_USER.

Prototype

String ADD_USER

To view the source code for com.liferay.portal.kernel.security.permission ActionKeys ADD_USER.

Click Source Link

Usage

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public User addNewUser(String screenName, String emailAddress, long companyId, String locale, long[] groupIds,
        long[] organizationIds, long[] roleIds, long[] userGroupIds) throws PortalException {
    User newUser = null;/*from www .j av  a  2  s .  c o  m*/
    long creatorId = 0;

    _log.info("Adding new user ...");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_USER);

    _log.debug("    .... processing ...");

    try {

        try {
            creatorId = this.getGuestOrUserId();
        } catch (PrincipalException pe) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to get current user ID", pe);
            }
        }

        boolean autoScreenName = false;

        if (StringUtils.isEmpty(screenName)) {
            _log.warn("No screen name given! Switching to auto screen name!");
            autoScreenName = true;
            screenName = "";
        }

        if (StringUtils.isEmpty(emailAddress)) {
            _log.warn("No email address given!");
            throw new PortalException("A email address is needed for new users!");
        }

        this.validateEmail(emailAddress, null);

        User existingUser = UserLocalServiceUtil.fetchUserByEmailAddress(companyId, emailAddress);

        if (existingUser == null) {

            ServiceContext serviceContext = new ServiceContext();

            this.checkAddUserPermission(creatorId, companyId, emailAddress, groupIds, organizationIds, roleIds,
                    userGroupIds, serviceContext);

            // Null safe actions
            if (groupIds == null) {
                groupIds = new long[0];
            }

            if (organizationIds == null) {
                organizationIds = new long[0];
            }

            if (roleIds == null) {
                roleIds = new long[0];
            }

            if (userGroupIds == null) {
                userGroupIds = new long[0];
            }

            _log.info("Creating new user with email address: " + emailAddress);

            boolean autoPassword = true;
            String password1 = "";
            String password2 = "";
            long facebookId = 0;
            String openId = "";
            Locale loc = ServiceUtils.getLocaleFromString(locale);
            String firstName = INVISIBLE_PLACEHOLDER;
            String middleName = "";
            String lastName = INVISIBLE_PLACEHOLDER;
            int prefixId = 0;
            int suffixId = 0;
            boolean male = false;

            int birthdayMonth = 1;
            int birthdayDay = 1;
            int birthdayYear = 1970;
            String jobTitle = "";

            boolean sendEmail = true;

            boolean workflowEnabled = WorkflowThreadLocal.isEnabled();

            try {

                newUser = UserLocalServiceUtil.addUserWithWorkflow(creatorId, companyId, autoPassword,
                        password1, password2, autoScreenName, screenName, emailAddress, facebookId, openId, loc,
                        firstName, middleName, lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
                        birthdayYear, jobTitle, groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
                        null);

                _log.debug("New user record: " + newUser.toString());

            } finally {
                WorkflowThreadLocal.setEnabled(workflowEnabled);
            }

        } else {
            _log.error(
                    "User cannot be created! Found already the following account: " + existingUser.toString());
            throw new PortalException("Fatal! User already exists");
        }
        // Send validation mail

    } catch (PortalException pe) {
        _log.error(pe);
        throw pe;
    }

    return newUser;
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

protected void checkUpdateUserPermission(long updateUserId, long userId, long companyId, long[] groupIds,
        long[] organizationIds, long[] roleIds, long[] userGroupIds, ServiceContext serviceContext)
        throws PortalException {

    Company company = CompanyLocalServiceUtil.getCompany(companyId);

    // Adding user must be member of everything he adds

    long[] userIds = new long[] { updateUserId };

    // Check if creator / updater is also part in the groups
    ServicePermissionUtil.checkMembership(userIds, groupIds, organizationIds, roleIds, userGroupIds);

    if (groupIds != null && groupIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_USER);
        angularGroupService.checkGroups(userId, groupIds);
    }//from  w  w w  .  j av a2 s.  c  om

    if (organizationIds != null && organizationIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ORGANIZATION);
        angularOrganizationService.checkOrganizations(userId, organizationIds);
    }

    if (roleIds != null && roleIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
        angularRoleService.checkRoles(userId, roleIds);
    }

    if (userGroupIds != null && userGroupIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_USER_GROUP);
        angularUserGroupService.checkUserGroupIds(userId, userGroupIds);
    }

    if (serviceContext == null) {
        serviceContext = new ServiceContext();
    }

    boolean anonymousUser = ParamUtil.getBoolean(serviceContext, "anonymousUser");

    long defaultUserId = userLocalService.getDefaultUserId(companyId);

    if (((updateUserId != 0) && (updateUserId != defaultUserId))
            || (!company.isStrangers() && !anonymousUser)) {

        UserPermissionUtil.check(getPermissionChecker(), updateUserId, ActionKeys.UPDATE_USER);

        UserPermissionUtil.check(getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
    }
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

protected void checkAddUserPermission(long creatorUserId, long companyId, String emailAddress, long[] groupIds,
        long[] organizationIds, long[] roleIds, long[] userGroupIds, ServiceContext serviceContext)
        throws PortalException {

    Company company = CompanyLocalServiceUtil.getCompany(companyId);

    // Check if user has an according right assigned with add user permission; Either he owns the right itself, by his roles or by his user roles
    User creator = this.getGuestOrUser();

    long[] userIds = new long[] { creatorUserId };

    // Adding user must be member of everything he adds
    ServicePermissionUtil.checkMembership(userIds, groupIds, organizationIds, roleIds, userGroupIds);

    if (groupIds != null && groupIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_TO_PAGE);
        angularGroupService.checkGroups(0, groupIds);
    }//from   w  w w . j a v  a2  s .  c  o  m

    if (organizationIds != null && organizationIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ORGANIZATION);
        angularOrganizationService.checkOrganizations(0, organizationIds);
    }

    if (roleIds != null && roleIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
        angularRoleService.checkRoles(0, roleIds);
    }

    if (userGroupIds != null && userGroupIds.length > 0) {
        PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_USER_GROUP);
        angularUserGroupService.checkUserGroupIds(0, userGroupIds);
    }

    boolean anonymousUser = ParamUtil.getBoolean(serviceContext, "anonymousUser");

    long defaultUserId = userLocalService.getDefaultUserId(companyId);

    if (((creatorUserId != 0) && (creatorUserId != defaultUserId))
            || (!company.isStrangers() && !anonymousUser)) {
        ServicePermissionUtil.checkAllPersonalPermissions(creator, ActionKeys.ADD_USER, companyId,
                getPermissionChecker(), User.class.getName());
    }

    if ((creatorUserId == 0) || (creatorUserId == defaultUserId)) {
        if (!company.isStrangersWithMx() && company.hasCompanyMx(emailAddress)) {

            throw new UserEmailAddressException.MustNotUseCompanyMx(emailAddress);
        }
    }
}