<% /*
    $Revision$
    $Id$

    File:           model_aut_idp.inc
    Description:
    Notes:          Documentatie in de wiki onder Authenticeren
*/

%>
<!-- #include file="./model_aut_idp_map.inc" -->
<%
function model_aut_idp(params)
{
    params = params || {};
    this.records_name = "identityproviders";
    this.record_name = "identityprovider";
    this.table = "aut_idp";
    this.primary = "aut_idp_key";
    this.autfunction = params.internal?false:"WEB_FACTAB";
    this.record_title = L("aut_idp");
    this.records_title = L("aut_idp_m");

    this.askfirst = "type";

    var sqlteller = "(SELECT (SELECT TO_CHAR(COALESCE(SUM (fac_gui_counter_count), 0)) "
                  + "          FROM fac_gui_counter "
                  +"         WHERE fac_gui_counter_group = 'IDP_Login' AND fac_gui_counter_refkey = aut_idp.aut_idp_key "
                  + "           AND fac_gui_counter_date >= TRUNC (SYSDATE)) "
                  + "       || '/' || "
                  + "       (SELECT TO_CHAR(COALESCE(SUM (fac_gui_counter_count), 0)) "
                  + "          FROM fac_gui_counter "
                  + "         WHERE fac_gui_counter_group = 'IDP_Login' AND fac_gui_counter_refkey = aut_idp.aut_idp_key "
                  + "           AND fac_gui_counter_date > SYSDATE - 7) "
                  + "       || '/' || "
                  + "       (SELECT TO_CHAR(COALESCE(SUM (fac_gui_counter_count), 0)) "
                  + "          FROM fac_gui_counter "
                  + "         WHERE fac_gui_counter_group = 'IDP_Login' AND fac_gui_counter_refkey = aut_idp.aut_idp_key "
                  + "           AND fac_gui_counter_date > SYSDATE - 30) "
                  + "  FROM DUAL) "


    this.fields = {
        "id": {
            "dbs": "aut_idp_key",
            "label": L("lcl_key"),
            "typ": "key",
            "seq": "aut_s_aut_idp_key"
        },
        "code": {
            "dbs": "aut_idp_code",
            "label": L("aut_idp_code"),
            "typ": "varchar"
        },
        "name": {
            "dbs": "aut_idp_omschrijving",
            "label": L("aut_idp_omschrijving"),
            "typ": "varchar",
            "required": true
        },
        "type": {
            "dbs": "aut_idp_type",
            "label": L("aut_idp_type"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "LOV": L("aut_idp_typeLOV")
        },
        "remark": {
            "dbs": "aut_idp_opmerking",
            "label": L("aut_idp_opmerking"),
            "typ": "memo"
        },
        "secret": {
            "dbs": "aut_idp_secret",
            "label": L("aut_idp_secret"),
            "typ": "varchar",
            "defaultvalue": shared.random(32),
            "secret": true,
            "clone": false
        },
        "audience": {
            "dbs": "aut_idp_audience",
            "label": L("aut_idp_audience"),
            "typ": "varchar",
            "placeholder": customerId + ".facilitor.nl"
        },
        "issuer": {
            "dbs": "aut_idp_issuer",
            "label": L("aut_idp_issuer"),
            "typ": "varchar"
        },
        "algorithm": {
            "dbs": "aut_idp_algorithm",
            "label": L("aut_idp_algorithm"),
            "typ": "varchar"
        },
        "clockskew": {
            "dbs": "aut_idp_clockskew",
            "label": L("aut_idp_clockskew"),
            "typ": "number",
            "defaultvalue": 30
        },
        "duration": {
            "dbs": "aut_idp_duration",
            "label": L("aut_idp_duration"),
            "typ": "number"
        },
        "remoteloginurl": {
            "dbs": "aut_idp_remote_loginurl",
            "label": L("aut_idp_remote_loginurl"),
            "typ": "varchar"
        },
        "samlmetaurl": {
            "dbs": "aut_idp_saml_metaurl",
            "label": L("aut_idp_saml_metaurl"),
            "typ": "varchar"
        },
        "remotelogouturl": {
            "dbs": "aut_idp_remote_logouturl",
            "label": L("aut_idp_remote_logouturl"),
            "typ": "varchar"
        },
        "ipfilter": {
            "dbs": "aut_idp_ipfilter",
            "label": L("aut_idp_ipfilter"),
            "typ": "varchar"
        },
        "_currentIP" : {
            "dbs": "",
            "label": "Current IP",
            "typ": "label",
            "labelvalue": String(Request.ServerVariables("REMOTE_ADDR"))
        },
        "ipauto": {
            "dbs": "aut_idp_ipauto",
            "label": L("aut_idp_ipauto"),
            "typ": "check0"
        },
        "autocreate": {
            "dbs": "aut_idp_autocreate",
            "label": L("aut_idp_autocreate"),
            "typ": "key",
            "required": "true",
            "LOV": L("aut_idp_autocreateLOV"),
            "defaultvalue": 0
        },
        "company": {
             "dbs": "prs_bedrijf_key",
             "typ": "key",
             "foreign": { tbl: "prs_bedrijf",
                          key: "prs_bedrijf_key",
                          desc: "prs_bedrijf_naam",
                          "where": "prs_bedrijf_intern = 1" },
             "label": L("lcl_idp_company")
        },
        "department": {
             "dbs": "prs_afdeling_key",
             "typ": "key",
             "foreign": "prs_afdeling",
             "label": L("lcl_idp_department")
        },
        "authorization": {
            "dbs": "fac_functie_key",
            "label": L("aut_idp_functie_key"),
            "typ": "key",
            "foreign": "fac_functie"
        },
        "loglevel": {
            "dbs": "aut_idp_loglevel",
            "label": L("aut_idp_loglevel"),
            "typ": "number",
            "required": "true",
            "defaultvalue": 0
        },
        "internal": {
            "dbs": "aut_idp_internal",
            "label": L("aut_idp_internal"),
            "typ": "check0",
            "readonly": true
        },
        "created": {
            "dbs": "aut_idp_aanmaak",
            "label": L("aut_idp_aanmaak"),
            "typ": "date",
            "readonly": true
        },
        "usage" : {
            "dbs": "dummy",
            "label": L("aut_idp_usage"),
            typ: "varchar",
            sql: sqlteller
        }
    }
    this.includes = {
        "idpmappings": {
            "model": new model_aut_idp_map(),
            "joinfield": "identityprovider",
            "enable_update": true
        }
    };

    this.hook_pre_edit = function (obj, fld)
    {
        if (obj.id > -1)
        {
            var fld_hide = [];
            switch (obj.type.id)
            {
                case 4: // jwt
                        fld_hide = ["samlmetaurl"];
                        break;
                case 5: // saml
                        fld_hide = "algorithm audience secret timeout duration remoteloginurl samlmetaurl clockskew".split(" ");
                        break;
            }
            for (var d=0; d<fld_hide.length; d++)
            {
                var t = fld_hide[d];
                delete fld[t];
            }
        }
    }

    this.REST_GET    = generic_REST_GET(this);
    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = generic_REST_PUT(this);
    this.REST_DELETE = generic_REST_DELETE(this);
}
%>