protected List<Role> getGroupRoles(long groupId, String resourceName) throws PortalException {

    List<Role> roles = groupRolesMap.get(groupId);

    if (roles != null) {
      return roles;
    }

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    roles =
        ListUtil.copy(
            ResourceActionsUtil.getRoles(group.getCompanyId(), group, resourceName, null));

    Map<Team, Role> teamRoleMap = RoleLocalServiceUtil.getTeamRoleMap(groupId);

    for (Map.Entry<Team, Role> entry : teamRoleMap.entrySet()) {
      Team team = entry.getKey();
      Role teamRole = entry.getValue();

      teamRole.setName(PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
      teamRole.setDescription(team.getDescription());

      roles.add(teamRole);
    }

    groupRolesMap.put(groupId, roles);

    return roles;
  }
  protected Team addTeam() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Team team = _persistence.create(pk);

    team.setMvccVersion(RandomTestUtil.nextLong());

    team.setCompanyId(RandomTestUtil.nextLong());

    team.setUserId(RandomTestUtil.nextLong());

    team.setUserName(RandomTestUtil.randomString());

    team.setCreateDate(RandomTestUtil.nextDate());

    team.setModifiedDate(RandomTestUtil.nextDate());

    team.setGroupId(RandomTestUtil.nextLong());

    team.setName(RandomTestUtil.randomString());

    team.setDescription(RandomTestUtil.randomString());

    _teams.add(_persistence.update(team));

    return team;
  }
  @Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    Team newTeam = addTeam();

    Team existingTeam = _persistence.fetchByPrimaryKey(newTeam.getPrimaryKey());

    Assert.assertEquals(existingTeam, newTeam);
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Team team = _persistence.create(pk);

    Assert.assertNotNull(team);

    Assert.assertEquals(team.getPrimaryKey(), pk);
  }
  @Test
  public void testRemove() throws Exception {
    Team newTeam = addTeam();

    _persistence.remove(newTeam);

    Team existingTeam = _persistence.fetchByPrimaryKey(newTeam.getPrimaryKey());

    Assert.assertNull(existingTeam);
  }
  @Test
  public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    Team newTeam = addTeam();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newTeam.getPrimaryKey());

    Map<Serializable, Team> teams = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, teams.size());
    Assert.assertEquals(newTeam, teams.get(newTeam.getPrimaryKey()));
  }
  public long[] getTeamIds() throws SystemException {
    List<Team> teams = getTeams();

    long[] teamIds = new long[teams.size()];

    for (int i = 0; i < teams.size(); i++) {
      Team team = teams.get(i);

      teamIds[i] = team.getTeamId();
    }

    return teamIds;
  }
  /**
   * Removes the user groups from the group.
   *
   * @param groupId the primary key of the group
   * @param userGroupIds the primary keys of the user groups
   */
  @Override
  public void unsetGroupUserGroups(long groupId, long[] userGroupIds) {
    List<Team> teams = teamPersistence.findByGroupId(groupId);

    for (Team team : teams) {
      teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
    }

    userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(userGroupIds, groupId);

    groupPersistence.removeUserGroups(groupId, userGroupIds);

    PermissionCacheUtil.clearCache();
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    Team newTeam = addTeam();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newTeam.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, Team> teams = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, teams.size());
    Assert.assertEquals(newTeam, teams.get(newTeam.getPrimaryKey()));
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    Team newTeam = addTeam();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(Team.class, Team.class.getClassLoader());

    dynamicQuery.add(RestrictionsFactoryUtil.eq("teamId", newTeam.getTeamId()));

    List<Team> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Team existingTeam = result.get(0);

    Assert.assertEquals(existingTeam, newTeam);
  }
  @Test
  public void testResetOriginalValues() throws Exception {
    if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
      return;
    }

    Team newTeam = addTeam();

    _persistence.clearCache();

    TeamModelImpl existingTeamModelImpl =
        (TeamModelImpl) _persistence.findByPrimaryKey(newTeam.getPrimaryKey());

    Assert.assertEquals(
        existingTeamModelImpl.getGroupId(), existingTeamModelImpl.getOriginalGroupId());
    Assert.assertTrue(
        Validator.equals(existingTeamModelImpl.getName(), existingTeamModelImpl.getOriginalName()));
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }

    if (!(obj instanceof Team)) {
      return false;
    }

    Team team = (Team) obj;

    long primaryKey = team.getPrimaryKey();

    if (getPrimaryKey() == primaryKey) {
      return true;
    } else {
      return false;
    }
  }
  @Override
  public int compareTo(Team team) {
    int value = 0;

    value = getName().compareTo(team.getName());

    if (value != 0) {
      return value;
    }

    return 0;
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    Team newTeam = addTeam();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(Team.class, Team.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("teamId"));

    Object newTeamId = newTeam.getTeamId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("teamId", new Object[] {newTeamId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingTeamId = result.get(0);

    Assert.assertEquals(existingTeamId, newTeamId);
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    Team newTeam1 = addTeam();
    Team newTeam2 = addTeam();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newTeam1.getPrimaryKey());
    primaryKeys.add(newTeam2.getPrimaryKey());

    Map<Serializable, Team> teams = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, teams.size());
    Assert.assertEquals(newTeam1, teams.get(newTeam1.getPrimaryKey()));
    Assert.assertEquals(newTeam2, teams.get(newTeam2.getPrimaryKey()));
  }
  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 = ResourceActionsUtil.getRoles(companyId, group, className, null);

    if (groupId > 0) {
      List<Team> teams = TeamLocalServiceUtil.getGroupTeams(groupId);

      for (Team team : teams) {
        Role role = RoleLocalServiceUtil.getTeamRole(team.getCompanyId(), team.getTeamId());

        roles.add(role);
      }
    }

    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()]));
  }
  public void importPermissions(String resourceName, long resourcePK, long newResourcePK)
      throws PortalException, SystemException {

    if (!MapUtil.getBoolean(_parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {

      return;
    }

    List<KeyValuePair> permissions =
        _permissionsMap.get(getPrimaryKeyString(resourceName, resourcePK));

    if (permissions == null) {
      return;
    }

    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();

    for (KeyValuePair permission : permissions) {
      String roleName = permission.getKey();

      Role role = null;

      Team team = null;

      if (roleName.startsWith(PermissionExporter.ROLE_TEAM_PREFIX)) {
        roleName = roleName.substring(PermissionExporter.ROLE_TEAM_PREFIX.length());

        try {
          team = TeamLocalServiceUtil.getTeam(_groupId, roleName);
        } catch (NoSuchTeamException nste) {
          if (_log.isWarnEnabled()) {
            _log.warn("Team " + roleName + " does not exist");
          }

          continue;
        }
      }

      try {
        if (team != null) {
          role = RoleLocalServiceUtil.getTeamRole(_companyId, team.getTeamId());
        } else {
          role = RoleLocalServiceUtil.getRole(_companyId, roleName);
        }
      } catch (NoSuchRoleException nsre) {
        if (_log.isWarnEnabled()) {
          _log.warn("Role " + roleName + " does not exist");
        }

        continue;
      }

      String[] actionIds = StringUtil.split(permission.getValue());

      roleIdsToActionIds.put(role.getRoleId(), actionIds);
    }

    if (roleIdsToActionIds.isEmpty()) {
      return;
    }

    if (ResourceBlockLocalServiceUtil.isSupported(resourceName)) {
      ResourceBlockLocalServiceUtil.setIndividualScopePermissions(
          _companyId, _groupId, resourceName, newResourcePK, roleIdsToActionIds);
    } else {
      ResourcePermissionLocalServiceUtil.setResourcePermissions(
          _companyId,
          resourceName,
          ResourceConstants.SCOPE_INDIVIDUAL,
          String.valueOf(newResourcePK),
          roleIdsToActionIds);
    }
  }
  public void addPermissions(String resourceName, long resourcePK)
      throws PortalException, SystemException {

    if (!MapUtil.getBoolean(_parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {

      return;
    }

    List<KeyValuePair> permissions = new ArrayList<KeyValuePair>();

    Group group = GroupLocalServiceUtil.getGroup(_groupId);

    List<Role> roles = RoleLocalServiceUtil.getRoles(_companyId);

    PrimitiveLongList roleIds = new PrimitiveLongList(roles.size());
    Map<Long, String> roleIdsToNames = new HashMap<Long, String>();

    for (Role role : roles) {
      int type = role.getType();

      if ((type == RoleConstants.TYPE_REGULAR)
          || ((type == RoleConstants.TYPE_ORGANIZATION) && group.isOrganization())
          || ((type == RoleConstants.TYPE_SITE)
              && (group.isLayoutSetPrototype() || group.isSite()))) {

        String name = role.getName();

        roleIds.add(role.getRoleId());
        roleIdsToNames.put(role.getRoleId(), name);
      } else if ((type == RoleConstants.TYPE_PROVIDER) && role.isTeam()) {
        Team team = TeamLocalServiceUtil.getTeam(role.getClassPK());

        if (team.getGroupId() == _groupId) {
          String name = PermissionExporter.ROLE_TEAM_PREFIX + team.getName();

          roleIds.add(role.getRoleId());
          roleIdsToNames.put(role.getRoleId(), name);
        }
      }
    }

    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(resourceName);

    Map<Long, Set<String>> roleIdsToActionIds =
        getActionIds(_companyId, roleIds.getArray(), resourceName, resourcePK, actionIds);

    for (Map.Entry<Long, String> entry : roleIdsToNames.entrySet()) {
      long roleId = entry.getKey();
      String name = entry.getValue();

      Set<String> availableActionIds = roleIdsToActionIds.get(roleId);

      if ((availableActionIds == null) || availableActionIds.isEmpty()) {
        continue;
      }

      KeyValuePair permission = new KeyValuePair(name, StringUtil.merge(availableActionIds));

      permissions.add(permission);
    }

    _permissionsMap.put(getPrimaryKeyString(resourceName, resourcePK), permissions);
  }
Beispiel #19
0
 @Override
 public Long get(Team team) {
   return team.getTeamId();
 }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static Team toModel(TeamSoap soapModel) {
    if (soapModel == null) {
      return null;
    }

    Team model = new TeamImpl();

    model.setMvccVersion(soapModel.getMvccVersion());
    model.setTeamId(soapModel.getTeamId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setUserId(soapModel.getUserId());
    model.setUserName(soapModel.getUserName());
    model.setCreateDate(soapModel.getCreateDate());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setGroupId(soapModel.getGroupId());
    model.setName(soapModel.getName());
    model.setDescription(soapModel.getDescription());

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

    Team newTeam = _persistence.create(pk);

    newTeam.setMvccVersion(RandomTestUtil.nextLong());

    newTeam.setCompanyId(RandomTestUtil.nextLong());

    newTeam.setUserId(RandomTestUtil.nextLong());

    newTeam.setUserName(RandomTestUtil.randomString());

    newTeam.setCreateDate(RandomTestUtil.nextDate());

    newTeam.setModifiedDate(RandomTestUtil.nextDate());

    newTeam.setGroupId(RandomTestUtil.nextLong());

    newTeam.setName(RandomTestUtil.randomString());

    newTeam.setDescription(RandomTestUtil.randomString());

    _teams.add(_persistence.update(newTeam));

    Team existingTeam = _persistence.findByPrimaryKey(newTeam.getPrimaryKey());

    Assert.assertEquals(existingTeam.getMvccVersion(), newTeam.getMvccVersion());
    Assert.assertEquals(existingTeam.getTeamId(), newTeam.getTeamId());
    Assert.assertEquals(existingTeam.getCompanyId(), newTeam.getCompanyId());
    Assert.assertEquals(existingTeam.getUserId(), newTeam.getUserId());
    Assert.assertEquals(existingTeam.getUserName(), newTeam.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingTeam.getCreateDate()),
        Time.getShortTimestamp(newTeam.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingTeam.getModifiedDate()),
        Time.getShortTimestamp(newTeam.getModifiedDate()));
    Assert.assertEquals(existingTeam.getGroupId(), newTeam.getGroupId());
    Assert.assertEquals(existingTeam.getName(), newTeam.getName());
    Assert.assertEquals(existingTeam.getDescription(), newTeam.getDescription());
  }