/**
  * @return bool
  */
 public function change()
 {
     if (!$this->validate()) {
         return false;
     }
     $this->user->password = $this->user->passwordToHash($this->password);
     return $this->user->save();
 }
Esempio n. 2
0
 /**
  * @param \app\core\models\User $identity
  * @param bool $cookieBased
  * @param int $duration
  */
 public function afterLogin($identity, $cookieBased, $duration)
 {
     $user = User::findIdentity($identity->user_id);
     if ($user) {
         //更新已存在用户
     } else {
         //保存新用户
         $user = new User();
         $user->attributes = $identity->attributes;
         $user->save();
     }
 }
 public function register()
 {
     if (!$this->validate()) {
         return false;
     }
     $user = new User();
     $user->attributes = $this->attributes;
     $user->password = $user->passwordToHash($this->password);
     $user->role = UserRole::USER;
     if (!$user->save()) {
         $this->addErrors($user->getErrors());
         return false;
     }
     \Yii::$app->mailer->compose('@app/auth/mail/registration', ['user' => $user])->setTo($user->email)->send();
     return true;
 }
Esempio n. 4
0
 /**
  *
  * @return User|null
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = User::findByLogin($this->username);
     }
     return $this->_user;
 }
 public function actionPassword($userUid = null)
 {
     $form = new PasswordUpdate();
     $form->user = $userUid ? User::findOne($userUid) : Yii::$app->user->model;
     if ($form->load(Yii::$app->request->post()) && $form->change()) {
         \Yii::$app->session->setFlash('success', 'Пароль успешно изменен!');
         return $this->refresh();
     }
     return $this->render('password', ['model' => $form]);
 }
 /**
  * @param string $userUid
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionView($userUid)
 {
     /** @var User $userModel */
     $userModel = User::findOne($userUid);
     if (!$userModel) {
         throw new NotFoundHttpException();
     }
     Yii::$app->megaMenu->getActiveItem()->label = $userModel->name;
     return $this->render('view', ['userModel' => $userModel]);
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = User::find()->with('info');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'role', $this->role]);
     return $dataProvider;
 }
 /**
  * Sends password recovery instructions via e-mail.
  * @return bool
  */
 public function send()
 {
     if (!$this->validate()) {
         return false;
     }
     /** @var User $user */
     $user = User::findByLogin($this->email);
     $user->recoveryKey = Yii::$app->getSecurity()->generateRandomString(32);
     $user->save(false);
     \Yii::$app->mailer->compose('@app/auth/mail/recovery', ['user' => $user, 'url' => Url::to(['/auth/recovery/code', 'code' => $user->recoveryKey], true)])->setTo($user->email)->send();
     return true;
 }
 /**
  * @param string $key password recovery key.
  * @throws Exception if can't update model.
  * @return bool
  */
 public function resetPassword($key)
 {
     /** @var User $user */
     $user = User::find()->where(['recoveryKey' => $key])->one();
     if (!$user || !$this->validate()) {
         return false;
     }
     $user->password = $user->passwordToHash($this->password);
     if (!$user->validate(['password'])) {
         $this->addErrors($user->getErrors());
         return false;
     }
     $user->recoveryKey = null;
     $user->saveOrPanic();
     // Auto login
     Yii::$app->user->login($user);
     return true;
 }
Esempio n. 10
0
 public function getUser()
 {
     return $this->hasOne(User::className(), ['uid' => 'userUid']);
 }
Esempio n. 11
0
 /**
  * Update an existing article
  *
  * @param  number     $id
  * @param  array      $input_data
  * @throws \Exception
  * @return unknown
  */
 public function update($input_data)
 {
     $article = Article::findFirstById($input_data['id']);
     if (!$article) {
         throw new \Exception('Article not found', 404);
     }
     $data = $this->prepareData($input_data);
     $article->setArticleIsPublished($data['article_is_published']);
     $article->setArticleUpdatedAt(new \Phalcon\Db\RawValue('NOW()'));
     foreach ($data['translations'] as $lang => $translation) {
         $article->getTranslations()->filter(function ($t) use($lang, $translation) {
             if ($t->getArticleTranslationLang() == $lang) {
                 $t->assign($translation);
                 $t->update();
             }
         });
     }
     $results = ArticleCategoryArticle::findByArticleId($input_data['id']);
     if ($results) {
         $results->delete();
     }
     if ($data['categories']) {
         $article->categories = Category::find(["id IN (" . $data['categories'] . ")"])->filter(function ($category) {
             return $category;
         });
     }
     $results = ArticleHashtagArticle::findByArticleId($input_data['id']);
     if ($results) {
         $results->delete();
     }
     if ($data['hashtags']) {
         $article->hashtags = Hashtag::find(["id IN (" . $data['hashtags'] . ")"])->filter(function ($hashtag) {
             return $hashtag;
         });
     }
     $user = User::findFirstById((int) $data['article_user_id']);
     if (!$user) {
         throw new \Exception('User not found', 404);
     }
     $article->setArticleUserId($data['article_user_id']);
     return $this->save($article, 'update');
 }
Esempio n. 12
0
 /**
  * Delete an existing record
  * @param  number     $id
  * @throws \Exception
  * @return boolean
  */
 public function delete($id)
 {
     $object = User::findFirstById($id);
     if (!$object) {
         throw new \Exception('Object not found');
     }
     if (false === $object->delete()) {
         foreach ($object->getMessages() as $message) {
             $error[] = (string) $message;
         }
         throw new \Exception(json_encode($error));
     }
     return true;
 }
Esempio n. 13
0
 public function getCreator()
 {
     return $this->hasOne(User::className(), ['uid' => 'creatorUserUid']);
 }
Esempio n. 14
0
 /**
  * Get the entity related to user in the active identity
  *
  * @return App\Core\Models\User
  */
 public function getUser()
 {
     $identity = $this->session->get('identity');
     if (isset($identity['id'])) {
         $user = User::findFirstById($identity['id']);
         if ($user == false) {
             throw new \Exception('The user does not exist');
         }
         return $user;
     }
     return false;
 }
Esempio n. 15
0
 /**
  * Creates the remember me environment settings, the related cookies and generates tokens
  *
  * @param App\Core\Models\User $user
  */
 public function createRememberEnvironment($user)
 {
     $user_agent = $this->request->getUserAgent();
     $token = md5($user->getUserEmail() . $user->getUserPassword() . $user_agent);
     $remember = new UserRememberTokens();
     $remember->setUserId($user->getId());
     $remember->setToken($token);
     $remember->setUserAgent($user_agent);
     $remember->setCreatedAt(time());
     if ($remember->save() != false) {
         $expire = time() + 86400 * 30;
         $this->cookies->set('RMU', $user->getId(), $expire);
         $this->cookies->set('RMT', $token, $expire);
     }
 }