Subversion Repositories VORC

Rev

Rev 71 | Rev 88 | 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 '') {
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
	}
47
}
48
 
49
# Keep track of the original referrer for the 'back' link/button
50
my $goback;
51
if ($FORM->{referer}) {
52
	$goback = $FORM->{referer};
53
} else {
54
	$goback = $ENV{HTTP_REFERER};
55
}
56
 
57
 
58
if ($FORM->{'SUB'} eq "Save") {
59
	process_form ($FORM);
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 {
65
 	$cookie_string = authenticate (1);
66
 	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
67
 	display_form (getUser ($EM)->{'RCid'}, "View");
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
 
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;
58 bgadell 87
#	$F->{mvp_pass}    = defined param ('mvp_pass') ? 1 : 0;
56 bgadell 88
	$F->{department}  = join ":", map { "$_-".param ("DEPT-".$_) } map { s/^DEPT-//; $_ } grep { param ($_) ne "" } grep { /^DEPT-/ } param ;
89
 
90
  if ($F->{RCid} eq "New") {
91
  # Saving a new User...
92
    # But first let's do some error checking...0
93
		if (!$F->{password})   { push @ERRORS, "Blank Password!"; }
58 bgadell 94
		if (!$F->{real_name})  { push @ERRORS, "Blank Full Name!"; }
56 bgadell 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} = ""; }
105
    # if (!$F->{department}) { push @ERRORS, "You need to request at least one Department!"; }
106
 
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()))");
122
 
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});
125
 
126
			$sth = $dbh->prepare ("select RCid, activation from official where email = ?");
127
			$sth->execute ($F->{email});
128
			($F->{RCid}, $F->{activation}) = $sth->fetchrow_array;
65 bgadell 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});
56 bgadell 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
		if (lc $EM eq lc $F->{email} and $AL < 5) { # They're editing their own record (and not a sysadmin).
138
 
139
			# Don't let users change their own mvp_pass setting...
58 bgadell 140
#		  $F->{mvp_pass} = getUser($EM)->{mvp_pass};
56 bgadell 141
			my $DBDepts = getUser($EM)->{department};
142
		  if ($F->{department} ne $DBDepts) {
143
		  	# They're trying to change one of their own departments.
144
		  	my $FORMDepts = convertDepartments $F->{department};
145
		  	$DBDepts =   convertDepartments $DBDepts;
146
        # the only change to a dept should be a request to be added, some depts are auto-approved.
147
        use tableViewer;
148
		  	map { $FORMDepts->{$_} = inArray ($_, \@AUTODEPTS) } keys %{$FORMDepts};
149
        # or they can retract their request
150
				map { do { delete $DBDepts->{$_} } if $DBDepts->{$_} == 0 and !defined $FORMDepts->{$_} } keys %{$DBDepts};
151
				# otherwise, keep the same depts as are in the DB (or have been auto-approved...)
152
		  	map { $FORMDepts->{$_} = max ($DBDepts->{$_}, $FORMDepts->{$_}) } keys %{$DBDepts};
153
		  	$F->{department} = convertDepartments $FORMDepts;
154
		  }
155
 
156
      if ($F->{password}) { # They've possibly included an updated password.
157
#    		my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
158
#    		$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})
62 bgadell 159
    		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(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
58 bgadell 160
    		$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})
56 bgadell 161
    			or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
162
    	} else { # No password was included, just keep the existing one.
163
#    		my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
164
#    		$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})
62 bgadell 165
    		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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
58 bgadell 166
    		$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})
56 bgadell 167
    			or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
168
    	}
169
 
170
			if ($ERRMSG) {
171
				logit ($F->{RCid}, "DB ERROR: Updating Self Details: $ERRMSG");
172
			} else {
173
				logit ($F->{RCid}, "Updated User Details");
174
			}
