<% /*
     $Revision$
     $Id$
*/ %>

<%
var job = {
    func_enabled_job: // Wat mag ik zoal op deze specifieke actie?
        function _func_enabled_job(job_key, params) { // Alleen maar 'afgeleide' autorisatie (geen WEB_JOBUSE oid)
            params = params || {};
            var result = {
                canRead: false,
                canWrite: false,
                ref_key: -1
            };
            result.has_PRSSYS = user.has("WEB_PRSSYS");

            var authparamsBES = user.checkAutorisation(["WEB_BESUSE", "WEB_BESFOF"], true);
            var authparamsMLD = user.checkAutorisation(["WEB_MLDUSE", "WEB_MLDFOF"], true);
            var authparamsORD = user.checkAutorisation(["WEB_ORDBOF"], true);
            var authparamsMGT = user.checkAutorisation(["WEB_MLDMGT"], true);

            result.canBESRead = (authparamsBES  && (authparamsBES.ALGreadlevel < 9  || authparamsBES.PRSreadlevel < 9));
            result.canBESWrite = (authparamsBES && (authparamsBES.ALGwritelevel < 9 || authparamsBES.PRSwritelevel < 9));
            result.canMLDRead = (authparamsMLD  && (authparamsMLD.ALGreadlevel < 9  || authparamsMLD.PRSreadlevel < 9));
            result.canMLDWrite = (authparamsMLD && (authparamsMLD.ALGwritelevel < 9 || authparamsMLD.PRSwritelevel < 9));
            result.canORDRead = (authparamsORD  && (authparamsORD.ALGreadlevel < 9  || authparamsORD.PRSreadlevel < 9));
            result.canORDWrite = (authparamsORD && (authparamsORD.ALGwritelevel < 9 || authparamsORD.PRSwritelevel < 9));
            result.canMGTRead = (authparamsMGT  && (authparamsMGT.ALGreadlevel < 9  || authparamsMGT.PRSreadlevel < 9)) || false;
            result.canMGTWrite = (authparamsMGT && (authparamsMGT.ALGwritelevel < 9 || authparamsMGT.PRSwritelevel < 9)) || false;

            result.canReadAny =  result.has_PRSSYS || result.canBESRead  || result.canMLDRead  || result.canORDRead;
            result.canWriteAny = result.has_PRSSYS || result.canBESWrite || result.canMLDWrite || result.canORDWrite;

            if (job_key > 0 || ("module" in params)) {
                var xmlnode, aanvrager_key, ref_sql;
                if (job_key > 0) {
                    var sql = "SELECT fac_activiteit_user_key"
                            + "     , fac_activiteit_xmlnode"
                            + "     , fac_activiteit_eenheid"
                            + "  FROM fac_activiteit"
                            + " WHERE fac_activiteit_key = " + job_key;
                    var oRs = Oracle.Execute(sql);
                    aanvrager_key = oRs("fac_activiteit_user_key").Value;
                    xmlnode = oRs("fac_activiteit_xmlnode").Value;
                    template = oRs("fac_activiteit_eenheid").Value == 5;
                    oRs.Close();
                }
                var module = xmlnode || params.module;
                var template = template || params.template;
                switch (module) {
                    case "bestelling":
                        result.canRead = result.canBESRead;
                        result.canWrite = result.canBESWrite;
                        ref_sql = "SELECT bes_bestelling_key FROM bes_bestelling";
                        break;
                    case "melding":
                        result.canRead = result.canMLDRead;
                        result.canWrite = result.canMLDWrite;
                        ref_sql = "SELECT mld_melding_key FROM mld_melding";
                        break;
                    case "opdracht":
                        result.canRead = result.canORDRead;
                        result.canWrite = result.canORDWrite;
                        ref_sql = "SELECT mld_opdr_key FROM mld_opdr";
                        break;
                }
                ref_sql += " WHERE fac_activiteit_key = " + job_key
                         + " ORDER BY 1 DESC"; // Voor de zekerheid; de nieuwste, maar er mag er maar 1 zijn
                var oRs = Oracle.Execute(ref_sql);
                if (!oRs.EoF) {
                    result.ref_key = oRs(0).Value;
                }
                oRs.Close();

                if (result.has_PRSSYS) {
                    result.canRead  = true;
                } else if (job_key > 0) {
                    result.canRead  = user_key == aanvrager_key;
                    result.canWrite = result.canWrite && user_key == aanvrager_key;
                } else {
                    /* Staat al goed */
                }

                if (template) {
                    result.canReadTemplate = result.canMLDRead; 
                    result.canWriteTemplate  = result.canMLDWrite && result.canMGTWrite;
                }
            }
            return result;
        }
    }
%>
