Skip to content

Commit

Permalink
Added the possibility to use user names in as well as ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vassyli committed Apr 13, 2019
1 parent 2c34ab6 commit e1d0c8f
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Console/Commands/RoleGrantCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LotGD\Crate\WWW\Console\Commands;


use Doctrine\DBAL\DBALException;
use LotGD\Core\Console\Command\BaseCommand;
use LotGD\Crate\WWW\Model\Role;
use LotGD\Crate\WWW\Model\User;
Expand All @@ -29,7 +30,7 @@ protected function configure()
->setDescription('Grants role to a given user.')
->setDefinition(new InputDefinition([
new InputArgument("roleId", InputArgument::REQUIRED, "Name of the role"),
new InputArgument("userId", InputArgument::REQUIRED, "ID of the user")
new InputArgument("userId", InputArgument::REQUIRED, "ID or name of the user")
]));
}

Expand All @@ -52,13 +53,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/** @var User $user */
$user = $em->getRepository(User::class)->find($userId);
try {
$user = $em->getRepository(User::class)->find($userId);
} catch (\Exception $e) {
$user = $em->getRepository(User::class)->findOneBy(["displayName" => $userId]);
}


if (!$user) {
$style->error("Cannot find user with id {$userId}");
return;
$user = $em->getRepository(User::class)->findOneBy(["displayName" => $userId]);

if (!$user) {
$style->error("Cannot find user with id or name {$userId}");
return;
}
}

try {
if ($user->getUserRoles()->contains($role)) {
$style->error("This role was already given to this user.");
return;
}

$user->addRole($role);
$this->game->getEntityManager()->flush();

Expand Down

0 comments on commit e1d0c8f

Please sign in to comment.