add customerapi + customer auth
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
<?php
|
||||
namespace common\models;
|
||||
|
||||
use common\components\Helper;
|
||||
use Yii;
|
||||
use yii\base\Exception;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\web\IdentityInterface;
|
||||
|
||||
/**
|
||||
* This is the model class for table "customer".
|
||||
@@ -33,8 +38,11 @@ use Yii;
|
||||
* @property integer towel_count
|
||||
* @property \common\models\User user
|
||||
* @property mixed bank_account
|
||||
* @property string password_plain
|
||||
* @property string password_hash
|
||||
* @property string auth_key
|
||||
*/
|
||||
class Customer extends BaseFitnessActiveRecord
|
||||
class Customer extends BaseFitnessActiveRecord implements IdentityInterface
|
||||
{
|
||||
|
||||
const STATUS_DELETED = 0;
|
||||
@@ -198,7 +206,135 @@ class Customer extends BaseFitnessActiveRecord
|
||||
Card::updateCardFlagTicket($this->id_customer_card);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Finds an identity by the given ID.
|
||||
* @param string|integer $id the ID to be looked for
|
||||
* @return void the identity object that matches the given ID.
|
||||
* Null should be returned if such an identity cannot be found
|
||||
* or the identity is not in an active state (disabled, deleted, etc.)
|
||||
*/
|
||||
public static function findIdentity($id)
|
||||
{
|
||||
Customer::findOne($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an identity by the given token.
|
||||
* @param mixed $token the token to be looked for
|
||||
* @param mixed $type the type of the token. The value of this parameter depends on the implementation.
|
||||
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
|
||||
* @return void the identity object that matches the given token.
|
||||
* Null should be returned if such an identity cannot be found
|
||||
* or the identity is not in an active state (disabled, deleted, etc.)
|
||||
* @throws NotSupportedException
|
||||
*/
|
||||
public static function findIdentityByAccessToken($token, $type = null)
|
||||
{
|
||||
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ID that can uniquely identify a user identity.
|
||||
* @return string|integer an ID that uniquely identifies a user identity.
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id_customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a key that can be used to check the validity of a given identity ID.
|
||||
*
|
||||
* The key should be unique for each individual user, and should be persistent
|
||||
* so that it can be used to check the validity of the user identity.
|
||||
*
|
||||
* The space of such keys should be big enough to defeat potential identity attacks.
|
||||
*
|
||||
* This is required if [[User::enableAutoLogin]] is enabled.
|
||||
* @return string a key that is used to check the validity of a given identity ID.
|
||||
* @see validateAuthKey()
|
||||
*/
|
||||
public function getAuthKey()
|
||||
{
|
||||
// todo: add authkey to customer
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given auth key.
|
||||
*
|
||||
* This is required if [[User::enableAutoLogin]] is enabled.
|
||||
* @param string $authKey the given auth key
|
||||
* @return boolean whether the given auth key is valid.
|
||||
* @see getAuthKey()
|
||||
*/
|
||||
public function validateAuthKey($authKey)
|
||||
{
|
||||
return $this->getAuthKey() === $authKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $password
|
||||
* @return bool
|
||||
* @throws \yii\base\InvalidConfigException
|
||||
*/
|
||||
public function validatePassword($password)
|
||||
{
|
||||
return Yii::$app->security->validatePassword($password, $this->password_hash);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates password hash from password and sets it to the model
|
||||
*
|
||||
* @param string $password
|
||||
* @throws Exception
|
||||
* @throws \yii\base\InvalidConfigException
|
||||
*/
|
||||
public function setPassword($password)
|
||||
{
|
||||
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates "remember me" authentication key
|
||||
* @throws InvalidConfigException if OpenSSL extension is needed but not installed.*@throws \yii\base\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function generateAuthKey()
|
||||
{
|
||||
$this->auth_key = Yii::$app->security->generateRandomString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new password reset token
|
||||
*/
|
||||
public function generatePasswordResetToken()
|
||||
{
|
||||
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes password reset token
|
||||
*/
|
||||
public function removePasswordResetToken()
|
||||
{
|
||||
$this->password_reset_token = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws InvalidConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setDefaultPassword(){
|
||||
$hasEmail = isset($this->email) && $this->email !== '';
|
||||
$hasBirthday = isset($this->birthdate) ;
|
||||
if ( $hasEmail && $hasBirthday){
|
||||
$customerBirthDate = new \DateTime($this->birthdate);
|
||||
$this->setPassword($customerBirthDate->format('Ymd'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user