package uk.ac.ox.oucs.oauth.advisor;

import org.sakaiproject.authz.api.SecurityAdvisor;

import java.util.HashSet;
import java.util.Set;

/**
 * Advisor used during test phases to determine which permissions must be enabled for the service
 * <p>
 * Every permission used during one request will be collected to be read later.
 * </p>
 *
 * @author Colin Hebert
 */
public class CollectingPermissionsAdvisor implements SecurityAdvisor {
    private Set<String> permissions = new HashSet<String>();

    public SecurityAdvice isAllowed(String userId, String function, String reference) {
        permissions.add(function);
        return SecurityAdvice.PASS;
    }

    public String toString() {
        return "Collected permissions: " + permissions;
    }
}

@@ -86,55 +86,94 @@
 or;%0A
-%0A
 import 
-java.util.HashSet;%0Aimport java.util.Set
+uk.ac.ox.oucs.oauth.dao.ConsumerDao;%0Aimport uk.ac.ox.oucs.oauth.domain.Consumer
 ;%0A%0A/
@@ -202,19 +202,20 @@
 ing 
-test
+record
  phase
-s
  to 
@@ -337,34 +337,29 @@
  be 
-collected to be read later
+enabled automatically
 .%0A *
@@ -482,180 +482,649 @@
 ate 
-Set%3CString%3E permissions = new HashSet%3CString%3E();%0A%0A    public SecurityAdvice isAllowed(String userId, String function, String reference) %7B%0A        permissions.add(function);
+ConsumerDao consumerDao;%0A    private Consumer consumer;%0A%0A    public CollectingPermissionsAdvisor(ConsumerDao consumerDao, Consumer consumer) %7B%0A        this.consumerDao = consumerDao;%0A        this.consumer = consumer;%0A    %7D%0A%0A    public SecurityAdvice isAllowed(String userId, String function, String reference) %7B%0A        if (!consumer.getRights().contains(function)) %7B%0A            try %7B%0A                consumer.getRights().add(function);%0A                consumerDao.update(consumer);%0A            %7D catch (Exception e) %7B%0A                //If the update doesn't work, carry on%0A                //TODO: Log this exception%0A            %7D%0A        %7D
 %0A   
@@ -1214,46 +1214,29 @@
 rn %22
-Collected permissions: %22 + permissions
+Permission collector%22
 ;%0A  

Update the permissions advisor to update rights