public void editRoleAssignments(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    long roleId = ParamUtil.getLong(actionRequest, "roleId");
    Role role = RoleLocalServiceUtil.getRole(roleId);

    if (role.getName().equals(RoleConstants.OWNER)) {
      throw new RoleAssignmentException(role.getName());
    }

    long[] addUserIds = StringUtil.split(ParamUtil.getString(actionRequest, "addUserIds"), 0L);
    long[] removeUserIds =
        StringUtil.split(ParamUtil.getString(actionRequest, "removeUserIds"), 0L);

    if (!ArrayUtil.isEmpty(addUserIds) || !ArrayUtil.isEmpty(removeUserIds)) {

      UserServiceUtil.addRoleUsers(roleId, addUserIds);
      UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
    }

    long[] addGroupIds = StringUtil.split(ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
    long[] removeGroupIds =
        StringUtil.split(ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);

    if (!ArrayUtil.isEmpty(addGroupIds) || !ArrayUtil.isEmpty(removeGroupIds)) {

      GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
      GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
    }
  }
  protected void updatePasswordPolicyUsers(ActionRequest actionRequest) throws Exception {

    long passwordPolicyId = ParamUtil.getLong(actionRequest, "passwordPolicyId");

    long[] addUserIds = StringUtil.split(ParamUtil.getString(actionRequest, "addUserIds"), 0L);
    long[] removeUserIds =
        StringUtil.split(ParamUtil.getString(actionRequest, "removeUserIds"), 0L);

    UserServiceUtil.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
    UserServiceUtil.unsetPasswordPolicyUsers(passwordPolicyId, removeUserIds);
  }
  @Test(expected = MembershipPolicyException.class)
  public void testAssignUsersToForbiddenGroup() throws Exception {
    long[] forbiddenGroupIds = addForbiddenGroups();

    UserServiceUtil.addGroupUsers(
        forbiddenGroupIds[0], addUsers(), ServiceContextTestUtil.getServiceContext());
  }
  public AssetRenderer getAssetRenderer(long classPK, int type)
      throws PortalException, SystemException {

    User user = UserServiceUtil.getUserById(classPK);

    return new UserAssetRenderer(user);
  }
  @Test
  public void testPropagateWhenAssigningUsersToRequiredGroup() throws Exception {

    long[] requiredGroupIds = addRequiredGroups();

    UserServiceUtil.addGroupUsers(
        requiredGroupIds[0], addUsers(), ServiceContextTestUtil.getServiceContext());

    Assert.assertTrue(isPropagateMembership());
  }
  @Test(expected = MembershipPolicyException.class)
  public void testUnassignUsersFromRequiredGroup() throws Exception {
    long[] requiredGroupIds = addRequiredGroups();

    User user = MembershipPolicyTestUtil.addUser(null, null, requiredGroupIds, null);

    UserServiceUtil.unsetGroupUsers(
        requiredGroupIds[0],
        new long[] {user.getUserId()},
        ServiceContextTestUtil.getServiceContext());
  }
  protected void updateEmailAddress(HttpServletRequest request) throws Exception {

    long userId = PortalUtil.getUserId(request);
    String password = AdminUtil.getUpdateUserPassword(request, userId);
    String emailAddress1 = ParamUtil.getString(request, "emailAddress1");
    String emailAddress2 = ParamUtil.getString(request, "emailAddress2");

    ServiceContext serviceContext = ServiceContextFactory.getInstance(request);

    UserServiceUtil.updateEmailAddress(
        userId, password, emailAddress1, emailAddress2, serviceContext);
  }
  @Test
  public void testAssignUsersToRequiredGroup() throws Exception {
    long[] requiredGroupIds = addRequiredGroups();

    int initialGroupUsersCount = UserLocalServiceUtil.getGroupUsersCount(requiredGroupIds[0]);

    UserServiceUtil.addGroupUsers(
        requiredGroupIds[0], addUsers(), ServiceContextTestUtil.getServiceContext());

    Assert.assertEquals(
        initialGroupUsersCount + 2, UserLocalServiceUtil.getGroupUsersCount(requiredGroupIds[0]));
    Assert.assertTrue(isPropagateMembership());
  }
  @Test
  public void testUnassignUsersFromGroup() throws Exception {
    long[] standardGroupIds = addStandardGroups();

    User user = MembershipPolicyTestUtil.addUser(null, null, standardGroupIds, null);

    int initialUserGroupCount = UserLocalServiceUtil.getGroupUsersCount(standardGroupIds[0]);

    UserServiceUtil.unsetGroupUsers(
        standardGroupIds[0],
        new long[] {user.getUserId()},
        ServiceContextTestUtil.getServiceContext());

    Assert.assertEquals(
        initialUserGroupCount - 1, UserLocalServiceUtil.getGroupUsersCount(standardGroupIds[0]));
    Assert.assertTrue(isPropagateMembership());
  }
  protected JSONObject updateIncompleteUser(
      ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(User.class.getName(), actionRequest);

    boolean autoPassword = true;
    String password1 = null;
    String password2 = null;
    boolean autoScreenName = false;
    String screenName = null;
    String emailAddress = ParamUtil.getString(actionRequest, "emailAddress");
    long facebookId = 0;
    String openId = null;
    String firstName = null;
    String middleName = null;
    String lastName = null;
    int prefixId = 0;
    int suffixId = 0;
    boolean male = true;
    int birthdayMonth = 0;
    int birthdayDay = 1;
    int birthdayYear = 1970;
    String jobTitle = null;
    boolean updateUserInformation = false;
    boolean sendEmail = true;

    User user =
        UserServiceUtil.updateIncompleteUser(
            themeDisplay.getCompanyId(),
            autoPassword,
            password1,
            password2,
            autoScreenName,
            screenName,
            emailAddress,
            facebookId,
            openId,
            themeDisplay.getLocale(),
            firstName,
            middleName,
            lastName,
            prefixId,
            suffixId,
            male,
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            jobTitle,
            updateUserInformation,
            sendEmail,
            serviceContext);

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
      jsonObject.put("userStatus", "user_added");
    } else {
      jsonObject.put("userStatus", "user_pending");
    }

    return jsonObject;
  }
  protected void addAnonymousUser(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    boolean autoPassword = true;
    String password1 = null;
    String password2 = null;
    boolean autoScreenName = true;
    String screenName = null;
    String emailAddress = ParamUtil.getString(actionRequest, "emailAddress");
    long facebookId = 0;
    String openId = StringPool.BLANK;
    String firstName = ParamUtil.getString(actionRequest, "firstName");
    String lastName = ParamUtil.getString(actionRequest, "lastName");
    int prefixId = 0;
    int suffixId = 0;
    boolean male = true;
    int birthdayMonth = 0;
    int birthdayDay = 1;
    int birthdayYear = 1970;
    String jobTitle = null;
    long[] groupIds = null;
    long[] organizationIds = null;
    long[] roleIds = null;
    long[] userGroupIds = null;
    boolean sendEmail = false;

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(User.class.getName(), actionRequest);

    serviceContext.setAttribute("anonymousUser", true);

    if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
      CaptchaUtil.check(actionRequest);
    }

    User user =
        UserServiceUtil.addUser(
            themeDisplay.getCompanyId(),
            autoPassword,
            password1,
            password2,
            autoScreenName,
            screenName,
            emailAddress,
            facebookId,
            openId,
            themeDisplay.getLocale(),
            firstName,
            null,
            lastName,
            prefixId,
            suffixId,
            male,
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            jobTitle,
            groupIds,
            organizationIds,
            roleIds,
            userGroupIds,
            sendEmail,
            serviceContext);

    UserLocalServiceUtil.updateStatus(user.getUserId(), WorkflowConstants.STATUS_INCOMPLETE);

    // Session messages

    SessionMessages.add(request, "userAdded", user.getEmailAddress());
    SessionMessages.add(request, "userAddedPassword", user.getPasswordUnencrypted());
  }