Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using names when adding users to multi-value roles #278

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions lib/RT/Record/Role/Roles.pm
Original file line number Diff line number Diff line change
Expand Up @@ -621,19 +621,27 @@ sub _ResolveRoles {
$self->loc("Couldn't load principal: [_1]", $msg);
}
} else {
my @addresses = RT::EmailParser->ParseEmailAddress( $value );
for my $address ( @addresses ) {
my $user = RT::User->new( RT->SystemUser );
my ($id, $msg) = $user->LoadOrCreateByEmail( $address );
if ( $id ) {
# Load it back as us, not as the system
# user, to be completely safe.
$user = RT::User->new( $self->CurrentUser );
$user->Load( $id );
push @{ $roles->{$role} }, $user->PrincipalObj;
} else {
push @errors,
$self->loc("Couldn't load or create user: [_1]", $msg);
# Try loading by id, name, then email. If all fail, catch that below
my $user = RT::User->new( $self->CurrentUser );
$user->Load( $value );

if ($user->id) {
push @{ $roles->{$role} }, $user->PrincipalObj;
} else {
my @addresses = RT::EmailParser->ParseEmailAddress( $value );
for my $address ( @addresses ) {
$user = RT::User->new( RT->SystemUser );
my ($id, $msg) = $user->LoadOrCreateByEmail( $address );
if ( $id ) {
# Load it back as us, not as the system
# user, to be completely safe.
$user = RT::User->new( $self->CurrentUser );
$user->Load( $id );
push @{ $roles->{$role} }, $user->PrincipalObj;
} else {
push @errors,
$self->loc("Couldn't load or create user: [_1]", $msg);
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions t/assets/api.t
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,28 @@ diag "Custom Field handling";

}

note "Create/update with Roles using names";
{
my $root = RT::User->new( RT->SystemUser );
$root->Load("root");
ok $root->id, "Found root";

my $bps = RT::Test->load_or_create_user( Name => "BPS" );
ok $bps->id, "Created BPS user";

my $asset = RT::Asset->new( RT->SystemUser );
my ($id, $msg) = $asset->Create(
Name => 'RT server 2',
HeldBy => 'root',
Owner => 'BPS',
Contact => 'BPS',
Catalog => $catalog->id,
);
ok $id, "Created: $msg";
is $asset->HeldBy->UserMembersObj->First->Name, "root", "root is Holder";
is $asset->Owner->Name, "BPS", "BPS is Owner";
is $asset->Contacts->UserMembersObj->First->Name, "BPS", "BPS is Contact";
}


done_testing;