make password-insecure option work when adding a new svc_acct, #40236
[freeside.git] / httemplate / edit / svc_acct.cgi
1 <& /elements/header.html, mt("$action [_1] account",$svc) &>
2
3 <& /elements/error.html &>
4
5 % if ( $cust_main ) { 
6
7   <& /elements/small_custview.html, $cust_main, '', 1,
8               popurl(2) . "view/cust_main.cgi" &>
9   <BR>
10 % } 
11
12 <FORM NAME="OneTrueForm" ACTION="<% $p1 %>process/svc_acct.cgi" METHOD=POST>
13 <INPUT TYPE="hidden" NAME="svcnum" VALUE="<% $svcnum %>">
14 <INPUT TYPE="hidden" NAME="pkgnum" VALUE="<% $pkgnum %>">
15 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $svcpart %>">
16
17 % if ( $svcnum ) {
18 % my $svclabel = emt("Service #[_1]",$svcnum);
19 % $svclabel =~ s/$svcnum/<B>$svcnum<\/B>/;
20 <% $svclabel %>
21 % } else {
22 <% mt("Service # (NEW)") |h %>
23 % }
24 <BR>
25
26 <% ntable("#cccccc",2) %>
27
28 <TR>
29   <TD ALIGN="right"><% mt('Service') |h %></TD>
30   <TD BGCOLOR="#eeeeee"><% $part_svc->svc %></TD>
31 </TR>
32
33 <% include('/elements/tr-td-label.html',
34      'label'    => mt('Username'),
35      'required' => $part_svc->part_svc_column('username')->required ) %>
36 % if ( $svcnum && $conf->exists('svc_acct-no_edit_username') ) {
37     <TD BGCOLOR="#eeeeee"><% $svc_acct->username() %></TD>
38     <INPUT TYPE="hidden" NAME="username" VALUE="<% $username %>">
39 % } else {
40     <TD>
41       <INPUT TYPE="text" NAME="username" VALUE="<% $username %>" SIZE=<% $ulen2 %> MAXLENGTH=<% $ulen %>>
42     </TD>
43 % }
44 </TR>
45
46 %if ( $part_svc->part_svc_column('_password')->columnflag ne 'F' ) {
47 % #XXX eventually should require "Edit Password" ACL
48 <% include('/elements/tr-td-label.html',
49      'label'    => mt('Password'),
50      'required' => $part_svc->part_svc_column('_password')->required ) %>
51   <TD>
52     <INPUT TYPE="text" ID="clear_password" NAME="clear_password" VALUE="<% $password %>" SIZE=<% $pmax2 %> MAXLENGTH=<% $pmax %>>
53     <& /elements/random_pass.html, 'clear_password' &><BR>
54     <DIV ID="clear_password_result" STYLE="font-size: smaller"></DIV>
55     <& /elements/validate_password.html, 
56         'fieldid' => 'clear_password',
57         'svcnum' => $svcnum ,
58         'pkgnum' => $pkgnum,
59     &>
60   </TD>
61 </TR>
62 %}else{
63     <INPUT TYPE="hidden" NAME="clear_password" VALUE="<% $password %>">
64 %}
65 <INPUT TYPE="hidden" NAME="_password_encoding" VALUE="<% $svc_acct->_password_encoding %>">
66 %
67 %my $sec_phrase = $svc_acct->sec_phrase;
68 %if ( $conf->exists('security_phrase') 
69 %  && $part_svc->part_svc_column('sec_phrase')->columnflag ne 'F' ) {
70
71 <% include('/elements/tr-td-label.html',
72      'label'    => mt('Security phrase'),
73      'required' => $part_svc->part_svc_column('sec_phrase')->required ) %>
74     <TD>
75       <INPUT TYPE="text" NAME="sec_phrase" VALUE="<% $sec_phrase %>" SIZE=32>
76       (<% mt('for forgotten passwords') |h %>)
77     </TD>
78   </TD>
79 % } else { 
80
81   <INPUT TYPE="hidden" NAME="sec_phrase" VALUE="<% $sec_phrase %>">
82 % } 
83 %
84 %#domain
85 %my $domsvc = $svc_acct->domsvc || 0;
86 %if ( $part_svc->part_svc_column('domsvc')->columnflag eq 'F' ) {
87 %
88
89   <INPUT TYPE="hidden" NAME="domsvc" VALUE="<% $domsvc %>">
90 % } else { 
91 %
92 %  my %svc_domain = ();
93 %
94 %  if ( $domsvc ) {
95 %    my $svc_domain = qsearchs('svc_domain', { 'svcnum' => $domsvc, } );
96 %    if ( $svc_domain ) {
97 %      $svc_domain{$svc_domain->svcnum} = $svc_domain->domain;
98 %    } else {
99 %      warn "unknown svc_domain.svcnum for svc_acct.domsvc: $domsvc";
100 %    }
101 %  }
102 %
103 %  %svc_domain = (%svc_domain,
104 %                 domain_select_hash FS::svc_acct('svcpart' => $svcpart,
105 %                                                 'pkgnum'  => $pkgnum,
106 %                                                )
107 %                );
108
109 <% include('/elements/tr-td-label.html',
110      'label'    => mt('Domain'),
111      'required' => $part_svc->part_svc_column('domsvc')->required ) %>
112     <TD>
113       <SELECT NAME="domsvc" SIZE=1>
114 % foreach my $svcnum (
115 %             sort { $svc_domain{$a} cmp $svc_domain{$b} }
116 %                  keys %svc_domain
117 %           ) {
118 %             my $svc_domain = $svc_domain{$svcnum};
119 %        
120
121              <OPTION VALUE="<% $svcnum %>" <% $svcnum == $domsvc ? ' SELECTED' : '' %>><% $svc_domain{$svcnum} %>
122 % } 
123
124       </SELECT>
125     </TD>
126   </TR>
127 % } 
128
129
130 % if ( $communigate ) {
131
132     <TR>
133       <TD ALIGN="right"><% mt('Aliases') |h %></TD>
134       <TD><INPUT TYPE="text" NAME="cgp_aliases" VALUE="<% $svc_acct->cgp_aliases %>"></TD>
135     </TR>
136
137 % } else {
138     <INPUT TYPE="hidden" NAME="cgp_aliases" VALUE="<% $svc_acct->cgp_aliases %>">
139 % }
140
141
142 <& /elements/tr-select-svc_pbx.html,
143              'curr_value' => $svc_acct->pbxsvc,
144              'part_svc'   => $part_svc,
145              'cust_pkg'   => $cust_pkg,
146 &>
147
148 %#pop
149 %my $popnum = $svc_acct->popnum || 0;
150 %if ( $part_svc->part_svc_column('popnum')->columnflag eq 'F' ) {
151 %
152
153   <INPUT TYPE="hidden" NAME="popnum" VALUE="<% $popnum %>">
154 % } else { 
155
156 <% include('/elements/tr-td-label.html',
157      'label'    => mt('Access number'),
158      'required' => $part_svc->part_svc_column('popnum')->required ) %>
159     <TD><% FS::svc_acct_pop::popselector($popnum) %></TD>
160   </TR>
161 % } 
162
163 %#tower (huh)
164 %if ( $conf->exists('svc_acct-tower_sector')
165 %     && $part_svc->part_svc_column('sectornum')->columnflag ne 'F' ) {
166     <& /elements/tr-select-tower_sector.html,
167          'curr_value' => $svc_acct->sectornum,
168          #'part_svc'   => $part_svc,
169          #'cust_pkg'   => $cust_pkg,
170          'required'   => $part_svc->part_svc_column('sectornum')->required,
171     &>
172 %} else {
173     <INPUT TYPE="hidden" NAME="sectornum" VALUE="<% $svc_acct->sectornum %>">
174 %}
175
176 <& /elements/tr-svc_export_machine.html,
177      'svc'      => $svc_acct,
178      'part_svc' => $part_svc,
179      'cgi'      => $cgi,
180 &>
181
182 % #uid/gid 
183 % foreach my $xid (qw( uid gid )) { 
184 %
185 %  if ( $part_svc->part_svc_column($xid)->columnflag =~ /^[FA]$/
186 %       || ! $conf->exists("svc_acct-edit_$xid")
187 %     ) {
188 %  
189 % if ( length($svc_acct->$xid()) ) { 
190
191 <% include('/elements/tr-td-label.html',
192      'label'    => uc($xid),
193      'required' => $part_svc->part_svc_column($xid)->required ) %>
194       <TR>
195         <TD ALIGN="right"><% uc($xid) %></TD>
196           <TD BGCOLOR="#eeeeee"><% $svc_acct->$xid() %></TD>
197         <TD>
198         </TD>
199       </TR>
200 % } 
201   
202     <INPUT TYPE="hidden" NAME="<% $xid %>" VALUE="<% $svc_acct->$xid() %>">
203 % } else { 
204   
205 <% include('/elements/tr-td-label.html',
206      'label'    => uc($xid),
207      'required' => $part_svc->part_svc_column($xid)->required ) %>
208       <TD>
209         <INPUT TYPE="text" NAME="<% $xid %>" SIZE=8 MAXLENGTH=6 VALUE="<% $svc_acct->$xid() %>">
210       </TD>
211     </TR>
212 % } 
213 % } 
214 %
215 %#finger
216 %if ( $part_svc->part_svc_column('uid')->columnflag eq 'F'
217 %     && ! $svc_acct->finger ) { 
218 %
219
220   <INPUT TYPE="hidden" NAME="finger" VALUE="">
221 % } else { 
222
223
224 <% include('/elements/tr-td-label.html',
225      'label'    => mt('Real Name'),
226      'required' => $part_svc->part_svc_column('finger')->required ) %>
227     <TD>
228       <INPUT TYPE="text" NAME="finger" VALUE="<% $svc_acct->finger %>">
229     </TD>
230   </TR>
231 % } 
232 %
233 %#dir
234 %if ( $part_svc->part_svc_column('dir')->columnflag eq 'F'
235 %     || !$curuser->access_right('Edit home dir')
236 %   ) { 
237
238
239 <INPUT TYPE="hidden" NAME="dir" VALUE="<% $svc_acct->dir %>">
240 % } else {
241
242
243 <% include('/elements/tr-td-label.html',
244      'label'    => mt('Home directory'),
245      'required' => $part_svc->part_svc_column('dir')->required ) %>
246     <TD><INPUT TYPE="text" NAME="dir" VALUE="<% $svc_acct->dir %>"></TD>
247   </TR>
248 % } 
249 %
250 %#shell
251 %my $shell = $svc_acct->shell;
252 %if ( $part_svc->part_svc_column('shell')->columnflag eq 'F'
253 %     || ( !$shell && $part_svc->part_svc_column('uid')->columnflag eq 'F' )
254 %   ) {
255 %
256
257   <INPUT TYPE="hidden" NAME="shell" VALUE="<% $shell %>">
258 % } else { 
259
260
261 <% include('/elements/tr-td-label.html',
262      'label'    => mt('Shell'),
263      'required' => $part_svc->part_svc_column('shell')->required ) %>
264     <TD>
265       <SELECT NAME="shell" SIZE=1>
266 %
267 %           my($etc_shell);
268 %           foreach $etc_shell (@shells) {
269 %        
270
271           <OPTION<% $etc_shell eq $shell ? ' SELECTED' : '' %>><% $etc_shell %>
272 % } 
273
274
275       </SELECT>
276     </TD>
277   </TR>
278 % } 
279
280 <& svc_acct/communigate.html,
281              'svc_acct'    => $svc_acct,
282              'part_svc'    => $part_svc,
283              'communigate' => $communigate,
284 &>
285
286 % if ( $conf->exists('svc_acct-ip_addr') ) {
287 %   # router/block selection UI
288 %   # (should we show this if slipip is fixed?)
289 <& /elements/tr-select-router_block_ip.html, 
290   'object' => $svc_acct,
291   'ip_field' => 'slipip',
292   'required' => $part_svc->part_svc_column('routernum')->required,
293   'ip_addr_required' => $part_svc->part_svc_column('slipip')->required,
294 &>
295 % } else {
296 %   # don't expose these to the user--they're only useful in the other case
297   <INPUT TYPE="hidden" NAME="routernum" VALUE="<% $svc_acct->routernum %>">
298   <INPUT TYPE="hidden" NAME="blocknum"  VALUE="<% $svc_acct->blocknum  %>">
299 %   if ( $part_svc->part_svc_column('slipip')->columnflag =~ /^[FA]$/ ) { 
300     <INPUT TYPE="hidden" NAME="slipip" VALUE="<% $svc_acct->slipip %>">
301 %   } else { 
302 <% include('/elements/tr-td-label.html',
303      'label'    => mt('IP'),
304      'required' => $part_svc->part_svc_column('slipip')->required ) %>
305       <TD><INPUT TYPE="text" NAME="slipip" VALUE="<% $svc_acct->slipip %>"></TD>
306     </TR>
307 %   }
308 % }
309
310 % my %label = ( seconds => 'Time',
311 %               upbytes => 'Upload bytes',
312 %               downbytes => 'Download bytes',
313 %               totalbytes => 'Total bytes',
314 %             );
315 % foreach my $uf (keys %label) {
316 %   my $tf = $uf . "_threshold";
317 %   if ( $curuser->access_right('Edit usage') ) { 
318   <TR>
319     <TD ALIGN="right"><% mt("[_1] remaining",$label{$uf}) |h %> </TD>
320     <TD><INPUT TYPE="text" NAME="<% $uf %>" VALUE="<% $svc_acct->$uf %>">(<% mt('blank disables') |h %>)</TD>
321   </TR>
322   <TR>
323     <TD ALIGN="right"><% mt("[_1] threshold",$label{$uf}) |h %> </TD>
324     <TD><INPUT TYPE="text" NAME="<% $tf %>" VALUE="<% $svc_acct->$tf %>">(<% mt('blank disables') |h %>)</TD>
325   </TR>
326 %   }else{
327       <INPUT TYPE="hidden" NAME="<% $uf %>" VALUE="<% $svc_acct->$uf %>">
328       <INPUT TYPE="hidden" NAME="<% $tf %>" VALUE="<% $svc_acct->$tf %>">
329 %   } 
330 % }
331 %
332 %foreach my $r ( grep { /^r(adius|[cr])_/ } fields('svc_acct') ) {
333 %  $r =~ /^^r(adius|[cr])_(.+)$/ or next; #?
334 %  my $a = $2;
335 %
336 % if ( $part_svc->part_svc_column($r)->columnflag =~ /^[FA]$/ ) { 
337
338     <INPUT TYPE="hidden" NAME="<% $r %>" VALUE="<% $svc_acct->getfield($r) %>">
339 % } else { 
340
341     <TR>
342       <TD ALIGN="right"><% $FS::raddb::attrib{$a} %></TD>
343       <TD><INPUT TYPE="text" NAME="<% $r %>" VALUE="<% $svc_acct->getfield($r) %>"></TD>
344     </TR>
345 % } 
346 % } 
347
348
349 <% include('/elements/tr-td-label.html',
350      'label'    => mt('RADIUS groups'),
351      'required' => $part_svc->part_svc_column('usergroup')->required ) %>
352 % if ( $part_svc_usergroup->columnflag eq 'F' ) { 
353     <TD BGCOLOR="#eeeeee"><% join('<BR>', @groupnames) %></TD>
354 % } else { 
355 %   my $radius_group_selected = '';
356 %   if ( $svc_acct->svcnum ) {
357 %      $radius_group_selected = join(',',$svc_acct->radius_groups('groupnum'));
358 %   }
359 %   elsif ( !$svc_acct->svcnum && $part_svc_usergroup->columnflag eq 'D' ) {
360 %       $radius_group_selected = $part_svc_usergroup->columnvalue;
361 %   }
362     <TD><& /elements/select-radius_group.html, 
363                 curr_value => $radius_group_selected,
364                 element_name => 'radius_usergroup',
365                 multiple => 1,
366         &>
367     </TD>
368 % } 
369
370 </TR>
371
372 % if ( $part_svc->has_router ) {
373 <& /elements/hidden.html,
374     field => 'router_routernum',
375     curr_value => $svc_acct->router_routernum
376 &>
377 <& /elements/tr-input-text.html,
378     label => 'Attached router name',
379     field => 'router_routername',
380     size  => 32,
381     curr_value => $svc_acct->router_routername
382 &>
383 <& /elements/tr-select-table.html,
384     label         => 'Attached address block',
385     field         => 'router_blocknum',
386     table         => 'addr_block',
387     hashref       => { 'routernum' => '0' },
388     extra_sql     => ($svc_acct->router_routernum ?
389                         ' OR routernum = '.$svc_acct->router_routernum : ''),
390     agent_virt    => 1,
391     agent_null    => 1,
392     name_col      => 'cidr',
393     order_by      => 'ORDER BY ip_gateway, ip_netmask',
394     empty_label   => '(none)',
395     disable_empty => 0,
396     curr_value    => $svc_acct->router_blocknum
397 &>
398 % }
399
400 % foreach my $field ($svc_acct->virtual_fields) { 
401 % # If the flag is X, it won't even show up in $svc_acct->virtual_fields. 
402 % if ( $part_svc->part_svc_column($field)->columnflag ne 'F' ) { 
403
404     <% $svc_acct->pvf($field)->widget('HTML', 'edit', $svc_acct->getfield($field)) %>
405 % } 
406 % } 
407   
408 </TABLE>
409 <BR>
410
411 % if ( $captcha_url ) {
412 <IMG SRC="<% $captcha_url %>"><BR>
413 <% mt('Enter the word shown above:') |h %> <INPUT TYPE="text" NAME="captcha_response"><BR>
414 <BR>
415 % }
416
417 <INPUT TYPE="submit" VALUE="Submit">
418
419 </FORM>
420
421 <& /elements/footer.html &>
422
423 <%init>
424
425 die "access denied"
426   unless $FS::CurrentUser::CurrentUser->access_right('Provision customer service'); #something else more specific?
427
428 my $conf = new FS::Conf;
429 my @shells = $conf->config('shells');
430
431 my $curuser = $FS::CurrentUser::CurrentUser;
432
433 my($svcnum, $pkgnum, $svcpart, $part_svc, $svc_acct, @groups);
434 if ( $cgi->param('error') ) {
435
436   $svc_acct = new FS::svc_acct ( {
437     map { $_, scalar($cgi->param($_)) } fields('svc_acct')
438   } );
439   $svcnum = $svc_acct->svcnum;
440   $pkgnum = $cgi->param('pkgnum');
441   $svcpart = $cgi->param('svcpart');
442   $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
443   die "No part_svc entry for svcpart $svcpart!" unless $part_svc;
444   @groups = $cgi->param('radius_usergroup');
445
446 } elsif ( $cgi->param('pkgnum') && $cgi->param('svcpart') ) { #adding
447
448   $cgi->param('pkgnum') =~ /^(\d+)$/ or die 'unparsable pkgnum';
449   $pkgnum = $1;
450   $cgi->param('svcpart') =~ /^(\d+)$/ or die 'unparsable svcpart';
451   $svcpart = $1;
452
453   $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
454   die "No part_svc entry!" unless $part_svc;
455
456   $svc_acct = new FS::svc_acct({svcpart => $svcpart}); 
457
458   $svcnum='';
459
460   $svc_acct->password_recover('Y'); #default. hmm.
461
462 } else { #editing
463
464   my($query) = $cgi->keywords;
465   $query =~ /^(\d+)$/ or die "unparsable svcnum";
466   $svcnum=$1;
467   $svc_acct=qsearchs('svc_acct',{'svcnum'=>$svcnum})
468     or die "Unknown (svc_acct) svcnum!";
469
470   my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum})
471     or die "Unknown (cust_svc) svcnum!";
472
473   $pkgnum=$cust_svc->pkgnum;
474   $svcpart=$cust_svc->svcpart;
475
476   $part_svc = qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
477   die "No part_svc entry for svcpart $svcpart!" unless $part_svc;
478
479   @groups = $svc_acct->radius_groups;
480
481 }
482
483 my $communigate = scalar($part_svc->part_export('communigate_pro'));
484                 # || scalar($part_svc->part_export('communigate_pro_singledomain'));
485
486 my( $cust_pkg, $cust_main ) = ( '', '' );
487 if ( $pkgnum ) {
488   $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $pkgnum } );
489   $cust_main = $cust_pkg->cust_main;
490 }
491
492 unless ( $svcnum || $cgi->param('error') ) { #adding
493
494   #set gecos
495   if ($cust_main) {
496     unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
497       $svc_acct->setfield('finger',
498         $cust_main->getfield('first') . " " . $cust_main->getfield('last')
499       );
500     }
501   }
502
503   $svc_acct->set_default_and_fixed( {
504     #false laziness w/svc-acct::_fieldhandlers
505     'usergroup' => sub { 
506                          my( $self, $groups ) = @_;
507                          if ( ref($groups) eq 'ARRAY' ) {
508                            @groups = @$groups;
509                            $groups;
510                          } elsif ( length($groups) ) {
511                            @groups = split(/\s*,\s*/, $groups);
512                            [ @groups ];
513                          } else {
514                            @groups = ();
515                            [];
516                          }
517                        }
518   } );
519
520 }
521
522 my $part_svc_usergroup = $part_svc->part_svc_column('usergroup');
523 #fixed radius groups always override & display
524 my @groupnames; # only used for display of Fixed RADIUS groups
525 if ( $part_svc_usergroup->columnflag eq 'F' ) {
526   @groups = split(',',$part_svc_usergroup->columnvalue);
527   @groupnames = map { $_->long_description } 
528                     qsearch({ 'table'         => 'radius_group',
529                            'extra_sql'     => "where groupnum in (".$part_svc_usergroup->columnvalue.")",
530                         }) if length($part_svc_usergroup->columnvalue);
531 }
532
533 my $action = $svcnum ? 'Edit' : 'Add';
534
535 my $svc = $part_svc->getfield('svc');
536
537 my $username = $svc_acct->username;
538
539 my $password = '';
540 if ( $cgi->param('error') ) {
541   $password = $cgi->param('clear_password');
542 } elsif ( $svcnum ) {
543   my $password_encryption = $svc_acct->_password_encryption;
544   if ( $password = $svc_acct->get_cleartext_password ) {
545     $password = '*HIDDEN*' unless $conf->exists('showpasswords');
546   } elsif( $svc_acct->_password and $password_encryption ne 'plain' ) {
547     $password = "(".uc($password_encryption)." encrypted)";
548   }
549 }
550
551 my $ulen = 
552   $conf->exists('usernamemax')
553   ? $conf->config('usernamemax')
554   : dbdef->table('svc_acct')->column('username')->length;
555 my $ulen2 = $ulen+2;
556
557 my $pmax = max($conf->config('passwordmax') || 12);
558 my $pmax2 = $pmax+2;
559
560 my $p1 = popurl(1);
561
562 sub max {
563   (sort(@_))[-1]
564 }
565
566 my $captcha_url;
567 my ($export_google) = $part_svc->part_export('acct_google');
568 if ( $export_google ) {
569   my $error = $export_google->auth_error;
570   if ( $error ) {
571     if ( $error->{'captcha_url'} ) {
572       $captcha_url = $error->{'captcha_url'};
573     }
574     else {
575       $cgi->param('error', $error->{'message'});
576     }
577   } #if $error
578 }
579
580 if ( $part_svc->has_router ) { # duplicates the one in elements/svc_Common
581   if ( $svcnum ) {
582     my $router = qsearchs('router', {svcnum => $svc_acct->svcnum});
583     if ( $router ) {
584       $svc_acct->set("router_$_", $router->get($_))
585         foreach ('routername', 'routernum');
586       my ($block) = $router->addr_block;
587       $svc_acct->set('router_blocknum', $block->blocknum) if ( $block );
588     }
589   }
590   foreach (qw(router_routername router_routernum router_blocknum)) {
591     if ( $cgi->param($_) =~ /^(\w+)$/ ) {
592       $svc_acct->set($_, $1);
593     }
594   }
595 }
596
597 </%init>