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;
13 
21 
22  protected const ACCESS_LEVELS = ['public', 'logged_in', 'logged_out', 'admin'];
23 
27  protected const UNPROTECTED_ACTIONS = ['logout'];
28 
35  public function __construct(protected RouteRegistrationService $routes, protected HandlersService $handlers) {
36  }
37 
52  public function register(string $action, string $handler = '', string $access = 'logged_in', array $params = []): void {
53  if (!in_array($access, self::ACCESS_LEVELS)) {
54  throw new DomainException("Unrecognized value '{$access}' for \$access in " . __METHOD__);
55  }
56 
57  // plugins are encouraged to call actions with a trailing / to prevent 301
58  // redirects but we store the actions without it
59  $action = trim($action, '/');
60 
61  if (empty($handler)) {
62  $handler = Paths::sanitize(Paths::elgg() . "actions/{$action}.php", false);
63  }
64 
65  if (str_ends_with($handler, '.php')) {
66  $file = $handler;
67  $controller = null;
68  } else {
69  $file = null;
70  $controller = $handler;
71  }
72 
73  $middleware = [];
74 
75  if (!in_array($action, self::UNPROTECTED_ACTIONS)) {
76  $middleware[] = CsrfFirewall::class;
77  }
78 
79  if ($access === 'admin') {
80  $middleware[] = AdminGatekeeper::class;
81  } elseif ($access === 'logged_in') {
82  $middleware[] = MiddlewareGateKeeper::class;
83  } elseif ($access === 'logged_out') {
84  $middleware[] = LoggedOutGatekeeper::class;
85  }
86 
87  $middleware[] = ActionMiddleware::class;
88 
89  $additional_middleware = (array) elgg_extract('middleware', $params);
90  $middleware = array_merge($middleware, $additional_middleware);
91 
92  $this->routes->register("action:{$action}", [
93  'path' => "/action/{$action}",
94  'file' => $file,
95  'controller' => $controller,
96  'middleware' => $middleware,
97  'walled' => false,
98  ]);
99  }
100 
110  public function unregister(string $action): void {
111  $action = trim($action, '/');
112 
113  $this->routes->unregister("action:{$action}");
114  }
115 
125  public function exists(string $action): bool {
126  $action = trim($action, '/');
127  $route = $this->routes->get("action:$action");
128  if (!$route) {
129  return false;
130  }
131 
132  $file = $route->getDefault('_file');
133  $controller = $route->getDefault('_controller');
134 
135  if (!$file && !$controller) {
136  return false;
137  }
138 
139  if ($file && !file_exists($file)) {
140  return false;
141  }
142 
143  if ($controller && !$this->handlers->isCallable($controller)) {
144  return false;
145  }
146 
147  return true;
148  }
149 
155  public function getAllActions(): array {
156  $actions = [];
157  $routes = $this->routes->all();
158  foreach ($routes as $name => $route) {
159  if (!str_starts_with($name, 'action:')) {
160  continue;
161  }
162 
163  $action = substr($name, strlen('action:'));
164 
165  $access = 'public';
166  $middleware = (array) $route->getDefault('_middleware');
167  if (in_array(MiddlewareGateKeeper::class, $middleware)) {
168  $access = 'logged_in';
169  } elseif (in_array(LoggedOutGatekeeper::class, $middleware)) {
170  $access = 'logged_out';
171  } elseif (in_array(AdminGatekeeper::class, $middleware)) {
172  $access = 'admin';
173  }
174 
175  $actions[$action] = array_filter([
176  'file' => $route->getDefault('_file'),
177  'controller' => $route->getDefault('_controller'),
178  'access' => $access,
179  ]);
180  }
181 
182  return $actions;
183  }
184 }
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.
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