Rev 5 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/perl# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)#my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";#close STDERR;#open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";#warn "Redirecting errors to ${error_log_path}vorc_error.log";use strict;use PEEPS;use CGI qw/param header start_html url/;use CGI::Cookie;our $h = HTML::Tiny->new( mode => 'html' );my $dbh = getRCDBH ();$ENV{HTTPS} = 'ON' if $ENV{SERVER_NAME} =~ /^peeps/;my $cookie_string = authenticate (1) || die;my ($EML, $PWD, $LVL) = split /&/, $cookie_string;my $user = $ORCUSER;my @ERRORS;#my $activated = $ORCUSER->{access};print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));#foreach (sort keys %ENV) {# print "$_: $ENV{$_}\n<br>";#}use DateTime;my $dt = DateTime->today;$dt =~ s/T00\:00\:00$//;#for my $key (sort keys %{$user}) {# print "$key: $user->{$key}<br>";#}my $league_id = WebDB::trim param ("league_id") // printError ("League ID not set.");my $person_id = WebDB::trim param ("person_id") // printError ("Person ID not set.");# Does the league exist?my $league_name = getLeagueName ($league_id);printError ("League not found ($league_id).") unless $league_name;# Is the current user a League (or WFTDA) Admin?my ($isALeagueAdmin) = $dbh->selectrow_array ("select 1 from role where role = ? and member_org_id = ? and person_id = ?", undef, "League Admin", $league_id, $user->{person_id});my ($isAWFTDAAdmin) = $dbh->selectrow_array ("select 1 from role where role = ? and member_org_id = ? and person_id = ?", undef, "System Admin", 4276, $user->{person_id});printError ("Not a League Admin for $league_name") unless ($isALeagueAdmin or $isAWFTDAAdmin);# Does the person exist...my $target_person = getUser ($person_id);# ...within the leauge?my @roles = @{ $dbh->selectall_arrayref ("select id, role from role where person_id = ? and member_org_id = ? order by role", undef, $person_id, $league_id) };my %rolename = map { $_->[0] => $_->[1] } @roles;printError ("Person ID not found, or they don't exist within your League") unless scalar @roles;if (my $RoleID = WebDB::trim scalar param ("Delete")) {printError ("Improperly formatted RoleID [$_]") unless $RoleID =~ /^\d+$/;if (scalar @roles == 1) {my $confirmed = WebDB::trim scalar param ("Confirmed") // "";if (!$confirmed) {# confirm removing the user from the org first...printRCHeader ("User Role Manager");print $h->h2 ("Deleting role from user...");print $h->open ("form", { action=>url, method=>'POST', name=>'UserRoleForm', id=>'UserForm' });print $h->input ({ type=>"hidden", name=>"person_id", value=>$person_id}),$h->input ({ type=>"hidden", name=>"league_id", value=>$league_id}),$h->input ({ type=>"hidden", name=>"Delete", value=>$RoleID});print $h->div ("Removing this role will remove this person from your Organization.".$h->br."Are you sure you want to do that?");print $h->div ([$h->input ({type=>"submit", name=>"Confirmed", value=>"Yes"}), ' ', $h->input ({type=>"submit", name=>"Confirmed", value=>"Cancel"})]);print $h->close ("form", "body", "html");exit;} elsif ($confirmed eq "Yes") {# delete the role$dbh->do ("delete from role where member_org_id = ? and person_id = ? and id = ?", undef, $league_id, $person_id, $RoleID);$dbh->do ("replace into full_person select * from v_person where id = ? and league_id = ?", undef, $person_id, $league_id);logit ($person_id, "Removed from the '$rolename{$RoleID}' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");logit ($user->{person_id}, "Removed user ".$target_person->{derby_name}." [ID:$person_id] from the '$rolename{$RoleID}' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");orglogit ($user->{person_id}, $league_id, "Removed user ".$target_person->{derby_name}." [ID:$person_id] from the '$rolename{$RoleID}' role [ID:$RoleID] and league");#warn "Removing RoleID: $RoleID";}# They didn't confirm the delete, don't do anything} else {$dbh->do ("delete from role where member_org_id = ? and person_id = ? and id = ?", undef, $league_id, $person_id, $RoleID);$dbh->do ("replace into full_person select * from v_person where id = ? and league_id = ?", undef, $person_id, $league_id);logit ($person_id, "Removed from the '$rolename{$RoleID}' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");logit ($user->{person_id}, "Removed user ".$target_person->{derby_name}." [ID:$person_id] from the '$rolename{$RoleID}' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");orglogit ($user->{person_id}, $league_id, "Removed user ".$target_person->{derby_name}." [ID:$person_id] from the '$rolename{$RoleID}' role [ID:$RoleID]");#warn "Removing RoleID: $RoleID";}@roles = @{ $dbh->selectall_arrayref ("select id, role from role where person_id = ? and member_org_id = ? order by role", undef, $person_id, $league_id) };}if (WebDB::trim scalar param ("savenewrole") eq "Save") {my $NewRole = WebDB::trim scalar param ("newrole");# We should check to make sure the new role is acceptable...my ($RoleID) = $dbh->selectrow_array ("select id from role where person_id = ? and member_org_id = ? and role = ?", undef, $person_id, $league_id, "Pending");if ($RoleID) {$dbh->do ("update role set role = ? where member_org_id = ? and person_id = ? and id = ?", undef, $NewRole, $league_id, $person_id, $RoleID);$dbh->do ("replace into full_person select * from v_person where id = ? and league_id = ?", undef, $person_id, $league_id);logit ($person_id, "Updated to the '$NewRole' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");logit ($user->{person_id}, "Updated user ".$target_person->{derby_name}." [ID:$person_id] to the '$NewRole' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");orglogit ($user->{person_id}, $league_id, "Added user ".$target_person->{derby_name}." [ID:$person_id] to the league and '$NewRole' role [ID:$RoleID]");# Maybe send an email to the user that they've been added to the league...} else {$dbh->do ("insert into role (member_org_id, person_id, role) values (?, ?, ?)", undef, $league_id, $person_id, $NewRole);$RoleID = $dbh->last_insert_id ();$dbh->do ("replace into full_person select * from v_person where id = ? and league_id = ?", undef, $person_id, $league_id);logit ($person_id, "Added to the '$NewRole' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");logit ($user->{person_id}, "Added user ".$target_person->{derby_name}." [ID:$person_id] to the '$NewRole' role [ID:$RoleID] for ".getLeagueName ($league_id)." [ID:$league_id]");orglogit ($user->{person_id}, $league_id, "Added user ".$target_person->{derby_name}." [ID:$person_id] to the '$NewRole' role [ID:$RoleID]");}@roles = @{ $dbh->selectall_arrayref ("select id, role from role where person_id = ? and member_org_id = ? order by role", undef, $person_id, $league_id) };}printRCHeader ("User Role Manager");print $h->h2 ("Roles for $target_person->{derby_name} in $league_name:");print $h->open ("form", { action=>url, method=>'POST', name=>'UserRoleForm', id=>'UserForm' });print $h->input ({ type=>"hidden", name=>"person_id", value=>$person_id}),$h->input ({ type=>"hidden", name=>"league_id", value=>$league_id});my @leagueroles = ($h->div ({ class=>"rTableRow" },[ $h->div ({ class=>"rTableHead", style=>"font-size: smaller;" }, "Role".' '.$h->input ({type=>"submit", name=>"Add", value=>"Add"}), "Change" ) ]));foreach (@roles) {}push @leagueroles, map { $h->div ({ class=>"rTableRow shaded" },[ $h->div ({ class=>"rTableCellr".($_->[1] eq "Pending" ? " highlighted" : ""), style=>"font-size: smaller;".($_->[1] eq "Pending" ? " font-style: italic;" : "") },$_->[1], [ $h->input ({type=>"submit", name=>"Delete", value=>"Delete", onClick=>"this.value=$_->[0]"}) ] ) ]) } @roles; # Change button -> , $h->input ({type=>"submit", name=>"Change", value=>"Change", onClick=>"this.value=$_->[0]"})if (WebDB::trim scalar param ("Add")) {use tableViewer qw(notInArray);my @allowed_roles = grep { notInArray ($_, [map { $_->[1] } @roles]) } map { $_->[0] } @{ $dbh->selectall_arrayref ("select distinct role from role order by role") };push @leagueroles, $h->div ({ class=>"rTableRow shaded" },[ $h->div ({ class=>"rTableCellr"}, $h->select ({name=>"newrole"}, [ map { $h->option ($_) } @allowed_roles ]), $h->input ({type=>"submit", name=>"savenewrole", value=>"Save"}).' '.$h->button ("Cancel")) ]);}print $h->ul ([@leagueroles]);print $h->ul ($h->input ({type=>"button", onClick=>"window.location.href='view_user?person_id=$person_id'", value=>"Back"}));print $h->close ("form");sub printError {my $message = shift // "Unknown Error";printRCHeader("Manage Role");print $h->close ("table");print $h->h2 ("Error:");print $h->div ({ style=>"max-width:450px;" }, $message, " ");print $h->button ({onclick => "window.location.href='/';"}, "Home");print $h->close ("BODY", "HTML");logit ($user->{id}, "ERROR (Manage Role) ".$message);exit;}