Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Authenticator.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Authenticator class
8  *
9  * Top-level class to authenticate and authorize users
10  *
11  * @author Anakeen
12  * @version $Id: Class.Authenticator.php,v 1.6 2009/01/16 13:33:00 jerome Exp $
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
19 abstract class Authenticator
20 {
21  /* Authentication success */
22  const AUTH_OK = 0;
23  /* Authentication failed */
24  const AUTH_NOK = 1;
25  /* Authentication status cannot be determined, and credentials should be asked */
26  const AUTH_ASK = 2;
27 
28  const nullProvider = "__for_logout__";
29  /**
30  * @var Provider
31  */
32  public $provider = null;
33 
34  public function __construct($authtype, $authprovider)
35  {
36 
37  include_once ('WHAT/Lib.Common.php');
38 
39  if ($authtype == "") throw new Dcp\Exception(__METHOD__ . " " . "Error: authentication mode not set");
40  if ($authprovider == "") throw new Dcp\Exception(__METHOD__ . " " . "Error: authentication provider not set");
41 
42  $tx = array(
43  'type' => $authtype,
44  'provider' => $authprovider
45  );
47  if ($authprovider != self::nullProvider) {
48  $tp = self::getAuthParam($authprovider);
49  $this->parms = array_merge($tx, $ta, $tp);
50 
51  if (!array_key_exists('provider', $this->parms)) {
52  throw new Dcp\Exception(__METHOD__ . " " . "Error: provider parm not specified at __construct");
53  }
54  $providerClass = $this->parms{'provider'} . 'Provider';
55 
56  $classFile = 'WHAT/Class.' . $providerClass . '.php';
57  $ret = file_exists($classFile);
58  if ($ret === FALSE) {
59  throw new Dcp\Exception(__METHOD__ . " " . "Error: ." . $classFile . " not found");
60  }
61  include_once ($classFile);
62  if (!class_exists($providerClass)) {
63  throw new Dcp\Exception(__METHOD__ . " " . "Error: " . $providerClass . " class not found");
64  }
65  // error_log("Using authentication provider [".$providerClass."]");
66  $this->provider = new $providerClass($authprovider, $this->parms);
67  if (!is_a($this->provider, 'Provider')) {
68  throw new Dcp\Exception(__METHOD__ . " " . sprintf("Error: provider with class '%s' does not inherits from class 'Provider'.", $providerClass));
69  }
70  } else {
71  $this->parms = array_merge($tx, $ta);
72  }
73  }
74  public static function getAuthParam($provider = "")
75  {
76  if ($provider == "") return array();
77  $freedom_providers = getDbAccessValue('freedom_providers');
78  if (!is_array($freedom_providers)) {
79  return array();
80  }
81 
82  if (!array_key_exists($provider, $freedom_providers)) {
83  error_log(__FUNCTION__ . ":" . __LINE__ . "provider " . $provider . " does not exists in freedom_providers");
84  return array();
85  }
86 
87  return $freedom_providers[$provider];
88  }
89 
90  public static function getAuthTypeParams()
91  {
92  $freedom_authtypeparams = getDbAccessValue('freedom_authtypeparams');
93  if (!is_array($freedom_authtypeparams)) {
94  throw new Dcp\Exception('FILE0006');
95  }
96 
97  if (!array_key_exists(AuthenticatorManager::getAuthType() , $freedom_authtypeparams)) {
98  return array();
99  }
100 
101  return $freedom_authtypeparams[AuthenticatorManager::getAuthType() ];
102  }
103 
104  public static function freedomUserExists($username)
105  {
106  include_once ('FDL/Class.Doc.php');
107  include_once ('WHAT/Class.User.php');
108 
109  $u = new Account();
110  if ($u->SetLoginName($username)) {
112  $du = new_Doc($dbaccess, $u->fid);
113  if ($du->isAlive()) {
114  return TRUE;
115  }
116  }
117  return FALSE;
118  }
119 
120  public function tryInitializeUser($username)
121  {
122  if (!$this->provider->canICreateUser()) {
123  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . sprintf("Authentication failed for user '%s' because auto-creation is disabled for provider '%s'!", $username, $this->provider->pname));
124  return FALSE;
125  }
126  $err = $this->provider->initializeUser($username);
127  if ($err != "") {
128  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . sprintf("Error creating user '%s' err=[%s]", $username, $err));
129  return FALSE;
130  }
131  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . sprintf("Initialized user '%s'!", $username));
132  return TRUE;
133  }
134 
135  public function getProviderErrno()
136  {
137  if ($this->provider) {
138  return $this->provider->errno;
139  }
140  return 0;
141  }
142 
143  public function getAuthApp()
144  {
145  if (isset($this->parms['auth']['app'])) {
146  return $this->parms['auth']['app'];
147  }
148  return false;
149  }
150 
151  abstract function checkAuthentication();
152  abstract function checkAuthorization($opt);
153  abstract function askAuthentication($args);
154  abstract function getAuthUser();
155  abstract function getAuthPw();
156  abstract function logout($redir_uri = '');
157  abstract function setSessionVar($name, $value);
158  abstract function getSessionVar($name);
159 }
if(substr($wsh, 0, 1)!= '/') $args
getDbAccessValue($varName)
Definition: Lib.Common.php:425
askAuthentication($args)
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
$ret
static getAuthParam($provider="")
__construct($authtype, $authprovider)
tryInitializeUser($username)
getSessionVar($name)
getAuthParam($freedomctx="", $provider="")
Definition: Lib.Common.php:582
setSessionVar($name, $value)
getAuthTypeParams($freedomctx="")
Definition: Lib.Common.php:575
static getAuthTypeParams()
getDbAccess()
Definition: Lib.Common.php:368
new_Doc($dbaccess, $id= '', $latest=false)
logout($redir_uri= '')
$dbaccess
Definition: checkVault.php:17
static freedomUserExists($username)
checkAuthorization($opt)
if($file) if($subject==""&&$file) if($subject=="") $err
$value
← centre documentaire © anakeen