package org.togglz.core.user.jaas;

import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Principal;
import java.util.Iterator;

import javax.security.auth.Subject;

import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.UserProvider;
import org.togglz.core.user.SimpleFeatureUser;

/**
 * 
 * This implementation supports looking up the current user the JAAS AccessControlContext. The class currently doesn't support
 * determining whether a user is a feature admin.
 * 
 * @author Christian Kaltepoth
 * 
 */
public class JAASUserProvider implements UserProvider {

    @Override
    public FeatureUser getCurrentUser() {

        AccessControlContext acc = AccessController.getContext();
        if (acc != null) {
            Subject subject = Subject.getSubject(acc);
            if (subject != null) {
                Iterator<Principal> iter = subject.getPrincipals().iterator();
                if (iter.hasNext()) {
                    return new SimpleFeatureUser(iter.next().getName(), false);
                }
            }
        }

        return null;
    }

}

@@ -277,26 +277,31 @@
 re.user.
-UserProvid
+SimpleFeatureUs
 er;%0Aimpo
@@ -320,39 +320,34 @@
 z.core.user.
-SimpleFeatureUs
+UserProvid
 er;%0A%0A/**%0A * 
@@ -522,16 +522,139 @@
 re admin
+ and therefore always sets it to false. Overriding%0A * %7B@link #isFeatureAdmin(String)%7D allows to change the default behavior
 .%0A * %0A *
@@ -1128,110 +1128,582 @@
     
-return new SimpleFeatureUser(iter.next().getName(), false);%0A           
+String name = iter.next().getName();%0A                    return new SimpleFeatureUser(name, isFeatureAdmin(name));%0A                %7D%0A            %7D%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    /**%0A     * Checks if the supplied user is a feature admin. The default implementation always returns %3Ccode%3Efalse%3C/code%3E. Users can%0A     * overwrite this method to implement a different behavior.%0A
      
-%7D
+* 
 %0A     
-       %7D%0A        %7D%0A
+* @param user The name of the user%0A     * @return %3Ccode%3Etrue%3C/code%3E for feature admins, %3Ccode%3Efalse%3C/code%3E otherwise%0A     */%0A    protected boolean isFeatureAdmin(String user) %7B
 %0A   
@@ -1706,31 +1706,32 @@
 %0A        return 
-null
+false
 ;%0A    %7D%0A%0A%7D%0A

Allow to customize the feature admin flag in JAASUserProvider