auth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. $secret = "cake";
  31. // Adds remember feature for form-based authentications.
  32. Auth::applyFilter('check', function($self, $params, $chain) use ($secret)
  33. {
  34. $query = 'first';
  35. $scope = array();
  36. extract($self::invokeMethod('_config', array($params['name'])));
  37. if ($result = $chain->next($self, $params, $chain))
  38. {
  39. $request = $params['credentials'];
  40. if ($request && $adapter == 'Form' && !empty($request->data['remember']))
  41. {
  42. $data = array_intersect_key($result, array_combine($fields, $fields));
  43. $data = serialize($data);
  44. Session::write(
  45. "Auth.{$params['name']}",
  46. base64_encode($data),
  47. array('name' => 'cookie')
  48. );
  49. }
  50. return $result;
  51. }
  52. if ($adapter == 'Form')
  53. {
  54. $data = Session::read("Auth.{$params['name']}", array('name' => 'cookie'));
  55. if ($data)
  56. {
  57. $data = base64_decode($data);
  58. $data = unserialize($data);
  59. if (array_keys($data) == $fields)
  60. {
  61. $model = Libraries::locate('models', $model);
  62. $data = array_map('strval', $data);
  63. $user = $model::$query($scope + $data);
  64. if ($user) {
  65. return $self::set($params['name'], $user->data());
  66. }
  67. }
  68. }
  69. }
  70. return $result;
  71. });
  72. // Removes remember cookie after sign out.
  73. Auth::applyFilter('clear', function($self, $params, $chain)
  74. {
  75. $config = $self::invokeMethod('_config', array($params['name']));
  76. if ($config['adapter'] == 'Form')
  77. {
  78. if (Session::read("Auth.{$params['name']}", array('name' => 'cookie')))
  79. {
  80. Session::delete("Auth.{$params['name']}", array('name' => 'cookie'));
  81. }
  82. }
  83. return $chain->next($self, $params, $chain);
  84. });
  85. //So that we can filter a bunch of methods in one
  86. Dispatcher::applyFilter('_callable', function($self, $params, $chain)
  87. {
  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. {
  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. {
  109. return $ctrl;
  110. }
  111. //Save the location they are going to
  112. $value = array('controller' => $ctrl->request->controller, 'action' => $ctrl->request->action, 'args' => $ctrl->request->args);
  113. Session::write('url', $value);
  114. //Redirect them to the login page
  115. return function() use ($request)
  116. {
  117. return new Response(compact('request') + array('location' => '/login'));
  118. };
  119. });
  120. ?>