Subversion Repositories VORC

Rev

Rev 86 | Rev 93 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 bgadell 1
#!/usr/bin/perl
2
 
3
# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)
4
my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";
5
close STDERR;
6
open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";
7
#warn "Redirecting errors to ${error_log_path}vorc_error.log";
8
 
9
use strict;
10
use cPanelUserConfig;
11
use RollerCon;
12
use CGI qw/param cookie header start_html url/;
13
use Email::Valid;
14
use WebDB;
15
use HTML::Tiny;
16
our $h = HTML::Tiny->new( mode => 'html' );
17
 
18
my ($FORM, $cookie_string, $ERRMSG);
19
my @ERRORS;
20
my $dbh = getRCDBH;
21
#my $dbh = WebDB->connect ();
22
my $depts = getDepartments (); # HashRef of the department TLAs -> Display Names...
23
my $deptDesc = getDepartmentDescriptions ();
24
my $deptLink = getDepartmentLinks ();
25
my $AccessLevel = getAccessLevels;
26
my @tshirtOptions = ("", "MS", "MM", "ML", "MXL", "M2X", "M3X");
86 bgadell 27
my @AUTODEPTS = map { $_->[0] } @{$dbh->selectall_arrayref ("select TLA from department where autoapprove = true")};
56 bgadell 28
 
29
# The page's form might be submitted as a POST or a GET (or both?)
30
#  The initial _view_ likely comes as a GET request (making it easier to embed in an HREF as a URL)
31
#  Unpack any values sent in the GET and add them to the FORM hash
32
$FORM->{'SUB'} = param ('submit') // '';
33
$FORM->{'RCid'} = param ('RCid') // '';
34
$FORM->{referer} = param ("referer") // "";
35
if ($FORM->{'SUB'} eq '') {
88 bgadell 36
  if ($ENV{'REQUEST_URI'}) {
37
    my ($g, $keep) = split /\?/, $ENV{'REQUEST_URI'};
38
    if ($keep) {
39
      foreach (split /&/, $keep) {
40
        my ($k, $v) = split /=/;
41
        $k =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
42
        $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
43
        $k eq "submit" ? $FORM->{'SUB'} = $v : $FORM->{$k} = $v;
44
      }
45
    }
46
  }
56 bgadell 47
}
48
 
49
# Keep track of the original referrer for the 'back' link/button
50
my $goback;
51
if ($FORM->{referer}) {
88 bgadell 52
  $goback = $FORM->{referer};
56 bgadell 53
} else {
88 bgadell 54
  $goback = $ENV{HTTP_REFERER};
56 bgadell 55
}
56
 
57
 
58
if ($FORM->{'SUB'} eq "Save") {
88 bgadell 59
  process_form ($FORM);
56 bgadell 60
} elsif ($FORM->{'SUB'} eq "New User") {
61
  display_form ("New", "New User"); # blank form
62
} elsif ($FORM->{'RCid'}) {
63
  display_form ($FORM->{'RCid'}, $FORM->{'SUB'});
64
} else {
88 bgadell 65
  $cookie_string = authenticate (1);
66
  my ($EM, $PWD, $AL) = split /&/, $cookie_string;
67
  display_form (getUser ($EM)->{'RCid'}, "View");
56 bgadell 68
}
69
 
70
 
