auth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. 'validators' => array(
  26. 'password' => function($form, $data) {
  27. return password::check($form, $data);
  28. })
  29. )
  30. );
  31. $secret = "cake";
  32. // Adds remember feature for form-based authentications.
  33. Auth::applyFilter('check', function($self, $params, $chain) use ($secret)
  34. {
  35. $query = 'first';
  36. $scope = array();
  37. extract($self::invokeMethod('_config', array($params['name'])));
  38. if ($result = $chain->next($self, $params, $chain))
  39. {
  40. $request = $params['credentials'];
  41. if ($request && $adapter == 'Form' && !empty($request->data['remember']))
  42. {
  43. $data = array_intersect_key($result, array_combine($fields, $fields));
  44. $data = serialize($data);
  45. Session::write(
  46. "Auth.{$params['name']}",
  47. base64_encode($data),
  48. array('name' => 'cookie')
  49. );
  50. }
  51. return $result;
  52. }
  53. if ($adapter == 'Form')
  54. {
  55. $data = Session::read("Auth.{$params['name']}", array('name' => 'cookie'));
  56. if ($data)
  57. {
  58. $data = base64_decode($data);
  59. $data = unserialize($data);
  60. if (array_keys($data) == $fields)
  61. {
  62. $model = Libraries::locate('models', $model);
  63. $data = array_map('strval', $data);
  64. $user = $model::$query($scope + $data);
  65. if ($user) {
  66. return $self::set($params['name'], $user->data());
  67. }
  68. }
  69. }
  70. }
  71. return $result;
  72. });
  73. // Removes remember cookie after sign out.
  74. Auth::applyFilter('clear', function($self, $params, $chain)
  75. {
  76. $config = $self::invokeMethod('_config', array($params['name']));
  77. if ($config['adapter'] == 'Form')
  78. {
  79. if (Session::read("Auth.{$params['name']}", array('name' => 'cookie')))
  80. {
  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. {
  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. {
  100. //Users / index isn't public derp.
  101. return new Response(compact('request') + array('location' => '/'));
  102. };
  103. }
  104. //If they aren't trying to access admin, return
  105. return $ctrl;
  106. }
  107. //If they are performing a public action continue,
  108. if (isset($ctrl->publicActions) && in_array($params['request']->action, $ctrl->publicActions))
  109. {
  110. return $ctrl;
  111. }
  112. //Save the location they are going to
  113. $value = array('controller' => $ctrl->request->controller, 'action' => $ctrl->request->action, 'args' => $ctrl->request->args);
  114. Session::write('url', $value);
  115. //Redirect them to the login page
  116. return function() use ($request)
  117. {
  118. return new Response(compact('request') + array('location' => '/login'));
  119. };
  120. });
  121. ?>