175
		} elsif ($AL > 1) { # A lead or higher is updating someone else's record
176
 
65 bgadell 177
#		  use List::Util qw/sum/;
56 bgadell 178
#		  if (sum (values %{ convertDepartments ($F->{department}) }) > 0 and $F->{access} == 0) {
65 bgadell 179
#		  if ($F->{department} and sum (values %{ convertDepartments ($F->{department}) }) > 0 and $F->{access} == 1) {
56 bgadell 180
		    # activating a user for the first time...
65 bgadell 181
#		    $F->{access} = 1;
56 bgadell 182
#		    sendNewUserEMail ("Activate", $F);
65 bgadell 183
#		  }
56 bgadell 184
 
185
			if ($FORM->{password}) {
186
#				my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
187
#				$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})
58 bgadell 188
				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(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
189
				$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})
56 bgadell 190
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
191
			} else {
192
#				my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, phone = ?, level = ?, type = ?, access = ?, department = ?, clinic_pass = ? where RCid = ?");
193
#				$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})
58 bgadell 194
				my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, pronouns = ?, tshirt = ?, phone = ?, timeformat = ?, access = ?, department = ? where RCid = ?");
195
				$sth->execute ($F->{email}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{timeformat}, $F->{access}, $F->{department}, $F->{RCid})
56 bgadell 196
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
197
			}
198
			if ($ERRMSG) {
199
				logit ($F->{RCid}, "DB ERROR: Updating Someone Else: $ERRMSG");
200
			} else {
201
				logit ($F->{RCid}, "Updated User Details (by ".getUser($EM)->{derby_name}.")");
202
				logit (getUser($EM)->{RCid}, "Updated User Details: ".$F->{derby_name}." (".$F->{RCid}.")");
203
			}
204
		} else {
205
			$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
206
			logit ($F->{RCid}, "FAIL: ($EM) doesn't have access to update ($F->{email})'s record");
207
		}
208
	}
209
	$F->{password} = "*******";
210
	$F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
58 bgadell 211
#	if ($F->{mvp_pass}) {
212
# 		$F->{mvp_pass}	= $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>1, readonly=>[], disabled=>[], checked=>[] }), $h->span ({ class=>"slider round" })]);
213
# 	} else {
214
# 		$F->{mvp_pass}	= $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0, readonly=>[], disabled=>[] }), $h->span ({ class=>"slider round" })]);
215
#	}
56 bgadell 216
	$F->{department} = convertDepartments ($F->{department});
58 bgadell 217
	$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 218
 
219
	display_form ($F->{RCid}, "View");
220
}
221
 
222
sub display_form {
223
  my $RCID = shift // "";
224
  my $view = shift; # // "New User";
225
  my $errors = shift // "";
226
  my $F = shift; # // "";
227
 
228
  if ($view eq 'Edit') {
229
  	$cookie_string = authenticate (1);
230
  	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
231
  	$F = getUser ($RCID);
232
  	my $currentuser = getUser ($EM);
233
#  	$currentuser->{department} = convertDepartments ($currentuser->{department});
234
 
235
#  	if (lc $EM eq lc $F->{email} or $AL > 1) {
236
  	if (canView ($currentuser, $F)) {
237
  	  # Editing your own record OR you're a lead/higher
238
  		if (lc $EM eq lc $F->{email} or $currentuser->{access} < $F->{access}) {
239
  		  # If you're editing your own record, or someone who has higher access than you, make access level read-only
240
  			$F->{access}			= $h->input ({ type=>"hidden", name=>"access", value=>$F->{access} }).$AccessLevel->{$F->{access}};
241
  		} else {
242
  			$F->{access}			= $h->select ({ name=>"access" }, [map { $F->{access} == $_ ? $h->option ({ value=>$_, selected=>[] }, $AccessLevel->{$_}) : $h->option ({ value=>$_ }, $AccessLevel->{$_}) } (-1..$currentuser->{access})]);
243
  		}
244
  		if ($currentuser->{access} > 2) {  #this would be the place to test for other types of managers that can update the MVP Pass setting
58 bgadell 245
#				if ($F->{mvp_pass}) {
246
# 					$F->{mvp_pass}	= $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>1, checked=>[] }), $h->span ({ class=>"slider round" })]);
247
# 				} else {
248
# 					$F->{mvp_pass}	= $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0 }), $h->span ({ class=>"slider round" })]);
249
#				}
250
        if ($F->{MVPid}) {
251
          $F->{MVPid} .= "->link to change...<-";
252
        }
56 bgadell 253
  		} else {
58 bgadell 254
#				if ($F->{mvp_pass}) {
255
# 					$F->{mvp_pass}	= $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>1, readonly=>[], disabled=>[], checked=>[] }), $h->span ({ class=>"slider round" })]);
256
# 				} else {
257
# 					$F->{mvp_pass}	= $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"mvp_pass", value=>0, readonly=>[], disabled=>[] }), $h->span ({ class=>"slider round" })]);
258
#				}
56 bgadell 259
  		}
260
      if ($AL == 5) {
261
    	  $F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email} });
262
    	} else {
263
  		  $F->{email}      = $F->{email}.$h->input ({ type=>"hidden", name=>"email", value=>$F->{email} });
264
  		}
265
  		if ($currentuser->{RCid} eq $F->{RCid} or $currentuser->{access} > 4) {
266
  			$F->{password}   = $h->input ({ type=>"password", name=>"password" });
267
  			$F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
268
  			$F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
269
  			$F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
270
  			$F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
271
  			$F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
272
     	  $F->{timeformat} = $h->select ({ name=>"timeformat" }, [map { $F->{timeformat} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } qw(24hr ampm)] );
273
  		} else {
274
  			$F->{password}   = '*******';
275
  		}
276
#  		$F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, [qw(AA A B C)])."</SELECT>";
277
#  		$F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, [qw(official nso referee)])."</SELECT>";
278
  		$F->{RCid}       = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} })."$F->{RCid}&nbsp;";
279
  		$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" });
280
 
281
    	$F->{department} = convertDepartments ($F->{department});
282
    	$currentuser->{department} = convertDepartments ($currentuser->{department});
283
    	foreach my $k (keys %{$depts}) {
284
    	  next if $k eq "CMP";
285
    	  if ($currentuser->{access} > 4) {
286
    	    # SysAdmin can change anyone's department level
287
    	    $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) ]);
288
    	  } elsif ($currentuser->{department}->{$k} > 1 and $currentuser->{department}->{$k} > $F->{department}->{$k}) {
289
    	    # Department Leads and above can change someone's level within the dept (up to their own level -1)
290
    	    $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) ]);
291
    	  } else {
292
    	    # Or it's your own record, you can still submit a request to be added to the dept.
293
    	    if (!defined $F->{department}->{$k}) {
86 bgadell 294
            $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 295
          } elsif ($F->{department}->{$k} == 0) {
296
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
297
          }
298
    	  }
299
    	}
300
  	} else {
301
  		$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
302
  	}
303
 
304
  } elsif ($view eq 'New User') {
305
    $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.";
306
  	# Skip authentication
307
 		$F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email} });
308
 		$F->{password}   = $h->input ({ type=>"password", name=>"password" });
309
 		$F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
310
 		$F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
311
		$F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
312
		$F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
313
 		$F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
314
 	  $F->{timeformat} = $h->select ({ name=>"timeformat" }, [map { $F->{timeformat} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } qw(24hr ampm)] );
315
#  	$F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, ["", qw(AA A B C)])."</SELECT>";
316
#  	$F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, ["", qw(official nso referee)])."</SELECT>";
317
 		$F->{RCid}         = $h->input ({ type=>"hidden", name=>"RCid", value=>"New" })."TBD&nbsp;";
318
  	$F->{access}			= $h->input ({ type=>"hidden", name=>"access", value=>0 })."0";
58 bgadell 319
#  	$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 320
 
321
    $F->{department} = convertDepartments ($F->{department});
322
  	foreach (sort keys %{$depts}) {
323
  	  next if $_ eq "CMP";
86 bgadell 324
  	  next unless inArray($_, \@AUTODEPTS);
56 bgadell 325
  	  if (defined param ("DEPT-$_")) {
326
  	    $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
327
  	  } else {
328
  	    $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0 }), $h->span ({ class=>"slider round" })]);
329
  	  }
330
  	}
331
  	$F->{buttons}		= $h->input ({ type=>"submit", name=>"submit", value=>"Save" })." ".$h->input ({ type=>"reset", value=>"Reset" })." ".$h->input ({ type=>"submit", name=>"submit", value=>"Cancel" });
332
  	$cookie_string = '';
