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