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 #2
0
  protected void notifyUsers(AnnouncementsEntry entry) throws PortalException {

    Company company = companyPersistence.findByPrimaryKey(entry.getCompanyId());

    String className = entry.getClassName();
    long classPK = entry.getClassPK();

    String toName = PropsValues.ANNOUNCEMENTS_EMAIL_TO_NAME;
    String toAddress = PropsValues.ANNOUNCEMENTS_EMAIL_TO_ADDRESS;

    long teamId = 0;

    LinkedHashMap<String, Object> params = new LinkedHashMap<>();

    params.put("announcementsDeliveryEmailOrSms", entry.getType());

    if (classPK > 0) {
      if (className.equals(Group.class.getName())) {
        Group group = groupPersistence.findByPrimaryKey(classPK);

        toName = group.getDescriptiveName();

        params.put("inherit", Boolean.TRUE);
        params.put("usersGroups", classPK);
      } else if (className.equals(Organization.class.getName())) {
        Organization organization = organizationPersistence.findByPrimaryKey(classPK);

        toName = organization.getName();

        params.put("usersOrgsTree", ListUtil.fromArray(new Organization[] {organization}));
      } else if (className.equals(Role.class.getName())) {
        Role role = rolePersistence.findByPrimaryKey(classPK);

        toName = role.getName();

        if (role.getType() == RoleConstants.TYPE_REGULAR) {
          params.put("inherit", Boolean.TRUE);
          params.put("usersRoles", classPK);
        } else if (role.isTeam()) {
          teamId = role.getClassPK();
        } else {
          params.put("userGroupRole", new Long[] {Long.valueOf(0), classPK});
        }
      } else if (className.equals(UserGroup.class.getName())) {
        UserGroup userGroup = userGroupPersistence.findByPrimaryKey(classPK);

        toName = userGroup.getName();

        params.put("usersUserGroups", classPK);
      }
    }

    if (className.equals(User.class.getName())) {
      User user = userPersistence.findByPrimaryKey(classPK);

      if (Validator.isNull(user.getEmailAddress())) {
        return;
      }

      notifyUsers(
          ListUtil.fromArray(new User[] {user}),
          entry,
          company.getLocale(),
          user.getEmailAddress(),
          user.getFullName());
    } else {
      int count = 0;

      if (teamId > 0) {
        count = userLocalService.getTeamUsersCount(teamId);
      } else {
        count =
            userLocalService.searchCount(
                company.getCompanyId(), null, WorkflowConstants.STATUS_APPROVED, params);
      }

      int pages = count / Indexer.DEFAULT_INTERVAL;

      for (int i = 0; i <= pages; i++) {
        int start = (i * Indexer.DEFAULT_INTERVAL);
        int end = start + Indexer.DEFAULT_INTERVAL;

        List<User> users = null;

        if (teamId > 0) {
          users = userLocalService.getTeamUsers(teamId, start, end);
        } else {
          users =
              userLocalService.search(
                  company.getCompanyId(),
                  null,
                  WorkflowConstants.STATUS_APPROVED,
                  params,
                  start,
                  end,
                  (OrderByComparator<User>) null);
        }

        notifyUsers(users, entry, company.getLocale(), toAddress, toName);
      }
    }
  }