333
  } elsif ($view eq 'View' or $view eq 'Cancel' or !$view) {
334
  	$cookie_string = authenticate (1);
335
  	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
336
 
337
  	if (!$view) {
338
      $F->{'RCid'} = getUser ($EM)->{'RCid'};
339
  	}
340
 
341
  	# Check to make sure they're only looking up their own ID unless they're a lead or higher
342
  	my $currentuser = getUser ($EM);
343
    my	$targetuser = getUser ($RCID);
344
 
345
  	if (canView ($currentuser, $targetuser)) {
346
    	$F = $targetuser;
347
    	$F->{department} = convertDepartments ($F->{department});
348
      $F->{access} = $AccessLevel->{$F->{access}};
349
    	$F->{'password'} = "*******";
350
      $F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
71 bgadell 351
 
66 bgadell 352
      if ($currentuser->{access} > 2 or ($currentuser->{department} and convertDepartments($currentuser->{department})->{MVP} >= 2)) {
58 bgadell 353
        if($F->{MVPid}) {
354
          $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");
355
        } else {
356
          $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");
357
          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
358
            select id, full_name from v_ticket where isnull(RCid) = true and full_name = (select real_name from official where RCid = ?) union
359
            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});
360
 
361
          foreach my $match (@$possible_matches) {
362
            my ($MVPid, $fullname) = @$match;
71 bgadell 363
 
58 bgadell 364
            $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")]);
365
          }
366
        }
367
      }
56 bgadell 368
  	} else {
369
  	  logit ($currentuser->{RCid}, "SECURITY: $currentuser->{derby_name} attempted to view another user's ($RCID) info");
370
  	  $errors = "Unauthorized attempt to view another user.  This has been logged.";
371
    	$F->{email}      = "&nbsp;";
372
    	$F->{password}   = "&nbsp;";
373
    	$F->{derby_name} = "&nbsp;";
374
    	$F->{real_name}  = "&nbsp;";
375
    	$F->{pronouns}   = "&nbsp;";
376
    	$F->{tshirt}     = "&nbsp;";
377
    	$F->{phone}      = "&nbsp;";
378
    	$F->{timeformat} = "&nbsp;";
379
#    	$F->{level}      = "&nbsp;";
380
#    	$F->{type}       = "&nbsp;";
381
    	$F->{RCid}       = "&nbsp;";
382
    	$F->{access}		 = "&nbsp;";
58 bgadell 383
    	$F->{MVPid}      = "&nbsp;";
56 bgadell 384
    	$F->{buttons}		 = "&nbsp;";
385
    }
386
 
387
#  	if (lc $EM eq lc $F->{email} or $AL > 1) {
388
#      $F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
389
#  	} else {
390
#  		$F->{buttons} = "";
391
#  	}
392
  } #else {
393
  #	$cookie_string = authenticate(1);
394
  #	$FORM->{email}      = "&nbsp;";
395
  #	$FORM->{password}   = "&nbsp;";
396
  #	$FORM->{derby_name} = "&nbsp;";
397
  #	$FORM->{real_name}  = "&nbsp;";
398
  #	$FORM->{phone}      = "&nbsp;";
399
  #	$FORM->{level}      = "&nbsp;";
400
  #	$FORM->{type}       = "&nbsp;";
401
  #	$FORM->{RCid}         = "&nbsp;";
402
  #	$FORM->{access}			= "&nbsp;";
403
  #	$FORM->{mvp_pass} 	= "&nbsp;";
404
  #	$FORM->{buttons}		= "&nbsp;";
405
  #}
406
 
407
  #---------------START THE HTML--------------------
408
 
409
  my $RCAUTH_cookie = cookie (-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
410
 
411
  print header (-cookie=>$RCAUTH_cookie);
412
 
413
  #foreach (keys %ENV) {
414
  #	print "$_: $ENV{$_}\n<br>";
415
  #}
416
 
417
  if ($errors) {
418
  	$errors = $h->div ({ class=>"error" }, $errors);
419
  } else {
420
  	$errors = "";
421
  }
422
 
58 bgadell 423
   my @printDepartments = ( $h->div ({ class=>"index", style=>"display: unset;" }, $h->p ({ class=>"heading" }, "Volunteer Department Access:")) );
56 bgadell 424
#  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:"));
425
  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:")) ]);
426
#  push @printDepartments, $h->div ({ class=>"hint", style=>"display: unset;" }, "Here is where you're signed up to volunteer at RollerCon:");
427
  foreach (sort grep { !/^PER$/ } keys %{$F->{department}}) {
428
    push @printDepartments, $h->div ({ class=>"rTableRow" }, [
429
      $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" },
430
        [ $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->{$_}.":" ],
431
        $F->{department}->{$_} =~ /^\d$/ ? $AccessLevel->{$F->{department}->{$_}} : $F->{department}->{$_}),
432
    ]);
433
  }
