@Nikolaymac

Как передать password при редактировании?

Суть такая. Когда создается пользователь стоит сценарий Create, все ок password передается и есть в модели после load
Когда делаю update сценарий стоит default и поле password не передается ! где это можно поправить ? что бы не переписывать сценарий для каждой модели
public function actionUpdate($id)
    {

        // load user data
        $user = $this->findModel($id);
        // $user->scenario = 'create';
        $auth = Yii::$app->authManager;
        // get user role if he has one  
        if ($roles = $auth->getRolesByUser($id)) {
            // it's enough for us the get first assigned role name
            $role = array_keys($roles)[0]; 
        }
        // if user has role, set oldRole to that role name, else offer 'member' as sensitive default
        $oldRole = (isset($role)) ? $auth->getRole($role) : $auth->getRole('member');
        // set property item_name of User object to this role name, so we can use it in our form
        $user->item_name = $oldRole->name;

        if (!$user->load(Yii::$app->request->post())) {

            return $this->render('update', ['user' => $user, 'role' => $user->item_name]);
        }
        // print_r($user);
        // only if user entered new password we want to hash and save it
       
        if ($user->password) {
           
            $user->setPassword($user->password);
        }
        // if admin is activating user manually we want to remove account activation token
        // if ($user->status == User::STATUS_ACTIVE && $user->account_activation_token != null) {
        //     $user->removeAccountActivationToken();
        // }         
        if (!$user->save()) {
            return $this->render('update', ['user' => $user, 'role' => $user->item_name]);
        }
        // take new role from the form
        $newRole = $auth->getRole($user->item_name);
        // get user id too
        $userId = $user->getId();
        
        // we have to revoke the old role first and then assign the new one
        // this will happen if user actually had something to revoke
        if ($auth->revoke($oldRole, $userId)) {
            $info = $auth->assign($newRole, $userId);
        }
        // in case user didn't have role assigned to him, then just assign new one
        if (!isset($role)) {
            $info = $auth->assign($newRole, $userId);
        }
        if (!$info) {
            Yii::$app->session->setFlash('error', Yii::t('app', 'There was some error while saving user role.'));
        }
        return $this->redirect(['view', 'id' => $user->id]);
    }
  • Вопрос задан
  • 122 просмотра
Пригласить эксперта
Ответы на вопрос 1
@BorisKorobkov Куратор тега PHP
Web developer
И правильно, что не передается.
Исходный пароль не должен храниться на сервере. Только его хэш, желательно посоленный.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы