@Override
  public boolean contains(
      PermissionChecker permissionChecker, long userId, long[] organizationIds, String actionId) {

    if ((actionId.equals(ActionKeys.DELETE)
            || actionId.equals(ActionKeys.IMPERSONATE)
            || actionId.equals(ActionKeys.PERMISSIONS)
            || actionId.equals(ActionKeys.UPDATE))
        && PortalUtil.isOmniadmin(userId)
        && !permissionChecker.isOmniadmin()) {

      return false;
    }

    try {
      User user = null;

      if (userId != ResourceConstants.PRIMKEY_DNE) {
        user = UserLocalServiceUtil.getUserById(userId);

        Contact contact = user.getContact();

        if (permissionChecker.hasOwnerPermission(
                permissionChecker.getCompanyId(),
                User.class.getName(),
                userId,
                contact.getUserId(),
                actionId)
            || (permissionChecker.getUserId() == userId)) {

          return true;
        }
      }

      if (permissionChecker.hasPermission(0, User.class.getName(), userId, actionId)) {

        return true;
      }

      if (user == null) {
        return false;
      }

      if (organizationIds == null) {
        organizationIds = user.getOrganizationIds();
      }

      for (long organizationId : organizationIds) {
        Organization organization = OrganizationLocalServiceUtil.getOrganization(organizationId);

        if (OrganizationPermissionUtil.contains(
            permissionChecker, organization, ActionKeys.MANAGE_USERS)) {

          if (permissionChecker.getUserId() == user.getUserId()) {
            return true;
          }

          Group organizationGroup = organization.getGroup();

          // Organization administrators can only manage normal users.
          // Owners can only manage normal users and administrators.

          if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
              user.getUserId(),
              organizationGroup.getGroupId(),
              RoleConstants.ORGANIZATION_OWNER,
              true)) {

            continue;
          } else if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
                  user.getUserId(),
                  organizationGroup.getGroupId(),
                  RoleConstants.ORGANIZATION_ADMINISTRATOR,
                  true)
              && !UserGroupRoleLocalServiceUtil.hasUserGroupRole(
                  permissionChecker.getUserId(),
                  organizationGroup.getGroupId(),
                  RoleConstants.ORGANIZATION_OWNER,
                  true)) {

            continue;
          }

          return true;
        }
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    return false;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Contact newContact = _persistence.create(pk);

    newContact.setMvccVersion(RandomTestUtil.nextLong());

    newContact.setCompanyId(RandomTestUtil.nextLong());

    newContact.setUserId(RandomTestUtil.nextLong());

    newContact.setUserName(RandomTestUtil.randomString());

    newContact.setCreateDate(RandomTestUtil.nextDate());

    newContact.setModifiedDate(RandomTestUtil.nextDate());

    newContact.setClassNameId(RandomTestUtil.nextLong());

    newContact.setClassPK(RandomTestUtil.nextLong());

    newContact.setAccountId(RandomTestUtil.nextLong());

    newContact.setParentContactId(RandomTestUtil.nextLong());

    newContact.setEmailAddress(RandomTestUtil.randomString());

    newContact.setFirstName(RandomTestUtil.randomString());

    newContact.setMiddleName(RandomTestUtil.randomString());

    newContact.setLastName(RandomTestUtil.randomString());

    newContact.setPrefixId(RandomTestUtil.nextLong());

    newContact.setSuffixId(RandomTestUtil.nextLong());

    newContact.setMale(RandomTestUtil.randomBoolean());

    newContact.setBirthday(RandomTestUtil.nextDate());

    newContact.setSmsSn(RandomTestUtil.randomString());

    newContact.setFacebookSn(RandomTestUtil.randomString());

    newContact.setJabberSn(RandomTestUtil.randomString());

    newContact.setSkypeSn(RandomTestUtil.randomString());

    newContact.setTwitterSn(RandomTestUtil.randomString());

    newContact.setEmployeeStatusId(RandomTestUtil.randomString());

    newContact.setEmployeeNumber(RandomTestUtil.randomString());

    newContact.setJobTitle(RandomTestUtil.randomString());

    newContact.setJobClass(RandomTestUtil.randomString());

    newContact.setHoursOfOperation(RandomTestUtil.randomString());

    _contacts.add(_persistence.update(newContact));

    Contact existingContact = _persistence.findByPrimaryKey(newContact.getPrimaryKey());

    Assert.assertEquals(existingContact.getMvccVersion(), newContact.getMvccVersion());
    Assert.assertEquals(existingContact.getContactId(), newContact.getContactId());
    Assert.assertEquals(existingContact.getCompanyId(), newContact.getCompanyId());
    Assert.assertEquals(existingContact.getUserId(), newContact.getUserId());
    Assert.assertEquals(existingContact.getUserName(), newContact.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingContact.getCreateDate()),
        Time.getShortTimestamp(newContact.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingContact.getModifiedDate()),
        Time.getShortTimestamp(newContact.getModifiedDate()));
    Assert.assertEquals(existingContact.getClassNameId(), newContact.getClassNameId());
    Assert.assertEquals(existingContact.getClassPK(), newContact.getClassPK());
    Assert.assertEquals(existingContact.getAccountId(), newContact.getAccountId());
    Assert.assertEquals(existingContact.getParentContactId(), newContact.getParentContactId());
    Assert.assertEquals(existingContact.getEmailAddress(), newContact.getEmailAddress());
    Assert.assertEquals(existingContact.getFirstName(), newContact.getFirstName());
    Assert.assertEquals(existingContact.getMiddleName(), newContact.getMiddleName());
    Assert.assertEquals(existingContact.getLastName(), newContact.getLastName());
    Assert.assertEquals(existingContact.getPrefixId(), newContact.getPrefixId());
    Assert.assertEquals(existingContact.getSuffixId(), newContact.getSuffixId());
    Assert.assertEquals(existingContact.getMale(), newContact.getMale());
    Assert.assertEquals(
        Time.getShortTimestamp(existingContact.getBirthday()),
        Time.getShortTimestamp(newContact.getBirthday()));
    Assert.assertEquals(existingContact.getSmsSn(), newContact.getSmsSn());
    Assert.assertEquals(existingContact.getFacebookSn(), newContact.getFacebookSn());
    Assert.assertEquals(existingContact.getJabberSn(), newContact.getJabberSn());
    Assert.assertEquals(existingContact.getSkypeSn(), newContact.getSkypeSn());
    Assert.assertEquals(existingContact.getTwitterSn(), newContact.getTwitterSn());
    Assert.assertEquals(existingContact.getEmployeeStatusId(), newContact.getEmployeeStatusId());
    Assert.assertEquals(existingContact.getEmployeeNumber(), newContact.getEmployeeNumber());
    Assert.assertEquals(existingContact.getJobTitle(), newContact.getJobTitle());
    Assert.assertEquals(existingContact.getJobClass(), newContact.getJobClass());
    Assert.assertEquals(existingContact.getHoursOfOperation(), newContact.getHoursOfOperation());
  }