71
sub process_form {
72
  my $F = shift // "";
73
  push @ERRORS, "Tried to save an empty form." and return unless $F;
74
 
88 bgadell 75
  $F->{email}       = lc WebDB::trim param ('email')   // '';
76
  $F->{password}    = WebDB::trim param ('password')   // '';
77
  $F->{derby_name}  = WebDB::trim param ('derby_name') // '';
78
  $F->{real_name}   = WebDB::trim param ('real_name')  // '';
79
  $F->{pronouns}    = WebDB::trim param ('pronouns')   // '';
80
  $F->{tshirt}      = WebDB::trim param ('tshirt')     // '';
81
  $F->{phone}       = WebDB::trim param ('phone')      // '';
82
  $F->{timeformat}  = WebDB::trim param ('timeformat') // '24hr';
83
# $F->{level}       = param ('level')      // '';
84
# $F->{type}        = param ('type')       // '';
85
  $F->{RCid}        = param ('RCid')       // '';
86
  $F->{access}      = param ('access')     // 0;
87
# $F->{mvp_pass}    = defined param ('mvp_pass') ? 1 : 0;
88
  $F->{department}  = join ":", map { "$_-".param ("DEPT-".$_) } map { s/^DEPT-//; $_ } grep { param ($_) ne "" } grep { /^DEPT-/ } param ;
56 bgadell 89
 
90
  if ($F->{RCid} eq "New") {
91
  # Saving a new User...
92
    # But first let's do some error checking...0
88 bgadell 93
    if (!$F->{password})   { push @ERRORS, "Blank Password!"; }
94
    if (!$F->{real_name})  { push @ERRORS, "Blank Full Name!"; }
95
    if (!$F->{derby_name}) { $F->{derby_name} = $F->{real_name}; } # If they leave derby_name blank, use their real_name
96
    if (checkDupes ('derby_name', $F->{derby_name})) { push @ERRORS, "Derby Name already in use. Pick a different one."; $F->{derby_name} = ""; }
97
#   if (!$F->{level})      { $F->{level} = "B"; } # People keep leaving level blank.  Default 'em if they do.
98
#   if (!$F->{type})       { $F->{type} = "official"; } # and now they left the other drop-down blank!!!
99
    if (!$F->{email})      { push @ERRORS, "Blank Email (User-ID)!"; } else {
100
      $F->{email} =~ s/\s+//g; # make sure people aren't accidentally including spaces
101
      $F->{email} = lc $F->{email}; # sometimes people capitalize their email addresses and that's annoying...
102
      if (! Email::Valid->address (-address => $F->{email}, -mxcheck => 1, -tldcheck => 1)) { push @ERRORS, "Mal-formatted (or fake) Email Address!"; $F->{email} = ""; }
103
    }
104
    if (checkDupes ('email', $F->{email})) { push @ERRORS, "Email Address already in use. Pick a different one."; $F->{email} = ""; }
56 bgadell 105
    # if (!$F->{department}) { push @ERRORS, "You need to request at least one Department!"; }
106
 
88 bgadell 107
    if (scalar @ERRORS) {
108
      $ERRMSG = join $h->br, @ERRORS;
109
      display_form ("New", "New User", $ERRMSG, $F);
110
      return;
111
    } else {
112
      # We have a correctly formatted email address with a mail host record, go ahead and add the user
113
 
114
      # Check to see if any of the departments they've requested are set to autoapprove.
115
      $F->{department} = convertDepartments $F->{department};
116
      use tableViewer;
117
      map { $F->{department}->{$_} = inArray ($_, \@AUTODEPTS) } keys %{$F->{department}};
118
      $F->{department} = convertDepartments $F->{department};
119
 
120
#     my $sth = $dbh->prepare ("insert into official (email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
121
      my $sth = $dbh->prepare ("insert into official (email, password, derby_name, real_name, pronouns, tshirt, phone, timeformat, access, department, added, activation) values (?, password(?), ?, ?, ?, ?, ?, ?, ?, ?, CONVERT_TZ(now(), 'America/Chicago', 'America/Los_Angeles'), md5(rand()))");
56 bgadell 122
 
88 bgadell 123
#     $sth->execute ($F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{phone}, $F->{level}, $F->{type}, 0, $F->{department}, 0);
124
      $sth->execute ($F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{timeformat}, 0, $F->{department});
56 bgadell 125
 
88 bgadell 126
      $sth = $dbh->prepare ("select RCid, activation from official where email = ?");
127
      $sth->execute ($F->{email});
128
      ($F->{RCid}, $F->{activation}) = $sth->fetchrow_array;
129
      $dbh->do ("replace into RCid_ticket_link select official.RCid, v_ticket.id from official join v_ticket on official.email = v_ticket.email and official.real_name = v_ticket.full_name where official.RCid = ?", undef, $F->{RCid});
130
      logit ($F->{RCid}, "New User Registration");
131
      sendNewUserEMail ("New User", $F);
132
      $cookie_string = authenticate (1);
133
    }
134
  } else {
135
    $cookie_string = authenticate (1);
136
    my ($EM, $PWD, $AL) = split /&/, $cookie_string;
137
 
138
    my $OG = getUser ($F->{RCid});
139
    if ($F->{derby_name} ne $OG->{derby_name} and checkDupes ('derby_name', $F->{derby_name})) { push @ERRORS, "Derby Name already in use. Pick a different one."; $F->{derby_name} = ""; }
140
    if ($F->{email} ne $OG->{email} and checkDupes ('email', $F->{email})) { push @ERRORS, "Email Address already in use. Pick a different one."; $F->{email} = ""; }
141
    if (!$F->{real_name})  { push @ERRORS, "Blank Full Name!"; }
142
    if (scalar @ERRORS) {
143
      $ERRMSG = join $h->br, @ERRORS;
144
      display_form ($F->{RCid}, "Edit", $ERRMSG, $F);
145
      return;
146
    }
147
 
148
    if (lc $EM eq lc $F->{email} and $AL < 5) { # They're editing their own record (and not a sysadmin).
56 bgadell 149
 
88 bgadell 150
      # Don't let users change their own mvp_pass setting...
151
#     $F->{mvp_pass} = getUser($EM)->{mvp_pass};
152
      my $DBDepts = getUser($EM)->{department};
153
      if ($F->{department} ne $DBDepts) {
154
        # They're trying to change one of their own departments.
155
        my $FORMDepts = convertDepartments $F->{department};
156
        $DBDepts =   convertDepartments $DBDepts;
56 bgadell 157
        # the only change to a dept should be a request to be added, some depts are auto-approved.
158
        use tableViewer;
88 bgadell 159
        map { $FORMDepts->{$_} = inArray ($_, \@AUTODEPTS) } keys %{$FORMDepts};
56 bgadell 160
        # or they can retract their request
88 bgadell 161
        map { do { delete $DBDepts->{$_} } if $DBDepts->{$_} == 0 and !defined $FORMDepts->{$_} } keys %{$DBDepts};
162
        # otherwise, keep the same depts as are in the DB (or have been auto-approved...)
163
        map { $FORMDepts->{$_} = max ($DBDepts->{$_}, $FORMDepts->{$_}) } keys %{$DBDepts};
164
        $F->{department} = convertDepartments $FORMDepts;
165
      }
56 bgadell 166
 
167
      if ($F->{password}) { # They've possibly included an updated password.
88 bgadell 168
#       my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
169
#       $sth->execute ($F->{RCid}, $EM, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{phone}, $F->{level}, $F->{type}, $F->{access}, $F->{department}, $F->{clinic_pass})
170
        my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, activation, timeformat, access, department, added, last_login) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
171
        $sth->execute ($F->{RCid}, lc $EM, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, getUser($EM)->{activation}, $F->{timeformat}, $F->{access}, $F->{department}, getUser($EM)->{added}, getUser($EM)->{last_login})
172
          or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
173
      } else { # No password was included, just keep the existing one.
174
#       my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
175
#       $sth->execute($F->{RCid}, $EM, $PWD, $F->{derby_name}, $F->{real_name}, $F->{phone}, $F->{level}, $F->{type}, $F->{access}, $F->{department}, $F->{clinic_pass})
176
        my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, activation, timeformat, access, department, added, last_login) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
177
        $sth->execute($F->{RCid}, lc $EM, $PWD, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, getUser($EM)->{activation}, $F->{timeformat}, $F->{access}, $F->{department}, getUser($EM)->{added}, getUser($EM)->{last_login})
178
          or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
179
      }
56 bgadell 180
 
88 bgadell 181
      if ($ERRMSG) {
182
        logit ($F->{RCid}, "DB ERROR: Updating Self Details: $ERRMSG");
183
      } else {
184
        logit ($F->{RCid}, "Updated User Details");
185
      }
186
    } elsif ($AL > 1) { # A lead or higher is updating someone else's record
187
 
188
#     use List::Util qw/sum/;
189
#     if (sum (values %{ convertDepartments ($F->{department}) }) > 0 and $F->{access} == 0) {
190
#     if ($F->{department} and sum (values %{ convertDepartments ($F->{department}) }) > 0 and $F->{access} == 1) {
191
        # activating a user for the first time...
192
#       $F->{access} = 1;
193
#       sendNewUserEMail ("Activate", $F);
194
#     }
195
 
196
      if ($FORM->{password}) {
197
#       my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
198
#       $sth->execute ($F->{RCid}, $F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{phone}, $F->{level}, $F->{type}, $F->{access}, $F->{department}, $F->{clinic_pass})
199
        my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, activation, timeformat, access, department, added, last_login) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
200
        $sth->execute ($F->{RCid}, $F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, getUser($F->{email})->{activation}, $F->{timeformat}, $F->{access}, $F->{department}, getUser($F->{email})->{added}, getUser($F->{email})->{last_login})
201
          or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
202
      } else {
203
#       my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, phone = ?, level = ?, type = ?, access = ?, department = ?, clinic_pass = ? where RCid = ?");
204
#       $sth->execute ($F->{email}, $F->{derby_name}, $F->{real_name}, $F->{phone}, $F->{level}, $F->{type}, $F->{access}, $F->{department}, $F->{clinic_pass}, $F->{RCid})
205
        my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, pronouns = ?, tshirt = ?, phone = ?, timeformat = ?, access = ?, department = ? where RCid = ?");
206
        $sth->execute ($F->{email}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{timeformat}, $F->{access}, $F->{department}, $F->{RCid})
207
          or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
208
      }
209
      if ($ERRMSG) {
210
        logit ($F->{RCid}, "DB ERROR: Updating Someone Else: $ERRMSG");
211
      } else {
212
        logit ($F->{RCid}, "Updated User Details (by ".getUser($EM)->{derby_name}.")");
213
        logit (getUser($EM)->{RCid}, "Updated User Details: ".$F->{derby_name}." (".$F->{RCid}.")");
214
      }
215
    } else {
216
      $ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
217
      logit ($F->{RCid}, "FAIL: ($EM) doesn't have access to update ($F->{email})'s record");
218
    }
