Example usage for com.liferay.portal.kernel.service UserGroupLocalServiceUtil addUserUserGroups

List of usage examples for com.liferay.portal.kernel.service UserGroupLocalServiceUtil addUserUserGroups

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.service UserGroupLocalServiceUtil addUserUserGroups.

Prototype

public static void addUserUserGroups(long userId, long[] userGroupIds) 

Source Link

Usage

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

License:Open Source License

@Override
public void assignUserUserGroups(long userId, long[] userGroupIds) throws PortalException {

    _log.info("Assigning user with id " + userId + " to multiple teams " + userGroupIds);

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

    this.checkMultiplePersonalPermission(new String[] { AngularActionKeys.UPDATE_USER_GROUP,
            AngularActionKeys.ASSIGN_USER_TO_USER_GROUP, AngularActionKeys.UPDATE_USER });

    com.liferay.portal.kernel.model.User user = UserLocalServiceUtil.getUser(userId);
    this.crossCompanyCheck(user.getCompanyId());

    if (userGroupIds != null) {
        for (long userGroupId : userGroupIds) {

            // Cross company checks
            com.liferay.portal.kernel.model.UserGroup userGroup = UserGroupLocalServiceUtil
                    .getUserGroup(userGroupId);
            this.crossCompanyCheck(userGroup.getCompanyId());

            // Resource checks
            this.hasResourcePermission(userGroupId, ActionKeys.ASSIGN_MEMBERS);
        }//w ww  .j  a  v a 2s .  co  m

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

        UserGroupLocalServiceUtil.addUserUserGroups(userId, userGroupIds);
    }

}

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

License:Open Source License

@Override
public void updateUserUserGroups(long userId, long[] userGroupIds) throws PortalException {

    _log.info("Updating user groups ... ");

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

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

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

    long creatorId = 0;

    try {/*w w w .jav a  2s .  c  o  m*/
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, userGroupIds,
            null);

    // User group membership policy

    long[] oldUserGroupIds = user.getUserGroupIds();

    List<Long> addUserGroupIds = new ArrayList<>();
    List<Long> removeUserGroupIds = Collections.emptyList();

    if (userGroupIds != null) {
        removeUserGroupIds = ListUtil.toList(oldUserGroupIds);

        userGroupIds = angularUserGroupService.checkUserGroupIds(userId, userGroupIds);

        for (long userGroupId : userGroupIds) {
            if (ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
                removeUserGroupIds.remove(userGroupId);
            } else {
                addUserGroupIds.add(userGroupId);
            }
        }

        UserGroupLocalServiceUtil.addUserUserGroups(user.getUserId(), userGroupIds);

        if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
            UserGroupMembershipPolicyUtil.checkMembership(new long[] { userId },
                    ArrayUtil.toLongArray(addUserGroupIds), ArrayUtil.toLongArray(removeUserGroupIds));
        }
    }
}