/** * Set info for vkontakte registration * * @author Ilya Sheershoff <*****@*****.**> * @param array $attributes * @return array [$user, $profile] */ protected function setInfoVkontakte($attributes) { /** @var \amnah\yii2\user\models\User $user */ /** @var \amnah\yii2\user\models\Profile $profile */ $user = $this->module->model("User"); $profile = $this->module->model("Profile"); foreach ($_SESSION as $k => $v) { if (is_object($v) && get_class($v) == "yii\\authclient\\OAuthToken") { /** @var \yii\authclient\OAuthToken $v */ $user->email = $v->getParam('email'); } } // set email/username if they are set // note: email may be missing if user signed up using a phone number if (!empty($attributes["email"])) { $user->email = $attributes["email"]; } if (!empty($attributes["username"])) { $user->username = $attributes["username"]; } else { $user->username = "******"id"]}"; } $profile->full_name = "{$attributes["first_name"]} {$attributes["last_name"]}"; return [$user, $profile]; }
/** * Send forgot email * @return bool */ public function sendForgotEmail() { /** @var Mailer $mailer */ /** @var Message $message */ /** @var \amnah\yii2\user\models\UserToken $userToken */ if ($this->validate()) { // get user $user = $this->getUser(); // calculate expireTime $expireTime = $this->module->resetExpireTime; $expireTime = $expireTime ? gmdate("Y-m-d H:i:s", strtotime($expireTime)) : null; // create userToken $userToken = $this->module->model("UserToken"); $userToken = $userToken::generate($user->id, $userToken::TYPE_PASSWORD_RESET, null, $expireTime); // modify view path to module views $mailer = Yii::$app->mailer; $oldViewPath = $mailer->viewPath; $mailer->viewPath = $this->module->emailViewPath; // send email $subject = Yii::$app->id . " - " . Yii::t("user", "Forgot password"); $result = $mailer->compose('forgotPassword', compact("subject", "user", "userToken"))->setTo($user->email)->setSubject($subject)->send(); // restore view path and return result $mailer->viewPath = $oldViewPath; return $result; } return false; }
/** * Find the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { /** @var \amnah\yii2\user\models\User $user */ $user = $this->module->model("User"); $user = $user::findOne($id); if ($user) { return $user; } throw new NotFoundHttpException('The requested page does not exist.'); }
/** * Get user based on email and/or username * @return \amnah\yii2\user\models\User|null */ public function getUser() { // check if we need to get user if ($this->user === false) { // build query based on email and/or username login properties $user = $this->module->model("User"); $user = $user::find(); if ($this->module->loginEmail) { $user->orWhere(["email" => $this->username]); } if ($this->module->loginUsername) { $user->orWhere(["username" => $this->username]); } $this->user = $user->one(); } return $this->user; }
/** * Send forgot email * @return bool */ public function sendEmail() { if (!$this->validate()) { return false; } /** @var \amnah\yii2\user\models\UserToken $userToken */ $user = $this->getUser(); $userToken = $this->module->model("UserToken"); // calculate type based on user status if ($user->status == $user::STATUS_INACTIVE) { $type = $userToken::TYPE_EMAIL_ACTIVATE; } else { $type = $userToken::TYPE_EMAIL_CHANGE; } // generate userToken and send email confirmation $userToken = $userToken::generate($user->id, $type); return $user->sendEmailConfirmation($userToken); }
/** * Reset password */ public function actionReset($token) { /** @var \amnah\yii2\user\models\User $user */ /** @var \amnah\yii2\user\models\UserToken $userToken */ // get user token and check expiration $userToken = $this->module->model("UserToken"); $userToken = $userToken::findByToken($token, $userToken::TYPE_PASSWORD_RESET); if (!$userToken) { return $this->render('reset', ["invalidToken" => true]); } // get user and set "reset" scenario $success = false; $user = $this->module->model("User"); $user = $user::findOne($userToken->user_id); $user->setScenario("reset"); // load post data and reset user password if ($user->load(Yii::$app->request->post()) && $user->save()) { // delete userToken and set success = true $userToken->delete(); $success = true; } return $this->render('reset', compact("user", "success")); }
/** * @return \yii\db\ActiveQuery */ public function getUserTokens() { $userToken = $this->module->model("UserToken"); return $this->hasMany($userToken::className(), ['user_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getUser() { $user = $this->module->model("User"); return $this->hasOne($user::className(), ['id' => 'user_id']); }
/** * @return \yii\db\ActiveQuery */ public function getUsers() { $user = $this->module->model("User"); return $this->hasMany($user::className(), ['role_id' => 'id']); }
/** * Modify createController() to handle routes in the default controller * * This is a temporary hack until they add in url management via modules * * @link https://github.com/yiisoft/yii2/issues/810 * @link http://www.yiiframework.com/forum/index.php/topic/21884-module-and-url-management/ * * "user", "user/default", "user/admin", and "user/copy" work like normal * any other "user/xxx" gets changed to "user/default/xxx" * * @inheritdoc */ public function createController($route) { // check valid routes $validRoutes = [$this->defaultRoute, "admin", "copy", "auth"]; $isValidRoute = false; foreach ($validRoutes as $validRoute) { if (strpos($route, $validRoute) === 0) { $isValidRoute = true; break; } } return (empty($route) or $isValidRoute) ? parent::createController($route) : parent::createController("{$this->defaultRoute}/{$route}"); }