diff options
Diffstat (limited to 'protected/models/User.php')
| -rw-r--r-- | protected/models/User.php | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/protected/models/User.php b/protected/models/User.php index b6a1609..13163c3 100644 --- a/protected/models/User.php +++ b/protected/models/User.php @@ -79,6 +79,17 @@ class User extends CActiveRecord ); } + protected function beforeSave() { + if ($this->isNewRecord) { + $this->created_at = new CDbExpression("NOW()"); + $this->salt = $this->generateRandomKey(); + } + + $this->password = $this->encryptPassword($this->password); + + return parent::beforeSave(); + } + /** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. @@ -104,6 +115,21 @@ class User extends CActiveRecord } public function checkPassword($password) { - return sha1($this->salt.$password) == $this->password; + return $this->encryptPassword($password) == $this->password; + } + + protected function encryptPassword($password) { + return sha1($this->salt.$password); + } + + protected function generateRandomKey($len = 20) {
+ $string = '';
+ $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
+ for ($i = 1; $i <= $len; $i++) {
+ $string .= substr($pool, rand(0, 61), 1);
}
+
+ return md5($string);
+ }
+ }
\ No newline at end of file |
