scalar cgi param
[freeside.git] / httemplate / edit / process / svc_acct.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "svc_acct.cgi?". $cgi->query_string ) %>
4 %} else {
5 <% $cgi->redirect(popurl(3). "view/svc_acct.cgi?" . $svcnum ) %>
6 %}
7 <%init>
8 use CGI::Carp;
9 die "access denied"
10   unless $FS::CurrentUser::CurrentUser->access_right('Provision customer service'); #something else more specific?
11
12 $cgi->param('svcnum') =~ /^(\d*)$/ or die "Illegal svcnum!";
13 my $svcnum = $1;
14
15 my $old;
16 if ( $svcnum ) {
17   $old = qsearchs('svc_acct', { 'svcnum' => $svcnum } )
18     or die "fatal: can't find account (svcnum $svcnum)!";
19 } else {
20   $old = '';
21 }
22
23 #unmunge popnum
24 $cgi->param('popnum', (split(/:/, $cgi->param('popnum') ))[0] );
25
26 #unmunge usergroup
27 $cgi->param('usergroup', [ $cgi->param('radius_usergroup') ] );
28
29 #unmunge bytecounts
30 foreach (map { $_,$_."_threshold" } qw( upbytes downbytes totalbytes )) {
31   $cgi->param($_, FS::UI::bytecount::parse_bytecount($cgi->param($_)) );
32 }
33
34 #for slipip, convert '(automatic)' to null
35 my $ip_addr = $cgi->param('slipip');
36 $ip_addr =~ s/[^\d\.]//g;
37 $cgi->param('slipip', $ip_addr);
38
39 #unmunge cgp_accessmodes (falze laziness-ish w/part_svc.pm::process &svc_domain)
40 unless ( $cgi->param('cgp_accessmodes') ) {
41   $cgi->param('cgp_accessmodes', 
42     join(' ',
43       sort map { /^cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
44                grep $cgi->param($_),
45                     grep /^cgp_accessmodes_([\w\/]+)$/,
46                          $cgi->param()
47         )
48   );
49 }
50
51 my %hash = $svcnum ? $old->hash : ();
52 for ( fields('svc_acct'), qw( pkgnum svcpart usergroup ) ) {
53     $hash{$_} = scalar($cgi->param($_));
54 }
55 if ( $svcnum ) {
56   for ( grep $old->$_, qw( cf_privatekey ) ) {
57     $hash{$_} = $old->$_;
58   }
59 }
60 my $new = new FS::svc_acct ( \%hash );
61
62 my $error = '';
63
64 my $part_svc = $svcnum ? 
65                 $old->part_svc : 
66                 qsearchs( 'part_svc', 
67                   { 'svcpart' => scalar($cgi->param('svcpart')) }
68                 );
69
70 # google captcha auth
71 if ( $cgi->param('captcha_response') ) {
72   my ($export) = $part_svc->part_export('acct_google');
73   if ( $export and
74       ! $export->captcha_auth($cgi->param('captcha_response')) ) { 
75     $error = 'Re-enter the security word.';
76   }
77 }
78
79 # check whether the password is set as "fixed" in the service def. if so,
80 # ignore the password that was submitted and use the fixed value.
81
82 my $psc = $part_svc->part_svc_column('_password');
83 if ( $psc->columnflag eq 'F' ) {
84
85   $new->set('_password', $psc->columnvalue);
86
87 } else {
88
89   $new->_password($old->_password) if $old;
90   if (     $cgi->param('clear_password') eq '*HIDDEN*'
91         || $cgi->param('clear_password') =~ /^\(.* encrypted\)$/ ) {
92     die "fatal: no previous account to recall hidden password from!"
93       unless $old;
94   } else {
95     my $newpass = $cgi->param('clear_password');
96     if ( !$old or ! $old->check_password($newpass) ) {
97       # then the password is being changed
98       $error ||= $new->is_password_allowed($newpass)
99              ||  $new->set_password($newpass);
100     }
101   }
102
103 }
104
105 if ( ! $error ) {
106
107   my $export_info = FS::part_export::export_info();
108
109   my @child_objects =
110     map FS::svc_export_machine->new({
111           'svcnum'     => $svcnum,
112           'exportnum'  => $_->exportnum,
113           'machinenum' => scalar($cgi->param('exportnum'.$_->exportnum.'machinenum')),
114         }),
115       grep { $_->machine eq '_SVC_MACHINE' }
116         $part_svc->part_export;
117
118   if ( $part_svc->has_router ) {
119     my $router = FS::router->new({
120       map { $_ => scalar($cgi->param("router_$_")) }
121       qw( routernum routername blocknum )
122     });
123     if (length($router->routername) == 0) {
124       #sensible default
125       $router->set('routername', $new->label);
126     }
127     if (length($router->blocknum) == 0) {
128       #unset it
129       $router->set('blocknum', 0);
130     }
131     push @child_objects, $router;
132   }
133
134
135   if ( $svcnum ) {
136     foreach ( grep { $old->$_ != $new->$_ }
137                    qw( seconds upbytes downbytes totalbytes )
138             )
139     {
140       my %hash = map { $_ => $new->$_ } 
141                  grep { $new->$_ }
142                  qw( seconds upbytes downbytes totalbytes );
143
144       $error ||= "invalid $_" foreach grep { $hash{$_} !~ /^-?\d+$/ } keys %hash;
145       $error ||= $new->set_usage(\%hash);  #unoverlimit and trigger radius changes
146       last;                                #once is enough
147     }
148     $error ||= $new->replace($old, 'child_objects'=>\@child_objects);
149   } else {
150     $error ||= $new->insert('child_objects'=>\@child_objects);
151     $svcnum = $new->svcnum;
152   }
153 }
154
155 </%init>