PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/_app/core/auth.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 151 lines | 57 code | 17 blank | 77 comment | 6 complexity | f53f2c011aa125d849df86ea36dcf683 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * Statamic_Auth
  4. * Handles user authentication within Statamic
  5. *
  6. * @author Mubashar Iqbal
  7. * @author Jack McDade
  8. * @author Fred LeBlanc
  9. * @copyright 2012 Statamic
  10. * @link http://www.statamic.com
  11. * @license http://www.statamic.com
  12. */
  13. class statamic_auth
  14. {
  15. /**
  16. * login
  17. * Attempts to log in a user
  18. *
  19. * @param string $username Username of the user
  20. * @param string $password Password of the user
  21. * @param boolean $remember Remember this user later?
  22. * @return boolean
  23. */
  24. public static function login($username, $password, $remember=false)
  25. {
  26. $u = self::get_user($username);
  27. if ($u && $u->correct_password($password)) {
  28. $app = \Slim\Slim::getInstance();
  29. $hash = $username.":".md5($u->get_hashed_password().$app->config['_cookies.secret_key']);
  30. $expire = $app->config['_cookies.lifetime'];
  31. $app->setEncryptedCookie('stat_auth_cookie', $hash, $expire);
  32. return true;
  33. }
  34. return false;
  35. }
  36. /**
  37. * logout
  38. * Logs a user out
  39. *
  40. * @return void
  41. */
  42. public static function logout()
  43. {
  44. $app = \Slim\Slim::getInstance();
  45. $cookie = $app->deleteCookie('stat_auth_cookie');
  46. }
  47. /**
  48. * user_exists
  49. * Determines if a given $username exists
  50. *
  51. * @param string $username Username to check for existence
  52. * @return boolean
  53. */
  54. public static function user_exists($username)
  55. {
  56. return !(self::get_user($username) == null);
  57. }
  58. /**
  59. * is_logged_in
  60. * Checks to see if the current session is logged in
  61. *
  62. * @return mixed
  63. */
  64. public static function is_logged_in()
  65. {
  66. $user = null;
  67. $app = \Slim\Slim::getInstance();
  68. $cookie = $app->getEncryptedCookie('stat_auth_cookie');
  69. if ($cookie) {
  70. list($username, $hash) = explode(":", $cookie);
  71. $user = self::get_user($username);
  72. if ($user) {
  73. $hash = $username.":".md5($user->get_hashed_password().$app->config['_cookies.secret_key']);
  74. if ($cookie === $hash) {
  75. # validated
  76. $expire = $app->config['_cookies.lifetime'];
  77. $app->setEncryptedCookie('stat_auth_cookie', $cookie, $expire);
  78. return $user;
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84. /**
  85. * get_user
  86. * Gets complete information about a given $username
  87. *
  88. * @param string $username Username to look up
  89. * @return Statamic_User object
  90. */
  91. public static function get_user($username)
  92. {
  93. $u = Statamic_User::load($username);
  94. return $u;
  95. }
  96. /**
  97. * get_current_user
  98. * Gets complete information about the currently logged-in user
  99. *
  100. * @return Statamic_User object
  101. */
  102. public static function get_current_user()
  103. {
  104. $u = self::is_logged_in();
  105. return $u;
  106. }
  107. /**
  108. * get_user_list
  109. * Gets a full list of registered users
  110. *
  111. * @param boolean $protected Displaying information in a protected area?
  112. * @return array
  113. */
  114. public static function get_user_list($protected = true)
  115. {
  116. $users = array();
  117. $folder = "_config/users/*.yaml";
  118. $list = glob($folder);
  119. if ($list) {
  120. foreach ($list as $name) {
  121. $start = strrpos($name, "/")+1;
  122. $end = strrpos($name, ".");
  123. $username = substr($name, $start, $end-$start);
  124. if ($protected) {
  125. $users[$username] = self::get_user($username);
  126. } else {
  127. $users[$username] = Statamic_User::get_profile($username);
  128. }
  129. }
  130. }
  131. return $users;
  132. }
  133. }