Created
December 9, 2015 15:57
-
-
Save Darkside73/2c53967300db554b8d35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends UserAbstract { | |
public $old_password; | |
public $auth_hash; | |
public $usernameOnForum; | |
public function rules() | |
{ | |
return array_merge(parent::rules(), array( | |
array('email', 'unsafe'), | |
array('email', 'safe', 'on' => 'insert'), | |
array('old_password', 'length', 'min' => 6, 'max' => 32), | |
array('old_password', 'checkOldPassword', 'on' => 'changePassword') | |
)); | |
} | |
public function checkOldPassword($attribute, $params) | |
{ | |
if ($this->$attribute === null) { | |
if ($this->hash != $this->auth_hash) | |
$this->addError($attribute, $this->getAttributeLabel($attribute) . ' is required'); | |
else | |
return; | |
} | |
$isValid = (bool) self::model()->findByAttributes( | |
array('id' => $this->id, 'password' => self::getPasswordHash($this->$attribute)) | |
); | |
if (!$isValid) | |
$this->addError($attribute, $this->getAttributeLabel($attribute) . ' is not correct'); | |
} | |
protected function beforeValidate() | |
{ | |
if ($this->isAutoRegistration()) { | |
$this->password = $this->cpassword = $this->generatePassword(); | |
$this->is_auto_password = true; | |
} | |
return parent::beforeValidate(); | |
} | |
private function isAutoRegistration() | |
{ | |
return !isset($this->password) && $this->isNewRecord; | |
} | |
public static function findByCredentials($email, $password) | |
{ | |
return self::model()->findByAttributes( | |
array('email' => $email, 'password' => self::getPasswordHash($password)) | |
); | |
} | |
public static function findByHash($hash) | |
{ | |
return self::model()->findByAttributes(array('hash' => $hash)); | |
} | |
protected function afterSave() | |
{ | |
parent::afterSave(); | |
if ($this->isNewRecord && $this->scenario == 'registerOnForum') { | |
$this->usernameOnForum = Yii::app()->services->registerOnForum( | |
$this->email, $this->getPlainPassword() | |
); | |
} | |
if (!$this->isNewRecord && $this->scenario == 'activation') | |
$this->updateOnProductServerIfExists(); | |
} | |
public function updateEmptyAttributes() | |
{ | |
$this->throwMethodNotApplicableExceptionIfNewRecord(); | |
$emptyAttributes = array(); | |
foreach ($this->arBefore->attributes as $attribute => $value) { | |
if (!isset($value) || $value == '') | |
$emptyAttributes[] = $attribute; | |
} | |
return $this->save(true, $emptyAttributes); | |
} | |
public function changeBalance($amount) | |
{ | |
$this->throwMethodNotApplicableExceptionIfNewRecord(); | |
$amount = (float) $amount; | |
if ($amount <= 0) | |
return false; | |
if (!isset($this->balance)) { | |
$balance = new UserBalance(); | |
$balance->id = $this->id; | |
$this->balance = $balance; | |
} | |
$this->balance->amount += $amount; | |
return $this->balance->save(false); | |
} | |
public function getFullName() | |
{ | |
$fullName = ''; | |
if ($this->first_name) | |
$fullName = $this->first_name; | |
if ($this->last_name) | |
$fullName .= ($fullName ? ' ' : '') . $this->last_name; | |
return $fullName; | |
} | |
public function toArray(array $attributes = null) | |
{ | |
$data = parent::toArray($attributes); | |
$data['country'] = isset($this->country) ? $this->country->name : null; | |
if (!$this->isNewRecord) { | |
$coupon = $this->getPersonalCoupon(); | |
if ($coupon) | |
$data['coupon_code'] = $coupon->code; | |
} | |
return $data; | |
} | |
public function unsubscribe() | |
{ | |
$this->throwMethodNotApplicableExceptionIfNewRecord(); | |
if (!$this->subscribed) | |
return true; | |
$this->subscribed = 0; | |
return $this->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment