summaryrefslogtreecommitdiffstats
path: root/controllers/SignupController.php
blob: f368997fc1a630391352720cce35b9a13362bf37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php 

namespace app\controllers; 

use li3_swiftmailer\mailer\Transports;
use li3_swiftmailer\mailer\Message;
use app\models\User;
use app\models\confirmKey;

class SignupController extends \lithium\action\Controller {
	public $publicActions = array('index','confirm', 'cancel' );
	public $secret = "marshmallows"; //I don't know why either?

	public function index() {
		$user = null;
		//If the request isn't empty
		if($this->request->data) 
		{
			//Create a user from the data
			$user = User::Create($this->request->data);	

			//The user isn't active until after they confirm.
			$user->confirmed = false;
			$user->active = false; 
			$user->joinedon = date('Y-m-d H:i:s', time());
			$user->level = "user";

			//By default save does validation at the same time, 
			//If there are errors its stuffs them into the $user->_erorrs variable,
			//Accessible from $user->errors(), this is automatically passed to the view.
			if ($user->save()) 
			{
				//Generate a confirmation key for the user
				$key = confirmKey::Create(array('key' => confirmKey::generate($user->email), 'username' => $user->username));
				
				//Save it to the database 
				$key->save();

				//Create the link for the user to click.
				$link = $this->html->link('Here', array('controller' => 'signup',
														'action' => 'confirm',
														'args' => $key->key));


				$mailer = Transports::adapter('default');
				$message = Message::newInstance()            
              				->setSubject('Welcome to OtakuHUB')
               	 			->setFrom(array('admin@weareotak.us' => 'OtakuHUB signup team'))
              				->setTo(array($user->email))
              				->setBody("Hey! Wecome to our awesome site! Click $link to get started");
            
              $result = $mailer->send($message);


				return compact('key', 'link', 'user');
			}
		}
		//If there are validation errors, send them back to the form
		return compact('user');
	}

	public function confirm($key = null) {
		//Situation one
		//They have a key
		if (!(empty($key)))
		{
			//Find the key in the database
			$foundKey = confirmKey::find('first', array('conditions' => compact('key')));
			
			//If the key exists
			if($foundKey)
			{
				//Find that user in the database
				$foundUser = User::find('first', array('conditions' => array("username" => $foundKey->username)));
				$valid = ($foundUser != NULL);

				//Set the users account active and confirmed.
				$foundUser->confirmed = true;
				$foundUser->active = true;	

				//If the user is saved sucsessfully,
				if($foundUser->save(null, array('validate' => false)))
				{
					/* If the save is sucsessful we are done */
					//Delete their key,
					$foundKey->delete();

					//Send them to the homepage (probably login though)
					$this->redirect("/");

				}
				else
				{
					FlashMessage::set("There was an error.");
				}

			}
			else
			{
				//Otherwise
				FlashMessage::set("There was an error finding the key.");
				return;
			}
		}
	}

	public function cancel($key = null) {
		$thisKey = Key::find('first', array('conditions' => compact('key')));
		
		//If the key exists
		if ($thisKey) 
		{
			$user = User::find('first', array('conditions' => array('username' => $thisKey->username)));

			$user->delete();
			$thisKey->delete();
		}
		return; 
	}
}