/** * @inheritdoc */ public function rules() { /** @var Module $module */ $module = Module::getInstance(); /** @var Account $accountClass */ $accountClass = $module->getClassName(Module::CLASS_ACCOUNT); return ArrayHelper::merge(parent::rules(), [[['email', 'username'], 'required'], ['username', 'string', 'min' => Module::getParam(Module::PARAM_MIN_USERNAME_LENGTH)], ['email', 'email'], [['username', 'email'], 'unique', 'targetClass' => $accountClass], ['captcha', 'captcha', 'captchaAction' => $module->createRoute(Module::URL_ROUTE_CAPTCHA), 'on' => 'captcha']]); }
/** * @inheritdoc */ public function rules() { return [[['email', 'username'], 'required'], ['username', 'string', 'min' => Module::getParam(Module::PARAM_MIN_USERNAME_LENGTH)], ['email', 'email'], [['username', 'email'], 'unique', 'targetClass' => Account::className()]]; }
/** * Logs in a user using the provided username and password. * * @return boolean whether the user is logged in successfully */ public function login() { if ($this->validate()) { $account = $this->getAccount(); $duration = $this->rememberMe ? Module::getParam(Module::PARAM_LOGIN_EXPIRE_TIME) : 0; $success = Yii::$app->user->login($account, $duration); $this->createHistoryEntry($account, $success); $dataContract = Module::getInstance()->getDataContract(); $dataContract->updateAccountAttributes($account, ['lastLoginAt' => new Expression('NOW()')]); return $success; } else { return false; } }
/** * Displays the 'connect' page. * * @param string $providerId provider identifier. * @return string|\yii\web\Response */ public function actionConnect($providerId) { $dataContract = $this->module->getDataContract(); $provider = $dataContract->findProvider($providerId); if ($provider === null || !empty($provider->accountId)) { $this->pageNotFound(); } $model = $dataContract->createConnectForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $account = $dataContract->createAccount(['attributes' => [$this->module->usernameAttribute => $model->username, $this->module->emailAttribute => $model->email, $this->module->passwordAttribute => $this->module->getTokenGenerator()->generate()]]); if (!$account->save()) { $this->fatalError(); } $provider->updateAttributes(['accountId' => $account->id]); $this->afterSignup(); Yii::$app->user->login($account, Module::getParam(Module::PARAM_LOGIN_EXPIRE_TIME)); return $this->redirect($this->module->getRedirectUrl(Module::REDIRECT_CONNECT)); } else { return $this->render('connect', ['model' => $model, 'provider' => $provider]); } }
/** * Invoked after a successful authentication with a client. * * @param ClientInterface $client client instance. * @return \yii\web\Response */ public function clientLogin(ClientInterface $client) { $attributes = $client->getUserAttributes(); $name = $client->getId(); $dataContract = $this->module->getDataContract(); $provider = $dataContract->findProvider(['name' => $name, 'clientId' => $attributes['id']]); if ($provider === null) { $provider = $dataContract->createProvider(['attributes' => ['name' => $name, 'clientId' => $attributes['id'], 'data' => $attributes]]); if (!$provider->save(false)) { $this->fatalError(); } } if ($provider->account !== null) { Yii::$app->user->login($provider->account, Module::getParam(Module::PARAM_LOGIN_EXPIRE_TIME)); return $this->goHome(); } else { return $this->redirect([Module::URL_ROUTE_CONNECT, 'providerId' => $provider->id]); } }
/** * @inheritdoc * @return AccountToken token model. */ public function findValidToken($type, $token) { $tokenExpireTime = Module::getParam(Module::PARAM_TOKEN_EXPIRE_TIME); /** @var AccountToken $modelClass */ $modelClass = Module::getInstance()->getClassName(Module::CLASS_TOKEN); return $modelClass::find()->where(['type' => $type, 'token' => $token, 'status' => $this->getStatusCode(Module::CLASS_TOKEN, Module::STATUS_UNUSED)])->andWhere('(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(createdAt)) < :expireTime', [':expireTime' => $tokenExpireTime])->one(); }