PageRenderTime 19ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/auth.php

https://github.com/DregondRahl/auth
PHP | 429 lines | 251 code | 50 blank | 128 comment | 40 complexity | f2a60b41106f549cb8e6ad4ad40eeb32 MD5 | raw file
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Auth;
  13. class AuthException extends \Fuel_Exception {}
  14. /**
  15. * Auth
  16. *
  17. * @package Fuel
  18. * @subpackage Auth
  19. */
  20. class Auth {
  21. /**
  22. * @var Auth_Login_Driver
  23. */
  24. protected static $_instance = null;
  25. /**
  26. * @var Array contains references if multiple were loaded
  27. */
  28. protected static $_instances = array();
  29. /**
  30. * @var Array Login drivers that verified a current login
  31. */
  32. protected static $_verified = array();
  33. /**
  34. * @var bool Whether to verify multiple
  35. */
  36. protected static $_verify_multiple = false;
  37. /**
  38. * @var Array subdriver registry, takes driver name and method for checking it
  39. */
  40. protected static $_drivers = array(
  41. 'group' => 'member',
  42. 'acl' => 'has_access',
  43. );
  44. public static function _init()
  45. {
  46. \Config::load('auth', true);
  47. // Whether to allow multiple drivers of any type, defaults to not allowed
  48. static::$_verify_multiple = \Config::get('auth.verify_multiple_logins', false);
  49. foreach((array) \Config::get('auth.driver', array()) as $driver => $config)
  50. {
  51. $config = is_int($driver)
  52. ? array('driver' => $config)
  53. : array_merge($config, array('driver' => $driver));
  54. static::factory($config);
  55. }
  56. // set the first (or only) as the default instance for static usage
  57. if ( ! empty(static::$_instances))
  58. {
  59. static::$_instance = reset(static::$_instances);
  60. static::check();
  61. }
  62. }
  63. /**
  64. * Load a login driver to the array of loaded drivers
  65. *
  66. * @param Array settings for the new driver
  67. * @throws AuthException on driver load failure
  68. */
  69. public static function factory($custom = array())
  70. {
  71. // Driver is given as array key or just string in custom
  72. $custom = ! is_array($custom) ? array('driver' => $custom) : $custom;
  73. $config = \Config::get('auth.'.$custom['driver'].'_config', array());
  74. $config = array_merge($config, $custom);
  75. // Driver must be set
  76. if (empty($config['driver']) || ! is_string($config['driver']))
  77. {
  78. throw new \AuthException('No auth driver given.');
  79. }
  80. // determine the driver to load
  81. $driver = \Auth_Login_Driver::factory($config);
  82. // get the driver's cookie name
  83. $id = $driver->get_id();
  84. // do we already have a driver instance for this cookie?
  85. if (isset(static::$_instances[$id]))
  86. {
  87. // if so, they must be using the same driver class!
  88. $class = get_class($driver);
  89. if ( ! static::$_instances[$id] instanceof $class)
  90. {
  91. throw new \AuthException('You can not instantiate two different login drivers using the same id "'.$id.'"');
  92. }
  93. }
  94. else
  95. {
  96. // store this instance
  97. static::$_instances[$id] = $driver;
  98. }
  99. return static::$_instances[$id];
  100. }
  101. /**
  102. * Prevent instantiation
  103. */
  104. final private function __construct() {}
  105. /**
  106. * Remove individual driver, or all drivers of $type
  107. *
  108. * @param string driver id or null for default driver
  109. * @throws AuthException when $driver_id isn't valid or true
  110. */
  111. public static function unload($driver_id = null)
  112. {
  113. if ($driver_id === null && ! empty(static::$_instance))
  114. {
  115. unset(static::$_instances[static::$_instance->get_id()]);
  116. static::$_instance = null;
  117. return true;
  118. }
  119. elseif (array_key_exists($driver_id, static::$_instances))
  120. {
  121. return false;
  122. }
  123. unset(static::$_instances[$driver_id]);
  124. return true;
  125. }
  126. /**
  127. * Return a specific driver, or the default instance (is created if necessary)
  128. *
  129. * @param string driver id
  130. * @return Auth_Login_Driver
  131. */
  132. public static function instance($instance = null)
  133. {
  134. if ($instance !== null)
  135. {
  136. if ( ! array_key_exists($instance, static::$_instances))
  137. {
  138. return false;
  139. }
  140. return static::$_instances[$instance];
  141. }
  142. if (static::$_instance === null)
  143. {
  144. static::$_instance = static::factory();
  145. }
  146. return static::$_instance;
  147. }
  148. /**
  149. * Check login drivers for validated login
  150. *
  151. * @param string|Array specific driver or drivers, in this case it will always terminate after first success
  152. * @return bool
  153. */
  154. public static function check($specific = null)
  155. {
  156. $drivers = $specific === null ? static::$_instances : (array) $specific;
  157. foreach ($drivers as $i)
  158. {
  159. if ( ! static::$_verify_multiple && ! empty(static::$_verified))
  160. {
  161. return true;
  162. }
  163. $i = $i instanceof Auth_Login_Driver ? $i : static::instance($i);
  164. if ( ! array_key_exists($i->get_id(), static::$_verified))
  165. {
  166. $i->check();
  167. }
  168. if ($specific)
  169. {
  170. if (array_key_exists($i->get_id(), static::$_verified))
  171. {
  172. return true;
  173. }
  174. }
  175. }
  176. return $specific === null && ! empty(static::$_verified);
  177. }
  178. /**
  179. * Get verified driver or all verified drivers
  180. * returns false when specific driver has not validated
  181. * when all were requested and none validated an empty array is returned
  182. *
  183. * @param null|string driver id or null for all verified driver in an array
  184. * @return Array|Auth_Login_Driver|false
  185. */
  186. public static function verified($driver = null)
  187. {
  188. if ($driver === null)
  189. {
  190. return static::$_verified;
  191. }
  192. if ( ! array_key_exists($driver, static::$_verified))
  193. {
  194. return false;
  195. }
  196. return static::$_verified[$driver];
  197. }
  198. /**
  199. * Logs out all current logged in drivers
  200. */
  201. public static function logout()
  202. {
  203. foreach (static::$_verified as $v)
  204. {
  205. $v->logout();
  206. }
  207. static::$_verified = array();
  208. }
  209. /**
  210. * Register verified Login driver
  211. *
  212. * @param Auth_Login_Driver
  213. */
  214. public static function _register_verified(Auth_Login_Driver $driver)
  215. {
  216. static::$_verified[$driver->get_id()] = $driver;
  217. }
  218. /**
  219. * Unregister verified Login driver
  220. *
  221. * @param Auth_Login_Driver
  222. */
  223. public static function _unregister_verified(Auth_Login_Driver $driver)
  224. {
  225. unset(static::$_verified[$driver->get_id()]);
  226. }
  227. /**
  228. * Register a new driver type
  229. *
  230. * @param string name of the driver type, may not conflict with class method name
  231. * @param string name of the method to use for checking this type of driver, also cannot conflict with method
  232. * @return bool
  233. */
  234. public static function register_driver_type($type, $check_method)
  235. {
  236. $driver_exists = ! is_string($type)
  237. || array_key_exists($type, static::$_drivers)
  238. || method_exists(get_called_class(), $check_method)
  239. || in_array($type, array('login', 'group', 'acl'));
  240. $method_exists = ! is_string($type)
  241. || array_search($check_method, static::$_drivers)
  242. || method_exists(get_called_class(), $type);
  243. if ($driver_exists && static::$_drivers[$type] == $check_method)
  244. {
  245. return true;
  246. }
  247. if ($driver_exists || $method_exists)
  248. {
  249. \Error::notice('Cannot add driver type, its name conflicts with another driver or method.');
  250. return false;
  251. }
  252. static::$_drivers[$type] = $check_method;
  253. return true;
  254. }
  255. /**
  256. * Unregister a driver type
  257. *
  258. * @param string name of the driver type
  259. * @return bool
  260. */
  261. public static function unregister_driver_type($type)
  262. {
  263. if (in_array('login', 'group', 'acl'))
  264. {
  265. \Error::notice('Cannot remove driver type, included drivers login, group and acl cannot be removed.');
  266. return false;
  267. }
  268. unset(static::$_drivers[$type]);
  269. return true;
  270. }
  271. /**
  272. * Magic method used to retrieve driver instances and check them for validity
  273. *
  274. * @param string
  275. * @param array
  276. * @return mixed
  277. * @throws BadMethodCallException
  278. */
  279. public static function __callStatic($method, $args)
  280. {
  281. $args = array_pad($args, 3, null);
  282. if (array_key_exists($method, static::$_drivers))
  283. {
  284. return static::_driver_instance($method, $args[0]);
  285. }
  286. if ($type = array_search($method, static::$_drivers))
  287. {
  288. return static::_driver_check($type, $args[0], $args[1], @$args[2]);
  289. }
  290. if (static::$_verify_multiple !== true and method_exists(static::$_instance, $method))
  291. {
  292. return call_user_func_array(array(static::$_instance, $method), $args);
  293. }
  294. throw new \BadMethodCallException('Invalid method.');
  295. }
  296. /**
  297. * Retrieve a loaded driver instance
  298. * (loading must be done by other driver class)
  299. *
  300. * @param string driver type
  301. * @param string|true driver id or true for an array of all loaded drivers
  302. * @return Auth_Driver|array
  303. */
  304. protected static function _driver_instance($type, $instance)
  305. {
  306. $class = 'Auth_'.ucfirst($type).'_Driver';
  307. return $class::instance($instance);
  308. }
  309. /**
  310. * Check driver
  311. *
  312. * @param string driver type
  313. * @param mixed condition for which the driver is checked
  314. * @param string driver id or null to check all
  315. * @param Array identifier to check, should default to current user or relation therof and be
  316. * in the form of array(driver_id, user_id)
  317. * @return bool
  318. */
  319. public static function _driver_check($type, $condition, $driver = null, $entity = null)
  320. {
  321. $method = static::$_drivers[$type];
  322. if ($driver === null)
  323. {
  324. if ($entity === null)
  325. {
  326. if ( ! empty(static::$_verified))
  327. {
  328. foreach (static::$_verified as $v)
  329. {
  330. if ($v->$method($condition))
  331. {
  332. return true;
  333. }
  334. }
  335. }
  336. else
  337. {
  338. foreach (static::$_instances as $i)
  339. {
  340. if ($i->guest_login() and $i->$method($condition))
  341. {
  342. return true;
  343. }
  344. }
  345. }
  346. }
  347. else
  348. {
  349. foreach (static::$_instances as $i)
  350. {
  351. if ($i->$method($condition, null, $entity))
  352. {
  353. return true;
  354. }
  355. }
  356. }
  357. return false;
  358. }
  359. else
  360. {
  361. if ($entity === null)
  362. {
  363. foreach (static::$_verified as $v)
  364. {
  365. if (static::$type($driver)->$method($condition))
  366. {
  367. return true;
  368. }
  369. }
  370. }
  371. elseif (static::$type($driver)->$method($condition, $entity))
  372. {
  373. return true;
  374. }
  375. return false;
  376. }
  377. }
  378. }
  379. /* end of file auth.php */