<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$


     File:                  prs_perslid_apikey_save.asp
     Description:           Opslaan van de API-key van persoon.
     Parameters:
        prs_key:            perslid
        prs_apikey:         Nieuwe API-key.

     Context:
     Note:
*/
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../prs/prs.inc" -->

<%
protectRequest.validateToken();

//user.checkAutorisation("WEB_FACTAB"); // altijd nodig

var prs_perslid_apikey = getFParam("prs_apikey");
if (!user.has("WEB_FACTAB"))
{
  prs_perslid_apikey = shared.random(20); // altijd genereren
}
var prs_perslid_key = getFParamInt("prs_key");

var prsauthparams = prs.checkAutorisation(prs_perslid_key);
user.auth_required_or_abort(prsauthparams.writesys);

var result = { prs_key: prs_perslid_key, success: true };

var sql = "SELECT prs_perslid_apikey"
        + "  FROM prs_perslid"
        + " WHERE prs_perslid_key = " + prs_perslid_key;
var oRs = Oracle.Execute(sql);
var oldapi = oRs("prs_perslid_apikey").Value || "";
oRs.Close();

var newapi = prs_perslid_apikey || "";
if (oldapi != newapi)
{
  var sql = "UPDATE prs_perslid"
          + "   SET prs_perslid_apikey = " + safe.quoted_sql(prs_perslid_apikey)
          + " WHERE prs_perslid_key = " + prs_perslid_key;
  Oracle.Execute(sql);

  var trackingtxt = L("lcl_prs_api_key_upd");
  if (oldapi && !newapi)
    trackingtxt = L("lcl_tracktoempty")
  else if (!oldapi && newapi)
    trackingtxt = L("lcl_prs_api_key_set");

  shared.trackaction("PRSUPD", prs_perslid_key, L("lcl_prs_apiuser") + " " + trackingtxt);
}
else
  result.message = L("lcl_scf_no_changes");

Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
