/** * @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; }
/** * Create a user. */ public function actionCreate() { $groups = Group::find()->disableAccessCheck()->orderBy('name')->all(); $this->out("Groups"); $options = []; $i = 1; $defaultGroup = null; foreach ($groups as $group) { $extra = ''; if ($group->system === 'users') { $defaultGroup = $group->primaryKey; $extra = '*'; } $options[$i] = $group->primaryKey; $this->out("{$i}) {$group->descriptor}{$extra}"); $i++; } $options[''] = $defaultGroup; $group = Console::select("Choose", $options); if (empty($group)) { $group = $defaultGroup; } else { $group = $options[$group]; } $user = new User(); $user->scenario = 'creation'; $user->first_name = $this->prompt("First name"); $user->last_name = $this->prompt("Last name"); $user->email = $this->prompt("Email"); $user->status = 1; $user->username = $this->prompt("Username"); $user->password = $this->prompt("Password"); $user->registerRelationModel(['parent_object_id' => $group]); if (!$user->validate()) { \d($user->errors); $this->stderr("User didn't validate!"); exit; } $individual = $user->guessIndividual(); if (empty($individual)) { if (!Console::confirm("No matching individual was found. Continue?")) { $this->stderr("Bye!"); exit; } } elseif (is_object($individual)) { $user->object_individual_id = $individual->primaryKey; if (!Console::confirm("Matching individual was found ({$individual->descriptor})! Continue?")) { $this->stderr("Bye!"); exit; } } else { $options = []; $i = 1; $this->out("Possible Individual Matches..."); foreach ($individual as $ind) { $options[$i] = $ind->primaryKey; $this->out("{$i}) {$ind->descriptor}"); $i++; } $user->object_individual_id = Console::select("Choose", $options); } if ($user->save()) { $this->out("User created!"); } else { \d($user->errors); $this->out("Error creating user!"); } }
/** * [[@doctodo method_description:systemUser]]. * * @throws Exception [[@doctodo exception_description:Exception]] * @return [[@doctodo return_type:systemUser]] [[@doctodo return_description:systemUser]] * */ 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; }
/** * Set up. * * @return [[@doctodo return_type:setup]] [[@doctodo return_description:setup]] */ public function setup() { $results = [true]; if (!empty($this->primaryModel) && !empty($this->collectorItem->parents)) { $groups = ['top']; foreach ($groups as $groupName) { $group = Group::getBySystemName($groupName, false); if (empty($group)) { continue; } if ($this->inheritParentAccess) { $results[] = $this->objectTypeModel->parentAccess(null, $group); } } } return min($results); }
/** * */ 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; } }
/** * @inheritdoc */ public function getPrimaryModel() { return Group::className(); }