PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/category_tool/fuel/packages/auth/classes/auth/login/driver.php

https://github.com/connvoi/dev
PHP | 253 lines | 109 code | 36 blank | 108 comment | 8 complexity | ab286bfdd87b76b5ac8e128c124d8aa7 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  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. abstract class Auth_Login_Driver extends \Auth_Driver
  14. {
  15. /**
  16. * @var Auth_Driver
  17. */
  18. protected static $_instance = null;
  19. /**
  20. * @var array contains references if multiple were loaded
  21. */
  22. protected static $_instances = array();
  23. public static function forge(array $config = array())
  24. {
  25. // default driver id to driver name when not given
  26. ! array_key_exists('id', $config) && $config['id'] = $config['driver'];
  27. $class = \Inflector::get_namespace($config['driver']).'Auth_Login_'.ucfirst(\Inflector::denamespace($config['driver']));
  28. $driver = new $class($config);
  29. static::$_instances[$driver->get_id()] = $driver;
  30. is_null(static::$_instance) and static::$_instance = $driver;
  31. foreach ($driver->get_config('drivers', array()) as $type => $drivers)
  32. {
  33. foreach ($drivers as $d => $custom)
  34. {
  35. $custom = is_int($d)
  36. ? array('driver' => $custom)
  37. : array_merge($custom, array('driver' => $d));
  38. $class = 'Auth_'.ucfirst($type).'_Driver';
  39. $class::forge($custom);
  40. }
  41. }
  42. return $driver;
  43. }
  44. // ------------------------------------------------------------------------
  45. /**
  46. * @var array config values
  47. */
  48. protected $config = array();
  49. /**
  50. * @var object PHPSecLib hash object
  51. */
  52. private $hasher = null;
  53. /**
  54. * Check for login
  55. * (final method to (un)register verification, work is done by _check())
  56. *
  57. * @return bool
  58. */
  59. final public function check()
  60. {
  61. if ( ! $this->perform_check())
  62. {
  63. \Auth::_unregister_verified($this);
  64. return false;
  65. }
  66. \Auth::_register_verified($this);
  67. return true;
  68. }
  69. /**
  70. * Return user info in an array, always includes email & screen_name
  71. * Additional fields can be requested in the first param or set in config,
  72. * all additional fields must have their own method "get_" + fieldname
  73. *
  74. * @param array additional fields
  75. * @return array
  76. */
  77. final public function get_user_array(Array $additional_fields = array())
  78. {
  79. $user = array(
  80. 'email' => $this->get_email(),
  81. 'screen_name' => $this->get_screen_name(),
  82. 'groups' => $this->get_groups(),
  83. );
  84. $additional_fields = array_merge($this->config['additional_fields'], $additional_fields);
  85. foreach($additional_fields as $af)
  86. {
  87. // only works if it actually can be fetched through a get_ method
  88. if (is_callable(array($this, $method = 'get_'.$af)))
  89. {
  90. $user[$af] = $this->$method();
  91. }
  92. }
  93. return $user;
  94. }
  95. /**
  96. * Verify Group membership
  97. *
  98. * @param mixed group identifier to check for membership
  99. * @param string group driver id or null to check all
  100. * @param array user identifier to check in form array(driver_id, user_id)
  101. * @return bool
  102. */
  103. public function member($group, $driver = null, $user = null)
  104. {
  105. $user = $user ?: $this->get_user_id();
  106. if ($driver === null)
  107. {
  108. foreach (\Auth::group(true) as $g)
  109. {
  110. if ($g->member($group, $user))
  111. {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. return \Auth::group($driver)->member($group, $user);
  118. }
  119. /**
  120. * Verify Acl access
  121. *
  122. * @param mixed condition to validate
  123. * @param string acl driver id or null to check all
  124. * @param array user identifier to check in form array(driver_id, user_id)
  125. * @return bool
  126. */
  127. public function has_access($condition, $driver = null, $entity = null)
  128. {
  129. $entity = $entity ?: $this->get_user_id();
  130. if ($driver === null)
  131. {
  132. foreach (\Auth::acl(true) as $acl)
  133. {
  134. if ($acl->has_access($condition, $entity))
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. return \Auth::acl($driver)->has_access($condition, $entity);
  142. }
  143. /**
  144. * Default password hash method
  145. *
  146. * @param string
  147. * @return string
  148. */
  149. public function hash_password($password)
  150. {
  151. return base64_encode($this->hasher()->pbkdf2($password, \Config::get('auth.salt'), 10000, 32));
  152. }
  153. /**
  154. * Returns the hash object and creates it if necessary
  155. *
  156. * @return PHPSecLib\Crypt_Hash
  157. */
  158. public function hasher()
  159. {
  160. if ( ! class_exists('PHPSecLib\\Crypt_Hash', false))
  161. {
  162. import('phpseclib/Crypt/Hash', 'vendor');
  163. }
  164. is_null($this->hasher) and $this->hasher = new \PHPSecLib\Crypt_Hash();
  165. return $this->hasher;
  166. }
  167. // ------------------------------------------------------------------------
  168. /**
  169. * Perform the actual login check
  170. *
  171. * @return bool
  172. */
  173. abstract protected function perform_check();
  174. /**
  175. * Perform the actual login check
  176. *
  177. * @return bool
  178. */
  179. abstract public function validate_user();
  180. /**
  181. * Login method
  182. *
  183. * @return bool whether login succeeded
  184. */
  185. abstract public function login();
  186. /**
  187. * Logout method
  188. */
  189. abstract public function logout();
  190. /**
  191. * Get User Identifier of the current logged in user
  192. * in the form: array(driver_id, user_id)
  193. *
  194. * @return array
  195. */
  196. abstract public function get_user_id();
  197. /**
  198. * Get User Groups of the current logged in user
  199. * in the form: array(array(driver_id, group_id), array(driver_id, group_id), etc)
  200. *
  201. * @return array
  202. */
  203. abstract public function get_groups();
  204. /**
  205. * Get emailaddress of the current logged in user
  206. *
  207. * @return string
  208. */
  209. abstract public function get_email();
  210. /**
  211. * Get screen name of the current logged in user
  212. *
  213. * @return string
  214. */
  215. abstract public function get_screen_name();
  216. }
  217. /* end of file driver.php */