-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
185 lines (150 loc) · 4.31 KB
/
User.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
class Recipe_Model_User extends Recipe_Model_GenericModel implements Zend_Acl_Resource_Interface, Zend_Acl_Role_Interface
{
protected $_ownerUserId = null;
protected $_data = array(
'id' => null,
'name' => null,
'last_login' => null,
'status' => null,
'comments_count' => 0,
'ratings_count' => 0,
'recipes_count' => 0,
'role' => null,
'preferences' => array()
);
public function getResourceId()
{
return 'user';
}
/**
* Retrieves the roleID, required by the inclusion of implements Zend_Acl_Role_Interface
*
* @return string
*/
public function getRoleId()
{
if ($this->_data['role'] == null)
return 'guest';
return $this->_data['role'];
}
/**
* Retieves the user information if you supply an email address
*
* @param string $email
* @return array|false
*/
public function getUserByEmail($email)
{
if ( !$row = parent::getSingleByField('email', $email) )
return false;
$this->_ownerUserId = $this->_data['id'];
return true;
}
/**
* Login for the User, this sends the username/password to the Auth Adapter
*
* @param string $email
* @param string $password
* @return Zend_Auth_Result
*/
public function login( $email, $password )
{
$auth = Zend_Auth::getInstance();
// @todo Move this to bootstrap
$authAdapter = new Zend_Auth_Adapter_DbTable( $this->db );
$authAdapter->setTableName( 'users' )
->setIdentityColumn( 'email' )
->setCredentialColumn( 'password' )
->setCredentialTreatment( 'MD5(?)' )
->setIdentity( $email )
->setCredential( $password );
$result = $auth->authenticate( $authAdapter );
if ( ! $result->isValid() )
return join(',', $result->getMessages());
$this->getUserByEmail($auth->getIdentity());
$msg = $this->checkStatus();
if ( $msg != '' )
{
$auth->clearIdentity();
$this->log->info('User '.sq_brackets( $this->_data['name'] ).' tried to login but got ' . sq_brackets( $msg ) );
return $msg;
}
$this->table->update(
array('last_login' => new Zend_Db_Expr('NOW()')),
'id = '.$this->_data['id']
);
// @todo get the preferences
$up = new Recipe_Model_UserPreferences($this->_data['id']);
$this->_data['preferences'] = $up;
$auth->getStorage()->write($this);
return true;
}
/**
* Prepares a forgotten password mail and sends it out the current user
* @return bool
*/
public function forgottenPasswordMail($row)
{
// Reset the password
$password = $this->generatePassword();
$row['password'] = new Zend_Db_Expr('PASSWORD("'.$password.'")');
// remove the role from the row because we cannot update that
unset($row['role']);
$this->table->update($row, 'id = '.$row['id']);
$e = new Recipe_Email( $row['email'], $row['name'], 'Forgotten Password' );
$e->setTemplate( 'forgotten-password.phtml' );
$e->view->password = $password;
return $e->sendMail();
}
/**
* Sends out e-mail to use to verify e-mail
* @return bool
*/
public function sendConfirmationEmail($row)
{
$e = new Recipe_Email( $row['email'], $row['name'], 'Registration' );
$e->setTemplate( 'user-registration.phtml' );
$e->view->verificationURL = 'http://' . $_SERVER['HTTP_HOST'] . '/user/confirm/code/' . $row['confirm'];
return $e->sendMail();
}
/**
* Used to check the status of the user, based on the ENUM of the DB field
* @return string
*/
public function checkStatus()
{
$message = '';
switch ($this->_data['status'])
{
case 'banned':
$message = 'Your account has been banned, you need to get in touch with us to find out why';
break;
case 'suspended':
$message = 'Your account has been suspended, you should of been mailed the reason';
break;
case 'pending':
$message = 'You need to fully activate your account to continue, maybe you need a <a href="/login/gsend-confirmation">confirmation e-mail</a>';
case 'admin':
case 'active':
break;
}
return $message;
}
/**
* Generates a password between 6 and 12 characters
* @return string
*/
private function _generatePassword()
{
$chars = "1234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
$length = mt_rand( 6, 12 );
$password = "";
while ($i <= $length) {
$password .= $chars{mt_rand(0,strlen($chars))};
$i++;
}
return $password;
}
}