admin-addon-user-manager.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use RocketTheme\Toolbox\Event\Event;
  5. use \Grav\Common\Utils;
  6. use \Grav\Common\User\User;
  7. use Grav\Common\File\CompiledYamlFile;
  8. use AdminAddonUserManager\Users\Manager as UsersManager;
  9. use AdminAddonUserManager\Groups\Manager as GroupsManager;
  10. use AdminAddonUserManager\Users\ExpertManager as UsersExpertManager;
  11. class AdminAddonUserManagerPlugin extends Plugin {
  12. /**
  13. * Returns the plugin's configuration key
  14. *
  15. * @param String $key
  16. * @return String
  17. */
  18. public function getPluginConfigKey($key = null) {
  19. $pluginKey = 'plugins.' . $this->name;
  20. return ($key !== null) ? $pluginKey . '.' . $key : $pluginKey;
  21. }
  22. public function getPluginConfigValue($key = null, $default = null) {
  23. return $this->config->get($this->getPluginConfigKey($key), $default);
  24. }
  25. public function getConfigValue($key, $default = null) {
  26. return $this->config->get($key, $default);
  27. }
  28. public function getPreviousUrl() {
  29. return $this->grav['session']->{$this->name . '.previous_url'};
  30. }
  31. public function getModalsConfiguration() {
  32. return CompiledYamlFile::instance(__DIR__ . DS . 'modals.yaml')->content();
  33. }
  34. public static function getSubscribedEvents() {
  35. return [
  36. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  37. 'onAdminRegisterPermissions' => ['onAdminRegisterPermissions', 1000]
  38. ];
  39. }
  40. public function onPluginsInitialized() {
  41. if (!$this->isAdmin() || !$this->grav['user']->authenticated) {
  42. return;
  43. }
  44. $this->grav['locator']->addPath('blueprints', '', __DIR__ . DS . 'blueprints');
  45. include __DIR__ . DS . 'vendor' . DS . 'autoload.php';
  46. $this->managers[] = new UsersManager($this->grav, $this);
  47. $this->managers[] = new GroupsManager($this->grav, $this);
  48. $this->managers[] = new UsersExpertManager($this->grav, $this);
  49. $this->enable([
  50. 'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', -10],
  51. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
  52. 'onAdminMenu' => ['onAdminMenu', 0],
  53. 'onAssetsInitialized' => ['onAssetsInitialized', 0],
  54. 'onAdminTaskExecute' => ['onAdminTaskExecute', 0],
  55. ]);
  56. $this->registerPermissions();
  57. }
  58. public function onAssetsInitialized() {
  59. $assets = $this->grav['assets'];
  60. foreach ($this->managers as $manager) {
  61. $manager->initializeAssets($assets);
  62. }
  63. }
  64. public function onAdminMenu() {
  65. $twig = $this->grav['twig'];
  66. $twig->plugins_hooked_nav = (isset($twig->plugins_hooked_nav)) ? $twig->plugins_hooked_nav : [];
  67. foreach ($this->managers as $manager) {
  68. $nav = $manager->getNav();
  69. if ($nav) {
  70. $twig->plugins_hooked_nav[$nav['label']] = $nav;
  71. }
  72. }
  73. }
  74. public function onAdminTwigTemplatePaths($e) {
  75. $paths = $e['paths'];
  76. $paths[] = __DIR__ . DS . 'templates';
  77. $e['paths'] = $paths;
  78. }
  79. public function onTwigSiteVariables() {
  80. $page = $this->grav['page'];
  81. $twig = $this->grav['twig'];
  82. $session = $this->grav['session'];
  83. $uri = $this->grav['uri'];
  84. foreach ($this->managers as $manager) {
  85. if ($page->slug() === $manager->getLocation() && $this->grav['admin']->authorize(['admin.super', $manager->getRequiredPermission()])) {
  86. $session->{$this->name . '.previous_url'} = $uri->route() . $uri->params();
  87. $page = $this->grav['admin']->page(true);
  88. $page->id('aaum-' . implode('/', $uri->paths()));
  89. $twig->twig_vars['context'] = $page;
  90. $vars = $manager->handleRequest();
  91. $twig->twig_vars = array_merge($twig->twig_vars, $vars);
  92. return true;
  93. }
  94. }
  95. }
  96. public function onAdminTaskExecute($e) {
  97. foreach ($this->managers as $manager) {
  98. if ($this->grav['admin']->authorize(['admin.super', $manager->getRequiredPermission()])) {
  99. $result = $manager->handleTask($e);
  100. if ($result) {
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. public function registerPermissions() {
  108. foreach ($this->managers as $manager) {
  109. $this->grav['admin']->addPermissions([$manager->getRequiredPermission() => 'boolean']);
  110. }
  111. // Custom permissions
  112. $customPermissions = $this->getPluginConfigValue('custom_permissions', []);
  113. foreach ($customPermissions as $permission) {
  114. $this->grav['admin']->addPermissions([$permission => 'boolean']);
  115. }
  116. }
  117. public function onAdminRegisterPermissions() {
  118. if (!$this->isAdmin() || !$this->grav['user']->authenticated) {
  119. return;
  120. }
  121. $this->grav['admin']->addPermissions(['site.login' => 'boolean']);
  122. }
  123. }