Subversion Repositories VORC

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 - 1
#!/usr/bin/perl
2
 
3
use strict;
8 - 4
use cPanelUserConfig;
7 - 5
use RollerCon;
6
use CGI qw/param cookie header start_html url/;
7
use Email::Valid;
8
use WebDB;
9
use HTML::Tiny;
10
our $h = HTML::Tiny->new( mode => 'html' );
11
 
12
my ($FORM, $cookie_string, $ERRMSG);
13
my @ERRORS;
14
my $dbh = WebDB->connect ();
15
my $depts = getDepartments (); # HashRef of the department TLAs -> Display Names...
16
my $AccessLevel = getAccessLevels;
17
my @tshirtOptions = ("", "MS", "MM", "ML", "MXL", "M2X", "M3X");
18
 
19
# The page's form might be submitted as a POST or a GET (or both?)
20
#  The initial _view_ likely comes as a GET request (making it easier to embed in an HREF as a URL)
21
#  Unpack any values sent in the GET and add them to the FORM hash
22
$FORM->{'SUB'} = param ('submit') // '';
23
$FORM->{'RCid'} = param ('RCid') // '';
24
$FORM->{referer} = param ("referer") // "";
25
if ($FORM->{'SUB'} eq '') {
26
	if ($ENV{'REQUEST_URI'}) {
27
		my ($g, $keep) = split /\?/, $ENV{'REQUEST_URI'};
28
		if ($keep) {
29
			foreach (split /&/, $keep) {
30
				my ($k, $v) = split /=/;
31
				$k =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
32
				$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
33
				$k eq "submit" ? $FORM->{'SUB'} = $v : $FORM->{$k} = $v;
34
			}
35
		}
36
	}
37
}
38
 
39
# Keep track of the original referrer for the 'back' link/button
40
my $goback;
41
if ($FORM->{referer}) {
42
	$goback = $FORM->{referer};
43
} else {
44
	$goback = $ENV{HTTP_REFERER};
45
}
46
 
47
 
48
if ($FORM->{'SUB'} eq "Save") {
49
	process_form ($FORM);
50
} elsif ($FORM->{'SUB'} eq "New User") {
51
  display_form ("New", "New User"); # blank form
52
} elsif ($FORM->{'RCid'}) {
53
  display_form ($FORM->{'RCid'}, $FORM->{'SUB'});
54
} else {
55
 	$cookie_string = authenticate (1);
56
 	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
57
 	display_form (getUser ($EM)->{'RCid'}, "View");
58
}
59
 
60
 
