diff --git a/src/Command/User/UsersPermissionsCommand.php b/src/Command/User/UsersPermissionsCommand.php new file mode 100644 index 00000000..e53e26f5 --- /dev/null +++ b/src/Command/User/UsersPermissionsCommand.php @@ -0,0 +1,124 @@ +entityManager = $entityManager; + $this->userRepository = $entityManager->getRepository(User::class); + $this->permissionResolver = $permissionResolver; + $this->translator = $translator; + + parent::__construct(self::$defaultName); + } + + protected function configure(): void + { + $this + ->addArgument('user', InputArgument::REQUIRED, 'The username of the user to view') + ->addOption('noInherit', null, InputOption::VALUE_NONE, 'Do not inherit permissions from groups') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $username = $input->getArgument('user'); + $inherit = !$input->getOption('noInherit'); + + //Find user + $io->note('Finding user with username: ' . $username); + $user = $this->userRepository->findByEmailOrName($username); + if ($user === null) { + $io->error('No user found with username: ' . $username); + return Command::FAILURE; + } + + $io->note(sprintf('Found user %s with ID %d', $user->getFullName(true), $user->getId())); + + $this->renderPermissionTable($output, $user, $inherit); + + return Command::SUCCESS; + } + + protected function renderPermissionTable(OutputInterface $output, User $user, bool $inherit): array + { + $table = new Table($output); + + $perms = $this->permissionResolver->getPermissionStructure()['perms']; + + if ($inherit) { + $table->setHeaderTitle('Inherited Permissions for '.$user->getFullName(true)); + } else { + $table->setHeaderTitle('Non Inherited Permissions for '.$user->getFullName(true)); + } + + $table->setHeaders(['', 'Permission', 'Operation', 'Value']); + + $perm_index = '1'; + + foreach ($perms as $perm_name => $perm_obj) { + $op_index = 1; + foreach ($perm_obj['operations'] as $operation_name => $operation_obj) { + $table->addRow([ + sprintf('%d-%d', $perm_index, $op_index), + $this->translator->trans($perm_obj['label']), //Permission name + $this->translator->trans($operation_obj['label']), //Operation name + $this->getPermissionValue($user, $perm_name, $operation_name, $inherit), + ]); + + $op_index++; + } + $table->addRow(new TableSeparator()); + + $perm_index++; + } + + $table->render(); + } + + protected function getPermissionValue(User $user, string $permission, string $op, bool $inherit = true): string + { + if ($inherit) { + $permission_value = $this->permissionResolver->inherit($user, $permission, $op); + } else { + $permission_value = $this->permissionResolver->dontInherit($user, $permission, $op); + } + + if ($permission_value === true) { + return 'Allow'; + } else if ($permission_value === false) { + return 'Disallow'; + } else if ($permission_value === null && !$inherit) { + return 'Inherit'; + } else if ($permission_value === null && $inherit) { + return 'Disallow (Inherited)'; + } + + return '???'; + } +}