diff --git a/Modules/User/Permissions/PermissionsAdder.php b/Modules/User/Permissions/PermissionsAdder.php new file mode 100644 index 000000000..3cb87c279 --- /dev/null +++ b/Modules/User/Permissions/PermissionsAdder.php @@ -0,0 +1,55 @@ +moduleName = $moduleName; + $this->role = app(RoleRepository::class); + } + + public function addAll() + { + $permissions = $this->buildPermissionList(); + + $role = $this->role->find(1); + foreach ($permissions as $permission) { + $role->addPermission($permission); + $role->save(); + } + } + + /** + * @return array + */ + private function buildPermissionList() : array + { + $permissionsConfig = config('asgard.' . strtolower($this->moduleName) . '.permissions'); + $list = []; + + if ($permissionsConfig === null) { + return $list; + } + + foreach ($permissionsConfig as $mainKey => $subPermissions) { + foreach ($subPermissions as $key => $description) { + $list[] = $mainKey . '.' . $key; + } + } + + return $list; + } +} diff --git a/Modules/User/Tests/Permissions/PermissionsAdderTest.php b/Modules/User/Tests/Permissions/PermissionsAdderTest.php new file mode 100644 index 000000000..5a68ee2d3 --- /dev/null +++ b/Modules/User/Tests/Permissions/PermissionsAdderTest.php @@ -0,0 +1,59 @@ +role = app(RoleRepository::class); + $this->user = app(UserRepository::class); + + $this->role->create([ + 'name' => 'Admin', + 'slug' => 'admin', + 'permissions' => [ + 'dashboard.index' => true, + ] + ]); + $this->user->create([ + 'email' => 'n.widart@gmail.com', + 'password' => 'demo1234', + 'permissions' => ['dashboard.index' => true,], + ]); + $this->app->config->set('asgard.user.permissions', [ + 'user.users' => [ + 'index' => 'user::users.list user', + 'create' => 'user::users.create user', + ], + ]); + } + + /** @test */ + public function it_can_add_permissions_to_the_admin_role() + { + $role = $this->role->findByName('Admin'); + + $this->assertCount(1, $role->permissions); + + (new PermissionsAdder('User'))->addAll(); + + $role->refresh(); + $this->assertCount(3, $role->permissions); + } +}