Subversion Repositories ORC

Rev

Rev 17 | Rev 25 | Go to most recent revision | Details | Compare with Previous | 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
16 - 153
 
154
		  use List::Util qw/sum/;
155
		  if (sum (values %{ convertDepartments ($F->{department}) }) > 0 and $F->{access} == 0) {
156
		    # activating a user for the first time...
157
		    $F->{access} = 1;
158
		    sendEMail ("Activate", $F);
159
		  }
160
 
7 - 161
			if ($FORM->{password}) {
162
#				my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, department, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?)");
163
#				$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})
164
				my $sth = $dbh->prepare ("replace into official (RCid, email, password, derby_name, real_name, pronouns, tshirt, phone, access, department, added, last_login) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)");
165
				$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})
166
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
167
			} else {
168
#				my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, phone = ?, level = ?, type = ?, access = ?, department = ?, clinic_pass = ? where RCid = ?");
169
#				$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})
170
				my $sth = $dbh->prepare ("update official set email = ?, derby_name = ?, real_name = ?, pronouns = ?, tshirt = ?, phone = ?, access = ?, department = ? where RCid = ?");
171
				$sth->execute ($F->{email}, $F->{derby_name}, $F->{real_name}, $F->{pronouns}, $F->{tshirt}, $F->{phone}, $F->{access}, $F->{department}, $F->{RCid})
172
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
173
			}
174
			if ($ERRMSG) {
175
				logit ($F->{RCid}, "DB ERROR: Updating Someone Else: $ERRMSG");
176
			} else {
177
				logit ($F->{RCid}, "Updated User Details (by ".getUser($EM)->{derby_name}.")");
178
				logit (getUser($EM)->{RCid}, "Updated User Details: ".$F->{derby_name}." (".$F->{RCid}.")");
179
			}
180
		} else {
181
			$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
182
			logit ($F->{RCid}, "FAIL: $EM doesn't have access to update $FORM->{email}'s record");
183
		}
184
	}
185
	$F->{password} = "*******";
186
	$F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
187
#	my $checked = $F->{clinic_pass} ? "checked" : "";
188
#	$F->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' $checked disabled readonly>";
189
	$F->{department} = convertDepartments ($F->{department});
190
 
191
	display_form ($F->{RCid}, "View");
192
}
193
 
194
sub display_form {
195
  my $RCID = shift // "";
196
  my $view = shift; # // "New User";
197
  my $errors = shift // "";
198
  my $F;
199
 
200
  if ($view eq 'Edit') {
201
  	$cookie_string = authenticate (1);
202
  	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
203
  	$F = getUser ($RCID);
204
  	my $currentuser = getUser ($EM);
205
#  	$currentuser->{department} = convertDepartments ($currentuser->{department});
206
 
207
#  	if (lc $EM eq lc $F->{email} or $AL > 1) {
208
  	if (canView ($currentuser, $F)) {
209
  	  # Editing your own record OR you're a lead/higher
210
  		if (lc $EM eq lc $F->{email} or $currentuser->{access} < $F->{access}) {
211
  		  # If you're editing your own record, or someone who has higher access than you, make access level read-only
212
  			$F->{access}			= $h->input ({ type=>"hidden", name=>"access", value=>$F->{access} }).$AccessLevel->{$F->{access}};
213
  		} else {
214
  			$F->{access}			= $h->select ({ name=>"access" }, [map { $F->{access} == $_ ? $h->option ({ value=>$_, selected=>[] }, $AccessLevel->{$_}) : $h->option ({ value=>$_ }, $AccessLevel->{$_}) } (-1..$currentuser->{access})]);
215
  		}
216
#  		my $checked = $F->{clinic_pass} ? "checked" : "";
217
#  		if ($currentuser->{access} > 2) {
218
#  			$F->{clinic_pass} = "<INPUT type='checkbox' name='clinic_pass' value=$F->{clinic_pass} $checked>";
219
#  		} else {
220
#  			$F->{clinic_pass} = "<INPUT type='checkbox' name='clinic_pass' $checked disabled readonly>";
221
#  		}
222
#  		$F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email}, readonly=>[] });
223
  		$F->{email}      = $F->{email}.$h->input ({ type=>"hidden", name=>"email", value=>$F->{email} });
224
  		if ($currentuser->{RCid} eq $F->{RCid} or $currentuser->{access} > 4) {
225
  			$F->{password}   = $h->input ({ type=>"password", name=>"password" });
226
  			$F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
227
  			$F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
228
  			$F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
229
  			$F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
230
  			$F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
231
  		} else {
232
  			$F->{password}   = '*******';
233
  		}
234
#  		$F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, [qw(AA A B C)])."</SELECT>";
235
#  		$F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, [qw(official nso referee)])."</SELECT>";
236
  		$F->{RCid}       = $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{RCid} })."$F->{RCid}&nbsp;";
237
  		$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" });
238
 
239
    	$F->{department} = convertDepartments ($F->{department});
240
    	$currentuser->{department} = convertDepartments ($currentuser->{department});
241
    	foreach my $k (keys %{$depts}) {
242
    	  if ($currentuser->{access} > 4) {
243
    	    # SysAdmin can change anyone's department level
244
    	    $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) ]);
245
    	  } elsif ($currentuser->{department}->{$k} > 1 and $currentuser->{department}->{$k} > $F->{department}->{$k}) {
246
    	    # Department Leads and above can change someone's level within the dept (up to their own level -1)
247
    	    $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) ]);
248
    	  } else {
249
    	    # Or it's your own record, you can still submit a request to be added to the dept.
250
    	    if (!defined $F->{department}->{$k}) {
251
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0 }), $h->span ({ class=>"slider round" })]);
252
          } elsif ($F->{department}->{$k} == 0) {
253
            $F->{department}->{$k} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$k", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
254
          }
255
    	  }
256
    	}
257
  	} else {
258
  		$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
259
  	}
260
 
261
  } elsif ($view eq 'New User') {
262
  	# Skip authentication
263
 		$F->{email}      = $h->input ({ type=>"text", name=>"email", value=>$F->{email} });
264
 		$F->{password}   = $h->input ({ type=>"password", name=>"password" });
265
 		$F->{derby_name} = $h->input ({ type=>"text", name=>"derby_name", value=>$F->{derby_name} });
266
 		$F->{real_name}  = $h->input ({ type=>"text", name=>"real_name", value=>$F->{real_name} });
267
		$F->{pronouns}   = $h->input ({ type=>"text", name=>"pronouns", value=>$F->{pronouns} });
268
		$F->{tshirt}     = $h->select ({ name=>"tshirt" }, [map { $F->{tshirt} eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @tshirtOptions] );
269
 		$F->{phone}      = $h->input ({ type=>"text", name=>"phone", value=>$F->{phone} });
270
#  	$F->{level}      = "<SELECT NAME=level>".selectOptions ($F->{level}, ["", qw(AA A B C)])."</SELECT>";
271
#  	$F->{type}       = "<SELECT NAME=type>".selectOptions ($F->{type}, ["", qw(official nso referee)])."</SELECT>";
272
 		$F->{RCid}         = $h->input ({ type=>"hidden", name=>"RCid", value=>"New" })."TBD&nbsp;";
273
  	$F->{access}			= $h->input ({ type=>"hidden", name=>"access", value=>0 })."0";
274
#  	$F->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' disabled readonly>";
275
  	foreach (keys %{$depts}) {
276
  	  if (defined param ("DEPT-$_")) {
277
  	    $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
278
  	  } else {
279
  	    $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0 }), $h->span ({ class=>"slider round" })]);
280
  	  }
281
  	}
282
  	$F->{buttons}		= $h->input ({ type=>"submit", name=>"submit", value=>"Save" })." ".$h->input ({ type=>"reset", value=>"Reset" })." ".$h->input ({ type=>"submit", name=>"submit", value=>"Cancel" });
283
  	$cookie_string = '';
284
  } elsif ($view eq 'View' or $view eq 'Cancel' or !$view) {
285
  	$cookie_string = authenticate (1);
286
  	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
287
 
288
  	if (!$view) {
289
      $F->{'RCid'} = getUser ($EM)->{'RCid'};
290
  	}
291
 
292
  	# Check to make sure they're only looking up their own ID unless they're a lead or higher
293
  	my $currentuser = getUser ($EM);
294
    my	$targetuser = getUser ($RCID);
295
 
296
  	if (canView ($currentuser, $targetuser)) {
297
    	$F = $targetuser;
298
    	$F->{department} = convertDepartments ($F->{department});
299
      $F->{access} = $AccessLevel->{$F->{access}};
300
    	$F->{'password'} = "*******";
301
      $F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
302
#    	my $checked = $F->{clinic_pass} ? "checked" : "";
303
#    	$F->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' value=$F->{clinic_pass} $checked disabled readonly>";
304
  	} else {
305
  	  logit ($currentuser->{RCid}, "SECURITY: $currentuser->{derby_name} attempted to view another user's ($RCID) info");
306
  	  $errors = "Unauthorized attempt to view another user.  This has been logged.";
307
    	$F->{email}      = "&nbsp;";
308
    	$F->{password}   = "&nbsp;";
309
    	$F->{derby_name} = "&nbsp;";
310
    	$F->{real_name}  = "&nbsp;";
311
    	$F->{pronouns}      = "&nbsp;";
312
    	$F->{tshirt}      = "&nbsp;";
313
    	$F->{phone}      = "&nbsp;";
314
#    	$F->{level}      = "&nbsp;";
315
#    	$F->{type}       = "&nbsp;";
316
    	$F->{RCid}       = "&nbsp;";
317
    	$F->{access}			= "&nbsp;";
318
#    	$F->{clinic_pass}	= "&nbsp;";
319
    	$F->{buttons}		 = "&nbsp;";
320
    }
321
 
322
#  	if (lc $EM eq lc $F->{email} or $AL > 1) {
323
#      $F->{buttons}		= $h->input ({ type=>"hidden", name=>"RCid", value=>$F->{'RCid'} }).$h->input ({ type=>"submit", name=>"submit", value=>"Edit" });
324
#  	} else {
325
#  		$F->{buttons} = "";
326
#  	}
327
  } #else {
