auth.php 9.8 KB

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