61
sub process_form {
62
  my $F = shift // "";
63
  push @ERRORS, "Tried to save an empty form." and return unless $F;
64
 
65
	$F->{email}       = WebDB::trim param ('email')      // '';
66
	$F->{password}    = WebDB::trim param ('password')   // '';
67
	$F->{derby_name}  = WebDB::trim param ('derby_name') // '';
68
	$F->{real_name}   = WebDB::trim param ('real_name')  // '';
69
	$F->{pronouns}    = WebDB::trim param ('pronouns')   // '';
70
	$F->{tshirt}      = WebDB::trim param ('tshirt')   // '';
71
	$F->{phone}       = WebDB::trim param ('phone')      // '';
72
#	$F->{level}       = param ('level')      // '';
73
#	$F->{type}        = param ('type')       // '';
74
	$F->{RCid}        = param ('RCid')       // '';
75
	$F->{access}      = param ('access')     // 0;
76
#	$F->{clinic_pass} = defined param ('clinic_pass') ? 1 : 0;
8 - 77
	$F->{department}  = join ":", map { "$_-".param ("DEPT-".$_) } map { s/^DEPT-//; $_ } grep { param ($_) ne "" } grep { /^DEPT-/ } param ;
7 - 78
 
79
  if ($F->{RCid} eq "New") {
80
  # Saving a new User...
81
    # But first let's do some error checking...0
82
		if (!$F->{password})   { push @ERRORS, "Blank Password!"; }
83
		if (!$F->{real_name})  { push @ERRORS, "Blank Real Name!"; }
84
		if (!$F->{derby_name}) { $F->{derby_name} = $F->{real_name}; } # If they leave derby_name blank, use their real_name
85
		if (checkDupes ('derby_name', $F->{derby_name})) { push @ERRORS, "Derby Name already in use. Pick a different one."; $F->{derby_name} = ""; }
86
#		if (!$F->{level})      { $F->{level} = "B"; } # People keep leaving level blank.  Default 'em if they do.
87
#		if (!$F->{type})       { $F->{type} = "official"; } # and now they left the other drop-down blank!!!
88
		if (!$F->{email})      { push @ERRORS, "Blank Email (User-ID)!"; } else {
89
			$F->{email} =~ s/\s+//g; # make sure people aren't accidentally including spaces
90
			$F->{email} = lc $F->{email}; # sometimes people capitalize their email addresses and that's annoying...
91
			if (! Email::Valid->address (-address => $F->{email}, -mxcheck => 1, -tldcheck => 1)) { push @ERRORS, "Mal-formatted (or fake) Email Address!"; $F->{email} = ""; }
92
		}
93
		if (checkDupes ('email', $F->{email})) { push @ERRORS, "Email Address already in use. Pick a different one."; $F->{email} = ""; }
8 - 94
    if (!$F->{department}) { push @ERRORS, "You need to request at least one Department!"; }
95
 
7 - 96
		if (scalar @ERRORS) {
97
			$ERRMSG = join $h->br, @ERRORS;
98
			display_form ("New", "New User", $ERRMSG);
99
			return;
100
		} else {
101
			# We have a correctly formatted email address with a mail host record, go ahead and add the user
102
#			my $sth = $dbh->prepare ("insert into official (email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
103
			my $sth = $dbh->prepare ("insert into official (email, password, derby_name, real_name, pronouns, tshirt, phone, access, department, added) values (?, password(?), ?, ?, ?, ?, ?, ?, ?, now())");
104
 
105
#			$sth->execute ($F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{phone}, $F->{level}, $F->{type}, 0, $F->{department}, 0);
106
			$sth->execute ($F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, 0, $F->{department});
107
 
108
			$sth = $dbh->prepare ("select RCid from official where email = ?");
109
			$sth->execute ($F->{email});
110
			($F->{RCid}) = $sth->fetchrow_array;
111
			logit ($F->{RCid}, "New User Registration");
112
			sendEMail ("New User", $F);
113
			$cookie_string = authenticate (1);
114
		}
115
	} else {
116
		$cookie_string = authenticate (1);
117
		my ($EM, $PWD, $AL) = split /&/, $cookie_string;
118
		if (lc $EM eq lc $F->{email} and $AL < 5) { # They're editing their own record (and not a sysadmin).
119
 
120
			# Don't let users change their own clinic_pass setting...
121
#		  $F->{clinic_pass} = getUser($EM)->{clinic_pass};
122
			my $DBDepts = getUser($EM)->{department};
123
		  if ($F->{department} ne $DBDepts) {
124
		  	# They're trying to change one of their own departments.
125
		  	my $FORMDepts = convertDepartments $F->{department};
126
		  	$DBDepts =   convertDepartments $DBDepts;
127
		  	map { $FORMDepts->{$_} = 0 } keys %{$FORMDepts};  # the only change to a dept should be a request to be added
128
				map { do { delete $DBDepts->{$_} } if $DBDepts->{$_} == 0 and !defined $FORMDepts->{$_} } keys %{$DBDepts};  # or they can retract their request
129
		  	map { $FORMDepts->{$_} = $DBDepts->{$_} } keys %{$DBDepts};  # otherwise, keep the same depts as are in the DB
130
		  	$F->{department} = convertDepartments $FORMDepts;
131
		  }
132
 
133
      if ($F->{password}) { # They've possibly included an updated password.
134
#    		my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
135
#    		$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})
136
    		my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, access, department, added, last_login) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)");
137
    		$sth->execute ($F->{RCid}, $EM, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{access}, $F->{department}, getUser($EM)->{added}, getUser($EM)->{last_login})
138
    			or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
139
    	} else { # No password was included, just keep the existing one.
140
#    		my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
141
#    		$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})
142
    		my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, access, department, added, last_login) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
143
    		$sth->execute($F->{RCid}, $EM, $PWD, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{access}, $F->{department}, getUser($EM)->{added}, getUser($EM)->{last_login})
144
    			or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
145
    	}
146
 
147
			if ($ERRMSG) {
148
				logit ($F->{RCid}, "DB ERROR: Updating Self Details: $ERRMSG");
149
			} else {
150
				logit ($F->{RCid}, "Updated User Details");
151
			}
152
		} elsif ($AL > 1) { # A lead or higher is updating someone else's record
153
			if ($FORM->{password}) {
154
#				my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
155
#				$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})
156
				my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, access, department, added, last_login) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)");