328
  #	$cookie_string = authenticate(1);
329
  #	$FORM->{email}      = "&nbsp;";
330
  #	$FORM->{password}   = "&nbsp;";
331
  #	$FORM->{derby_name} = "&nbsp;";
332
  #	$FORM->{real_name}  = "&nbsp;";
333
  #	$FORM->{phone}      = "&nbsp;";
334
  #	$FORM->{level}      = "&nbsp;";
335
  #	$FORM->{type}       = "&nbsp;";
336
  #	$FORM->{RCid}         = "&nbsp;";
337
  #	$FORM->{access}			= "&nbsp;";
338
  #	$FORM->{clinic_pass}	= "&nbsp;";
339
  #	$FORM->{buttons}		= "&nbsp;";
340
  #}
341
 
342
  #---------------START THE HTML--------------------
343
 
344
  my $RCAUTH_cookie = cookie (-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
345
 
346
  print header (-cookie=>$RCAUTH_cookie);
347
 
348
  #foreach (keys %ENV) {
349
  #	print "$_: $ENV{$_}\n<br>";
350
  #}
351
 
352
  if ($errors) {
353
  	$errors = $h->div ({ class=>"error" }, $errors);
354
  } else {
355
  	$errors = "";
356
  }
357
 
8 - 358
  my @printDepartments = ( $h->div ({ class=>"index", style=>"display: unset;" }, $h->p ({ class=>"heading" }, "Department Access:")) );
7 - 359
  foreach (sort grep { !/^PER$/ } keys %{$F->{department}}) {
360
    push @printDepartments, $h->div ({ class=>"rTableRow" }, [
8 - 361
      $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, $depts->{$_}.":", $F->{department}->{$_} =~ /^\d$/ ? $AccessLevel->{$F->{department}->{$_}} : $F->{department}->{$_}),
7 - 362
    ]);
363
  }
364
 
365
  printRCHeader ("User Manager");
366
 
367
  print $errors;
368
  print $h->form ({ action=>url, method=>'POST', name=>'Req' },[
369
    $h->input ({ type=>"hidden", name=>"referer", value=>$goback }),
8 - 370
    $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "User Details:"),
371
      $h->div ({ class=>"rTable", style=>"min-width: 0%;" },[
372
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User-ID / Email Address: ", $F->{email}) ]),
373
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Password: ",                $F->{password}) ]),
374
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Derby Name: ",              $F->{derby_name}) ]),
375
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Real Name: ",               $F->{real_name}) ]),
376
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Pronouns: ",                $F->{pronouns}) ]),
377
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "TShirt Size: ",             $F->{tshirt}) ]),
378
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Phone: ",                   $F->{phone}) ]),
379
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Database ID: ",             $F->{RCid}) ]),
380
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "User Added: ",              $F->{added}) ]),
381
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "Last Login: ",              $F->{last_login}) ]),
382
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr", style=>"font-size: unset;" }, "vORC Access Level: ",       $F->{access}) ]),
383
        @printDepartments,
384
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCell" }, "&nbsp;") ]),
385
        $h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableCellr" }, $h->a ({ href=>$goback }, "[go back]"), $F->{buttons}) ])
386
      ])
387
    ])
388
  ]); #  print $h->close('form');
389
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Schedule:"), getSchedule ($RCID)]) unless $RCID !~ /^\d+$/;
390
  print $h->div ({ class=>"index" }, [$h->p ({ class=>"heading" }, "Recent Activity:"), getLog ($RCID)]) unless $RCID !~ /^\d+$/;
7 - 391
  print $h->close ('html');
392
}
393
 
394
#sub selectOptions {
395
#	my $selectedOption = shift;
396
#	my $options = shift;
397
#	return join " ", map { $selectedOption eq $_ ?
398
#	                        $h->option ({ value=>$_, selected=>[] }, $_) :
399
#													$h->option ({ value=>$_ }, $_)
400
#						 					} @$options;
401
#}
402
 
