SignupController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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("F j, Y, g:i a");
  21. //By default save does validation at the same time,
  22. //If there are errors its stuffs them into the $user->_erorrs variable,
  23. //Accessible from $user->errors(), this is automatically passed to the view.
  24. if ($user->save())
  25. {
  26. //Generate a confirmation key for the user
  27. $key = confirmKey::Create(array('key' => confirmKey::generate($user->email), 'username' => $user->username));
  28. //Save it to the database
  29. $key->save();
  30. //Create the link for the user to click.
  31. $link = $this->html->link('Here', array('controller' => 'signup',
  32. 'action' => 'confirm',
  33. 'args' => $key->key));
  34. $mailer = Transports::adapter('default');
  35. $message = Message::newInstance()
  36. ->setSubject('Welcome to OtakuHUB')
  37. ->setFrom(array('admin@weareotak.us' => 'OtakuHUB signup team'))
  38. ->setTo(array($user->email))
  39. ->setBody("Hey! Wecome to our awesome site! Click $link to get started");
  40. $result = $mailer->send($message);
  41. return compact('key', 'link', 'user');
  42. }
  43. }
  44. //If there are validation errors, send them back to the form
  45. return compact('user');
  46. }
  47. public function confirm($key = null) {
  48. //Situation one
  49. //They have a key
  50. if (!(empty($key)))
  51. {
  52. //Find the key in the database
  53. $foundKey = confirmKey::find('first', array('conditions' => compact('key')));
  54. //If the key exists
  55. if($foundKey)
  56. {
  57. //Find that user in the database
  58. $foundUser = User::find('first', array('conditions' => array("username" => $foundKey->username)));
  59. $valid = ($foundUser != NULL);
  60. //Set the users account active and confirmed.
  61. $foundUser->confirmed = true;
  62. $foundUser->active = true;
  63. //If the user is saved sucsessfully,
  64. if($foundUser->save(null, array('validate' => false)))
  65. {
  66. /* If the save is sucsessful we are done */
  67. //Delete their key,
  68. $foundKey->delete();
  69. //Send them to the homepage (probably login though)
  70. $this->redirect("/");
  71. }
  72. else
  73. {
  74. FlashMessage::set("There was an error.");
  75. }
  76. }
  77. else
  78. {
  79. //Otherwise
  80. FlashMessage::set("There was an error finding the key.");
  81. return;
  82. }
  83. }
  84. }
  85. public function cancel($key = null) {
  86. $thisKey = Key::find('first', array('conditions' => compact('key')));
  87. //If the key exists
  88. if ($thisKey)
  89. {
  90. $user = User::find('first', array('conditions' => array('username' => $thisKey->username)));
  91. $user->delete();
  92. $thisKey->delete();
  93. }
  94. return;
  95. }
  96. }