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

     File:          pchange.asp
     Description:   Laat de user het wachtwoord wijzigen
     Context:       Modal vanuit bepaalde schermen, submit naar ..?

     NOTE:          Men kan zelf het eigen wachtwoord wijzigen, en
                    met WEB_PRSSYS of WEB_FACMGT rechten kan men dat ook
                    voor een ander (er wordt geen PRS-scope op FACMGT toegepast!)
*/ %>
<%
var EXPIRED_PASSWORD_OK = true;
var NO_OTP_OK = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../aut/login.inc" -->
<%
    var isExpired = getQParamInt("expired",0) == 1; // Ben ik hier naar toe gestuurd vanwege een expired wachtwoord?
                                                    // Dan redirecten we na afloop naar de voorpagina

    var theUser_key = getQParamInt("prs_key", user_key);
    var xfunc = user.func_enabled2("FAC", { prs_key: theUser_key, isOptional: true } );

    if (theUser_key != user_key && !xfunc.canWrite("WEB_PRSSYS")&& !xfunc.canWrite("WEB_FACMGT"))
      abort_with_warning(L("lcl_no_auth"));

    var user_info = new Perslid(theUser_key);
    // Als ik zelf geen PRSSYS heb mag ik niet het wachtwoord veranderen van mensen die dat wel hebben.
    // Anders een ernstig security lek
    if (!xfunc.canWrite("WEB_PRSSYS") && user_info.checkAutorisation("WEB_PRSSYS", true))
      abort_with_warning(L("lcl_no_auth"));

%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">
      function pass_submit_callback(json)
      {
        <% if (isExpired) { %>
            window.top.location.href = "<%=safe.jsstring(rooturl + "/")%>";
        <% } else { %>
            FcltMgr.closeDetail();
        <% } %>
      }

      function logOffCallback(json, textStatus)
      {
        if (json && json.message)
          FcltMgr.alert(json.message);
        else
          parent.location.href="<%=S("logoff_return_url")%>"
      };

      function logOff()
      {
        var data = {};
        <% protectRequest.dataToken("data"); %>
        $.post("../aut/LogOff.asp",
               data,
               logOffCallback,
               "json");
      }

      async function doOK()
      {
          if (!await validateForm("u2"))
              return false;

          $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackAndThen(pass_submit_callback), "json");
      }

      function doCancel()
      {
        <% if (isExpired) { %>
            logOff();
        <% } else { %>
          FcltMgr.closeDetail( {cancel: true });
        <% } %>
      }

      function doClearSessions()
      {
        FcltMgr.confirm(L("lcl_qrc_auth_clear"), function() {
            var data = {};
            <% protectRequest.dataToken("data"); %>
            $.post("prs_perslid_clear_sessions.asp?prs_key=<%=theUser_key%>", data, FcltCallbackAndThen(pass_submit_callback), "json");
        });
      }

      function doClearPassword()
      {
        FcltMgr.confirm(L("lcl_clear_password"), function() {
            var data = {
                clear: 1,
                password0: $("#password0").val() || ""
            };
            <% protectRequest.dataToken("data"); %>
            $.post($("form[name=u2]")[0].action, data, FcltCallbackAndThen(pass_submit_callback), "json");
        });
      }

      function clear_otp()
      {
          FcltMgr.confirm(L("lcl_R_U_sure"), function() {
              var data = {
                  prs_key: <%= theUser_key %>
              };
              <% protectRequest.dataToken("data"); %>
              $.post("prs_perslid_otp_clear.asp",
                  data,
                  FcltCallbackClose,
                  "json");
          });
      }


      $(function()
      {
          FcltMgr.resized();
      });

    </script>
  </head>

  <body class="fclt-modal" id="mod_prs_pwdchange">
  <% MODAL_START(); %>
    <form action="pchange_save.asp?prs_key=<%=theUser_key%>&submit=1" method="post" name="u2">
<%    MODAL_BLOCK_START("prsPwd", L("lcl_password_title") + " " + user_info.naam(), { icon: "fa-lock-alt" });
      ROFIELDTR("fld", L("lcl_vis_log_name"), user_info.oslogin() || user_info.oslogin2() || user_info.prs_perslid_email() );
      if (Session("must_reset_password") == 1)
      {
        %><tr><td colspan="2" class='label'><%=L("lcl_password_expired_title")%></td></tr><%
      }
      else
      {
         if (  (!xfunc.canWrite("WEB_PRSSYS") && !xfunc.canWrite("WEB_FACMGT"))
            || (theUser_key == user_key && user_info.haspw())
            )
            RWFIELDTR("password0", "fld", L("lcl_ch_password_old"), "", { required: true, html: " type=password" });
      }
      RWFIELDTR("password1", "fld", L("lcl_ch_password_new"), "", { required: true, html: " type=password autocomplete=new-password" });
      RWFIELDTR("password2", "fld", L("lcl_ch_password_con"), "", { required: true, html: " type=password autocomplete=new-password" });
      if (theUser_key != user_key)
      {
        RWCHECKBOXTR("pass_expired", "fldcheck", L("lcl_ch_pass_expired"), false, { boxfirst: true });
        // Een vinkje met 'Stuur bevestiging naar user_info.prs_perslid_email()' klinkt leuk maar dan staat
        // het wachtwoord plain-text in een e-mail wat not done is.
        // Als bovenstaand vinkje aanstaat zouden we wel de volgende aan kunnen zetten? Ooit...
        // if (user_info.prs_perslid_email())
        //    RWCHECKBOXTR("pass_mail", "fldcheck", L("lcl_ch_pass_mail"));
      }

      MODAL_BLOCK_END();
IFACE.FORM_END();
%>
    </form>

<%
      var buttons = [];
      if (!isExpired && (theUser_key == user_key || xfunc.canWrite("WEB_PRSSYS") || xfunc.canWrite("WEB_FACMGT")))
        buttons.push({title: L("lcl_delete_sessions"), icon:"fa-trash-alt", action:"doClearSessions()", importance: 1});
      if (!isExpired && (xfunc.canWrite("WEB_PRSSYS") || xfunc.canWrite("WEB_FACMGT")))
        buttons.push({title: L("lcl_delete_password"), icon:"fa-lock-open",   action:"doClearPassword()", importance: 1});

      if (!isExpired && user_info.otpsecret() && (xfunc.canWrite("WEB_PRSSYS") || xfunc.canWrite("WEB_FACMGT")))
        buttons.push({title: L("lcl_clear_otp"), icon:"fa-lock-open",   action:"clear_otp()", importance: 1});

      buttons.push({title: L("lcl_submit"), icon: "fa-fclt-save", action:"doOK()", importance: 1});
      buttons.push({title: L("lcl_cancel"), icon: "fa-fclt-cancel",    action:"doCancel()", importance: 3});

      SIMPLE_BLOCK_START();
        CreateButtons(buttons);
      SIMPLE_BLOCK_END();
%>
<% MODAL_END(); %>
  </body>
</html>

<% ASPPAGE_END(); %>
