Advertisement
Guest User

Untitled

a guest
Nov 30th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <?php
  2.  
  3. class SessionController extends ControllerBase {
  4.  
  5. private function _registerSession($user)
  6. {
  7. $this->session->set(
  8. "auth",
  9. [
  10. "ID" => $user->ID,
  11. "name" => $user->name,
  12. ]
  13. );
  14. }
  15.  
  16. /**
  17. * This action authenticate and logs a user into the application
  18. */
  19. public function startAction()
  20. {
  21. if ($this->request->isPost()) {
  22. // Get the data from the user
  23. $username = $this->request->getPost("username");
  24. $password = $this->request->getPost("password");
  25.  
  26. // Find the user in the database
  27. $user = Users::findFirst(
  28. [
  29. "username = :username: AND password = :password:",
  30. "bind" => [
  31. "username" => $username,
  32. "password" => mda5($password),
  33. ]
  34. ]
  35. );
  36.  
  37. if ($user !== false) {
  38. $this->_registerSession($user);
  39.  
  40. $this->flash->success(
  41. "Welcome " . $user->name
  42. );
  43.  
  44. // Forward to the 'invoices' controller if the user is valid
  45. return $this->dispatcher->forward(
  46. [
  47. "controller" => "articles",
  48. "action" => "index",
  49. ]
  50. );
  51. }
  52.  
  53. $this->flash->error(
  54. "Wrong username/password"
  55. );
  56. }
  57.  
  58. // Forward to the login form again
  59. return $this->dispatcher->forward(
  60. [
  61. "controller" => "session",
  62. "action" => "index",
  63. ]
  64. );
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement