summaryrefslogtreecommitdiff
path: root/demos/time-tracker/protected/pages/TimeTracker/UserCreate.php
blob: 34e6971343011261c485bb1c833095796c64af22 (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
<?php
/**
 * UserCreate page class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package Demos
 */

/**
 * Create new user wizard page class. Validate that the usernames are unique and
 * set the new user credentials as the current application credentials.
 *
 * If logged in as admin, the user role can be change during creation.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package Demos
 * @since 3.1
 */
class UserCreate extends TPage
{
	/**
	 * Sets the default new user roles, default role is set in config.xml
	 */
	public function onLoad($param)
	{
		if(!$this->IsPostBack)
		{
			$this->role->SelectedValue =
				$this->Application->Parameters['NewUserRoles'];
		}
	}

	/**
	 * Verify that the username is not taken.
	 * @param TControl custom validator that created the event.
	 * @param TServerValidateEventParameter validation parameters.
	 */
	public function checkUsername($sender, $param)
	{
		$userDao = $this->Application->Modules['daos']->getDao('UserDao');
		if($userDao->usernameExists($this->username->Text))
		{
			$param->IsValid = false;
			$sender->ErrorMessage =
				"The user name is already taken, try '{$this->username->Text}01'";
		}
	}

	/**
	 * Skip the role assignment step if not admin.
	 */
	public function userWizardNextStep($sender, $param)
	{
		if($param->CurrentStepIndex == 0)
		{
			//create user with admin credentials
			if(!$this->User->isInRole('admin'))
			{
				$this->createNewUser($sender, $param);
				$param->NextStepIndex = 2;
			}
		}
	}

	/**
	 * Create a new user if all data entered are valid.
	 * The default user roles are obtained from "config.xml". The new user
	 * details is saved to the database and the new credentials are used as the
	 * application user. The user is redirected to the requested page.
	 * @param TControl button control that created the event.
	 * @param TEventParameter event parameters.
	 */
	public function createNewUser($sender, $param)
	{
		if($this->IsValid)
		{
			$newUser = new TimeTrackerUser($this->User->Manager);
			$newUser->EmailAddress = $this->email->Text;
			$newUser->Name = $this->username->Text;
			$newUser->IsGuest = false;
			$newUser->Roles = $this->role->SelectedValue;

			//save the user
			$userDao = $this->Application->Modules['daos']->getDao('UserDao');
			$userDao->addNewUser($newUser, $this->password->Text);

			//update the user credentials if not admin
			if(!$this->User->isInRole('admin'))
			{
				$auth = $this->Application->getModule('auth');
				$auth->updateCredential($newUser);
			}
		}
	}

	/**
	 * Continue with requested page.
	 */
	public function wizardCompleted($sender, $param)
	{
		//return to requested page
		$auth = $this->Application->getModule('auth');
		$this->Response->redirect($auth->getReturnUrl());
	}
}