backend/controllers/UsersController.php
/**
     * Creates a new Users model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new Users model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new Users model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $user = new Users();
        $user->user_balance = '0.00';
        $password = new DynamicModel(['password']);
        $password->addRule(['password'], 'required')
                 ->addRule(['password'], 'string', ['min' => 6]);
        if ($user->load(Yii::$app->request->post()) && $password->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('create', [
                    '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('create', [
                        'user' => $user,
                        'password' => $password,
                        'Admin' => $this->Admins,
                    ]);
                }
                $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;
            }
            $user->user_name = $user->user_email;
            $user->setPassword($password->password);
            $user->generateAuthKey();
            if ($this->Admins->admin_role != Admins::ROLE_ROOT) {
                $user->user_ref_id = $this->Admins->admin_id;
            }
            if (isset($businessAdmin) && $businessAdmin) {
                $user->license_type = Licenses::TYPE_FREE_DEFAULT;
            }
            if ($user->save()) {
                if (isset($businessAdmin) && $businessAdmin) {
                    $modelShare = new ShareElementForm(['colleague_email']);
                    $modelShare->colleague_email = $user->user_email;
                    $ret = $modelShare->adminPanelCreateNullCollaboration($businessAdmin);
                }
                if (isset($_POST['send_email_about_registration'])) {
                    MailTemplatesStatic::sendByKey(MailTemplatesStatic::template_key_newRegister, $user->user_email, ['UserObject' => $user]);
                }
                Yii::$app->session->setFlash('success', 'User successfully created.');
                return $this->redirect(['view', 'id' => $user->user_id]);
            } else {
                Yii::$app->session->setFlash('danger', 'There was an error on creating new User.');
            }
        }
        $user->user_status = Users::STATUS_ACTIVE;
        return $this->render('create', [
            'user' => $user,
            'password' => $password,
            'Admin' => $this->Admins,
        ]);
    }
public function actionCreate ( ) { $ user = new Users ( ) ; $ user -> user_balance = '0.00' ; $ password = new DynamicModel ( [ 'password' ] ) ; $ password -> addRule ( [ 'password' ] , 'required' ) -> addRule ( [ 'password' ] , 'string' , [ 'min' => 6 ] ) ; if ( $ user -> load ( Yii :: $ app -> request -> post ( ) ) && $ password -> 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 ( 'create' , [ '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 ( 'create' , [ 'user' => $ user , 'password' => $ password , 'Admin' => $ this -> Admins , ] ) ; } $ 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 ; } $ user -> user_name = $ user -> user_email ; $ user -> setPassword ( $ password -> password ) ; $ user -> generateAuthKey ( ) ; if ( $ this -> Admins -> admin_role != Admins :: ROLE_ROOT ) { $ user -> user_ref_id = $ this -> Admins -> admin_id ; } if ( isset ( $ businessAdmin ) && $ businessAdmin ) { $ user -> license_type = Licenses :: TYPE_FREE_DEFAULT ; } if ( $ user -> save ( ) ) { if ( isset ( $ businessAdmin ) && $ businessAdmin ) { $ modelShare = new ShareElementForm ( [ 'colleague_email' ] ) ; $ modelShare -> colleague_email = $ user -> user_email ; $ ret = $ modelShare -> adminPanelCreateNullCollaboration ( $ businessAdmin ) ; } if ( isset ( $ _POST [ 'send_email_about_registration' ] ) ) { MailTemplatesStatic :: sendByKey ( MailTemplatesStatic :: template_key_newRegister , $ user -> user_email , [ 'UserObject' => $ user ] ) ; } Yii :: $ app -> session -> setFlash ( 'success' , 'User successfully created.' ) ; return $ this -> redirect ( [ 'view' , 'id' => $ user -> user_id ] ) ; } else { Yii :: $ app -> session -> setFlash ( 'danger' , 'There was an error on creating new User.' ) ; } } $ user -> user_status = Users :: STATUS_ACTIVE ; return $ this -> render ( 'create' , [ 'user' => $ user , 'password' => $ password , 'Admin' => $ this -> Admins , ] ) ; }
Creates a new Users model.
Creates a new Users model .
//return $this->redirect('index'); /* Автоназначение админа для юзеров в случае СХ */ /** @var \common\models\Users $businessAdminAuto */ /* если бизнес-юзер лицензия, то должно быть задано поле license_business_from иначе на даем сохранять такое в БД */ /* Если же лицензия не бизнес-юзер, то установим license_business_from = null */ /* Если задан license_business_from то проверим что он существует в БД
             * А так же что у бизнес-админа есть свободная лицензия для этого юзера
             */ /*
             * тут специально ставим лицензию ФРИТРИАЛ что бы сработала
             * вот эта хуйня ниже adminPanelCreateNullCollaboration
             * она списывает лицуху с бизнес-админа только если лицуха у колеги
             * фри или фритриал
             */ /* сохраняем юзера */ /* Если это бизнес-юзер, нужно создать нулл-колаборацию что бы сняло лицензию у бизнес-админа и показало его в списке коллег */ //var_dump($ret); exit; /* отправка письма юзеру, если установлена галка что нужно отправить */ //exit; /* флеш-сообщение */