<%@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;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/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 = getQParamInt("prs_key", user_key);
    var xfunc = user.func_enabled2("FAC", { prs_key: theUser, isOptional: true } );

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

    // 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") && new Perslid(theUser).checkAutorisation("WEB_PRSSYS", true))
      abort_with_warning(L("lcl_no_auth"));

    var user_info = new Perslid(theUser);
%>
<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)
          alert(json.message);
        else
          parent.location.href="<%=S("logoff_return_url")%>"
      };

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

      function doOK()
      {
          if (!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()
      {
        if (confirm(L("lcl_qrc_auth_clear")))
        {
          var data = {};
          <% protectRequest.dataToken("data"); %>
          $.post( "prs_perslid_clear_sessions.asp?prs_key=<%=theUser%>"
                , data
                , FcltCallbackAndThen(pass_submit_callback)
                , "json");
        }
      }

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

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

    </script>
  </head>

  <body class="modal" id="mod_prs_pwdchange">
    <form action="pchange_save.asp?prs_key=<%=theUser%>&submit=1" method="post" name="u2">
<%    BLOCK_START("prsPwd", L("lcl_password_title") + " " + safe.html(user_info.naam()));
      if (Session("must_reset_password") == 1)
      {
        %><tr><td colspan="2"><%=L("lcl_password_expired_title")%></td></tr><%
      }
      else
      {
         if (  (!xfunc.canWrite("WEB_PRSSYS") && !xfunc.canWrite("WEB_FACMGT"))
            || (theUser == user_key && user_info.haspw())
            )
            RWFIELDTR("password0", "fldnotresponsive", L("lcl_ch_password_old"), "", { required: true, html: " type=password" });
      }
      RWFIELDTR("password1", "fldnotresponsive", L("lcl_ch_password_new"), "", { required: true, html: " type=password" });
      RWFIELDTR("password2", "fldnotresponsive", L("lcl_ch_password_con"), "", { required: true, html: " type=password" });
      // 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 absoluut not done is.
      BLOCK_END();
IFACE.FORM_END();
%>
    </form>

<%
      var buttons = [];
      if (!isExpired && (theUser == user_key || xfunc.canWrite("WEB_PRSSYS") || xfunc.canWrite("WEB_FACMGT")))
        buttons.push({title: L("lcl_delete_sessions"), icon:"../Pictures/reject.png", action:"doClearSessions()"});
      if (!isExpired && (xfunc.canWrite("WEB_PRSSYS") || xfunc.canWrite("WEB_FACMGT")))
        buttons.push({title: L("lcl_delete_password"), icon:"../Pictures/undo.png",   action:"doClearPassword()"});

      buttons.push({title: L("lcl_submit"), icon:"../Pictures/opslaan.png", action:"doOK()"});
      buttons.push({title: L("lcl_cancel"), icon:"../Pictures/undo.png",    action:"doCancel()"});

      CreateButtons(buttons)
%>
  </body>
</html>

