Update of 
/var/cvs/contributions/didactor2/src/education/java/nl/didactor/component/education
In directory james.mmbase.org:/tmp/cvs-serv28925

Modified Files:
        DidactorEducation.java 
Log Message:
using 1.9's Actions in


See also: 
http://cvs.mmbase.org/viewcvs/contributions/didactor2/src/education/java/nl/didactor/component/education


Index: DidactorEducation.java
===================================================================
RCS file: 
/var/cvs/contributions/didactor2/src/education/java/nl/didactor/component/education/DidactorEducation.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- DidactorEducation.java      21 Jan 2008 16:13:03 -0000      1.10
+++ DidactorEducation.java      21 Jan 2008 17:30:55 -0000      1.11
@@ -1,21 +1,24 @@
 package nl.didactor.component.education;
 
-import java.util.Map;
-import java.util.Vector;
-import java.lang.Integer;
+import java.util.*;
+
 import javax.servlet.jsp.JspTagException;
 import nl.didactor.component.Component;
 import nl.didactor.util.ClassRoom;
 import org.mmbase.module.core.*;
-import org.mmbase.bridge.Node;
-import org.mmbase.bridge.NodeList;
-import org.mmbase.bridge.Cloud;
+import org.mmbase.bridge.*;
+import org.mmbase.security.Action;
+import org.mmbase.security.ActionChecker;
+import org.mmbase.security.UserContext;
+import org.mmbase.util.functions.Parameters;
+import org.mmbase.util.functions.Parameter;
 import org.mmbase.util.logging.Logger;
 import org.mmbase.util.logging.Logging;
 
+
 /**
  * @javadoc
- * @version $Id: DidactorEducation.java,v 1.10 2008/01/21 16:13:03 michiel Exp 
$
+ * @version $Id: DidactorEducation.java,v 1.11 2008/01/21 17:30:55 michiel Exp 
$
  */
 public class DidactorEducation extends Component {
     private static Logger log = 
Logging.getLoggerInstance(DidactorEducation.class);
@@ -57,58 +60,73 @@
          return value;
     }
 
-    // javadoc inherited
-    @Override
-    public boolean[] may (String operation, Cloud cloud, Map context, String[] 
arguments) {
-        boolean mayvalue[]= new boolean[] {false, false};
-        try {
-            if (operation.equals("viewAnswers") || operation.equals("rate")) {
-                Object user         = context.get( "user");
-                Object educationobj = context.get( "education");
-                Object classobj     = context.get( "class");
-
-                MMObjectNode usernode = 
MMBase.getMMBase().getBuilder("people").getNode(((Integer)user).intValue());
-                if (usernode == null) {
-                    throw new JspTagException("User with number '" + user + "' 
not found");
-                }
-                int educationno = castIdentifier( educationobj);
-
-
-                int classno;
-                if((classobj != null) && ( (classobj instanceof String) && 
(!classobj.equals("null"))) ) {
-                    //the class is a number
-                    classno = castIdentifier(classobj);
-                } else {
-                    //the class is null
-                    classno = -1;
+    public static final Parameter EDUCATION = new Parameter("education", 
Node.class, true);
+    public static final Parameter CLASS     = new Parameter("class", 
Node.class, null);
+    private static final Parameter SUBJECT   = new Parameter("subject", 
Node.class, true);
+
+    private static final Parameter[] PARAMS = new Parameter[] {EDUCATION, 
CLASS};
+
+
+    /**
+     * TODO, 'view answers' actually sounds like normal 'node based' mmbase 
security.
+     * you can principally per answer node calculate whether you may see it or 
not.
+     */
+    private static final Action VIEW_ANSWERS = new 
Action("education","viewAnswers", new ActionChecker() {
+            public boolean check(UserContext user, Action ac, Parameters 
parameters) {
+                Node subject = (Node) parameters.get(SUBJECT);
+                Node education = (Node) parameters.get(EDUCATION);
+                Node clazz = (Node) parameters.get(CLASS);
+                int u = Integer.parseInt(user.getIdentifier());
+                return u == subject.getNumber() ||
+                    isTeacherOf(subject.getCloud(), u, subject.getNumber(), 
education.getNumber(), clazz == null ? -1 : clazz.getNumber());
+            }
+        }) {
+            public Parameters createParameters() {
+                return new Parameters(PARAMS);
                 }
+        };
 
 
-                int subjectno = 0;
 
-                if ((arguments.length > 0) && (arguments[0] != null)) {
-                    subjectno = castIdentifier(context.get( arguments[0]));
-                } else {
-                    throw new JspTagException("1 argument required: subject 
person ID");
+    /**
+     * Rating an answer is changing a certain field of a node. MMBase security 
is based on entire
+     * nodes. So we need something special. 'Action' framework is used.
+     */
+    private static final Action RATE         = new Action("education", "rate", 
new ActionChecker() {
+            public boolean check(UserContext user, Action ac, Parameters 
parameters) {
+                Node subject = (Node) parameters.get(SUBJECT);
+                Node education = (Node) parameters.get(EDUCATION);
+                Node clazz = (Node) parameters.get(CLASS);
+                return isTeacherOf(subject.getCloud(), 
Integer.parseInt(user.getIdentifier()), subject.getNumber(), 
education.getNumber(), clazz == null ? -1 : clazz.getNumber());
+            }
+        }) {
+            public Parameters createParameters() {
+                return new Parameters(PARAMS);
                 }
-                //System.out.println( subjectno);
-                //System.out.println( usernode.getNumber());
+        };
+    private static final Map<String, Action> actions = new HashMap<String, 
Action>();
 
-                boolean isTeacherOf=
-                    ClassRoom.isClassMember(usernode, subjectno, classno, 
educationno, "teacher", cloud)
-                    || ClassRoom.isWorkgroupMember(usernode, subjectno, 
classno, educationno, "teacher", cloud);
+    static {
+        actions.put(VIEW_ANSWERS.getName(), VIEW_ANSWERS);
+        actions.put(RATE.getName(), VIEW_ANSWERS);
+    }
 
-                if (operation.equals("rate")) {
-                    mayvalue[0]= isTeacherOf;
-                } else {
-                    mayvalue[0]= (subjectno == usernode.getNumber()) || 
isTeacherOf;
+    public Map<String, Action> getActions() {
+        return Collections.unmodifiableMap(actions);
                 }
+
+    protected static boolean  isTeacherOf(Cloud cloud, int user, int subject, 
int education, int clazz) {
+        MMObjectNode usernode = 
MMBase.getMMBase().getBuilder("people").getNode(user);
+        return ClassRoom.isClassMember(usernode, subject, clazz, education, 
"teacher", cloud)
+            || ClassRoom.isWorkgroupMember(usernode, subject, clazz, 
education, "teacher", cloud);
             }
+
+    // javadoc inherited
+    @Override
+    public boolean[] may(Cloud cloud, Action action, Parameters arguments) {
+        boolean mayvalue[]= new boolean[] {false, false};
+        mayvalue[0] = action.getDefault().check(cloud.getUser(), action, 
arguments);
             return mayvalue;
-        } catch (JspTagException e) {
-            log.warn( "may: education: " + operation + " "  +e.getMessage());
-            return new boolean[] {false, false};
-        }
     }
 
     // javadoc inherited
_______________________________________________
Cvs mailing list
Cvs@lists.mmbase.org
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to