Skip to content

Instantly share code, notes, and snippets.

@Darkside73
Created December 9, 2015 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darkside73/2c53967300db554b8d35 to your computer and use it in GitHub Desktop.
Save Darkside73/2c53967300db554b8d35 to your computer and use it in GitHub Desktop.
<?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