Elgg  Version 5.1
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 
41  protected $routes;
42 
46  protected $handlers;
47 
54  public function __construct(RouteRegistrationService $routes, HandlersService $handlers) {
55  $this->routes = $routes;
56  $this->handlers = $handlers;
57  }
58 
73  public function register(string $action, string $handler = '', string $access = 'logged_in', array $params = []): void {
74  if (!in_array($access, self::$access_levels)) {
75  throw new DomainException("Unrecognized value '{$access}' for \$access in " . __METHOD__);
76  }
77 
78  // plugins are encouraged to call actions with a trailing / to prevent 301
79  // redirects but we store the actions without it
80  $action = trim($action, '/');
81 
82  if (empty($handler)) {
83  $path = Paths::elgg() . 'actions';
84  $handler = Paths::sanitize("{$path}/{$action}.php", false);
85  }
86 
87  $file = null;
88  $controller = null;
89 
90  if (str_ends_with($handler, '.php')) {
91  $file = $handler;
92  } else {
93  $controller = $handler;
94  }
95 
96  $middleware = [];
97 
98  if (!in_array($action, self::$bypass_csrf)) {
99  $middleware[] = CsrfFirewall::class;
100  }
101 
102  if ($access == 'admin') {
103  $middleware[] = AdminGatekeeper::class;
104  } elseif ($access == 'logged_in') {
105  $middleware[] = MiddlewareGateKeeper::class;
106  } elseif ($access == 'logged_out') {
107  $middleware[] = LoggedOutGatekeeper::class;
108  }
109 
110  $middleware[] = ActionMiddleware::class;
111 
112  $additional_middleware = (array) elgg_extract('middleware', $params);
113  $middleware = array_merge($middleware, $additional_middleware);
114 
115  $this->routes->register("action:{$action}", [
116  'path' => "/action/{$action}",
117  'file' => $file,
118  'controller' => $controller,
119  'middleware' => $middleware,
120  'walled' => false,
121  ]);
122  }
123 
133  public function unregister(string $action): void {
134  $action = trim($action, '/');
135 
136  $route = $this->routes->get("action:{$action}");
137  if (!$route) {
138  return;
139  }
140 
141  $this->routes->unregister("action:{$action}");
142  }
143 
153  public function exists(string $action): bool {
154  $action = trim($action, '/');
155  $route = $this->routes->get("action:$action");
156  if (!$route) {
157  return false;
158  }
159 
160  $file = $route->getDefault('_file');
161  $controller = $route->getDefault('_controller');
162 
163  if (!$file && !$controller) {
164  return false;
165  }
166 
167  if ($file && !file_exists($file)) {
168  return false;
169  }
170 
171  if ($controller && !$this->handlers->isCallable($controller)) {
172  return false;
173  }
174 
175  return true;
176  }
177 
183  public function getAllActions(): array {
184  $actions = [];
185  $routes = $this->routes->all();
186  foreach ($routes as $name => $route) {
187  if (!str_starts_with($name, 'action:')) {
188  continue;
189  }
190 
191  $action = substr($name, 7);
192 
193  $access = 'public';
194  $middleware = (array) $route->getDefault('_middleware');
195  if (in_array(MiddlewareGateKeeper::class, $middleware)) {
196  $access = 'logged_in';
197  } elseif (in_array(LoggedOutGatekeeper::class, $middleware)) {
198  $access = 'logged_out';
199  } elseif (in_array(AdminGatekeeper::class, $middleware)) {
200  $access = 'admin';
201  }
202 
203  $actions[$action] = array_filter([
204  'file' => $route->getDefault('_file'),
205  'controller' => $route->getDefault('_controller'),
206  'access' => $access,
207  ]);
208  }
209 
210  return $actions;
211  }
212 }
$params
Saves global plugin settings.
Definition: save.php:13
Helpers for providing callable-based APIs.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
__construct(RouteRegistrationService $routes, HandlersService $handlers)
Constructor.
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:25
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
unregister(string $action)
Unregisters an action.
$action
Definition: subscribe.php:11
Actions service.
$handler
Definition: add.php:7
var elgg
Definition: elgglib.js:4