157
				$sth->execute ($F->{RCid}, $F->{email}, $F->{password}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{access}, $F->{department}, getUser($F->{email})->{added}, getUser($F->{email})->{last_login})
158
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
159
			} else {
160
#				my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, phone = ?, level = ?, type = ?, access = ?, department = ?, clinic_pass = ? where RCid = ?");
161
#				$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})
162
				my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, pronouns = ?, tshirt = ?, phone = ?, access = ?, department = ? where RCid = ?");
163
				$sth->execute ($F->{email}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{access}, $F->{department}, $F->{RCid})
164
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
165
			}
166
			if ($ERRMSG) {
167
				logit ($F->{RCid}, "DB ERROR: Updating Someone Else: $ERRMSG");
168
			} else {
169
				logit ($F->{RCid}, "Updated User Details (by ".getUser($EM)->{derby_name}.")");
170
				logit (getUser($EM)->{RCid}, "Updated User Details: ".$F->{derby_name}." (".$F->{RCid}.")");
171
			}
172
		} else {
173
			$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
174
			logit ($F->{RCid}, "FAIL: $EM doesn't have access to update $FORM->{email}'s record");
175
		}
176
	}
177
	$F->{password} = "*******";
178
	$F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
179
#	my $checked = $F->{clinic_pass} ? "checked" : "";
180
#	$F->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' $checked disabled readonly>";
181
	$F->{department} = convertDepartments ($F->{department});
182
 
183
	display_form ($F->{RCid}, "View");
184
}
185
 
186
sub display_form {
187
  my $RCID = shift // "";
188
  my $view = shift; # // "New User";
189
  my $errors = shift // "";
190
  my $F;
191
 
192
  if ($view eq 'Edit') {
193
  	$cookie_string = authenticate (1);
194
  	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
195
  	$F = getUser ($RCID);
196
  	my $currentuser = getUser ($EM);
197
#  	$currentuser->{department} = convertDepartments ($currentuser->{department});
198
 
199
#  	if (lc $EM eq lc $F->{email} or $AL > 1) {
200
  	if (canView ($currentuser, $F)) {
201
  	  # Editing your own record OR you're a lead/higher
202
  		if (lc $EM eq lc $F->{email} or $currentuser->{access} < $F->{access}) {
203
  		  # If you're editing your own record, or someone who has higher access than you, make access level read-only
204
  			$F->{access}			= $h->input ({ type=>"hidden", name=>"access", value=>$F->{access} }).$AccessLevel->{$F->{access}};
205
  		} else {
206
  			$F->{access}			= $h->select ({ name=>"access" }, [map { $F->{access} == $_ ? $h->option ({ value=>$_, selected=>[] }, $AccessLevel->{$_}) : $h->option ({ value=>$_ }, $AccessLevel->{$_}) } (-1..$currentuser->{access})]);
207
  		}
208
#  		my $checked = $F->{clinic_pass} ? "checked" : "";
209
#  		if ($currentuser->{access} > 2) {
210
#  			$F->{clinic_pass} = "<INPUT type='checkbox' name='clinic_pass' value=$F->{clinic_pass} $checked>";
211
#  		} else {
212
#  			$F->{clinic_pass} = "<INPUT type='checkbox' name='clinic_pass' $checked disabled readonly>";
213
#  		}
214
#  		$F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email}, readonly=>[] });
215
  		$F->{email}      = $F->{email}.$h->input ({ type=>"hidden", name=>"email", value=>$F->{email} });
216
  		if ($currentuser->{RCid} eq $F->{RCid} or $currentuser->{access} > 4) {
217
  			$F->{password}   = $h->input ({ type=>"password", name=>"password" });
218
  			$F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
219
  			$F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
220
  			$F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
221
  			$F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
222
  			$F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
223
  		} else {
224
  			$F->{password}   = '*******';
225
  		}
226
#  		$F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, [qw(AA A B C)])."</SELECT>";
227
#  		$F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, [qw(official nso referee)])."</SELECT>";
228
  		$F->{RCid}       = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} })."$F->{RCid}&nbsp;";