219
  }
220
  $F->{password} = "*******";
221
  $F->{buttons}   = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
222
# if ($F->{mvp_pass}) {
223
#     $F->{mvp_pass}  = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>1, readonly=>[], disabled=>[], checked=>[] }), $h->span ({ class=>"slider round" })]);
224
#   } else {
225
#     $F->{mvp_pass}  = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0, readonly=>[], disabled=>[] }), $h->span ({ class=>"slider round" })]);
226
# }
227
  $F->{department} = convertDepartments ($F->{department});
228
  $dbh->do ("replace into RCid_ticket_link select official.RCid, v_ticket.id from official join v_ticket on official.email = v_ticket.email and official.real_name = v_ticket.full_name where official.RCid = ?", undef, $F->{RCid});
56 bgadell 229
 
88 bgadell 230
  display_form ($F->{RCid}, "View");
56 bgadell 231
}
232
 
233
sub display_form {
234
  my $RCID = shift // "";
235
  my $view = shift; # // "New User";
236
  my $errors = shift // "";
237
  my $F = shift; # // "";
238
 
239
  if ($view eq 'Edit') {
88 bgadell 240
    $cookie_string = authenticate (1);
241
    my ($EM, $PWD, $AL) = split /&/, $cookie_string;
242
    $F = getUser ($RCID);
243
    my $currentuser = getUser ($EM);
244
#   $currentuser->{department} = convertDepartments ($currentuser->{department});
245
 
246
#   if (lc $EM eq lc $F->{email} or $AL > 1) {
247
    if (canView ($currentuser, $F)) {
248
      # Editing your own record OR you're a lead/higher
249
      if (lc $EM eq lc $F->{email} or $currentuser->{access} < $F->{access}) {
250
        # If you're editing your own record, or someone who has higher access than you, make access level read-only
251
        $F->{access}      = $h->input ({ type=>"hidden", name=>"access", value=>$F->{access} }).$AccessLevel->{$F->{access}};
252
      } else {
253
        $F->{access}      = $h->select ({ name=>"access" }, [map { $F->{access} == $_ ? $h->option ({ value=>$_, selected=>[] }, $AccessLevel->{$_}) : $h->option ({ value=>$_ }, $AccessLevel->{$_}) } (-1..$currentuser->{access})]);
254
      }
255
      if ($currentuser->{access} > 2) {  #this would be the place to test for other types of managers that can update the MVP Pass setting
256
#       if ($F->{mvp_pass}) {
257
#           $F->{mvp_pass}  = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>1, checked=>[] }), $h->span ({ class=>"slider round" })]);
258
#         } else {
259
#           $F->{mvp_pass}  = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0 }), $h->span ({ class=>"slider round" })]);
260
#       }
58 bgadell 261
        if ($F->{MVPid}) {
262
          $F->{MVPid} .= "->link to change...<-";
263
        }
88 bgadell 264
      } else {
265
#       if ($F->{mvp_pass}) {
266
#           $F->{mvp_pass}  = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>1, readonly=>[], disabled=>[], checked=>[] }), $h->span ({ class=>"slider round" })]);
267
#         } else {
268
#           $F->{mvp_pass}  = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0, readonly=>[], disabled=>[] }), $h->span ({ class=>"slider round" })]);
269
#       }
270
      }
56 bgadell 271
      if ($AL == 5) {
88 bgadell 272
        $F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email} });
273
      } else {
274
        $F->{email}      = $F->{email}.$h->input ({ type=>"hidden", name=>"email", value=>$F->{email} });
275
      }
