Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
ElggPAM.php
Go to the documentation of this file.
1 <?php
8 class ElggPAM {
12  protected $policy;
13 
17  protected $messages;
18 
24  public function __construct($policy) {
25  $this->policy = $policy;
26  $this->messages = array('sufficient' => array(), 'required' => array());
27  }
28 
44  public function authenticate($credentials = array()) {
45  global $_PAM_HANDLERS;
46 
47  if (!isset($_PAM_HANDLERS[$this->policy]) ||
48  !is_array($_PAM_HANDLERS[$this->policy])) {
49  return false;
50  }
51 
52  $authenticated = false;
53 
54  foreach ($_PAM_HANDLERS[$this->policy] as $v) {
55  $handler = $v->handler;
56  if (!is_callable($handler)) {
57  continue;
58  }
59  /* @var callable $handler */
60 
61  $importance = $v->importance;
62 
63  try {
64  // Execute the handler
65  // @todo don't assume $handler is a global function
66  $result = call_user_func($handler, $credentials);
67  if ($result) {
68  $authenticated = true;
69  } elseif ($result === false) {
70  if ($importance == 'required') {
71  $this->messages['required'][] = "$handler:failed";
72  return false;
73  } else {
74  $this->messages['sufficient'][] = "$handler:failed";
75  }
76  }
77  } catch (Exception $e) {
78  if ($importance == 'required') {
79  $this->messages['required'][] = $e->getMessage();
80  return false;
81  } else {
82  $this->messages['sufficient'][] = $e->getMessage();
83  }
84  }
85  }
86 
87  return $authenticated;
88  }
89 
95  public function getFailureMessage() {
96  $message = _elgg_services()->translator->translate('auth:nopams');
97  if (!empty($this->messages['required'])) {
98  $message = $this->messages['required'][0];
99  } elseif (!empty($this->messages['sufficient'])) {
100  $message = $this->messages['sufficient'][0];
101  }
102 
103  return _elgg_services()->hooks->trigger('fail', 'auth', $this->messages, $message);
104  }
105 }
__construct($policy)
constructor
Definition: ElggPAM.php:24
global $_PAM_HANDLERS
Definition: pam.php:23
$e
Definition: metadata.php:12
$messages
Definition: ElggPAM.php:17
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
authenticate($credentials=array())
Authenticate a set of credentials against a policy This function will process all registered PAM hand...
Definition: ElggPAM.php:44
$handler
Definition: add.php:10
getFailureMessage()
Get a failure message to display to user.
Definition: ElggPAM.php:95