Subversion Repositories bacoAlunos

Rev

Rev 1561 | Rev 1616 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.domain;

import com.owlike.genson.Genson;
import com.owlike.genson.TransformationException;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import pt.estgp.estgweb.services.pageContent.PageContentAccessPolicyAcl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by jorgemachado on 03/02/16.
 */

public abstract class PageContentImpl extends PageContent implements ClearanceRuled
{

    private static 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(PageContentImpl.class);

    private UserSession sess = null;
    private Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> userSessionClearences = null;
    private boolean childsInitialized = false;
    private boolean parentInitialized = false;

    public static class 1.5.0/docs/api/java/security/acl/Acl.html">Acl
    {
        public static enum Access
        {
            PERMIT,
            DENY //default
        }
        public static enum 1.5.0/docs/api/java/lang/reflect/Type.html">Type
        {
            ALL,
            ANONYMOUS, //ONLY SUPER USER SHOULD ADD ANONYMOUS PERMISSIONS
            AUTHENTICATED,
            ROLE,
            USER,
            COURSEUNIT //SPECIFIC PERMISSIONS RELATIVE TO COURSE UNITS
        }
        public static enum 1.5.0/docs/api/java/rmi/server/Operation.html">Operation
        {
            ALL, //SPECIAL OPERATION FOR DENY AND PERMIT REPLACE FULL CONTROL

            READ_CONTENTS,

            READ_VERSIONS,
            READ_INVISIBLES,


            ORDER_CONTENTS,

            ADD_SECTIONS, //ADD TEXT USA ESTA PERMISSAO
            ADD_FILES,
            ADD_URLS,
            ADD_MODULES,

            REMOVE_SECTIONS,
            REMOVE_FILES,
            REMOVE_URLS,
            REMOVE_MODULES,

            EDIT_SECTIONS,
            EDIT_FILES,
            EDIT_URLS,
            EDIT_MODULES;

