auth.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. {
  28. return $password; //prevents li3 from hashing the password before hand.
  29. },
  30. function($data) {
  31. if(isset($data['username']))
  32. {
  33. //Get the user from the database
  34. $user = User::find('first', array('conditions' => array('username' => $data['username'])));
  35. //Hash the submitted password with the stored salt.
  36. $data['password'] = Password::hash($data['password'], $user->salt);
  37. }
  38. return $data;
  39. }
  40. )
  41. )
  42. )
  43. );
  44. $secret = "cake";
  45. // Adds remember feature for form-based authentications.
  46. Auth::applyFilter('check', function($self, $params, $chain) use ($secret)
  47. {
  48. $query = 'first';
  49. $scope = array();
  50. extract($self::invokeMethod('_config', array($params['name'])));
  51. if ($result = $chain->next($self, $params, $chain))
  52. {
  53. $request = $params['credentials'];
  54. if ($request && $adapter == 'Form' && !empty($request->data['remember']))
  55. {
  56. $data = array_intersect_key($result, array_combine($fields, $fields));
  57. $data = serialize($data);
  58. Session::write(
  59. "Auth.{$params['name']}",
  60. base64_encode($data),
  61. array('name' => 'cookie')
  62. );
  63. }
  64. return $result;
  65. }
  66. if ($adapter == 'Form')
  67. {
  68. $data = Session::read("Auth.{$params['name']}", array('name' => 'cookie'));
  69. if ($data)
  70. {
  71. $data = base64_decode($data);
  72. $data = unserialize($data);
  73. if (array_keys($data) == $fields)
  74. {
  75. $model = Libraries::locate('models', $model);
  76. $data = array_map('strval', $data);
  77. $user = $model::$query($scope + $data);
  78. if ($user) {
  79. return $self::set($params['name'], $user->data());
  80. }
  81. }
  82. }
  83. }
  84. return $result;
  85. });
  86. // Removes remember cookie after sign out.
  87. Auth::applyFilter('clear', function($self, $params, $chain)
  88. {
  89. $config = $self::invokeMethod('_config', array($params['name']));
  90. if ($config['adapter'] == 'Form')
  91. {
  92. if (Session::read("Auth.{$params['name']}", array('name' => 'cookie')))
  93. {
  94. Session::delete("Auth.{$params['name']}", array('name' => 'cookie'));
  95. }
  96. }
  97. return $chain->next($self, $params, $chain);
  98. });
  99. //So that we can filter a bunch of methods in one
  100. Dispatcher::applyFilter('_callable', function($self, $params, $chain)
  101. {
  102. //Invoke the _callable method, then execute the logic below
  103. $ctrl = $chain->next($self, $params, $chain);
  104. //if the user is logged in
  105. $user = Auth::check('default');
  106. if($user)
  107. {
  108. //check if they are accessing an admin function
  109. if ($ctrl->request->controller == 'admin' && !($user['level'] == 'root' || $user['level'] == 'admin'))
  110. {
  111. return function() use ($request)
  112. {
  113. //Users / index isn't public derp.
  114. return new Response(compact('request') + array('location' => '/'));
  115. };
  116. }
  117. //If they aren't trying to access admin, return
  118. return $ctrl;
  119. }
  120. //If they are performing a public action continue,
  121. if (isset($ctrl->publicActions) && in_array($params['request']->action, $ctrl->publicActions))
  122. {
  123. return $ctrl;
  124. }
  125. //Save the location they are going to
  126. $value = array('controller' => $ctrl->request->controller, 'action' => $ctrl->request->action, 'args' => $ctrl->request->args);
  127. Session::write('url', $value);
  128. //Redirect them to the login page
  129. return function() use ($request)
  130. {
  131. return new Response(compact('request') + array('location' => '/login'));
  132. };
  133. });
  134. ?>