Elgg  Version 6.3
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'): bool {
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  return true;
54  }
55 
64  public function unregisterHandler($handler, string $policy = 'user'): void {
65  $handler_string = $this->handlerService->describeCallable($handler);
66 
67  unset($this->handlers[$policy][$handler_string]);
68  }
69 
79  public function authenticate(string $policy, array $authentication_params = []): bool {
80  if (!isset($this->handlers[$policy])) {
81  return false;
82  }
83 
84  $authenticated = false;
85  $first_exception = null;
86 
87  foreach ($this->handlers[$policy] as $handler_string => $handler_config) {
88  $handler = $handler_config['handler'];
89  $importance = strtolower($handler_config['importance']);
90 
91  if (!$this->handlerService->isCallable($handler)) {
92  $this->getLogger()->warning("PAM handler '{$handler_string}' for policy '{$policy}' isn't callable");
93  continue;
94  }
95 
96  $callable = $this->handlerService->resolveCallable($handler);
97 
98  try {
99  $result = call_user_func($callable, $authentication_params);
100  if ($result === true) {
101  $authenticated = true;
102  } elseif ($result === false && $importance === 'required') {
103  return false;
104  }
105  } catch (\Exception $e) {
106  if (!$e instanceof AuthenticationException) {
107  $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e);
108  }
109 
110  if ($importance === 'required') {
111  throw $e;
112  }
113 
114  if (!isset($first_exception)) {
115  $first_exception = $e;
116  }
117  }
118  }
119 
120  if (!$authenticated && $first_exception instanceof AuthenticationException) {
121  throw $first_exception;
122  }
123 
124  return $authenticated;
125  }
126 }
$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