AuthController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Core\Auth;
  3. use ORM\DBInstance;
  4. use \App\Core\Template\Output as Output;
  5. use Singleton;
  6. use RR\Response;
  7. use \App\Core\Auth\Classes\Auth as Auth;
  8. use App\Core\Auth\Classes\AuthenticatedUser;
  9. use App\Core\Auth\Classes\Token;
  10. use \App\Core\Auth\Classes\User as User;
  11. class AuthController
  12. {
  13. private static $user;
  14. private static $permissions;
  15. /**
  16. * Index
  17. * Show the main Auth list
  18. */
  19. function index()
  20. {
  21. Output::render('index', ['list' => User::findAll()]);
  22. }
  23. /**
  24. * Create
  25. *
  26. * Render the main Auth formulary
  27. */
  28. function create(User $auth)
  29. {
  30. Output::render('form', $auth);
  31. }
  32. /**
  33. * Store
  34. *
  35. * Store the param on the database
  36. * @param User $auth
  37. */
  38. function store(User $auth)
  39. {
  40. $auth->charge($_POST);
  41. if (request('password') == '') {
  42. unset($auth->password);
  43. } else {
  44. $auth->password = md5(request('password'));
  45. }
  46. $auth->save();
  47. $auth->appendGroups(request('groups'));
  48. Response::json($auth)->send();
  49. }
  50. /**
  51. * Show
  52. *
  53. * Render one register
  54. *
  55. * @param Auth $auth
  56. */
  57. function show(Auth $auth)
  58. {
  59. }
  60. /**
  61. * Edit
  62. *
  63. * Render the formular for a database Auth
  64. *
  65. * @param Auth $auth
  66. */
  67. function edit(User $auth)
  68. {
  69. Output::render('form', $auth);
  70. }
  71. /**
  72. * Update
  73. * Store the changes of the param on the database
  74. *
  75. * @param User $auth
  76. */
  77. function update(User $auth)
  78. {
  79. $auth->charge($_POST);
  80. if (request('password') == '') {
  81. unset($auth->password);
  82. } else {
  83. $auth->password = md5(request('password'));
  84. }
  85. $auth->save();
  86. $auth->appendGroups(request('groups'));
  87. Response::json($auth)->send();
  88. }
  89. /**
  90. * Destroy
  91. * If the object has soft delete.
  92. *
  93. * @param Auth $auth
  94. */
  95. function destroy(Auth $auth)
  96. {
  97. }
  98. /**
  99. * Purge
  100. * Remove object even with soft delete.
  101. *
  102. * @param Auth $auth
  103. */
  104. function purge(Auth $auth)
  105. {
  106. }
  107. function login()
  108. {
  109. if ($user = User::findOne(['username' => ['=', $_POST['login']], 'password' => ['=', md5($_POST['password'])]])) {
  110. AuthController::executeLogin($user);
  111. } else {
  112. Output::setTemplate('NullTemplate');
  113. Output::render('login', ['errors' => ['name' => 'Não há usuário com essa senha']]);
  114. }
  115. }
  116. /**
  117. * Prints the system login form
  118. */
  119. function loginForm()
  120. {
  121. if (self::isLoggedIn()) {
  122. Response::redirect('/');
  123. } else {
  124. Output::setTemplate('NullTemplate');
  125. Output::render('login');
  126. }
  127. }
  128. /**
  129. * Just check if the user is set on the _SESSION variable
  130. *
  131. * @return bool
  132. */
  133. public static function isLoggedIn()
  134. {
  135. if ($_SESSION['user']) {
  136. AuthenticatedUser::load();
  137. } else {
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * Check if the token passed on the wstoken param is valid
  144. * by valid.
  145. * - Exist
  146. * - Not Expired
  147. * - Has the permission
  148. *
  149. * @return bool
  150. */
  151. public static function isTokenValid()
  152. {
  153. if ($token = Token::findOne(['token' => ['=', request('wstoken')]])) {
  154. AuthenticatedUser::load($token->user());
  155. } else {
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * Given a permission. Check if the loged user can execute it
  162. *
  163. * @param String $permission
  164. */
  165. public static function canAccess($permission)
  166. {
  167. if (AuthenticatedUser::permissions() == null) {
  168. return false;
  169. }
  170. if (in_array('sys:admin', AuthenticatedUser::permissions())) {
  171. return true;
  172. }
  173. if (in_array($permission, AuthenticatedUser::permissions())) {
  174. return true;
  175. }
  176. return false;
  177. }
  178. /**
  179. * In case the login form is valid. Persist user data on SESSION
  180. * so the login system words onwards
  181. *
  182. * TODO - Cache wantsPage
  183. *
  184. * @param User $user
  185. */
  186. function executeLogin(User $user)
  187. {
  188. $_SESSION['user']['user'] = $user->id;
  189. $_SESSION['user']['permission'] = $user->permissions();
  190. header("location: /");
  191. }
  192. /**
  193. * Execute the system logout
  194. */
  195. function logout()
  196. {
  197. session_destroy();
  198. header("location: /auth/login");
  199. }
  200. }