RT# 75817 - Added the ability to set contacts password on the backend
authorChristopher Burger <burgerc@freeside.biz>
Tue, 20 Jun 2017 13:04:14 +0000 (09:04 -0400)
committerChristopher Burger <burgerc@freeside.biz>
Tue, 20 Jun 2017 13:04:14 +0000 (09:04 -0400)
httemplate/elements/change_password.html
httemplate/elements/contact.html
httemplate/elements/validate_password.html
httemplate/misc/process/change-password.html
httemplate/misc/xmlhttp-validate_password.html

index a84e823..b3a432f 100644 (file)
@@ -8,19 +8,19 @@
   display: none;
 }
 </STYLE>
-<A ID="<%$pre%>link" HREF="javascript:void(0)" onclick="<%$pre%>toggle(true)">(<% emt('change') %>)</A>
+<A ID="<%$pre%>link" HREF="javascript:void(0)" onclick="<%$pre%>toggle(true)">(<% emt( $change_title ) %>)</A>
 <DIV ID="<%$pre%>form" CLASS="passwordbox">
   <FORM METHOD="POST" ACTION="<%$fsurl%>misc/process/change-password.html">
-    <INPUT TYPE="hidden" NAME="svcnum" VALUE="<% $svc_acct->svcnum |h%>">
+    <% $change_id_input %>
     <INPUT TYPE="text" ID="<%$pre%>password" NAME="password" VALUE="<% $curr_value |h%>">
     <& /elements/random_pass.html, $pre.'password', 'randomize' &>
     <INPUT TYPE="submit" VALUE="change">
     <INPUT TYPE="button" VALUE="cancel" onclick="<%$pre%>toggle(false)">
     <DIV ID="<%$pre%>password_result" STYLE="font-size: smaller"></DIV>
     <& '/elements/validate_password.html', 
