Elgg  Version master
ActionsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
10 use Elgg\Router\Middleware\Gatekeeper as MiddlewareGateKeeper;
14 
22 
23  use Loggable;
24 
28  private static $access_levels = ['public', 'logged_in', 'logged_out', 'admin'];
29 
34  private static $bypass_csrf = [
35  'logout',
36  ];
37 
44  public function __construct(protected RouteRegistrationService $routes, protected HandlersService $handlers) {
45  }
46 
61  public function register(string $action, string $handler = '', string $access = 'logged_in', array $params = []): void {
62  if (!in_array($access, self::$access_levels)) {
63  throw new DomainException("Unrecognized value '{$access}' for \$access in " . __METHOD__);
64  }
65 
66  // plugins are encouraged to call actions with a trailing / to prevent 301
67  // redirects but we store the actions without it
68  $action = trim($action, '/');
69 
70  if (empty($handler)) {
71  $path = Paths::elgg() . 'actions';
72  $handler = Paths::sanitize("{$path}/{$action}.php", false);
73  }
74 
75  $file = null;
76  $controller = null;
77 
78  if (str_ends_with($handler, '.php')) {
79  $file = $handler;
80  } else {
81  $controller = $handler;
82  }
83 
84  $middleware = [];
85 
86  if (!in_array($action, self::$bypass_csrf)) {
87  $middleware[] = CsrfFirewall::class;
88  }
89 
90  if ($access == 'admin') {
91  $middleware[] = AdminGatekeeper::class;
92  } elseif ($access == 'logged_in') {
93  $middleware[] = MiddlewareGateKeeper::class;
94  } elseif ($access == 'logged_out') {
95  $middleware[] = LoggedOutGatekeeper::class;
96  }
97 
98  $middleware[] = ActionMiddleware::class;
99 
100  $additional_middleware = (array) elgg_extract('middleware', $params);
101  $middleware = array_merge($middleware, $additional_middleware);
102 
103  $this->routes->register("action:{$action}", [
104  'path' => "/action/{$action}",
105  'file' => $file,
106  'controller' => $controller,
107  'middleware' => $middleware,
108  'walled' => false,
109  ]);
110  }
111 
121  public function unregister(string $action): void {
122  $action = trim($action, '/');
123 
124  $route = $this->routes->get("action:{$action}");
125  if (!$route) {
126  return;
127  }
128 
129  $this->routes->unregister("action:{$action}");
130  }
131 
141  public function exists(string $action): bool {
142  $action = trim($action, '/');
143  $route = $this->routes->get("action:$action");
144  if (!$route) {
145  return false;
146  }
147 
148  $file = $route->getDefault('_file');
149  $controller = $route->getDefault('_controller');
150 
151  if (!$file && !$controller) {
152  return false;
153  }
154 
155  if ($file && !file_exists($file)) {
156  return false;
157  }
158 
159  if ($controller && !$this->handlers->isCallable($controller)) {
160  return false;
161  }
162 
163  return true;
164  }
165 
171  public function getAllActions(): array {
172  $actions = [];
173  $routes = $this->routes->all();
174  foreach ($routes as $name => $route) {
175  if (!str_starts_with($name, 'action:')) {
176  continue;
177  }
178 
179  $action = substr($name, 7);
180 
181  $access = 'public';
182  $middleware = (array) $route->getDefault('_middleware');
183  if (in_array(MiddlewareGateKeeper::class, $middleware)) {
184  $access = 'logged_in';
185  } elseif (in_array(LoggedOutGatekeeper::class, $middleware)) {
186  $access = 'logged_out';
187  } elseif (in_array(AdminGatekeeper::class, $middleware)) {
188  $access = 'admin';
189  }
190 
191  $actions[$action] = array_filter([
192  'file' => $route->getDefault('_file'),
193  'controller' => $route->getDefault('_controller'),
194  'access' => $access,
195  ]);
196  }
197 
198  return $actions;
199  }
200 }
elgg
Definition: install.js:27
$params
Saves global plugin settings.
Definition: save.php:13
Helpers for providing callable-based APIs.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Exception thrown if a value does not adhere to a defined valid data domain.
exists(string $action)
Check if an action is registered and its script exists.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
getAllActions()
Get all actions.
$path
Definition: details.php:70
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
Protects a route from non-authenticated users.
Definition: Gatekeeper.php:11
if(!$menu instanceof\Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:16
__construct(protected RouteRegistrationService $routes, protected HandlersService $handlers)
Constructor.
unregister(string $action)
Unregisters an action.
$action
Definition: subscribe.php:11
Actions service.
$handler
Definition: add.php:7