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

import org.sakaiproject.authz.api.SecurityAdvisor;
import uk.ac.ox.oucs.oauth.dao.ConsumerDao;
import uk.ac.ox.oucs.oauth.domain.Consumer;

/**
 * Advisor used during record phase to determine which permissions must be enabled for the service
 * <p>
 * Every permission used during one request will be enabled automatically.
 * </p>
 *
 * @author Colin Hebert
 */
public class CollectingPermissionsAdvisor implements SecurityAdvisor {
    private ConsumerDao consumerDao;
    private Consumer consumer;

    public CollectingPermissionsAdvisor(ConsumerDao consumerDao, Consumer consumer) {
        this.consumerDao = consumerDao;
        this.consumer = consumer;
    }

    public SecurityAdvice isAllowed(String userId, String function, String reference) {
        if (!consumer.getRights().contains(function)) {
            try {
                consumer.getRights().add(function);
                consumerDao.update(consumer);
            } catch (Exception e) {
                //If the update doesn't work, carry on
                //TODO: Log this exception
            }
        }
        return SecurityAdvice.PASS;
    }

    public String toString() {
        return "Permission collector";
    }
}

@@ -82,16 +82,73 @@
 dvisor;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import u
@@ -183,16 +183,16 @@
 merDao;%0A
-
 import u
@@ -523,16 +523,118 @@
 visor %7B%0A
+    private static final Logger logger = LoggerFactory.getLogger(CollectingPermissionsAdvisor.class);%0A
     priv
@@ -1001,24 +1001,134 @@
 unction)) %7B%0A
+            logger.info(%22'%22+consumer.getId()+%22' requires '%22+function+%22' right in order to work, enable it.%22);%0A
             
@@ -1342,34 +1342,99 @@
     
-//TODO: Log this exception
+logger.warn(%22Activation of the '%22+function+%22' right on '%22+consumer.getId()+%22' failed.%22, e);
 %0A   

Enable logs on CollectingPermissionAdvisor