276
      if ($currentuser->{RCid} eq $F->{RCid} or $currentuser->{access} > 4) {
277
        $F->{password}   = $h->input ({ type=>"password", name=>"password" });
278
        $F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
279
        $F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
280
        $F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
281
        $F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
282
        $F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
283
        $F->{timeformat} = $h->select ({ name=>"timeformat" }, [map { $F->{timeformat} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } qw(24hr ampm)] );
284
      } else {
285
        $F->{password}   = '*******';
286
      }
287
#     $F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, [qw(AA A B C)])."</SELECT>";
288
#     $F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, [qw(official nso referee)])."</SELECT>";
289
      $F->{RCid}       = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} })."$F->{RCid}&nbsp;";
290
      $F->{buttons}    = join " ", $h->input ({ type=>"submit", name=>"submit", value=>"Save" }), $h->input ({ type=>"reset", value=>"Reset" }), $h->input ({ type=>"submit", name=>"submit", value=>"Cancel" });
291
 
292
      $F->{department} = convertDepartments ($F->{department});
293
      $currentuser->{department} = convertDepartments ($currentuser->{department});
294
      foreach my $k (keys %{$depts}) {
295
        next if $k eq "CMP";
296
        if ($currentuser->{access} > 4) {
297
          # SysAdmin can change anyone's department level
298
          $F->{department}->{$k} = $h->select ({ name=>"DEPT-".$k }, [ $h->option ({ value=>"" }, ""), map { $_ eq $F->{department}->{$k} ? $h->option ({ value=>$_, selected=>[] }, $AccessLevel->{$_}) : $h->option ({ value=>$_ }, $AccessLevel->{$_}) } (0..4) ]);
299
        } elsif ($currentuser->{department}->{$k} > 1 and $currentuser->{department}->{$k} > $F->{department}->{$k}) {
300
          # Department Leads and above can change someone's level within the dept (up to their own level -1)
301
          $F->{department}->{$k} = $h->select ({ name=>"DEPT-".$k }, [ $h->option ({ value=>"" }, ""), map { $_ eq $F->{department}->{$k} ? $h->option ({ value=>$_, selected=>[] }, $AccessLevel->{$_}) : $h->option ({ value=>$_ }, $AccessLevel->{$_}) } (0..$currentuser->{department}->{$k}-1) ]);
302
        } else {
303
          # Or it's your own record, you can still submit a request to be added to the dept.
304
          if (!defined $F->{department}->{$k}) {
86 bgadell 305
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0 }), $h->span ({ class=>"slider round" })]) unless !inArray ($k, \@AUTODEPTS);
56 bgadell 306
          } elsif ($F->{department}->{$k} == 0) {
88 bgadell 307
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
56 bgadell 308
          }