403
sub sendEMail {
404
	my $context = shift;
405
	my $data = shift;
406
	use RCMailer;
407
 
408
	my $email = $data->{email};
409
	my $subject = 'Officiating RollerCon Schedule Manager - New User Request';
16 - 410
	my $body;
411
	if ($context eq "New User") {
412
	  $body = "Greetings,
7 - 413
 
20 - 414
It appears as though you've registered a new account to Volunteer at RollerCon with the following information:
7 - 415
 
416
		Derby Name: $data->{derby_name}
417
		Real Name: 	$data->{real_name}
418
		Pronouns: 	$data->{pronouns}
419
		TShirt Size: $data->{tshirt}
420
		Email Address: $data->{email}
421
		Phone: $data->{phone}
422
 
423
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.
424
 
425
YOU WILL NOT BE ABLE TO LOG IN UNTIL YOU RECEIVE ANOTHER EMAIL STATING YOUR ACCOUNT REQUEST HAS BEEN APPROVED!
426
 
427
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.
428
 
16 - 429
If you're new to using vORC, you may want to read this:
430
 
431
https://volunteers.rollercon.com/info.html
432
 
7 - 433
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.
434
 
16 - 435
-RollerCon Management
7 - 436
";
16 - 437
  } elsif ($context eq "Activate") {
438
    my $tempDepartments = convertDepartments ($data->{department});
439
    my $printableDepartments = join "\n", map { $depts->{$_}.": ".$AccessLevel->{$tempDepartments->{$_}} } sort keys %{$tempDepartments};
440
    $body = "Greetings again,
441
 
442
You have been approved to volunteer at RollerCon in the following departments:
443
 
444
$printableDepartments
445
 
446
You may log into vORC and begin signing 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.
447
 
448
https://volunteers.rollercon.com/schedule/
449
 
450
Please note that you are limited to signing up to a limnited number of shifts per day.  (Meaning, once you sign up for X shifts, you'll have to wait until tomorrow to sign up for more.)  Please understand, while you are a nice, concientious, and good-looking person yourself, who knows how to share, there are others out there that will hogger up all of the shifts.  As time goes by and we get closer to the event, we may lift the limit.  Who knows?
451
 
452
If you've already signed up for your daily limit of shifts, and another shift REALLY strikes your fancy, try dropping one of your shifts.  That should allow you to pick up a different one.
453
 
454
We'll be adding shifts over time, again to throttle how fast some people (not you, mind you) gobble up the shifts.  Check back, maybe even daily.
455
 
456
If you're new to using vORC, you may want to read this:
457
 
458
https://volunteers.rollercon.com/info.html
459
 
460
If you didn't make this request, well, you're still the only one who received this email, and you now have an active account.  You should probably let us know that someone is messing with you.
461
 
462
-RollerCon Management
463
";
464
  } else {
465
    return;
466
  }
7 - 467
	# send the message
468
	EmailUser ($email, $subject, $body);
469
 
470
}
471
 
472
sub checkDupes {
473
  my $field = shift;
474
  my $nametocheck = shift;
475
  my $han = $dbh->prepare("select RCid from official where $field = ?");
476
  $han->execute($nametocheck);
477
  my ($rcid) = $han->fetchrow();
478
  return $rcid;
479
}
480
 
481
sub canView {
482
	my $A = shift // "";
483
	my $B = shift // "";
484
	# Is A a lead or higher of one of B's Depts? (or they're looking at themselves)
485
	# parameters should be a Hashref to the users' details
486
 
487
	return 1 if $A->{access} > 4 or $A->{RCid} == $B->{RCid}; # viewer and target are the same person or it's a SysAdmin.
488
 
489
	my $ADept = convertDepartments $A->{department};
490
	my $BDept = convertDepartments $B->{department};
491
 
492
	foreach (keys %{$BDept}) {
493
		if ($ADept->{$_} > 1) { # A is a Lead or higher of one of B's departments
494
			return 1;
495
		}
496
	}
497
 
498
	return 0;
499
}
500
 
501
sub getLog {
502
  my $RCID = shift;
503
 
504
  my @activity_log;
505
  my $alog = $dbh->prepare("select timestamp, event from v_log where RCid = ? limit 10");
506
  $alog->execute($RCID);
507
  while (my @logs = $alog->fetchrow_array) {
508
  	push @activity_log, $h->li ({ class=>"shaded" }, join " ", @logs);
509
  }
510
 
511
  return $h->ul ([@activity_log]).$h->h5 ($h->a ({ href=>"log.pl?filter-RCid=".$RCID }, "[Entire log history]"));
17 - 512
}