RT# 75817 - Added the ability to set contacts password on the backend
[freeside.git] / httemplate / misc / xmlhttp-validate_password.html
1 <%doc>
2 Requires cgi params 'password' (plaintext) and 'sub' ('validate_password' is
3 only acceptable value.)  Also accepts 'svcnum' (for svc_acct, will otherwise
4 create an empty dummy svc_acct), 'pkgnum' (for when the svc_acct isn't yet
5 inserted), and 'fieldid' (for html post-processing, passed along in results
6 for convenience.)
7
8 Returns a json-encoded hashref with keys of 'valid' (set to 1 if object is
9 valid), 'error' (error text if password is invalid) or 'syserror' (error text
10 if password could not be validated.)  Only one of these keys will be set.
11 Will also set 'fieldid' if it was passed.
12 </%doc>
13
14 <% encode_json($result) %>
15
16 <%init>
17
18 my $validate_password = sub {
19   my %arg = $cgi->param('arg');
20   my %result;
21
22   $result{'fieldid'} = $arg{'fieldid'}
23     if $arg{'fieldid'} =~ /^\w+$/;
24
25   $result{'syserror'} = 'Request is not POST' unless $cgi->request_method eq 'POST';
26   return \%result if $result{'syserror'};
27
28   my $password = $arg{'password'};
29   $result{'syserror'} = 'Invoked without password' unless $password;
30   return \%result if $result{'syserror'};
31
32   if ($arg{'contactnum'}) {
33     my $contactnum = $arg{'contactnum'};
34     $result{'syserror'} = 'Invalid contactnum' unless $contactnum =~ /^\d*$/;
35     return \%result if $result{'syserror'};
36
37     my $contact = $contactnum 
38       ? qsearchs('contact',{'contactnum' => $contactnum})
39       : '';
40
41     $result{'error'} = $contact->is_password_allowed($password);
42   }
43
44   if ($arg{'svcnum'}) {
45     my $pkgnum = $arg{'pkgnum'};
46     $result{'syserror'} = 'Invalid pkgnum' unless $pkgnum =~ /^\d*$/;
47     return \%result if $result{'syserror'};
48
49     my $svcnum = $arg{'svcnum'};
50     $result{'syserror'} = 'Invalid svcnum' unless $svcnum =~ /^\d*$/;
51     return \%result if $result{'syserror'};
52
53     my $svc_acct = $svcnum 
54       ? qsearchs('svc_acct',{'svcnum' => $svcnum})
55       : FS::svc_acct->new({ 'pkgnum' => $pkgnum });
56     $result{'syserror'} = 'Could not find service' unless $svc_acct;
57     return \%result if $result{'syserror'};
58
59     $result{'error'} = $svc_acct->is_password_allowed($password);
60   }
61
62  # $result{'error'} = $svc_acct->is_password_allowed($password);
63   $result{'valid'} = 1 unless $result{'error'};
64   return \%result;
65 };
66
67 my $result = ($cgi->param('sub') eq 'validate_password')
68              ? &$validate_password()
69              : { 'syserror' => 'Invalid sub' };
70
71 </%init>