ForgotForm   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 93
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 8 1
A attributeLabels() 0 6 1
A exists() 0 12 2
B initPasswordResetProcess() 0 36 4
1
<?php
2
3
class ForgotForm 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
	 * @var string $email     The user's email address
7
	 */
8
	public $email;
9
10
	/**
11
	 * @var Users $_user     The user's model
12
	 */
13
	private $_user = NULL;
14
15
	/**
16
	 * Validation rules
17
	 * @return array
18
	 */
19
	public function rules()
20
	{
21
		return array(
22
			array('email', 'required'),
23
			array('email', 'email'),
24
			array('email', 'exists')
25
		);
26
	}
27
28
	public function attributeLabels()
29
	{
30
		return array(
31
			'email' => Yii::t('ciims.models.ForgotForm', 'Email Address')
32
		);
33
	}
34
35
	/**
36
	 * Determines if we have a user in our database with that email address
37
	 * @param array $attributes
38
	 * @param array $params
39
	 * @return boolean
40
	 */
41
	public function exists($attributes, $params)
42
	{
43
		$this->_user = Users::model()->findByAttributes(array('email' => $this->email));
44
45
		if ($this->_user == NULL)
46
		{
47
			$this->addError('email', Yii::t('ciims.models.ForgotForm', 'The email address you entered is either invalid, or does not belong to a user in our system.'));
48
			return false;
49
		}
50
51
		return true;
52
	}
53
54
	/**
55
	 * Initiates the password reset process on behalf of the user
56
	 * Generates a unique hash and an expiration time that the hash is valid up until (defaults to 15 minutes)
57
	 * This key will internally expire (but not be expunged) after that time
58
	 */
59
	public function initPasswordResetProcess()
60
	{
61
		if (!$this->validate())
62
			return false;
63
64
		$hash = Cii::generateSafeHash();
65
		$expires = strtotime("+15 minutes");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal +15 minutes does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
66
67
		$meta = UserMetadata::model()->findByAttributes(array('user_id'=>$this->_user->id, 'key'=>'passwordResetCode'));
68
		if ($meta === NULL)
69
			$meta = new UserMetadata;
70
71
		$meta->user_id = $this->_user->id;
72
		$meta->key = 'passwordResetCode';
73
		$meta->value = $hash;
74
		$meta->save();
75
76
		$meta = UserMetadata::model()->findByAttributes(array('user_id'=>$this->_user->id, 'key'=>'passwordResetExpires'));
77
		if ($meta === NULL)
78
			$meta = new UserMetadata;
79
80
		$meta->user_id = $this->_user->id;
81
		$meta->key = 'passwordResetExpires';
82
		$meta->value = $expires;
83
		$meta->save();
84
85
		$emailSettings = new EmailSettings;
86
		$emailSettings->send($this->_user, Yii::t('ciims.email', 'Your Password Reset Information'), 'webroot.themes.' . Cii::getConfig('theme', 'default') .'.views.email.forgot', array('user' => $this->_user, 'hash' => $hash), true, true);
87
88
		// Set success flash
89
		Yii::app()->user->setFlash('success', Yii::t('ciims.controllers.Site', 'An email has been sent to {{email}} with further instructions on how to reset your password', array(
90
			'{{email}}' => $this->email
91
		)));
92
93
		return true;
94
	}
95
}
96