88 bgadell 309
        }
310
      }
311
    } else {
312
      $ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
313
    }
56 bgadell 314
 
315
  } elsif ($view eq 'New User') {
316
    $errors .= $h->br."NOTE: You will not be able to sign-up for things until your account has been reviewed and approved. Watch your email for notification.";
88 bgadell 317
    # Skip authentication
318
    $F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email} });
319
    $F->{password}   = $h->input ({ type=>"password", name=>"password" });
320
    $F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
321
    $F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
322
    $F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
323
    $F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
324
    $F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
325
    $F->{timeformat} = $h->select ({ name=>"timeformat" }, [map { $F->{timeformat} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } qw(24hr ampm)] );
326
#   $F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, ["", qw(AA A B C)])."</SELECT>";
327
#   $F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, ["", qw(official nso referee)])."</SELECT>";
328
    $F->{RCid}         = $h->input ({ type=>"hidden", name=>"RCid", value=>"New" })."TBD&nbsp;";
329
    $F->{access}      = $h->input ({ type=>"hidden", name=>"access", value=>0 })."0";
330
#   $F->{mvp_pass}   = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0, readonly=>[], disabled=>[] }), $h->span ({ class=>"slider round" })]);
56 bgadell 331
 
332
    $F->{department} = convertDepartments ($F->{department});