            public static 1.5.0/docs/api/java/rmi/server/Operation.html">Operation parse(1.5.0/docs/api/java/lang/String.html">String clearance)
            {
                if(clearance == null)
                    return null;
                for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation op: 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values())
                {
                    if(op.name().equals(clearance))
                        return op;
                }
                return null;
            }
        }

        public static enum CourseUnitsId
        {
            COURSE_UNIT_TEACHER("page.content.course.unit.teacher"),
            COURSE_UNIT_STUDENT("page.content.course.unit.student"),
            COURSE_UNIT_RESP_TEACHER("page.content.course.unit.resp.teacher"),
            COURSE_UNIT_COURSE_COMISSION("page.content.course.unit.course.comission");

            1.5.0/docs/api/java/lang/String.html">String msgKey;


            CourseUnitsId(1.5.0/docs/api/java/lang/String.html">String msgKey) {
                this.msgKey = msgKey;
            }
        }

        private 1.5.0/docs/api/java/lang/String.html">String access;
        private 1.5.0/docs/api/java/lang/String.html">String type;
        private 1.5.0/docs/api/java/lang/String.html">String id;
        private 1.5.0/docs/api/java/lang/String.html">String operation;

        public 1.5.0/docs/api/java/lang/String.html">String getAccess() {
            return access;
        }

        public void setAccess(1.5.0/docs/api/java/lang/String.html">String access) {
            this.access = access;
        }

        public void setAccessEnum(Access access) {
            this.access = access.name();
        }

        public 1.5.0/docs/api/java/lang/String.html">String getType() {
            return type;
        }

        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
            this.type = type;
        }
        public void setTypeEnum(1.5.0/docs/api/java/lang/reflect/Type.html">Type type) {
            this.type = type.name();
        }

        public 1.5.0/docs/api/java/lang/String.html">String getId() {
            return id;
        }

        public void setId(1.5.0/docs/api/java/lang/String.html">String id) {
            this.id = id;
        }

        public 1.5.0/docs/api/java/lang/String.html">String getOperation() {
            return operation;
        }

        public void setOperation(1.5.0/docs/api/java/lang/String.html">String operation) {
            this.operation = operation;
        }

        public void setOperationEnum(1.5.0/docs/api/java/rmi/server/Operation.html">Operation operation) {
            this.operation = operation.name();
        }

        public void setCourseUnitIdEnum(CourseUnitsId courseUnitIdEnum) {
            this.id = courseUnitIdEnum.name();
        }

        public Access getAccessEnum()
        {
            for(Access a : Access.values())
            {
                if(a.name().equals(getAccess()))
                    return a;
            }
            return Access.DENY;
        }

        public 1.5.0/docs/api/java/lang/reflect/Type.html">Type getTypeEnum()
        {
            for(1.5.0/docs/api/java/lang/reflect/Type.html">Type a : 1.5.0/docs/api/java/lang/reflect/Type.html">Type.values())
            {
                if(a.name().equals(getType()))
                    return a;
            }
            return 1.5.0/docs/api/java/lang/reflect/Type.html">Type.ROLE;
        }

        public 1.5.0/docs/api/java/rmi/server/Operation.html">Operation getOperationEnum()
        {
            for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation a : 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values())
            {
                if(a.name().equals(getOperation()))
                    return a;
            }
            return null;
        }

        public CourseUnitsId getCourseUnitIdEnum()
        {
            for(CourseUnitsId a : CourseUnitsId.values())
            {
                if(a.name().equals(getId()))
                    return a;
            }
            return null;
        }
    }

    /**
     * Empty Array Means no ACL's defined
     * build a json array form ACL's
     * @return example [{"access":"PERMIT","operation":"FULL_CONTROL","id":"services","type":"ROLE"}]
     * @throws JSONException
     */

    public JSONArray getAclJson() throws JSONException
    {
        if(getAcl() == null || getAcl().trim().length() == 0)
            return new JSONArray();
        return new JSONArray(getAcl());
    }

    /**
     * Sets the string ACL with the proper json
     * @param json
     * @throws JSONException
     */

    public void setAclJson(JSONArray json) throws JSONException
    {
        if(json == null)
            setAcl(null);
        else
            setAcl(json.toString());
    }

    /**
     * sets the string Acl with the json built from given List
     * @param acls
     * @throws JSONException
     */

    public void setAclList(ArrayList<Acl> acls) throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException {
        if(acls == null || acls.size() == 0)
        {
            setAcl(null);
            return;
        }
        setAcl(getAclJson(acls).toString());
    }

    /**
     * Empty Array Means no ACL's defined allays
     * @return allays a list if no acls return empty list
     * @throws JSONException
     */

    public ArrayList<Acl> getAclList() throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException {
        ArrayList<Acl> acls = new ArrayList<Acl>();
        if(getAcl() == null || getAcl().trim().length() == 0)
            return acls;
        Genson genson = getGenson();
        ArrayList<HashMap<String,String>> aclsDes = genson.deserialize(getAcl(),1.5.0/docs/api/java/util/ArrayList.html">ArrayList.class);
        for(HashMap<String,String> acl: aclsDes)
        {
            1.5.0/docs/api/java/security/acl/Acl.html">Acl a = new 1.5.0/docs/api/java/security/acl/Acl.html">Acl();
            a.setType(acl.get("type"));
            a.setOperation(acl.get("operation"));
            a.setAccess(acl.get("access"));
            a.setId(acl.get("id"));
            acls.add(a);
        }
        return acls;
    }

    /**
     * Add a acl to String json at the end
     * @param acl
     * @throws IOException
     * @throws TransformationException
     * @throws JSONException
     */

    public void addAcl(1.5.0/docs/api/java/security/acl/Acl.html">Acl acl) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {
        if(acl == null)
            return;
        ArrayList<Acl> acls = getAclList();
        acls.add(acl);
        setAclList(acls);
    }

    private static Genson getGenson(){
        Genson genson = new Genson.Builder()
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
                .include("access", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .include("type", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .include("id", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .include("operation", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .create();
        return genson;
    }

    /**
     * Utilitário
     * @param aclList
     * @return
     * @throws IOException
     * @throws TransformationException
     * @throws JSONException
     */

    public static JSONArray getAclJson(ArrayList<Acl> aclList) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {
        if(aclList == null)
            return new JSONArray();
        Genson genson = getGenson();
        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(aclList);
        //System.out.println(json);
        return new JSONArray(json);
    }


    public boolean isChildsInitialized() {
        return childsInitialized;
    }

    public void setChildsInitialized(boolean childsInitialized) {
        this.childsInitialized = childsInitialized;
    }

    public boolean isParentInitialized() {
        return parentInitialized;
    }

    public void setParentInitialized(boolean parentInitialized) {
        this.parentInitialized = parentInitialized;
    }

    /**
     * Metodo de testes
     * @param args
     * @throws IOException
     * @throws TransformationException
     * @throws JSONException
     */

    public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {



    }

    public boolean canAdmin(UserSessionImpl u)
    {
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
        {
            logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie());
            return false;
        }
        else if(userSessionClearences != null)
        {
            for(1.5.0/docs/api/java/util/Map.html">Map.Entry<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> entry: userSessionClearences.entrySet())
            {
                if(entry.getValue() == true &&
                        !entry.getKey().name().startsWith("READ"))
                {
                    return true;
                }
            }
            return false;
        }
        else
        {
            logger.warn("Checking clearance without sessionClearances initialized, not allowed");
            return false;
        }
    }




    public boolean isClear(UserSessionImpl u, 1.5.0/docs/api/java/lang/String.html">String OP)
    {
        1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation o = 1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.parse(OP);
        return isClear(u,o);
    }

    /**
     * Same logic in isClearanceInitialized Method
     * @param u
     * @return
     */

    public boolean isClear(UserSessionImpl u, PageContentImpl.1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation op)
    {
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
        {
            logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie());
            return false;
        }
        else if(userSessionClearences != null)
        {
            return userSessionClearences.get(op);
        }
        else
        {
            logger.warn("Checking clearance without sessionClearances initialized, not allowed");
            return false;
        }
    }

    /**
     * Same logic in isClear Method
     * @param u
     * @return
     */

    public boolean isClearanceInitialized(UserSessionImpl u)
    {
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
        {
            return false;
        }
        else if(userSessionClearences == null)
        {
            return false;
        }
        return true;
    }

    @1.5.0/docs/api/java/lang/Override.html">Override
    public void initClearances(UserSessionImpl u, boolean force) {
        throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("NotImplemented");
    }



    public void setUserSessionClearences(Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation, Boolean> userSessionClearences) {
        this.userSessionClearences = userSessionClearences;
    }

    public void setSess(UserSession sess) {
        this.sess = sess;
    }

    @1.5.0/docs/api/java/lang/Override.html">Override
    public 1.5.0/docs/api/java/lang/String.html">String getAccessPolicyClass() {
        if(super.getAccessPolicyClass() == null)
        {
            logger.debug("No AccessPolicyClass in PageContent " + getId() + ":" + getTitle() + " using default:" + PageContentAccessPolicyAcl.class.getName());
            return PageContentAccessPolicyAcl.class.getName();
        }
        return super.getAccessPolicyClass();
    }
}