SignupController.php 3.2 KB

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