backend/controllers/UsersController.php
/**
     * Updates an existing Users model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
Updates an existing Users model.
If update is successful, the browser will be redirected to the 'view' page.
@param string $id
@return mixed
Updates an existing Users model . If update is successful the browser will be redirected to the ' view ' page . @param string $id @return mixed
public function actionUpdate($id)
    {
        $user = $this->findModel($id);
        $current_license_business_from = $user->license_business_from;
        $current_license_type = $user->license_type;
        $password = new DynamicModel(['password']);
        $old_user_ref_id = $user->user_ref_id;
        if ($user->load(Yii::$app->request->post())) {
            if (Yii::$app->params['self_hosted']) {
                $user->license_type = Licenses::TYPE_PAYED_BUSINESS_USER;
                $businessAdminAuto = Users::find()
                    ->where([
                        'license_type' => Licenses::TYPE_PAYED_BUSINESS_ADMIN,
                    ])
                    ->orderBy(['user_id' => SORT_ASC])
                    ->limit(1)
                    ->one();
                if ($businessAdminAuto) {
                    $user->license_business_from = $businessAdminAuto->user_id;
                }
            }
            if ($user->license_type == Licenses::TYPE_PAYED_BUSINESS_USER && !$user->license_business_from) {
                $user->addError('license_business_from', 'Field license_business_from must be set for license_type = BUSINESS_USER');
                $user->addError('license_type', 'Field license_business_from must be set for license_type = BUSINESS_USER');
                $user->addError('user_email', 'Field license_business_from must be set for license_type = BUSINESS_USER');
                return $this->render('update', [
                    'user' => $user,
                    'password' => $password,
                    'Admin' => $this->Admins,
                ]);
            }
            if ($user->license_type != Licenses::TYPE_PAYED_BUSINESS_USER) {
                $user->license_business_from = null;
            }
            if ($user->license_business_from) {
                $businessAdmin = Users::findOne([
                    'user_id'      => $user->license_business_from,
                    'license_type' => Licenses::TYPE_PAYED_BUSINESS_ADMIN,
                ]);
                if (!$businessAdmin) {
                    $user->addError('license_business_from', "Not exist BusinessAdmin with UserID = {$user->license_business_from}");
                    $user->addError('user_email', "Not exist BusinessAdmin with UserID = {$user->license_business_from}");
                    return $this->render('update', [
                        'user' => $user,
                        'password' => $password,
                        'Admin' => $this->Admins,
                    ]);
                }
                if ($current_license_business_from != $user->license_business_from) {
                    $checkLicenseAvailable = UserLicenses::getFreeLicense($businessAdmin->user_id);
                    if (!$checkLicenseAvailable) {
                        $user->addError('license_business_from', "this business-admin (UserID = {$businessAdmin->user_id}) has no enough licenses to create a new user");
                        $user->addError('user_email', "this business-admin (UserID = {$businessAdmin->user_id}) has no enough licenses to create a new user");
                        return $this->render('create', [
                            'user' => $user,
                            'password' => $password,
                            'Admin' => $this->Admins,
                        ]);
                    }
                }
                $user->license_expire = $businessAdmin->license_expire;
                $user->license_period = $businessAdmin->license_period;
            }
            if ($this->Admins->admin_role != Admins::ROLE_ROOT) {
                $user->user_ref_id = $old_user_ref_id;
            }
            if (!empty($user->license_business_from) &&
                !empty($current_license_business_from) &&
                $user->license_business_from != $current_license_business_from)
            {
                $business_admin_changed = true;
            }
            if (($current_license_type == Licenses::TYPE_PAYED_BUSINESS_USER && $user->license_type != Licenses::TYPE_PAYED_BUSINESS_USER) || isset($business_admin_changed))
            {
                $modelShareDel = new ShareElementForm(['colleague_email']);
                $modelShareDel->colleague_email = $user->user_email;
                $modelShareDel->owner_user_id = $current_license_business_from;
                $modelShareDel->adminPanelColleagueDelete();
            }
            if ($user->save()) {
                    $modelShareDel = new ShareElementForm(['colleague_email']);
                    $modelShareDel->colleague_email = $user->user_email;
                    $modelShareDel->owner_user_id = $current_license_business_from;
                    $modelShareDel->adminPanelColleagueDelete();
                }
                if ($user->license_type == Licenses::TYPE_PAYED_BUSINESS_USER && isset($businessAdmin) && $businessAdmin) {
                    if ($current_license_business_from != $user->license_business_from) {
                        $user->license_type = Licenses::TYPE_FREE_DEFAULT;
                        $user->save();
                        $modelShareAdd = new ShareElementForm(['colleague_email']);
                        $modelShareAdd->colleague_email = $user->user_email;
                        $modelShareAdd->adminPanelCreateNullCollaboration($businessAdmin);
                    }
                }
                return $this->redirect(['view', 'id' => $user->user_id]);
            }
        }
        return $this->render('update', [
            'user' => $user,
            'password' => $password,
            'Admin' => $this->Admins,
        ]);
    }
public function actionUpdate ( $ id ) { $ user = $ this -> findModel ( $ id ) ; $ current_license_business_from = $ user -> license_business_from ; $ current_license_type = $ user -> license_type ; $ password = new DynamicModel ( [ 'password' ] ) ; $ old_user_ref_id = $ user -> user_ref_id ; if ( $ user -> load ( Yii :: $ app -> request -> post ( ) ) ) { if ( Yii :: $ app -> params [ 'self_hosted' ] ) { $ user -> license_type = Licenses :: TYPE_PAYED_BUSINESS_USER ; $ businessAdminAuto = Users :: find ( ) -> where ( [ 'license_type' => Licenses :: TYPE_PAYED_BUSINESS_ADMIN , ] ) -> orderBy ( [ 'user_id' => SORT_ASC ] ) -> limit ( 1 ) -> one ( ) ; if ( $ businessAdminAuto ) { $ user -> license_business_from = $ businessAdminAuto -> user_id ; } } if ( $ user -> license_type == Licenses :: TYPE_PAYED_BUSINESS_USER && ! $ user -> license_business_from ) { $ user -> addError ( 'license_business_from' , 'Field license_business_from must be set for license_type = BUSINESS_USER' ) ; $ user -> addError ( 'license_type' , 'Field license_business_from must be set for license_type = BUSINESS_USER' ) ; $ user -> addError ( 'user_email' , 'Field license_business_from must be set for license_type = BUSINESS_USER' ) ; return $ this -> render ( 'update' , [ 'user' => $ user , 'password' => $ password , 'Admin' => $ this -> Admins , ] ) ; } if ( $ user -> license_type != Licenses :: TYPE_PAYED_BUSINESS_USER ) { $ user -> license_business_from = null ; } if ( $ user -> license_business_from ) { $ businessAdmin = Users :: findOne ( [ 'user_id' => $ user -> license_business_from , 'license_type' => Licenses :: TYPE_PAYED_BUSINESS_ADMIN , ] ) ; if ( ! $ businessAdmin ) { $ user -> addError ( 'license_business_from' , " Not exist BusinessAdmin with UserID =  { $ user -> license_business_from } " ) ; $ user -> addError ( 'user_email' , " Not exist BusinessAdmin with UserID =  { $ user -> license_business_from } " ) ; return $ this -> render ( 'update' , [ 'user' => $ user , 'password' => $ password , 'Admin' => $ this -> Admins , ] ) ; } if ( $ current_license_business_from != $ user -> license_business_from ) { $ checkLicenseAvailable = UserLicenses :: getFreeLicense ( $ businessAdmin -> user_id ) ; if ( ! $ checkLicenseAvailable ) { $ user -> addError ( 'license_business_from' , " this business-admin (UserID =  { $ businessAdmin -> user_id } ) has no enough licenses to create a new user " ) ; $ user -> addError ( 'user_email' , " this business-admin (UserID =  { $ businessAdmin -> user_id } ) has no enough licenses to create a new user " ) ; return $ this -> render ( 'create' , [ 'user' => $ user , 'password' => $ password , 'Admin' => $ this -> Admins , ] ) ; } } $ user -> license_expire = $ businessAdmin -> license_expire ; $ user -> license_period = $ businessAdmin -> license_period ; } if ( $ this -> Admins -> admin_role != Admins :: ROLE_ROOT ) { $ user -> user_ref_id = $ old_user_ref_id ; } if ( ! empty ( $ user -> license_business_from ) && ! empty ( $ current_license_business_from ) && $ user -> license_business_from != $ current_license_business_from ) { $ business_admin_changed = true ; } if ( ( $ current_license_type == Licenses :: TYPE_PAYED_BUSINESS_USER && $ user -> license_type != Licenses :: TYPE_PAYED_BUSINESS_USER ) || isset ( $ business_admin_changed ) ) { $ modelShareDel = new ShareElementForm ( [ 'colleague_email' ] ) ; $ modelShareDel -> colleague_email = $ user -> user_email ; $ modelShareDel -> owner_user_id = $ current_license_business_from ; $ modelShareDel -> adminPanelColleagueDelete ( ) ; } if ( $ user -> save ( ) ) { if ( $ user -> license_type == Licenses :: TYPE_PAYED_BUSINESS_USER && isset ( $ businessAdmin ) && $ businessAdmin ) { if ( $ current_license_business_from != $ user -> license_business_from ) { $ user -> license_type = Licenses :: TYPE_FREE_DEFAULT ; $ user -> save ( ) ; $ modelShareAdd = new ShareElementForm ( [ 'colleague_email' ] ) ; $ modelShareAdd -> colleague_email = $ user -> user_email ; $ modelShareAdd -> adminPanelCreateNullCollaboration ( $ businessAdmin ) ; } } return $ this -> redirect ( [ 'view' , 'id' => $ user -> user_id ] ) ; } } return $ this -> render ( 'update' , [ 'user' => $ user , 'password' => $ password , 'Admin' => $ this -> Admins , ] ) ; }
Updates an existing Users model.
Updates an existing Users model .
//var_dump($old_user_ref_id); exit; /* Автоназначение админа для юзеров в случае СХ */ /** @var \common\models\Users $businessAdminAuto */ /* если бизнес-юзер лицензия, то должно быть задано поле license_business_from иначе на даем сохранять такое в БД */ /* Если же лицензия не бизнес-юзер, то установим license_business_from = null */ /* Если задан license_business_from то проверим что он существует в БД
             * А так же что у бизнес-админа есть свободная лицензия для этого юзера
             */ /**/ /* если сменился бизнес-админ */ /* если перестал быть бизнес-юзером или сменился бизнес админ освободить лицензии и колабы */ /* если сменился бизнес-админ для этого бизнес-юзера нужно удалить предыдущие колабы и лицензии освободить */ //                if ($user->license_type == Licenses::TYPE_PAYED_BUSINESS_USER && //                    isset($businessAdmin) && $businessAdmin && !empty($current_license_business_from) && //                    $user->license_business_from != $current_license_business_from) { // //                    $modelShareDel = new ShareElementForm(['colleague_email']); //                    $modelShareDel->colleague_email = $user->user_email; //                    $modelShareDel->owner_user_id = $current_license_business_from; //                    $modelShareDel->adminPanelColleagueDelete(); // //                } /* а тут назначить новую лицензию от нового бизнес-юзера */