auth.php 4.2 KB

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