1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class InvitationForm extends CFormModel |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* The email of the user to invite |
7
|
|
|
* @var string $email |
8
|
|
|
*/ |
9
|
|
|
public $email; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Validation rules |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
public function rules() |
16
|
|
|
{ |
17
|
|
|
return array( |
18
|
|
|
array('email', 'required'), |
19
|
|
|
array('email', 'email'), |
20
|
|
|
array('email', 'userExists') |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validates that a user with that email address isn't already invited |
26
|
|
|
* @param array $attributes |
27
|
|
|
* @param array $params |
28
|
|
|
* @return boolean |
29
|
|
|
*/ |
30
|
|
|
public function userExists($attributes, $params) |
31
|
|
|
{ |
32
|
|
|
$user = Users::model()->findByAttributes(array('email' => $this->email)); |
33
|
|
|
|
34
|
|
|
if ($user == NULL) |
35
|
|
|
return true; |
36
|
|
|
|
37
|
|
|
$this->addError('email', Yii::t('ciims.models.InvitationForm', 'A user with that email already exists.')); |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Attribute labels |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function attributeLabels() |
46
|
|
|
{ |
47
|
|
|
return array( |
48
|
|
|
'email' => Yii::t('ciims.models.InvitationForm', 'Email Address') |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sends an invite to a new user |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function invite() |
57
|
|
|
{ |
58
|
|
|
if (!$this->validate()) |
59
|
|
|
return false; |
60
|
|
|
|
61
|
|
|
$user = new Users; |
62
|
|
|
$user->attributes = array( |
63
|
|
|
'email' => $this->email, |
64
|
|
|
'firstName' => null, |
65
|
|
|
'lastName' => null, |
66
|
|
|
'displayName' => null, |
67
|
|
|
'password' => null, |
68
|
|
|
'user_role' => 5, |
69
|
|
|
'status' => Users::PENDING_INVITATION |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
// Create a new user, but bypass validation |
73
|
|
|
if ($user->save(false)) |
74
|
|
|
{ |
75
|
|
|
$meta = new UserMetadata; |
76
|
|
|
$meta->attributes = array( |
77
|
|
|
'user_id' => $user->id, |
78
|
|
|
'key' => 'invitationKey', |
79
|
|
|
'value' => Cii::generateSafeHash() |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// If the key was savedm send the email out |
83
|
|
|
if ($meta->save()) |
84
|
|
|
{ |
85
|
|
|
$emailSettings = new EmailSettings; |
86
|
|
|
$emailSettings->send( |
87
|
|
|
$user, |
88
|
|
|
Yii::t('ciims.models.InvitationForm', "You've Been Invited..."), |
89
|
|
|
'webroot.themes.' . Cii::getConfig('theme', 'default') .'.views.email.invite', |
90
|
|
|
array( |
91
|
|
|
'user' => $user, |
92
|
|
|
'hash' => $meta->value |
93
|
|
|
), |
94
|
|
|
true, |
95
|
|
|
true |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$user->delete(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.