Elgg  Version master
AuthenticationService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 use Elgg\Traits\Loggable;
7 
15 
16  use Loggable;
17 
23  protected array $handlers = [];
24 
30  public function __construct(protected HandlersService $handlerService) {
31  }
32 
42  public function registerHandler($handler, string $importance = 'sufficient', string $policy = 'user'): void {
43  $handler_string = $this->handlerService->describeCallable($handler);
44  if (!isset($this->handlers[$policy])) {
45  $this->handlers[$policy] = [];
46  }
47 
48  $this->handlers[$policy][$handler_string] = [
49  'handler' => $handler,
50  'importance' => strtolower($importance),
51  ];
52  }
53 
62  public function unregisterHandler($handler, string $policy = 'user'): void {
63  $handler_string = $this->handlerService->describeCallable($handler);
64 
65  unset($this->handlers[$policy][$handler_string]);
66  }
67 
77  public function authenticate(string $policy, array $authentication_params = []): bool {
78  if (!isset($this->handlers[$policy])) {
79  return false;
80  }
81 
82  $authenticated = false;
83  $first_exception = null;
84 
85  foreach ($this->handlers[$policy] as $handler_string => $handler_config) {
86  $handler = $handler_config['handler'];
87  $importance = strtolower($handler_config['importance']);
88 
89  if (!$this->handlerService->isCallable($handler)) {
90  $this->getLogger()->warning("PAM handler '{$handler_string}' for policy '{$policy}' isn't callable");
91  continue;
92  }
93 
94  $callable = $this->handlerService->resolveCallable($handler);
95 
96  try {
97  $result = call_user_func($callable, $authentication_params);
98  if ($result === true) {
99  $authenticated = true;
100  } elseif ($result === false && $importance === 'required') {
101  return false;
102  }
103  } catch (\Exception $e) {
104  if (!$e instanceof AuthenticationException) {
105  $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e);
106  }
107 
108  if ($importance === 'required') {
109  throw $e;
110  }
111 
112  if (!isset($first_exception)) {
113  $first_exception = $e;
114  }
115  }
116  }
117 
118  if (!$authenticated && $first_exception instanceof AuthenticationException) {
119  throw $first_exception;
120  }
121 
122  return $authenticated;
123  }
124 }
$handler
Definition: add.php:7
Authentication service handles registration of PAM handlers and calling of those handlers.
unregisterHandler($handler, string $policy='user')
Unregister an authentication handler.
registerHandler($handler, string $importance='sufficient', string $policy='user')
Register an authentication handler.
authenticate(string $policy, array $authentication_params=[])
Authenticate.
__construct(protected HandlersService $handlerService)
Create new service.
Thrown by the AuthenticationService.
Helpers for providing callable-based APIs.
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10