ActivationForm::validateKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
class ActivationForm extends CFormModel
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
	/**
6
	 * The password provided by the user
7
	 * @var string $password
8
	 */
9
	public $password;
10
11
	/**
12
	 * The activation key originally sent to the user's email
13
	 * @var string $activationKey
14
	 */
15
	public $activationKey;
16
17
	/**
18
	 * The user model
19
	 * @var Users $_user
20
	 */
21
	private $_user;
22
23
	/**
24
	 * The activation key metadata model. This is loaded to ensure it gets cleaned up properly
25
	 * @var UserMetadata $_meta
26
	 */
27
	private $_meta;
28
29
	/**
30
	 * Validation rules
31
	 * @return array
32
	 */
33
	public function rules()
34
	{
35
		return array(
36
			array('password, activationKey', 'required'),
37
			array('password', 'length', 'min'=>8),
38
			array('activationKey', 'validateKey'),
39
			array('password', 'validateUserPassword'),
40
		);
41
	}
42
43
	/**
44
	 * Attribute labels
45
	 * @return array
46
	 */
47
	public function attributeLabels()
48
	{
49
		return array(
50
			'password'      => Yii::t('ciims.models.ActivationForm', 'Your Password'),
51
			'activationKey' => Yii::t('ciims.models.ActivationForm', 'Your Activation Key')
52
		);
53
	}
54
55
	/**
56
	 * Validates that the activation key belongs to a user and is valid
57
	 * @param array $attributes
58
	 * @param array $params
59
	 * return array
60
	 */
61
	public function validateKey($attributes=array(), $params=array())
62
	{
63
		$this->_meta = UserMetadata::model()->findByAttributes(array('key'=>'activationKey', 'value'=>$this->activationKey));
64
65
		if ($this->_meta == NULL)
66
		{
67
			$this->addError('activationKey', Yii::t('ciims.models.ActivationForm', 'The activation key you provided is invalid.'));
68
			return false;
69
		}
70
71
		return true;
72
	}
73
74
	/**
75
	 * Ensures that the password entered matches the one provided during registration
76
	 * @param array $attributes
77
	 * @param array $params
78
	 * return array
79
	 */
80
	public function validateUserPassword($attributes, $params)
81
	{
82
		if ($this->_user == NULL)
83
		{
84
			$this->addError('activationKey', Yii::t('ciims.models.ActivationForm', 'The activation key you provided is invalid.'));
85
			return false;
86
		}
87
88
		$result = password_verify($this->password, $this->_user->password);
89
90
		if ($result == false)
91
		{
92
			$this->addError('password', Yii::t('ciims.models.ActivationForm', 'The password you entered does not match the password you registered with.'));
93
			return false;
94
		}
95
96
		return true;
97
	}
98
99
	/**
100
	 * Makes the user an active user in the database, and deletes their activation token
101
	 * @return boolean
102
	 */
103
	public function save()
104
	{
105
		$userId = $this->_meta->user_id;
106
		$this->_user = Users::model()->findByPk($userId);
107
108
		if (!$this->validate())
109
			return false;
110
111
		$this->_user->status = Users::ACTIVE;
112
		
113
		if ($this->_user->save())
114
			return $this->_meta->delete();
115
116
		return false;
117
	}
118
}
119