ProfileController.php 4.96 KB
<?php
    namespace backend\controllers;
    
    use artbox\core\behaviors\LinkBehavior;
    use artbox\core\models\ChangePassword;
    use common\models\User;
    use common\models\UserData;
    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' ],
                    ],
                ],
            ];
        }
        
    
        /**
         * User profile page, that allows to change UserData and password.
         *
         * @param \artbox\core\models\ChangePassword|null $changePasswordModel
         * @param \common\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;
            $userData = new UserData();
            if (empty( $changePasswordModel )) {
                /**
                 * @var ChangePassword $changePasswordModel
                 */
                $changePasswordModel = \Yii::createObject(ChangePassword::className());
            }
            return $this->render(
                'index',
                [
                    'user'                => $user,
                    'changePasswordModel' => $changePasswordModel,
                    'userData'            => $userData
                ]
            );
        }
    
        /**
         * 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
             */
            $data = new UserData();
            $changePassword = \Yii::createObject(ChangePassword::className());
            $successData = true;
            if ($data->load(\Yii::$app->request->post()) && $data->validate()) {
                $successData = $data->saveData();
            }
            $successPassword = true;
            if ($changePassword->load(\Yii::$app->request->post()) && $changePassword->validatePassword()){
                $successPassword = $changePassword->saveChanges();
            }
            if ($successPassword && $successData){
                \Yii::$app->session->setFlash('success', \Yii::t('core', 'You data changed.'));
            } else {
                \Yii::$app->session->setFlash('error', \Yii::t('core', 'Change failed.'));
            }
            return $this->actionIndex($changePassword, $data);
        }
    }