SignupController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\controllers;
  3. use li3_swiftmailer\mailer\Transports;
  4. use li3_swiftmailer\mailer\Message;
  5. use app\models\User;
  6. use app\models\confirmKey;
  7. class SignupController extends \lithium\action\Controller {
  8. public $publicActions = array('index','confirm', 'cancel' );
  9. public $secret = "marshmallows"; //I don't know why either?
  10. public function index() {
  11. $user = null;
  12. //If the request isn't empty
  13. if($this->request->data)
  14. {
  15. //Create a user from the data
  16. $user = User::Create($this->request->data);
  17. //The user isn't active until after they confirm.
  18. $user->confirmed = false;
  19. $user->active = false;
  20. $user->joinedon = date('Y-m-d H:i:s', time());
  21. $user->level = "user";
  22. //By default save does validation at the same time,
  23. //If there are errors its stuffs them into the $user->_erorrs variable,
  24. //Accessible from $user->errors(), this is automatically passed to the view.
  25. if ($user->save())
  26. {
  27. //Generate a confirmation key for the user
  28. $key = confirmKey::Create(array('key' => confirmKey::generate($user->email), 'username' => $user->username));
  29. //Save it to the database
  30. $key->save();
  31. $link = "/signup/confirm/$key->key";
  32. return compact('key', 'link', 'user');
  33. }
  34. }
  35. //If there are validation errors, send them back to the form
  36. return compact('user');
  37. }
  38. public function confirm($key = null) {
  39. //Situation one
  40. //They have a key
  41. if (!(empty($key)))
  42. {
  43. //Find the key in the database
  44. $foundKey = confirmKey::find('first', array('conditions' => compact('key')));
  45. //If the key exists
  46. if($foundKey)
  47. {
  48. //Find that user in the database
  49. $foundUser = User::find('first', array('conditions' => array("username" => $foundKey->username)));
  50. $valid = ($foundUser != NULL);
  51. //Set the users account active and confirmed.
  52. $foundUser->confirmed = true;
  53. $foundUser->active = true;
  54. //If the user is saved sucsessfully,
  55. if($foundUser->save(null, array('validate' => false)))
  56. {
  57. /* If the save is sucsessful we are done */
  58. //Delete their key,
  59. $foundKey->delete();
  60. //Send them to the homepage (probably login though)
  61. $this->redirect("/");
  62. }
  63. else
  64. {
  65. FlashMessage::set("There was an error.");
  66. }
  67. }
  68. else
  69. {
  70. //Otherwise
  71. FlashMessage::set("There was an error finding the key.");
  72. return;
  73. }
  74. }
  75. }
  76. public function cancel($key = null) {
  77. $thisKey = Key::find('first', array('conditions' => compact('key')));
  78. //If the key exists
  79. if ($thisKey)
  80. {
  81. $user = User::find('first', array('conditions' => array('username' => $thisKey->username)));
  82. $user->delete();
  83. $thisKey->delete();
  84. }
  85. return;
  86. }
  87. }