コード例 #1
0
 /**
  * Shows user's profile.
  *
  * @param int $id
  *
  * @return \yii\web\Response
  * @throws \yii\web\NotFoundHttpException
  */
 public function actionShow($id)
 {
     $profile = Profile::find()->byId($id)->one();
     if ($profile === null) {
         throw new NotFoundHttpException();
     }
     return $this->render('show', ['profile' => $profile]);
 }
コード例 #2
0
ファイル: User.php プロジェクト: yujin1st/yii2-user
 /** @inheritdoc */
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if ($insert) {
         if ($this->_profile == null) {
             $this->_profile = Yii::createObject(Profile::className());
         }
         $this->_profile->link('user', $this);
     }
     if ($insert) {
         $this->assignRoles();
     }
     if ($this->scenario == self::SCENARIO_UPDATE_ROLES) {
         $auth = Yii::$app->authManager;
         $auth->revokeAll($this->id);
         foreach ($this->_roles as $role) {
             $auth->assign($auth->getRole($role), $this->id);
         }
     }
 }
コード例 #3
0
 /**
  * Updates an existing profile.
  *
  * @param int $id
  *
  * @return mixed
  */
 public function actionUpdateProfile($id)
 {
     Url::remember('', 'actions-redirect');
     $user = $this->findModel($id);
     $profile = $user->profile;
     if ($profile == null) {
         $profile = new Profile();
         $profile->link('user', $user);
     }
     $event = $this->getProfileEvent($profile);
     $this->performAjaxValidation($profile);
     $this->trigger(self::EVENT_BEFORE_PROFILE_UPDATE, $event);
     if ($profile->load(Yii::$app->request->post()) && $profile->save()) {
         Yii::$app->getSession()->setFlash('success', Yii::t('users', 'Profile details have been updated'));
         $this->trigger(self::EVENT_AFTER_PROFILE_UPDATE, $event);
         return $this->refresh();
     }
     return $this->render('_profile', ['user' => $user, 'profile' => $profile]);
 }
コード例 #4
0
 /**
  * Shows profile settings form.
  *
  * @return string|\yii\web\Response
  */
 public function actionProfile()
 {
     $model = Profile::find()->byId(Yii::$app->user->identity->getId())->one();
     if ($model == null) {
         $model = Yii::createObject(Profile::className());
         $model->link('user', Yii::$app->user->identity);
     }
     $event = $this->getProfileEvent($model);
     $this->performAjaxValidation($model);
     $this->trigger(self::EVENT_BEFORE_PROFILE_UPDATE, $event);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Yii::$app->getSession()->setFlash('success', Yii::t('users', 'Your profile has been updated'));
         $this->trigger(self::EVENT_AFTER_PROFILE_UPDATE, $event);
         return $this->refresh();
     }
     return $this->render('profile', ['model' => $model]);
 }