ProfileController.php 5.81 KB
<?php
    namespace artbox\core\controllers;
    
    use artbox\core\behaviors\LinkBehavior;
    use artbox\core\models\ChangePassword;
    use artbox\core\models\User;
    use artbox\core\models\UserData;
    use yii\base\InvalidConfigException;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\web\Controller;

    /**
     * Class ProfileController
     *
     * @package artbox\core\controllers
     */
    class ProfileController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' => [
                                'login',
                                'error',
                            ],
                            'allow'   => true,
                        ],
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
                'verbs'  => [
                    'class'   => VerbFilter::className(),
                    'actions' => [//                        'change-password' => [ 'post' ],
                    ],
                ],
            ];
        }
        /**
         * @return bool|string
         */
        public function getViewPath()
        {
            return \Yii::getAlias('@artbox/core/views/profile');
        }
    
        /**
         * User profile page, that allows to change UserData and password.
         *
         * @param \artbox\core\models\ChangePassword|null $changePasswordModel
         * @param \artbox\core\models\UserData|null       $userData
         *
         * @return string
         * @throws \yii\base\InvalidConfigException
         */
        public function actionIndex(ChangePassword $changePasswordModel = null, UserData $userData = null)
        {
            /**
             * @var User $user
             */
            $user = \Yii::$app->user->identity;
            /**
             * @var LinkBehavior $userDataBehavior
             */
            if (empty( $userData )) {
                if (( $userDataBehavior = $user->getBehavior('userData') ) === null) {
                    throw new InvalidConfigException(
                        \Yii::t('core', '$app->user->identity class must have LinkBehavior to UserData model')
                    );
                }
                $userDataBehavior->ensureExistance();
                $userData = $userDataBehavior->getLinkObject();
            }
            if (empty( $changePasswordModel )) {
                /**
                 * @var ChangePassword $changePasswordModel
                 */
                $changePasswordModel = \Yii::createObject(ChangePassword::className());
            }
            return $this->render(
                'index',
                [
                    'user'                => $user,
                    'userData'            => $userData,
                    'changePasswordModel' => $changePasswordModel,
                ]
            );
        }
    
        /**
         * Change User password
         *
         * @return string|\yii\web\Response
         */
        public function actionChangePassword()
        {
            if (empty( \Yii::$app->request->post() )) {
                return $this->redirect([ 'index' ]);
            }
            /**
             * @var ChangePassword $model
             */
            $model = \Yii::createObject(ChangePassword::className());
            $success = false;
            if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
                if ($model->validatePassword()) {
                    if (( $success = $model->saveChanges() ) === true) {
                        \Yii::$app->session->setFlash('success', \Yii::t('core', 'Password was successfully changed.'));
                    }
                }
            }
            if (!$success) {
                \Yii::$app->session->setFlash('error', \Yii::t('core', 'Password change failed.'));
            }
            $model->oldPassword = $model->newPassword = $model->newPasswordRepeat = null;
            return $this->actionIndex($model);
        }
    
        /**
         * Update UserData
         *
         * @return string|\yii\web\Response
         * @throws \yii\base\InvalidConfigException
         */
        public function actionUpdate()
        {
            if (empty( \Yii::$app->request->post() )) {
                return $this->redirect([ 'index' ]);
            }
            /**
             * @var User $user
             */
            $user = \Yii::$app->user->identity;
            /**
             * @var LinkBehavior $userDataBehavior
             */
            if (( $userDataBehavior = $user->getBehavior('userData') ) === null) {
                throw new InvalidConfigException(
                    \Yii::t('core', '$app->user->identity class must have LinkBehavior to UserData model')
                );
            }
            $userDataBehavior->ensureExistance();
            $model = $userDataBehavior->getLinkObject();
            $success = false;
            if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
                if (( $success = $model->save() ) === true) {
                    \Yii::$app->session->setFlash(
                        'success',
                        \Yii::t('core', 'Personal information was successfully changed.')
                    );
                }
            }
            if (!$success) {
                \Yii::$app->session->setFlash('error', \Yii::t('core', 'Personal information change failed.'));
            }
            return $this->actionIndex(null, $model);
        }
    }