auth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. use lithium\storage\Session;
  3. use lithium\security\Auth;
  4. use lithium\util\String;
  5. use app\models\User;
  6. use lithium\core\Libraries;
  7. use lithium\action\Dispatcher;
  8. use lithium\net\http\Router;
  9. use lithium\action\Response;
  10. use lithium\security\Password;
  11. Session::config(array(
  12. 'cookie' => array('adapter' => 'Cookie'),
  13. 'default' => array('adapter' => 'Php'),
  14. 'flash_message' => array('adapter' => 'Php')
  15. ));
  16. Auth::config(array(
  17. 'default' => array(
  18. 'adapter' => 'Form',
  19. 'model' => 'User',
  20. 'cookie' => '',
  21. 'fields' => array('username', 'password'),
  22. //'scope' => array('active' => 'true'), //The active field must be true otherwise they can't auth, though we need
  23. //to eventually send them to a page that explains they are banned.
  24. 'session' => array('options' => array('name' => 'default')),
  25. 'filters' => array(
  26. 'password' => function($password) {
  27. return $password; //prevents li3 from hashing the password before hand.
  28. },
  29. function($data) {
  30. if(isset($data['username']))
  31. {
  32. //Gett the user from the database
  33. $user = User::find('first', array('conditions' => array('username' => $data['username'])));
  34. //Hash the submitted password with the stored salt.
  35. $data['password'] = Password::hash($data['password'], $user->salt);
  36. }
  37. return $data;
  38. })
  39. )
  40. ));
  41. $secret = "cake";
  42. // Adds remember feature for form-based authentications.
  43. Auth::applyFilter('check', function($self, $params, $chain) use ($secret) {
  44. $query = 'first';
  45. $scope = array();
  46. extract($self::invokeMethod('_config', array($params['name'])));
  47. if ($result = $chain->next($self, $params, $chain)) {
  48. $request = $params['credentials'];
  49. if ($request && $adapter == 'Form' && !empty($request->data['remember'])) {
  50. $data = array_intersect_key($result, array_combine($fields, $fields));
  51. $data = serialize($data);
  52. Session::write(
  53. "Auth.{$params['name']}",
  54. base64_encode($data),
  55. array('name' => 'cookie')
  56. );
  57. }
  58. return $result;
  59. }
  60. if ($adapter == 'Form') {
  61. $data = Session::read("Auth.{$params['name']}", array('name' => 'cookie'));
  62. if ($data) {
  63. $data = base64_decode($data);
  64. $data = unserialize($data);
  65. if (array_keys($data) == $fields) {
  66. $model = Libraries::locate('models', $model);
  67. $data = array_map('strval', $data);
  68. $user = $model::$query($scope + $data);
  69. if ($user) {
  70. return $self::set($params['name'], $user->data());
  71. }
  72. }
  73. }
  74. }
  75. return $result;
  76. });
  77. // Removes remember cookie after sign out.
  78. Auth::applyFilter('clear', function($self, $params, $chain) {
  79. $config = $self::invokeMethod('_config', array($params['name']));
  80. if ($config['adapter'] == 'Form') {
  81. if (Session::read("Auth.{$params['name']}", array('name' => 'cookie'))) {
  82. Session::delete("Auth.{$params['name']}", array('name' => 'cookie'));
  83. }
  84. }
  85. return $chain->next($self, $params, $chain);
  86. });
  87. //So that we can filter a bunch of methods in one
  88. Dispatcher::applyFilter('_callable', function($self, $params, $chain) {
  89. //Invoke the _callable method, then execute the logic below
  90. $ctrl = $chain->next($self, $params, $chain);
  91. //if the user is logged in
  92. $user = Auth::check('default');
  93. if($user)
  94. {
  95. //check if they are accessing an admin function
  96. if ($ctrl->request->controller == 'admin' && !($user['level'] == 'root' || $user['level'] == 'admin'))
  97. {
  98. return function() use ($request) {
  99. //Users / index isn't public derp.
  100. return new Response(compact('request') + array('location' => '/'));
  101. };
  102. }
  103. //If they aren't trying to access admin, return
  104. return $ctrl;
  105. }
  106. //If they are performing a public action continue,
  107. if (isset($ctrl->publicActions) && in_array($params['request']->action, $ctrl->publicActions)) {
  108. return $ctrl;
  109. }
  110. //Otherwise, send them to the login page
  111. return function() use ($request) {
  112. return new Response(compact('request') + array('location' => '/login'));
  113. };
  114. });
  115. ?>