コード例 #1
0
 public List<?> getEntities(EntityReference ref, Search search) {
   System.out.println("get entities");
   // get the setting which indicates if we are getting polls we can admin or polls we can take
   boolean adminControl = false;
   Restriction adminRes = search.getRestrictionByProperty("admin");
   if (adminRes != null) {
     adminControl = developerHelperService.convert(adminRes.getSingleValue(), boolean.class);
   }
   // get the location (if set)
   Restriction locRes =
       search.getRestrictionByProperty(
           CollectionResolvable
               .SEARCH_LOCATION_REFERENCE); // requestStorage.getStoredValueAsType(String.class,
   // "siteId");
   String[] siteIds = null;
   if (locRes != null) {
     String siteId = developerHelperService.getLocationIdFromRef(locRes.getStringValue());
     siteIds = new String[] {siteId};
   }
   // get the user (if set)
   Restriction userRes =
       search.getRestrictionByProperty(CollectionResolvable.SEARCH_USER_REFERENCE);
   String userId = null;
   if (userRes != null) {
     String currentUser = developerHelperService.getCurrentUserReference();
     String userReference = userRes.getStringValue();
     if (userReference == null) {
       throw new IllegalArgumentException(
           "Invalid request: Cannot limit polls by user when the value is null");
     }
     if (userReference.equals(currentUser) || developerHelperService.isUserAdmin(currentUser)) {
       userId =
           developerHelperService.getUserIdFromRef(
               userReference); // requestStorage.getStoredValueAsType(String.class, "userId");
     } else {
       throw new SecurityException(
           "Only the admin can get polls for other users, you requested polls for: "
               + userReference);
     }
   } else {
     userId = developerHelperService.getCurrentUserId();
     if (userId == null) {
       throw new EntityException(
           "No user is currently logged in so no polls data can be retrieved",
           ref.getId(),
           HttpServletResponse.SC_UNAUTHORIZED);
     }
   }
   String perm = PollListManager.PERMISSION_VOTE;
   if (adminControl) {
     perm = PollListManager.PERMISSION_ADD;
   }
   List<Poll> polls =
       pollListManager.findAllPollsForUserAndSitesAndPermission(userId, siteIds, perm);
   if (adminControl) {
     // add in options
     for (Poll p : polls) {
       List<Option> options = pollListManager.getOptionsForPoll(p.getPollId());
       p.setOptions(options);
     }
   } else {
     // add in the indicators that this user has replied
     Long[] pollIds = new Long[polls.size()];
     for (int i = 0; i < polls.size(); i++) {
       pollIds[i] = polls.get(i).getPollId();
     }
     Map<Long, List<Vote>> voteMap = pollVoteManager.getVotesForUser(userId, pollIds);
     for (Poll poll : polls) {
       Long pollId = poll.getPollId();
       List<Vote> l = voteMap.get(pollId);
       if (l != null) {
         poll.setCurrentUserVoted(true);
         poll.setCurrentUserVotes(l);
       } else {
         poll.setCurrentUserVoted(false);
       }
     }
   }
   return polls;
 }
コード例 #2
0
  public Object getEntity(EntityReference ref) {
    String id = ref.getId();
    if (id == null) {
      return new Poll();
    }
    Poll poll = getPollById(id);
    if (poll == null) {
      throw new IllegalArgumentException("No poll found for the given reference: " + ref);
    }
    Long pollId = poll.getPollId();
    String currentUserId = developerHelperService.getCurrentUserId();

    boolean allowedManage = false;
    if (!developerHelperService.isEntityRequestInternal(ref + "")) {
      if (!pollListManager.isPollPublic(poll)) {
        // this is not a public poll? (ie .anon role has poll.vote)
        String userReference = developerHelperService.getCurrentUserReference();
        if (userReference == null) {
          throw new EntityException(
              "User must be logged in in order to access poll data",
              ref.getId(),
              HttpServletResponse.SC_UNAUTHORIZED);
        }
        allowedManage =
            developerHelperService.isUserAllowedInEntityReference(
                userReference, PollListManager.PERMISSION_ADD, "/site/" + poll.getSiteId());
        boolean allowedVote =
            developerHelperService.isUserAllowedInEntityReference(
                userReference, PollListManager.PERMISSION_VOTE, "/site/" + poll.getSiteId());
        if (!allowedManage && !allowedVote) {
          throw new SecurityException(
              "User (" + userReference + ") not allowed to access poll data: " + ref);
        }
      }
    }

    Boolean includeVotes = requestStorage.getStoredValueAsType(Boolean.class, "includeVotes");
    if (includeVotes == null) {
      includeVotes = false;
    }
    if (includeVotes) {
      List<Vote> votes = pollVoteManager.getAllVotesForPoll(poll);
      poll.setVotes(votes);
    }
    Boolean includeOptions = requestStorage.getStoredValueAsType(Boolean.class, "includeOptions");
    if (includeOptions == null) {
      includeOptions = false;
    }
    if (includeOptions) {
      List<Option> options = pollListManager.getOptionsForPoll(poll);
      poll.setOptions(options);
    }
    // add in the indicator that this user has replied
    if (currentUserId != null) {
      Map<Long, List<Vote>> voteMap =
          pollVoteManager.getVotesForUser(currentUserId, new Long[] {pollId});
      List<Vote> l = voteMap.get(pollId);
      if (l != null) {
        poll.setCurrentUserVoted(true);
        poll.setCurrentUserVotes(l);
      } else {
        poll.setCurrentUserVoted(false);
      }
    }
    return poll;
  }