/** * Execute the console command. * * @return mixed */ public function handle() { $name = $this->argument('name'); /** @var Role $role */ $role = $this->loadRole($name); if ($role == null) { return; } $this->table(['Name', 'Display Name', 'Description', 'Id'], [[$role->{Role::PROPERTY_NAME}, $role->{Role::PROPERTY_DISPLAY}, $role->{Role::PROPERTY_DESC}, $role->{Role::PROPERTY_KEY}]]); $perms = $role->perms()->get([Permission::PROPERTY_NAME])->toArray(); if (empty($perms)) { $list = '<none>'; } else { $list = $this->toArrayImplode(', ', Permission::PROPERTY_NAME, $perms); } $this->info('Permissions: ' . $list); $identField = User::calcUserIdentityField(); $users = $role->users()->get([$identField])->toArray(); if (empty($users)) { $list = '<none>'; } else { $list = $this->toArrayImplode(', ', $identField, $users); } $this->info('Members: ' . $list); }
protected function loadUser($identity, $identField = null, $failOnSuccess = false) { $model = User::getAppModel(); $user = null; if (empty($identField)) { $user = $model->query()->where(User::PROPERTY_EMAIL, $identity)->first(); if ($user == null) { $user = $model->query()->where(User::PROPERTY_USERNAME, $identity)->first(); } } else { $user = $model->query()->where($identField, $identity)->first(); } if ($user == null) { if ($failOnSuccess) { return null; } $this->error("The user '{$identity}' does not exist."); } elseif ($failOnSuccess) { $this->error("The user '{$identity}' already exists."); } return $user; }