229
  		$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" });
230
 
231
    	$F->{department} = convertDepartments ($F->{department});
232
    	$currentuser->{department} = convertDepartments ($currentuser->{department});
233
    	foreach my $k (keys %{$depts}) {
234
    	  if ($currentuser->{access} > 4) {
235
    	    # SysAdmin can change anyone's department level
236
    	    $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) ]);
237
    	  } elsif ($currentuser->{department}->{$k} > 1 and $currentuser->{department}->{$k} > $F->{department}->{$k}) {
238
    	    # Department Leads and above can change someone's level within the dept (up to their own level -1)
239
    	    $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) ]);
240
    	  } else {
241
    	    # Or it's your own record, you can still submit a request to be added to the dept.
242
    	    if (!defined $F->{department}->{$k}) {
243
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0 }), $h->span ({ class=>"slider round" })]);
244
          } elsif ($F->{department}->{$k} == 0) {
245
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
246
          }
247
    	  }
248
    	}
249
  	} else {
250
  		$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
251
  	}
252
 
253
  } elsif ($view eq 'New User') {
254
  	# Skip authentication
255
 		$F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email} });
256
 		$F->{password}   = $h->input ({ type=>"password", name=>"password" });
257
 		$F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
258
 		$F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
259
		$F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
260
		$F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
261
 		$F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
262
#  	$F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, ["", qw(AA A B C)])."</SELECT>";
263
#  	$F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, ["", qw(official nso referee)])."</SELECT>";
264
 		$F->{RCid}         = $h->input ({ type=>"hidden", name=>"RCid", value=>"New" })."TBD&nbsp;";
265
  	$F->{access}			= $h->input ({ type=>"hidden", name=>"access", value=>0 })."0";
266
#  	$F->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' disabled readonly>";
267
  	foreach (keys %{$depts}) {
268
  	  if (defined param ("DEPT-$_")) {
269
  	    $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
270
  	  } else {
271
  	    $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0 }), $h->span ({ class=>"slider round" })]);
272
  	  }
273
  	}
274
  	$F->{buttons}		= $h->input ({ type=>"submit", name=>"submit", value=>"Save" })." ".$h->input ({ type=>"reset", value=>"Reset" })." ".$h->input ({ type=>"submit", name=>"submit", value=>"Cancel" });
275
  	$cookie_string = '';
