fix initial employee add w/password, RT#32456
[freeside.git] / httemplate / edit / process / access_user.html
1 %  if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
2 %    $cgi->param('error', "The passwords do not match");
3 %    print $cgi->redirect(popurl(2) . "access_user.html?" . $cgi->query_string);
4 %  } else {
5 <%   include( 'elements/process.html',
6                  'table'          => 'access_user',
7                  'viewall_dir'    => 'browse',
8                  'copy_on_empty'  => [ '_password', '_password_encoding' ],
9                  'clear_on_error' => [ '_password', '_password2' ],
10                  'process_m2m'    => { 'link_table'   => 'access_usergroup',
11                                        'target_table' => 'access_group',
12                                      },
13                  'precheck_callback'        => \&precheck_callback,
14                  'post_new_object_callback' => \&post_new_object_callback,
15                  'noerror_callback'         => \&noerror_callback,
16              )
17 %>
18 %   }
19 <%init>
20
21 die "access denied"
22   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
23
24 if ( FS::Conf->new->exists('disable_acl_changes') ) {
25   errorpage('ACL changes disabled in public demo.');
26   die "shouldn't be reached";
27 }
28
29 sub precheck_callback {
30   my $cgi = shift;
31
32   my $o = FS::access_user->new({username => scalar($cgi->param('username'))});
33   if( $o->is_system_user and !$cgi->param('usernum') ) {
34     $cgi->param('username','');
35     return "username '".$o->username."' reserved for system account."
36   }
37
38   return '';
39 }
40
41 #sub post_new_object_callback {
42 #  my( $cgi, $access_user ) = @_;
43 #
44 #  if ( length($cgi->param('_password')) ) {
45 #    my $password = scalar($cgi->param('_password'));
46 #    my $error = $access_user->is_password_allowed($password);
47 #    #XXX and then bubble the error back up to the UI
48 #  }
49 #}
50
51 sub noerror_callback {
52   my( $cgi, $access_user ) = @_;
53
54   if ( length($cgi->param('_password')) ) {
55     my $password = scalar($cgi->param('_password'));
56     $access_user->change_password($password);
57   }
58
59   #handle installer checkbox
60   my @sched_item = $access_user->sched_item;
61   my $sched_item = $sched_item[0];
62   if ( $cgi->param('sched_item_enabled') ) {
63
64     if ( ! $sched_item ) {
65       my $sched_item = new FS::sched_item {
66         'usernum' => $access_user->usernum,
67       };
68       my $error = $sched_item->insert;
69       die $error if $error; #wtf?  shouldn't happen
70     } elsif ( $sched_item->disabled ) {
71       $sched_item->disabled('');
72       my $error = $sched_item->replace;
73       die $error if $error; #wtf?  shouldn't happen
74     }
75
76   } elsif ( ! $cgi->param('sched_item_enabled')
77               && $sched_item
78               && ! $sched_item->disabled
79           )
80   {
81     $sched_item->disabled('Y');
82     my $error = $sched_item->replace;
83     die $error if $error; #wtf?  shouldn't happen
84   }
85
86 }
87
88 </%init>