Signed-off-by: Francois Gouget fgouget@codeweavers.com --- testbot/web/admin/UserDetails.pl | 129 ++++++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-)
diff --git a/testbot/web/admin/UserDetails.pl b/testbot/web/admin/UserDetails.pl index f11d127ad..798063dd7 100644 --- a/testbot/web/admin/UserDetails.pl +++ b/testbot/web/admin/UserDetails.pl @@ -2,7 +2,7 @@ # User details page # # Copyright 2009 Ge van Geldorp -# Copyright 2013 Francois Gouget +# Copyright 2013-2014, 2017-2018, 2022 Francois Gouget # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -20,6 +20,111 @@
use strict;
+ +package RolesBlock; + +use ObjectModel::CGI::CollectionBlock; +our @ISA = qw(ObjectModel::CGI::CollectionBlock); + +use URI::Escape; + +use ObjectModel::BasicPropertyDescriptor; +use WineTestBot::Utils; + + +sub _initialize($$$) +{ + my ($self, $Collection, $EnclosingPage) = @_; + + $self->SUPER::_initialize($self, $Collection, $EnclosingPage); + $self->{User} = $self->{EnclosingPage}->{Item}; + $self->{UserName} = $self->{User}->Name; +} + +sub GetPropertyDescriptors($) +{ + my ($self) = @_; + + my %Props; + map { $Props{$_->GetName()} = $_ } @{$self->{Collection}->GetPropertyDescriptors()}; + return [ + CreateBasicPropertyDescriptor($self->{UserName}, $self->{UserName}, !1, !1, "B", 1), + $Props{IsDefaultRole}, + $Props{Name}, + ]; +} + +sub GetPropertyValue($$$) +{ + my ($self, $Row, $Col) = @_; + + return $Col->{Descriptor}->GetName() eq $self->{UserName} ? + $self->{User}->Roles->HasItem($Row->{Item}->Name) : + $self->SUPER::GetPropertyValue($Row, $Col); +} + +sub GenerateFormStart($) +{ + my ($self) = @_; + + $self->SUPER::GenerateFormStart(); + print "<input type='hidden' name='Key' value='", + uri_escape($self->{User}->GetKey()), "'>\n"; +} + +sub GetItemActions($) +{ + #my ($self) = @_; + + return ["Add role", "Remove role"]; +} + +sub OnItemAction($$$) +{ + my ($self, $Item, $Action) = @_; + + if ($self->{RW} and $Action eq "Add role") + { + return 1 if ($self->{User}->Roles->HasItem($Item->Name)); + + $self->{User}->AddRole($Item); + my $ErrMessage = $self->{User}->Roles->Save(); + if (defined $ErrMessage) + { + $self->{EnclosingPage}->SetError(undef, $ErrMessage); + return 0; + } + NotifyAdministrator("New administrator user: $self->{UserName}", + "$self->{UserName} now is an administrator.\n"); + return 1; + } + if ($self->{RW} and $Action eq "Remove role") + { + my $UserRole = $self->{User}->Roles->GetItem($Item->Name); + return 1 if (!$UserRole); + + my $ErrMessage = $self->{User}->Roles->DeleteItem($UserRole); + if (defined $ErrMessage) + { + $self->{EnclosingPage}->SetError(undef, $ErrMessage); + return 0; + } + NotifyAdministrator("Removed an administrator: $self->{UserName}", + "$self->{UserName} is no longer an administrator.\n"); + return 1; + } + + $self->{EnclosingPage}->SetError(undef, "No per-Item action defined for $Action"); + return 0; +} + +sub GetActions($) +{ + #my ($self) = @_; + return []; +} + + package UserDetailsPage;
use ObjectModel::CGI::ItemPage; @@ -27,6 +132,7 @@ our @ISA = qw(ObjectModel::CGI::ItemPage);
use WineTestBot::CGI::Sessions; use WineTestBot::Config; +use WineTestBot::Roles; use WineTestBot::Users;
@@ -35,6 +141,10 @@ sub _initialize($$$) my ($self, $Request, $RequiredRole) = @_;
$self->SUPER::_initialize($Request, $RequiredRole, CreateUsers()); + if (!$self->{Item}->GetIsNew()) + { + $self->{RolesBlock} = new RolesBlock(CreateRoles(), $self); + } }
sub DisplayProperty($$) @@ -51,6 +161,18 @@ sub DisplayProperty($$) return $self->SUPER::DisplayProperty($PropertyDescriptor); }
+sub GenerateBody($) +{ + my ($self) = @_; + + $self->SUPER::GenerateBody(); + if ($self->{RolesBlock}) + { + print "<h2><a name='Roles'></a>Roles</h2>\n"; + $self->{RolesBlock}->GenerateList(); + } +} + sub GetActions($) { my ($self) = @_; @@ -118,6 +240,11 @@ sub OnAction($$) { return $self->OnSave(); } + elsif ($self->{RolesBlock} and + grep { $_ eq $Action } @{$self->{RolesBlock}->GetItemActions()}) + { + return $self->{RolesBlock}->OnAction($self->GetParam("Action")); + }
return $self->SUPER::OnAction($Action); }