-         'fieldid' => $pre.'password',
-         'svcnum'  => $svc_acct->svcnum,
-
+         'fieldid'    => $pre.'password',
+         'svcnum'     => $svcnum,
+         'contactnum' => $contactnum,
     &>
 % if ( $error ) {
     <BR><SPAN STYLE="color: #ff0000"><% $error |h %></SPAN>
@@ -40,8 +40,28 @@ function <%$pre%>toggle(val) {
 </SCRIPT>
 <%init>
 my %opt = @_;
-my $svc_acct = $opt{'svc_acct'};
+
+my $contactnum = $opt{'contact_num'};
 my $curr_value = $opt{'curr_value'} || '';
-my $pre = 'changepw'.$svc_acct->svcnum.'_';
-my $error = $cgi->param($pre.'error');
+my $change_title = $opt{'label'} || 'change';
+
+my $svcnum;
+my $change_id_input = '';
+my $pre = 'changepw';
+
+if ($opt{'svc_acct'}) {
+  my $svc_acct = $opt{'svc_acct'};
+  $change_id_input = '<INPUT TYPE="hidden" NAME="svcnum" VALUE="' . $svc_acct->svcnum . '">';
+  $pre .= $svc_acct->svcnum . '_';
+  $svcnum = $svc_acct->svcnum;
+}
+elsif ($opt{'contact_num'}) {
+  $change_id_input = '
+    <INPUT TYPE="hidden" NAME="contactnum" VALUE="' . $opt{'contact_num'} . '">
+    <INPUT TYPE="hidden" NAME="custnum" VALUE="' . $opt{'custnum'} . '">
+  ';
+  $pre .= $opt{'contact_num'} . '_';
+}
+
+ my $error = $cgi->param($pre.'error');
 </%init>
index b0a7cac..8936569 100644 (file)
           <FONT SIZE="-1"><% $label{$field} %></FONT>
         </TD>
 %     }
+%     my $pwd_change_label = 'change password';
+%     $pwd_change_label = 'setup password' unless $contact->_password;
+%     if ($curr_value) {
+        <TD>
+          <span STYLE="white-space: nowrap">
+            <& /elements/change_password.html,
+              'contact_num' => $curr_value,
+              'custnum'     => $opt{'custnum'},
+              'curr_value'  => '',
+              'label'       => $pwd_change_label,
+            &>
+          </span>
+          <br>
+          <FONT SIZE="-1">Password</FONT>
+        </TD>
+%     }
     </TR>
   </TABLE>
 
index a488c4f..bd385a6 100644 (file)
@@ -27,7 +27,7 @@ function add_password_validation (fieldid) {
     var resultfield = document.getElementById(fieldid);
     if (this.value) {
       resultfield.innerHTML = '<SPAN STYLE="color: blue;">Validating password...</SPAN>';
-      validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} %>','password',this.value,
+      validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} %>','contactnum','<% $opt{'contactnum'} %>','password',this.value,
         function (result) {
           result = JSON.parse(result);
           var resultfield = document.getElementById(result.fieldid);
index d58ce54..250e832 100644 (file)
@@ -1,28 +1,63 @@
 <%init>
 my $curuser = $FS::CurrentUser::CurrentUser;
 
-$cgi->param('svcnum') =~ /^(\d+)$/ or die "illegal svcnum";
+$cgi->param('svcnum') =~ /^(\d+)$/ or die "illegal svcnum" if $cgi->param('svcnum');
 my $svcnum = $1;
-my $svc_acct = FS::svc_acct->by_key($svcnum)
-  or die "svc_acct $svcnum not found";
-my $part_svc = $svc_acct->part_svc;
-die "access denied" unless (
-  $curuser->access_right('Provision customer service') or
-  ( $curuser->access_right('Edit password') and 
-    ! $part_svc->restrict_edit_password )
-  );
+
+$cgi->param('contactnum') =~ /^(\d+)$/ or die "illegal contactnum" if $cgi->param('contactnum');
+my $contactnum = $1;
+
 my $newpass = $cgi->param('password');
-my $error = $svc_acct->is_password_allowed($newpass)
-        ||  $svc_acct->set_password($newpass)
-        ||  $svc_acct->replace;
 
-# annoyingly specific to view/svc_acct.cgi, for now...
-$cgi->delete('password');
+my $error;
+
+if ($svcnum) {
+       my $svc_acct = FS::svc_acct->by_key($svcnum)
+         or die "svc_acct $svcnum not found";
+       my $part_svc = $svc_acct->part_svc;
+       die "access denied" unless (
+         $curuser->access_right('Provision customer service') or
+         ( $curuser->access_right('Edit password') and 
+           ! $part_svc->restrict_edit_password )
+         );
+
+       my $error = $svc_acct->is_password_allowed($newpass)
+               ||  $svc_acct->set_password($newpass)
+               ||  $svc_acct->replace;
+
+       # annoyingly specific to view/svc_acct.cgi, for now...
+       $cgi->delete('password');
+}
+elsif ($contactnum) {
+       my $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
+      or return { 'error' => "Contact not found" . $contactnum };
+
+       my $error = $contact->is_password_allowed($newpass)
+               ||  $contact->change_password($newpass);
+
+       # annoyingly specific to view/svc_acct.cgi, for now...
+       #$cgi->delete('password');
+}
+
 </%init>
 % if ( $error ) {
-%   $cgi->param('svcnum', $svcnum);
-%   $cgi->param("changepw${svcnum}_error", $error);
+%      if ($svcnum) {
+%              $cgi->param('svcnum', $svcnum);
+%              $cgi->param("changepw${svcnum}_error", $error);
+%      }
+%      elsif ($contactnum) {
+%              $cgi->param('contactnum', $contactnum);
+%              $cgi->param("changepw${contactnum}_error", $error);
+%      }
 % } else {
-%   $cgi->query_string($svcnum);
+%      if ($svcnum) { $cgi->query_string($svcnum); }
+%      elsif ($contactnum) { $cgi->query_string($contactnum); }
+% }
+%warn('my pwd error is ' . $error . ' end');
+% if ($svcnum) { 
+       <% $cgi->redirect($fsurl.'view/svc_acct.cgi?'.$cgi->query_string) %>
 % }
-<% $cgi->redirect($fsurl.'view/svc_acct.cgi?'.$cgi->query_string) %>
+% elsif ($contactnum) { 
+       <% $cgi->redirect($fsurl.'edit/cust_main-contacts.html?'.$cgi->param('custnum')) %>
+% }
+
index 28dbf64..4d9716b 100644 (file)
@@ -28,17 +28,33 @@ my $validate_password = sub {
   $result{'syserror'} = 'Invoked without password' unless $password;
   return \%result if $result{'syserror'};
 
-  my $svcnum = $arg{'svcnum'};
-  $result{'syserror'} = 'Invalid svcnum' unless $svcnum =~ /^\d*$/;
-  return \%result if $result{'syserror'};
+  if ($arg{'contactnum'}) {
+    my $contactnum = $arg{'contactnum'};
+    $result{'syserror'} = 'Invalid contactnum' unless $contactnum =~ /^\d*$/;
+    return \%result if $result{'syserror'};
 
-  my $svc_acct = $svcnum 
-    ? qsearchs('svc_acct',{'svcnum' => $svcnum})
-    : (new FS::svc_acct {});
-  $result{'syserror'} = 'Could not find service' unless $svc_acct;
-  return \%result if $result{'syserror'};
+    my $contact = $contactnum 
+      ? qsearchs('contact',{'contactnum' => $contactnum})
+      : '';
+
+    $result{'error'} = $contact->is_password_allowed($password);
+  }
+
+  if ($arg{'svcnum'}) {
+    my $svcnum = $arg{'svcnum'};
+    $result{'syserror'} = 'Invalid svcnum' unless $svcnum =~ /^\d*$/;
+    return \%result if $result{'syserror'};
+
+    my $svc_acct = $svcnum 
+      ? qsearchs('svc_acct',{'svcnum' => $svcnum})
+      : (new FS::svc_acct {});
+    $result{'syserror'} = 'Could not find service' unless $svc_acct;
+    return \%result if $result{'syserror'};
+
+    $result{'error'} = $svc_acct->is_password_allowed($password);
+  }
 
-  $result{'error'} = $svc_acct->is_password_allowed($password);
# $result{'error'} = $svc_acct->is_password_allowed($password);
   $result{'valid'} = 1 unless $result{'error'};
   return \%result;
 };