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