LookupUserCommand.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Console;
  9. use Grav\Common\Grav;
  10. use Grav\Common\User\Interfaces\UserCollectionInterface;
  11. use Grav\Common\User\Interfaces\UserInterface;
  12. use Grav\Console\ConsoleCommand;
  13. use Grav\Framework\Flex\FlexObject;
  14. use Grav\Plugin\Login\Login;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. /**
  18. * Class CleanCommand
  19. *
  20. * @package Grav\Console\Cli
  21. */
  22. class LookupUserCommand extends ConsoleCommand
  23. {
  24. /** @var array */
  25. protected $options = [];
  26. /** @var Login */
  27. protected $login;
  28. /**
  29. * Configure the command
  30. */
  31. protected function configure()
  32. {
  33. $this
  34. ->setName('lookup-user')
  35. ->setAliases(['lookup'])
  36. ->addArgument(
  37. 'key',
  38. InputArgument::REQUIRED,
  39. 'The username, email, id, or key to lookup'
  40. )
  41. ->setDescription('Finds user info based on some data')
  42. ->setHelp('The <info>lookup-user</info> finds a user based on some data query.')
  43. ;
  44. }
  45. /**
  46. * @return int|null|void
  47. */
  48. protected function serve()
  49. {
  50. include __DIR__ . '/../vendor/autoload.php';
  51. $io = new SymfonyStyle($this->input, $this->output);
  52. $grav = Grav::instance();
  53. $grav->setup();
  54. $io->title('Looking up user');
  55. // Initialize Plugins
  56. $grav['plugins']->init();
  57. $grav->fireEvent('onCliInitialize');
  58. $key = $this->input->getArgument('key');
  59. /** @var UserCollectionInterface $users */
  60. $users = $grav['accounts'];
  61. /** @var UserInterface $user */
  62. $user = $users->find($key, ['username', 'email', 'fullname', 'storage_key', 'flex_key']);
  63. if ($user->exists()) {
  64. /** @var $io SymfonyStyle */
  65. $io->text('Username: <green>'. $user->username . '</green>');
  66. $io->text('Name: <red>' . $user->fullname . '</red>');
  67. if ($user instanceof FlexObject) {
  68. $io->text('Flex Key: <cyan>' . $user->getFlexKey() . '</cyan>');
  69. }
  70. $io->text('Email: <yellow>' . $user->email . '</yellow>');
  71. $io->newLine();
  72. exit;
  73. }
  74. $io->error('Sorry, the user with query: "' . $key . '", was not found');
  75. }
  76. }