AuthController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. *---------------------------------------------------------------
  4. * Basic Authentication Controller
  5. *---------------------------------------------------------------
  6. *
  7. * This is an example that should help to explain how CCAuth
  8. * and a basic controller works. Feel free to edit.
  9. */
  10. class AuthController extends CCViewController
  11. {
  12. /**
  13. * Sign in action
  14. *
  15. * @return CCResponse
  16. */
  17. public function action_sign_in()
  18. {
  19. // Here we set the page topic seen in the title tag
  20. $this->theme->topic = __( ':action.topic' );
  21. // lets assign the view. Instead of getting the view directly
  22. // using CCView::create( 'path/to/view' ) we get the view from the
  23. // theme this allows us to have a diffrent sign_in for every theme.
  24. // If the view does not exist in the theme it will load the view from
  25. // the default view folder.
  26. $this->view = $this->theme->view( 'auth/sign_in.view' );
  27. $this->view->last_identifier = CCIn::post( 'identifier' );
  28. // By checking the HTTP method we figure out if this is a post request or not.
  29. if ( CCIn::method( 'post' ) )
  30. {
  31. // Validate the data and get the user object.
  32. // We use the key "identifier" because you can configure on
  33. // what fields the user is able to login. You could add for example
  34. // the username or the customer number etc.
  35. if ( $user = CCAuth::validate( CCIn::post( 'identifier' ), CCIn::post( 'password' ) ) )
  36. {
  37. // sign in the user with the current session.
  38. CCAuth::sign_in( $user );
  39. // flash a success message to the user that he has been
  40. // logged in succesfully.
  41. UI\Alert::flash( 'success', __( ':action.message.success' ) );
  42. // Redirect the user back to the url where he came from
  43. // this will only work when the next get parameter is set.
  44. return CCRedirect::next();
  45. }
  46. // If we could not recive a user object the login data were clearly invalid.
  47. UI\Alert::add( 'danger', __( ':action.message.invalid' ) );
  48. }
  49. }
  50. /**
  51. * Sign up action
  52. *
  53. * @return CCResponse
  54. */
  55. public function action_sign_up()
  56. {
  57. // When the user is already authenticated we redirect him home.
  58. if ( CCAuth::valid() )
  59. {
  60. return CCRedirect::to( '/' );
  61. }
  62. $this->theme->topic = __( ':action.topic' );
  63. $this->view = $this->theme->view( 'auth/sign_up.view' );
  64. // create a new user object as data holder
  65. $user = new User;
  66. // bind the newly created user object to our view
  67. $this->view->bind( 'user', $user );
  68. if ( CCIn::method( 'post' ) )
  69. {
  70. // Lets assign the email and the password to our
  71. // user object using the stirct assign method wich
  72. // will ignore all other post values in the assing process.
  73. $user->strict_assign( array( 'email', 'password' ), CCIn::all( 'post' ) );
  74. $validator = CCValidator::post();
  75. // assign the labels to the validator this way we get
  76. // correct translated error messages.
  77. $validator->label( array(
  78. 'email' => __( 'model/user.label.email' ),
  79. 'password' => __( 'model/user.label.password' ),
  80. 'password_match' => __( 'model/user.label.password_match' )
  81. ));
  82. // does the user already exist
  83. $validator->set( 'same_email', User::find( 'email', $user->email ) );
  84. $validator->message( __(':action.message.email_in_use'), 'negative', 'same_email' );
  85. // validate the other fields
  86. $validator->rules( 'email', 'required', 'email' );
  87. $validator->rules( 'password', 'required', 'min:6' );
  88. $validator->rules( 'password_match', 'required', 'match:password' );
  89. // when the data passes the validation
  90. if ( $validator->success() )
  91. {
  92. // because the user input is correct we can now save the
  93. // object to the database and sign the user in.
  94. $user->save();
  95. CCAuth::sign_in( $user );
  96. UI\Alert::flash( 'success', __( ':action.message.success' ) );
  97. return CCRedirect::to( '/' );
  98. }
  99. else
  100. {
  101. UI\Alert::add( 'danger', $validator->errors() );
  102. }
  103. }
  104. }
  105. /**
  106. * Sign out action
  107. */
  108. public function action_sign_out()
  109. {
  110. if ( !CCSession::valid_fingerprint() )
  111. {
  112. return CCRedirect::to( '/' );
  113. }
  114. CCAuth::sign_out(); return CCRedirect::to( '/' );
  115. }
  116. }