/
var
/
www
/
vhosts
/
nabawater
/
api
/
common
/
models
/
/var/www/vhosts/nabawater/api/common/models
mkdir
upload
Name
Size
Mode
Actions
CustomerChangePasswordForm.php
2072
0644
edit
dl
rm
LoginForm.php
7257
0644
edit
dl
rm
UserChangePassword.php
3181
0644
edit
dl
rm
Edit:
/var/www/vhosts/nabawater/api/common/models/LoginForm.php
(7257B)
<?php namespace api\common\models; use common\models\User; use common\models\Vendor; use common\models\Branch; use Yii; use yii\base\Model; /** * Login form */ class LoginForm extends Model { const SCENARIO_LOGIN_DEFAULT = 'login'; const SCENARIO_SOCIAL_MEDIA_LOGIN = 'social-media-login'; const SCENARIO_VENDOR_LOGIN = 'vendor-login'; const SCENARIO_BRANCH_LOGIN = 'branch-login'; const NORMAL_LOGIN = 1; const SOCIAL_MEDIA_FB = 2; const SOCIAL_MEDIA_GOOGLE = 3; public $username; public $password; public $login_type; public $social_token; public $device_token; public $device_type_id; public $rememberMe = false; public $otp; private $_user; /** * @return array */ public function rules() { return [ // username and password are both required [['username', 'password'], 'required'], [['device_token', 'device_type_id'], 'safe'], // rememberMe must be a boolean value ['rememberMe', 'boolean'], ['username', 'validateUsername'], ['otp', 'verifyOtp'], // password is validated by validatePassword() //['password', 'validatePassword'], /** * Social media login Fields rules */ [['social_token'], 'required'], ['social_token', 'validateSocialMediaToken'], ]; } /** * @return array */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios[self::SCENARIO_LOGIN_DEFAULT] = [ 'username', 'otp', //'password', 'rememberMe', 'device_type_id', 'device_token', 'login_type' ]; $scenarios[self::SCENARIO_SOCIAL_MEDIA_LOGIN] = [ 'login_type', 'social_token', 'rememberMe', 'device_type_id', 'device_token' ]; $scenarios[self::SCENARIO_VENDOR_LOGIN] = [ 'username', 'password', 'device_type_id', 'device_token' ]; $scenarios[self::SCENARIO_BRANCH_LOGIN] = [ 'username', 'password', 'device_type_id', 'device_token' ]; return $scenarios; } public function validateUsername() { $user = $this->getUser(); if (!$user) { $this->addError('Incorrect username'); } else { if ($this->scenario === self::SCENARIO_LOGIN_DEFAULT) { if ($user->status !== User::ACTIVE) { $this->addError('username', 'User is not active'); } } elseif ($this->scenario === self::SCENARIO_VENDOR_LOGIN) { if ($user->branch_status !== Vendor::ACTIVE) { $this->addError('User is not active'); } } } } /** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule * @throws \yii\base\InvalidParamException */ public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, 'Incorrect username or password.'); } } } /** * Validates the otp. * This method serves as the inline validation for otp. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule * @throws \yii\base\InvalidParamException */ public function verifyOtp($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if ($user != null) { if ($this->otp != $user->otp) { $this->addError($attribute, 'Invalid OTP supplied'); } } } } /** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule * @throws \yii\base\InvalidParamException */ public function validateSocialMediaToken($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user) { $this->addError($attribute, 'Incorrect username or password.'); } } } /** * Logs in a user using the provided username and password. * * @return bool whether the user is logged in successfully * @throws \yii\base\InvalidParamException */ public function login() { if ($this->validate()) { return Yii::$app->user->login($this->getUser()); } else { return false; } } /** * Logs in a user using the provided username and password. * * @return bool whether the user is logged in successfully * @throws \yii\base\InvalidParamException */ public function loginSocialMedia() { if ($this->validate()) { return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); } else { return false; } } /** * Logs in a vendor using the provided username and password. * * @return bool whether the user is logged in successfully * @throws \yii\base\InvalidParamException */ public function loginVendor() { if ($this->validate()) { return Yii::$app->getVendor()->login($this->getUser()); } else { return false; } } /** * Finds user by [[username]] * * @return User|Vendor */ protected function getUser() { switch ($this->scenario) { case self::SCENARIO_SOCIAL_MEDIA_LOGIN: if ($this->login_type == self::SOCIAL_MEDIA_GOOGLE) { $this->_user = User::findOne(['google_token' => $this->social_token]); } else if ($this->login_type == self::SOCIAL_MEDIA_FB) { $this->_user = User::findOne(['fb_token' => $this->social_token]); } else { return false; } break; case self::SCENARIO_VENDOR_LOGIN: $this->_user = Branch::find() ->where(['OR', ['contact_email' => $this->username]]) ->andWhere(['branch_status' => Vendor::ACTIVE]) ->limit(1) ->one(); break; default: $this->_user = User::findByUsername($this->username); break; } //print_r($this->_user); die; return $this->_user; } }
Save
cmd:
run