88 bgadell 333
    foreach (sort keys %{$depts}) {
334
      next if $_ eq "CMP";
335
      next unless inArray($_, \@AUTODEPTS);
336
      if (defined param ("DEPT-$_")) {
337
        $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
338
      } else {
339
        $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0 }), $h->span ({ class=>"slider round" })]);
340
      }
341
    }
342
    $F->{buttons}   = $h->input ({ type=>"submit", name=>"submit", value=>"Save" })." ".$h->input ({ type=>"reset", value=>"Reset" })." ".$h->input ({ type=>"submit", name=>"submit", value=>"Cancel" });
343
    $cookie_string = '';
56 bgadell 344
  } elsif ($view eq 'View' or $view eq 'Cancel' or !$view) {
88 bgadell 345
    $cookie_string = authenticate (1);
346
    my ($EM, $PWD, $AL) = split /&/, $cookie_string;
56 bgadell 347
 
88 bgadell 348
    if (!$view) {
56 bgadell 349
      $F->{'RCid'} = getUser ($EM)->{'RCid'};
88 bgadell 350
    }
56 bgadell 351
 
88 bgadell 352
    # Check to make sure they're only looking up their own ID unless they're a lead or higher
353
    my $currentuser = getUser ($EM);
354
    my  $targetuser = getUser ($RCID);
56 bgadell 355
 
88 bgadell 356
    if (canView ($currentuser, $targetuser)) {
357
      $F = $targetuser;
358
      $F->{department} = convertDepartments ($F->{department});
56 bgadell 359
      $F->{access} = $AccessLevel->{$F->{access}};
88 bgadell 360
      $F->{'password'} = "*******";
361
      $F->{buttons}   = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
71 bgadell 362
 
66 bgadell 363
      if ($currentuser->{access} > 2 or ($currentuser->{department} and convertDepartments($currentuser->{department})->{MVP} >= 2)) {
58 bgadell 364
        if($F->{MVPid}) {
365
          $F->{MVPid} .= '&nbsp;&nbsp;' . $h->button ({ onClick=>"window.open('update_mvp_ticket.pl?change=Delete&RCid=$F->{RCid}&MVPid=$F->{MVPid}','Change MVP Ticket','resizable,height=260,width=370'); return false;" }, "Delete Match");
366
        } else {
367
          $F->{MVPid} .= $h->button ({ onClick=>"window.open('update_mvp_ticket.pl?change=lookup&RCid=$F->{RCid}','Change MVP Ticket','resizable,height=260,width=370'); return false;" }, "Manual Match");
368
          my $possible_matches = $dbh->selectall_arrayref ("select id, full_name from v_ticket where isnull(RCid) = true and email = (select email from official where RCid = ?) union
369
            select id, full_name from v_ticket where isnull(RCid) = true and full_name = (select real_name from official where RCid = ?) union
370
            select id, full_name from v_ticket where isnull(RCid) = true and derby_name = (select derby_name from official where RCid = ?)", undef, $F->{RCid}, $F->{RCid}, $F->{RCid});
371
 
372
          foreach my $match (@$possible_matches) {
373
            my ($MVPid, $fullname) = @$match;
71 bgadell 374
 
58 bgadell 375
            $F->{MVPid} .= $h->div ({ class => "hint" }, ["Possible Match: @$match", '&nbsp;&nbsp;', $h->button ({ onClick=>"window.open('update_mvp_ticket.pl?change=add&RCid=$F->{RCid}&MVPid=$MVPid','Change MVP Ticket','resizable,height=260,width=370'); return false;" }, "Accept Match")]);
376
          }
377
        }
378
      }
88 bgadell 379
    } else {
380
      logit ($currentuser->{RCid}, "SECURITY: $currentuser->{derby_name} attempted to view another user's ($RCID) info");
381
      $errors = "Unauthorized attempt to view another user.  This has been logged.";
382
      $F->{email}      = "&nbsp;";
383
      $F->{password}   = "&nbsp;";
384
      $F->{derby_name} = "&nbsp;";
385
      $F->{real_name}  = "&nbsp;";
386
      $F->{pronouns}   = "&nbsp;";
387
      $F->{tshirt}     = "&nbsp;";
388
      $F->{phone}      = "&nbsp;";
389
      $F->{timeformat} = "&nbsp;";
390
#     $F->{level}      = "&nbsp;";
391
#     $F->{type}       = "&nbsp;";
392
      $F->{RCid}       = "&nbsp;";
393
      $F->{access}     = "&nbsp;";
394
      $F->{MVPid}      = "&nbsp;";
395
      $F->{buttons}    = "&nbsp;";
56 bgadell 396
    }
397
 
88 bgadell 398
#   if (lc $EM eq lc $F->{email} or $AL > 1) {
399
#      $F->{buttons}    = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
400
#   } else {
401
#     $F->{buttons} = "";
402
#   }
56 bgadell 403
  } #else {
88 bgadell 404
  # $cookie_string = authenticate(1);
405
  # $FORM->{email}      = "&nbsp;";
406
  # $FORM->{password}   = "&nbsp;";
407
  # $FORM->{derby_name} = "&nbsp;";
408
  # $FORM->{real_name}  = "&nbsp;";
409
  # $FORM->{phone}      = "&nbsp;";
410
  # $FORM->{level}      = "&nbsp;";
411
  # $FORM->{type}       = "&nbsp;";
412
  # $FORM->{RCid}         = "&nbsp;";
413
  # $FORM->{access}     = "&nbsp;";
414
  # $FORM->{mvp_pass}   = "&nbsp;";
415
  # $FORM->{buttons}    = "&nbsp;";
56 bgadell 416
  #}
417
 
418
  #---------------START THE HTML--------------------
419
 
420
  my $RCAUTH_cookie = cookie (-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
421
 
422
  print header (-cookie=>$RCAUTH_cookie);
423
 
424
  #foreach (keys %ENV) {
88 bgadell 425
  # print "$_: $ENV{$_}\n<br>";
56 bgadell 426
  #}
427
 
428
  if ($errors) {
88 bgadell 429
    $errors = $h->div ({ class=>"error" }, $errors);
56 bgadell 430
  } else {
88 bgadell 431
    $errors = "";
56 bgadell 432
  }
433
 
58 bgadell 434
   my @printDepartments = ( $h->div ({ class=>"index", style=>"display: unset;" }, $h->p ({ class=>"heading" }, "Volunteer Department Access:")) );
56 bgadell 435
#  push @printDepartments, $h->div ({ class=>"rTableRow" }, $h->div ({ class=>"rTableCellr hint", style=>"display:block;" }, "Here is where you're signed up to volunteer at RollerCon:"));
436
  push @printDepartments, $h->div ({ class=>"rTableRowSpan" },[ $h->div ({ style=>"rTableCellr" }, $h->div ({ class=>"hint" }, "Here is where you're signed up to volunteer at RollerCon:")) ]);
437
#  push @printDepartments, $h->div ({ class=>"hint", style=>"display: unset;" }, "Here is where you're signed up to volunteer at RollerCon:");
438
  foreach (sort grep { !/^PER$/ } keys %{$F->{department}}) {
439
    push @printDepartments, $h->div ({ class=>"rTableRow" }, [
440
      $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" },
441
        [ $h->span ({ class=>"tooltip-wrap" }, [$h->img ({src=>"/images/qm.png", width=>"18", height=>"18"}), $h->div ({ class=>"tooltip-content" }, $h->div ({class=>"bold"}, $depts->{$_}).$deptDesc->{$_} . (exists $deptLink->{$_} ? $h->a ({ href=>$deptLink->{$_}, target=>"_new"}, " [More Info]") : "") )]), $depts->{$_}.":" ],
442
        $F->{department}->{$_} =~ /^\d$/ ? $AccessLevel->{$F->{department}->{$_}} : $F->{department}->{$_}),
443
    ]);
444
  }
445
 
446
  printRCHeader ("User Manager");
447
 
448
  print $errors;
449
  print $h->form ({ action=>url, method=>'POST', name=>'Req' },[
450
    $h->input ({ type=>"hidden", name=>"referer", value=>$goback }),
451
    $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "User Details:"),
452
      $h->div ({ class=>"rTable", style=>"min-width: 0%;" },[
453
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User-ID / Email Address: ", $F->{email}) ]),
454
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Password: ",                $F->{password}) ]),
455
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Derby Name: ",              $F->{derby_name}) ]),
58 bgadell 456
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Full Name: ",               $F->{real_name}) ]),
56 bgadell 457
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Pronouns: ",                $F->{pronouns}) ]),
458
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "TShirt Size: ",             $F->{tshirt}) ]),
459
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Phone: ",                   $F->{phone}) ]),
460
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Time Format: ",       $F->{timeformat}) ]),
461
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Database ID: ",             $F->{RCid}) ]),
462
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User Added: ",              $F->{added}) ]),
463
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Last Login: ",              $F->{last_login}) ]),
464
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "vORC Access Level: ",       $F->{access}) ]),
58 bgadell 465
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "MVP Pass: ",                $F->{MVPid}) ]),
56 bgadell 466
        @printDepartments,
467
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCell" }, "&nbsp;") ]),
468
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr" }, $h->a ({ href=>$goback }, "[go back]"), $F->{buttons}) ])
469
      ])
470
    ])
471
  ]); #  print $h->close('form');
472
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Schedule:"), getSchedule ($RCID, "all")]) unless $RCID !~ /^\d+$/;
473
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Recent Activity:"), getLog ($RCID)]) unless $RCID !~ /^\d+$/;
474
  print $h->close ('html');
475
}
476
 
477
#sub selectOptions {
88 bgadell 478
# my $selectedOption = shift;
479
# my $options = shift;
480
# return join " ", map { $selectedOption eq $_ ?
481
#                         $h->option ({ value=>$_, selected=>[] }, $_) :
482
#                         $h->option ({ value=>$_ }, $_)
483
#                     } @$options;
56 bgadell 484
#}
485
 
486
 
487
sub checkDupes {
488
  my $field = shift;
489
  my $nametocheck = shift;
490
  my $han = $dbh->prepare("select RCid from official where $field = ?");
491
  $han->execute($nametocheck);
492
  my ($rcid) = $han->fetchrow();
493
  return $rcid;
494
}
495
 
496
sub getLog {
497
  my $RCID = shift;
498
 
499
  my @activity_log;
500
  my $alog = $dbh->prepare("select timestamp, event from v_log where RCid = ? limit 10");
501
  $alog->execute($RCID);
502
  while (my @logs = $alog->fetchrow_array) {
88 bgadell 503
    push @activity_log, $h->li ({ class=>"shaded" }, join " ", @logs);
56 bgadell 504
  }
505
 
506
  return $h->ul ([@activity_log]).$h->h5 ($h->a ({ href=>"log.pl?filter-RCid=".$RCID }, "[Entire log history]"));
507
}
508
 
509
sub getDepartmentDescriptions {
88 bgadell 510
  my %HASH;
511
  my $sth = $dbh->prepare("select TLA, description from department");
512
  $sth->execute();
513
  while (my ($tla, $name) = $sth->fetchrow) {
514
    $HASH{$tla} = $name;
56 bgadell 515
  }
516
  return \%HASH;
517
}
518
 
519
sub getDepartmentLinks {
88 bgadell 520
  my %HASH;
521
  my $sth = $dbh->prepare("select TLA, link from department where link <> ''");
522
  $sth->execute();
523
  while (my ($tla, $name) = $sth->fetchrow) {
524
    $HASH{$tla} = $name;
56 bgadell 525
  }
526
  return \%HASH;
527
}