276
  } elsif ($view eq 'View' or $view eq 'Cancel' or !$view) {
277
  	$cookie_string = authenticate (1);
278
  	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
279
 
280
  	if (!$view) {
281
      $F->{'RCid'} = getUser ($EM)->{'RCid'};
282
  	}
283
 
284
  	# Check to make sure they're only looking up their own ID unless they're a lead or higher
285
  	my $currentuser = getUser ($EM);
286
    my	$targetuser = getUser ($RCID);
287
 
288
  	if (canView ($currentuser, $targetuser)) {
289
    	$F = $targetuser;
290
    	$F->{department} = convertDepartments ($F->{department});
291
      $F->{access} = $AccessLevel->{$F->{access}};
292
    	$F->{'password'} = "*******";
293
      $F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
294
#    	my $checked = $F->{clinic_pass} ? "checked" : "";
295
#    	$F->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' value=$F->{clinic_pass} $checked disabled readonly>";
296
  	} else {
297
  	  logit ($currentuser->{RCid}, "SECURITY: $currentuser->{derby_name} attempted to view another user's ($RCID) info");
298
  	  $errors = "Unauthorized attempt to view another user.  This has been logged.";
299
    	$F->{email}      = "&nbsp;";
300
    	$F->{password}   = "&nbsp;";
301
    	$F->{derby_name} = "&nbsp;";
302
    	$F->{real_name}  = "&nbsp;";
303
    	$F->{pronouns}      = "&nbsp;";
304
    	$F->{tshirt}      = "&nbsp;";
305
    	$F->{phone}      = "&nbsp;";
306
#    	$F->{level}      = "&nbsp;";
307
#    	$F->{type}       = "&nbsp;";
308
    	$F->{RCid}       = "&nbsp;";
309
    	$F->{access}			= "&nbsp;";
310
#    	$F->{clinic_pass}	= "&nbsp;";
311
    	$F->{buttons}		 = "&nbsp;";
312
    }
313
 
314
#  	if (lc $EM eq lc $F->{email} or $AL > 1) {
315
#      $F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
316
#  	} else {
317
#  		$F->{buttons} = "";
318
#  	}
319
  } #else {
320
  #	$cookie_string = authenticate(1);
321
  #	$FORM->{email}      = "&nbsp;";
322
  #	$FORM->{password}   = "&nbsp;";
323
  #	$FORM->{derby_name} = "&nbsp;";
324
  #	$FORM->{real_name}  = "&nbsp;";
325
  #	$FORM->{phone}      = "&nbsp;";
326
  #	$FORM->{level}      = "&nbsp;";
327
  #	$FORM->{type}       = "&nbsp;";
328
  #	$FORM->{RCid}         = "&nbsp;";
329
  #	$FORM->{access}			= "&nbsp;";
330
  #	$FORM->{clinic_pass}	= "&nbsp;";
331
  #	$FORM->{buttons}		= "&nbsp;";
332
  #}
333
 
334
  #---------------START THE HTML--------------------
335
 
336
  my $RCAUTH_cookie = cookie (-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
337
 
338
  print header (-cookie=>$RCAUTH_cookie);
339
 
340
  #foreach (keys %ENV) {
341
  #	print "$_: $ENV{$_}\n<br>";
342
  #}
343
 
344
  if ($errors) {
345
  	$errors = $h->div ({ class=>"error" }, $errors);
346
  } else {
347
  	$errors = "";
348
  }
349
 
8 - 350
  my @printDepartments = ( $h->div ({ class=>"index", style=>"display: unset;" }, $h->p ({ class=>"heading" }, "Department Access:")) );
7 - 351
  foreach (sort grep { !/^PER$/ } keys %{$F->{department}}) {
352
    push @printDepartments, $h->div ({ class=>"rTableRow" }, [
8 - 353
      $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, $depts->{$_}.":", $F->{department}->{$_} =~ /^\d$/ ? $AccessLevel->{$F->{department}->{$_}} : $F->{department}->{$_}),
7 - 354
    ]);
355
  }
356
 
357
  printRCHeader ("User Manager");
358
 
359
  print $errors;
360
  print $h->form ({ action=>url, method=>'POST', name=>'Req' },[
361
    $h->input ({ type=>"hidden", name=>"referer", value=>$goback }),
8 - 362
    $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "User Details:"),
363
      $h->div ({ class=>"rTable", style=>"min-width: 0%;" },[
364
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User-ID / Email Address: ", $F->{email}) ]),
365
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Password: ",                $F->{password}) ]),
366
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Derby Name: ",              $F->{derby_name}) ]),
367
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Real Name: ",               $F->{real_name}) ]),
368
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Pronouns: ",                $F->{pronouns}) ]),
369
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "TShirt Size: ",             $F->{tshirt}) ]),
370
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Phone: ",                   $F->{phone}) ]),
371
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Database ID: ",             $F->{RCid}) ]),
372
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User Added: ",              $F->{added}) ]),
373
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Last Login: ",              $F->{last_login}) ]),
374
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "vORC Access Level: ",       $F->{access}) ]),
375
        @printDepartments,
