Пример #1
0
 /**
  * __method_systemUser_description__
  * @return __return_systemUser_type__ __return_systemUser_description__
  * @throws Exception                  __exception_Exception_description__
  */
 public static function systemUser()
 {
     $user = self::findOne([self::tableName() . '.' . 'email' => self::SYSTEM_EMAIL], false);
     if (empty($user)) {
         $superGroup = Group::find()->disableAccessCheck()->where(['system' => 'super_administrators'])->one();
         if (!$superGroup) {
             return false;
         }
         $userClass = self::className();
         $user = new $userClass();
         $user->scenario = 'creation';
         $user->first_name = 'System';
         $user->last_name = 'User';
         $user->email = self::SYSTEM_EMAIL;
         $user->status = static::STATUS_INACTIVE;
         $user->password = Yii::$app->security->generateRandomKey();
         $user->relationModels = [['parent_object_id' => $superGroup->primaryKey]];
         if (!$user->save()) {
             \d($user->email);
             \d($user->errors);
             throw new Exception("Unable to save system user!");
         }
     }
     return $user;
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     $user = new User();
     $user->scenario = 'creation';
     $user->attributes = $this->input['admin'];
     $user->status = User::STATUS_ACTIVE;
     $superGroup = Group::find()->disableAccessCheck()->where(['system' => 'super_administrators'])->one();
     if (!$superGroup) {
         throw new Exception("Unable to find super_administrators group!");
     }
     $user->relationModels = [['parent_object_id' => $superGroup->primaryKey]];
     if ($user->save()) {
         return true;
     }
     foreach ($user->errors as $field => $errors) {
         $this->fieldErrors[$field] = implode('; ', $errors);
     }
     var_dump($this->fieldErrors);
     exit;
     return false;
 }
Пример #3
0
 /**
  *
  */
 public function groupWalker(&$item, $key, $mparent = null)
 {
     if (is_array($item)) {
         $parent = Group::find()->disableAccessCheck()->where(['name' => $key])->one();
         if (empty($parent)) {
             $parent = new Group();
             //$parent->disableAcl();
             $parent->name = $key;
             $parent->system = preg_replace('/ /', '_', strtolower($parent->name));
             $parent->level = $this->getGroupLevel($key);
             if (!$parent->save()) {
                 $this->errors[] = "Failed to create group {$key}!";
                 return false;
             }
             if (!empty($mparent)) {
                 $r = new Relation();
                 $r->parent_object_id = $mparent;
                 $r->child_object_id = $parent->id;
                 $r->active = 1;
                 if (!$r->save()) {
                     $this->errors[] = "Failed to create group relationship {$key}!";
                     return false;
                 }
             }
         }
         $item = array_walk($item, [$this, 'groupWalker'], $parent->id);
     } else {
         $sitem = Group::find()->disableAccessCheck()->where(['name' => $item])->one();
         if (empty($sitem)) {
             $sitem = new Group();
             //$sitem->disableAcl();
             $sitem->name = $item;
             $sitem->system = preg_replace('/ /', '_', strtolower($sitem->name));
             $sitem->level = $this->getGroupLevel($item);
             if (!$sitem->save()) {
                 $this->errors[] = "Failed to create group {$item}!";
                 return false;
             }
             if (!empty($mparent)) {
                 $r = new Relation();
                 $r->parent_object_id = $mparent;
                 $r->child_object_id = $sitem->id;
                 $r->active = 1;
                 if (!$r->save()) {
                     $this->errors[] = "Failed to create group relationship {$key}!";
                     return false;
                 }
             }
         }
         $setup->registry['Group'][$item] = $sitem->id;
     }
 }