434
 
435
  printRCHeader ("User Manager");
436
 
437
  print $errors;
438
  print $h->form ({ action=>url, method=>'POST', name=>'Req' },[
439
    $h->input ({ type=>"hidden", name=>"referer", value=>$goback }),
440
    $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "User Details:"),
441
      $h->div ({ class=>"rTable", style=>"min-width: 0%;" },[
442
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User-ID / Email Address: ", $F->{email}) ]),
443
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Password: ",                $F->{password}) ]),
444
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Derby Name: ",              $F->{derby_name}) ]),
58 bgadell 445
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Full Name: ",               $F->{real_name}) ]),
56 bgadell 446
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Pronouns: ",                $F->{pronouns}) ]),
447
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "TShirt Size: ",             $F->{tshirt}) ]),
448
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Phone: ",                   $F->{phone}) ]),
449
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Time Format: ",       $F->{timeformat}) ]),
450
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Database ID: ",             $F->{RCid}) ]),
451
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User Added: ",              $F->{added}) ]),
452
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Last Login: ",              $F->{last_login}) ]),
453
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "vORC Access Level: ",       $F->{access}) ]),
58 bgadell 454
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "MVP Pass: ",                $F->{MVPid}) ]),
56 bgadell 455
        @printDepartments,
456
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCell" }, "&nbsp;") ]),
457
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr" }, $h->a ({ href=>$goback }, "[go back]"), $F->{buttons}) ])
458
      ])
459
    ])
460
  ]); #  print $h->close('form');
461
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Schedule:"), getSchedule ($RCID, "all")]) unless $RCID !~ /^\d+$/;
462
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Recent Activity:"), getLog ($RCID)]) unless $RCID !~ /^\d+$/;
463
  print $h->close ('html');
464
}
465
 
466
#sub selectOptions {
467
#	my $selectedOption = shift;
468
#	my $options = shift;
469
#	return join " ", map { $selectedOption eq $_ ?
470
#	                        $h->option ({ value=>$_, selected=>[] }, $_) :
471
#													$h->option ({ value=>$_ }, $_)
472
#						 					} @$options;
473
#}
474
 
475
 
476
sub checkDupes {
477
  my $field = shift;
478
  my $nametocheck = shift;
479
  my $han = $dbh->prepare("select RCid from official where $field = ?");
480
  $han->execute($nametocheck);
481
  my ($rcid) = $han->fetchrow();
482
  return $rcid;
483
}
484
 
485
sub getLog {
486
  my $RCID = shift;
487
 
488
  my @activity_log;
489
  my $alog = $dbh->prepare("select timestamp, event from v_log where RCid = ? limit 10");
490
  $alog->execute($RCID);
491
  while (my @logs = $alog->fetchrow_array) {
492
  	push @activity_log, $h->li ({ class=>"shaded" }, join " ", @logs);
493
  }
494
 
495
  return $h->ul ([@activity_log]).$h->h5 ($h->a ({ href=>"log.pl?filter-RCid=".$RCID }, "[Entire log history]"));
496
}
497
 
498
sub getDepartmentDescriptions {
499
 	my %HASH;
500
 	my $sth = $dbh->prepare("select TLA, description from department");
501
 	$sth->execute();
502
 	while (my ($tla, $name) = $sth->fetchrow) {
503
 	  $HASH{$tla} = $name;
504
  }
505
  return \%HASH;
506
}
507
 
508
sub getDepartmentLinks {
509
 	my %HASH;
510
 	my $sth = $dbh->prepare("select TLA, link from department where link <> ''");
511
 	$sth->execute();
512
 	while (my ($tla, $name) = $sth->fetchrow) {
513
 	  $HASH{$tla} = $name;
514
  }
515
  return \%HASH;
516
}