376
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCell" }, "&nbsp;") ]),
377
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr" }, $h->a ({ href=>$goback }, "[go back]"), $F->{buttons}) ])
378
      ])
379
    ])
380
  ]); #  print $h->close('form');
381
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Schedule:"), getSchedule ($RCID)]) unless $RCID !~ /^\d+$/;
382
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Recent Activity:"), getLog ($RCID)]) unless $RCID !~ /^\d+$/;
7 - 383
  print $h->close ('html');
384
}
385
 
386
#sub selectOptions {
387
#	my $selectedOption = shift;
388
#	my $options = shift;
389
#	return join " ", map { $selectedOption eq $_ ?
390
#	                        $h->option ({ value=>$_, selected=>[] }, $_) :
391
#													$h->option ({ value=>$_ }, $_)
392
#						 					} @$options;
393
#}
394
 
395
sub sendEMail {
396
	my $context = shift;
397
	my $data = shift;
398
	use RCMailer;
399
 
400
	my $email = $data->{email};
401
	my $subject = 'Officiating RollerCon Schedule Manager - New User Request';
402
	my $body = "Greetings,
403
 
404
It appears as though you've registered a new account to Officiate at RollerCon with the following information:
405
 
406
		Derby Name: $data->{derby_name}
407
		Real Name: 	$data->{real_name}
408
		Pronouns: 	$data->{pronouns}
409
		TShirt Size: $data->{tshirt}
410
		Email Address: $data->{email}
411
		Phone: $data->{phone}
412
 
413
Please be patient while our Admins are reviewing your account request.  Each user is manually approved to help ensure robots, spiders, and shift hoggers don't get in.
414
 
415
YOU WILL NOT BE ABLE TO LOG IN UNTIL YOU RECEIVE ANOTHER EMAIL STATING YOUR ACCOUNT REQUEST HAS BEEN APPROVED!
416
 
417
Once approved, you'll be able to log in and view the schedule and sign up for shifts.  Please be considerate of others and don't hogger all of the shifts.  If you do, we will find you and randomly drop your shifts.
418
 
419
http://officials.rollercon.com/schedule/
420
 
421
If you didn't make this request, well, you're still the only one who received this email, and you now have an account request.  You should probably let us know that someone is messing with you.
422
 
423
-RollerCon Officiating Management
424
";
425
	# send the message
426
	EmailUser ($email, $subject, $body);
427
 
428
}
429
 
430
sub checkDupes {
431
  my $field = shift;
432
  my $nametocheck = shift;
433
  my $han = $dbh->prepare("select RCid from official where $field = ?");
434
  $han->execute($nametocheck);
435
  my ($rcid) = $han->fetchrow();
436
  return $rcid;
437
}
438
 
439
sub canView {
440
	my $A = shift // "";
441
	my $B = shift // "";
442
	# Is A a lead or higher of one of B's Depts? (or they're looking at themselves)
443
	# parameters should be a Hashref to the users' details
444
 
445
	return 1 if $A->{access} > 4 or $A->{RCid} == $B->{RCid}; # viewer and target are the same person or it's a SysAdmin.
446
 
447
	my $ADept = convertDepartments $A->{department};
448
	my $BDept = convertDepartments $B->{department};
449
 
450
	foreach (keys %{$BDept}) {
451
		if ($ADept->{$_} > 1) { # A is a Lead or higher of one of B's departments
452
			return 1;
453
		}
454
	}
455
 
456
	return 0;
457
}
458
 
459
sub getLog {
460
  my $RCID = shift;
461
 
462
  my @activity_log;
463
  my $alog = $dbh->prepare("select timestamp, event from v_log where RCid = ? limit 10");
464
  $alog->execute($RCID);
465
  while (my @logs = $alog->fetchrow_array) {
466
  	push @activity_log, $h->li ({ class=>"shaded" }, join " ", @logs);
467
  }
468
 
469
  return $h->ul ([@activity_log]).$h->h5 ($h->a ({ href=>"log.pl?filter-RCid=".$RCID }, "[Entire log history]"));
470
}