Auth.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?PHP
  2. /**
  3. *
  4. * This class is the central class for site-wide authentication.
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. class Staple_Auth
  25. {
  26. /**
  27. *
  28. * Holds the singleton instance for the Auth class
  29. * @var Staple_Auth
  30. */
  31. private static $instance;
  32. /**
  33. *
  34. * Holds the user identifier.
  35. * @var int | string
  36. */
  37. private $userid = NULL;
  38. /**
  39. *
  40. * Holds a reference to the AuthAdapter object.
  41. * @var Staple_AuthAdapter
  42. */
  43. private $adapter;
  44. /**
  45. *
  46. * Holds the configuration for this object.
  47. * @var array
  48. */
  49. private $_settings = array();
  50. /**
  51. *
  52. * Private variable holds the Auth status.
  53. * @var bool
  54. */
  55. private $authed = false;
  56. /**
  57. *
  58. * The level of authorization as set by the AuthAdapter for this session.
  59. * @var int
  60. */
  61. private $authLevel = 0;
  62. /**
  63. *
  64. * Status message from the Auth class.
  65. * @var string
  66. */
  67. protected $message = '';
  68. /**
  69. *
  70. * The private constructor ensures that the object is created as a Singleton. On initialization
  71. * the class checks for a configuration file. If none is found it throws an Exception. If a
  72. * config file is found, it parses the file and checks for the config keys required by this
  73. * class.
  74. *
  75. * @throws Exception
  76. */
  77. private function __construct()
  78. {
  79. $this->authed = false;
  80. if(file_exists(CONFIG_ROOT.'auth.ini'))
  81. {
  82. $curConfig = parse_ini_file(CONFIG_ROOT.'auth.ini');
  83. if($this->checkConfig($curConfig))
  84. {
  85. $this->_settings = $curConfig;
  86. }
  87. }
  88. elseif(file_exists(CONFIG_ROOT.'application.ini'))
  89. {
  90. $curConfig = parse_ini_file(CONFIG_ROOT.'application.ini',true);
  91. if($this->checkConfig($curConfig['auth']))
  92. {
  93. $this->_settings = $curConfig['auth'];
  94. }
  95. }
  96. else
  97. {
  98. throw new Exception('Authentication Module Failure', Staple_Error::AUTH_ERROR);
  99. }
  100. }
  101. /**
  102. * The destructor store the auth instance in the session.
  103. */
  104. public function __destruct()
  105. {
  106. $_SESSION['Staple']['auth'] = self::$instance;
  107. }
  108. /**
  109. *
  110. * Accepts the parsed configuration file and checks for configuration keys required by the
  111. * class. If a key is missing, it throws an Exception cancelling the execution of the script.
  112. *
  113. * @param array $conf
  114. * @throws Exception
  115. * @return bool
  116. */
  117. private function checkConfig(array $conf)
  118. {
  119. $keys = array('enabled','adapter','controller');
  120. foreach($keys as $keyval)
  121. {
  122. if(!array_key_exists($keyval, $conf))
  123. {
  124. throw new Exception('Authentication Module Configuration Error',Staple_Error::AUTH_ERROR);
  125. }
  126. }
  127. return true;
  128. }
  129. /**
  130. *
  131. * Gets the singleton instance of the object. Checks the session to see if a current auth
  132. * object already exists. If not a new Auth object is created.
  133. * @return Staple_Auth
  134. */
  135. public static function get()
  136. {
  137. if(!(self::$instance instanceof Staple_Auth))
  138. {
  139. if(array_key_exists('Staple', $_SESSION))
  140. if(array_key_exists('auth', $_SESSION['Staple']))
  141. self::$instance = $_SESSION['Staple']['auth'];
  142. if(!(self::$instance instanceof Staple_Auth))
  143. self::$instance = new Staple_Auth();
  144. }
  145. return self::$instance;
  146. }
  147. /**
  148. *
  149. * Returns a boolean representing authorization status. True for any level of authorization,
  150. * false for no authorization.
  151. * @return bool
  152. */
  153. public function isAuthed()
  154. {
  155. return $this->authed;
  156. }
  157. /**
  158. * Returns and integer representing the level of access. Defaults to 0 for no auth and 1
  159. * for general authorization. This is derived from information gathered by the AuthAdapter.
  160. * @return int
  161. */
  162. public function getAuthLevel()
  163. {
  164. return (int)$this->authLevel;
  165. }
  166. /**
  167. *
  168. * Returns the Auth ID
  169. * @return int | string
  170. */
  171. public function getAuthId()
  172. {
  173. return $this->userid;
  174. }
  175. /**
  176. *
  177. * Attempts authorization, accepting credentials and forwarding them to the AuthAdapter.
  178. * Throws and Exception if the AuthAdapter is not implemented from Staple_AuthAdapter.
  179. * Returns a boolean to signify if authorization succeeded.
  180. * @param array $credentials
  181. * @throws Exception
  182. * @return bool
  183. */
  184. public function doAuth(array $credentials)
  185. {
  186. //Make sure an adapter is loaded.
  187. if(!($this->adapter instanceof Staple_AuthAdapter))
  188. {
  189. $adapt = $this->_settings['adapter'];
  190. $this->adapter = new $adapt();
  191. if(!($this->adapter instanceof Staple_AuthAdapter))
  192. {
  193. throw new Exception('Invalid Authentication Adapter', Staple_Error::AUTH_ERROR);
  194. }
  195. }
  196. //Check Auth against the adapter
  197. if($this->adapter->getAuth($credentials) === true)
  198. {
  199. session_regenerate_id();
  200. $this->authed = true;
  201. $this->userid = $this->adapter->getUserId();
  202. $this->authLevel = $this->adapter->getLevel($this->userid);
  203. $this->message = "Authentication Successful";
  204. return true;
  205. }
  206. else
  207. {
  208. $this->authed = false;
  209. $this->userid = null;
  210. $this->authLevel = 0;
  211. $this->message = "Authentication Failed";
  212. }
  213. return false;
  214. }
  215. /**
  216. * In the event that authorization fails, this method is called by the framework. noAuth()
  217. * dispatches to the AuthController -> index action.
  218. */
  219. public function noAuth()
  220. {
  221. $this->dispatchAuthController();
  222. }
  223. /**
  224. *
  225. * General log out or clear credentials function.
  226. */
  227. public function clearAuth()
  228. {
  229. $this->userid = NULL;
  230. $this->authed = false;
  231. $this->authLevel = 0;
  232. $this->message = 'Logged Out';
  233. }
  234. /**
  235. *
  236. * Returns the Auth message from the class
  237. * @return string
  238. */
  239. public function getMessage()
  240. {
  241. return $this->message;
  242. }
  243. /**
  244. *
  245. * Dispatches to the AuthController -> index action. Throws an Exception if the controller does
  246. * not extend Staple_AuthController.
  247. * @throws Exception
  248. */
  249. private function dispatchAuthController()
  250. {
  251. $conString = $this->_settings['controller'];
  252. $class = substr($conString, 0, strlen($conString)-10);
  253. $authCon = Staple_Main::getController($class);
  254. if(!($authCon instanceof Staple_AuthController))
  255. {
  256. $authCon = new $conString();
  257. }
  258. if($authCon instanceof Staple_AuthController)
  259. {
  260. //Start the Controller
  261. $authCon->_start();
  262. //Register Auth Controller with the Front Controller
  263. Staple_Main::get()->registerController($authCon);
  264. //Set the view's controller to match the route
  265. $authCon->view->setController($class);
  266. //Set the view's action to match the route
  267. $authCon->view->setView('index');
  268. //Call the controller action, Send the route requested to the action
  269. //@todo Add option to customize the controller action
  270. call_user_func_array(array($authCon,'index'), array(Staple_Main::getRoute()));
  271. //Grab the buffer contents from the controller and post it after the header.
  272. $buffer = ob_get_contents();
  273. ob_clean();
  274. //Process the header
  275. Staple_Main::get()->processHeader();
  276. if($authCon->layout instanceof Staple_Layout)
  277. {
  278. $authCon->layout->build($buffer);
  279. }
  280. else
  281. {
  282. echo $buffer;
  283. $authCon->view->build();
  284. }
  285. }
  286. else
  287. {
  288. throw new Exception('Fatal Error connecting to Auth Controller', Staple_Error::AUTH_ERROR);
  289. }
  290. }
  291. }