Elgg  Version 3.0
ElggPAM.php
Go to the documentation of this file.
1 <?php
2 
6 class ElggPAM {
7 
13  public static $_handlers = [];
14 
18  protected $policy;
19 
23  protected $messages;
24 
30  public function __construct($policy) {
31  $this->policy = $policy;
32  $this->messages = ['sufficient' => [], 'required' => []];
33  }
34 
50  public function authenticate($credentials = []) {
51  if (!isset(self::$_handlers[$this->policy]) ||
52  !is_array(self::$_handlers[$this->policy])) {
53  return false;
54  }
55 
56  $authenticated = false;
57 
58  foreach (self::$_handlers[$this->policy] as $v) {
59  $handler = $v->handler;
60  if (!is_callable($handler)) {
61  continue;
62  }
63  /* @var callable $handler */
64 
65  $importance = $v->importance;
66 
67  try {
68  // Execute the handler
69  // @todo don't assume $handler is a global function
70  $result = call_user_func($handler, $credentials);
71  if ($result) {
72  $authenticated = true;
73  } elseif ($result === false) {
74  if ($importance == 'required') {
75  $this->messages['required'][] = "$handler:failed";
76  return false;
77  } else {
78  $this->messages['sufficient'][] = "$handler:failed";
79  }
80  }
81  } catch (Exception $e) {
82  if ($importance == 'required') {
83  $this->messages['required'][] = $e->getMessage();
84  return false;
85  } else {
86  $this->messages['sufficient'][] = $e->getMessage();
87  }
88  }
89  }
90 
91  return $authenticated;
92  }
93 
99  public function getFailureMessage() {
100  $message = _elgg_services()->translator->translate('auth:nopams');
101  if (!empty($this->messages['required'])) {
102  $message = $this->messages['required'][0];
103  } elseif (!empty($this->messages['sufficient'])) {
104  $message = $this->messages['sufficient'][0];
105  }
106 
107  return _elgg_services()->hooks->trigger('fail', 'auth', $this->messages, $message);
108  }
109 }
__construct($policy)
constructor
Definition: ElggPAM.php:30
authenticate($credentials=[])
Authenticate a set of credentials against a policy This function will process all registered PAM hand...
Definition: ElggPAM.php:50
static $_handlers
Definition: ElggPAM.php:13
Pluggable Authentication Module.
Definition: ElggPAM.php:6
$messages
Definition: ElggPAM.php:23
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$handler
Definition: add.php:7
getFailureMessage()
Get a failure message to display to user.
Definition: ElggPAM.php:99