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