Skip to content

Commit

Permalink
Merge pull request #2 from aryanjaya/update
Browse files Browse the repository at this point in the history
Add Generator Logic & Test
  • Loading branch information
backend-timedoor committed May 24, 2023
2 parents 2e1c4ba + ed21753 commit 8a3c92f
Show file tree
Hide file tree
Showing 29 changed files with 691 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
package-lock.json
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Import role & permission in Laravel into Javascript.

[![Latest Version on Packagist](https://img.shields.io/packagist/v/timedoor/laravel-role-js.svg?style=flat-square)](https://packagist.org/packages/timedoor/laravel-role-js)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/timedoor/laravel-role-js/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/backend-timedoor/laravel-role-js/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/timedoor/laravel-role-js/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/backend-timedoor/laravel-role-js/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/timedoor/laravel-role-js/run-tests.yml?branch=master&label=tests&style=flat-square)](https://github.com/backend-timedoor/laravel-role-js/actions?query=workflow%3Arun-tests+branch%3Amaster)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/timedoor/laravel-role-js/fix-php-code-style-issues.yml?branch=master&label=code%20style&style=flat-square)](https://github.com/backend-timedoor/laravel-role-js/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amaster)
[![Total Downloads](https://img.shields.io/packagist/dt/timedoor/laravel-role-js.svg?style=flat-square)](https://packagist.org/packages/timedoor/laravel-role-js)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0"
},
"require-dev": {
"jeremykenedy/laravel-roles": "^10.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^5.10 || ^6.4",
"nunomaduro/larastan": "^1.0 || ^2.0",
Expand Down Expand Up @@ -71,6 +72,9 @@
}
}
},
"suggest": {
"jeremykenedy/laravel-roles": "Required to use the RoleJs package with default generator."
},
"minimum-stability": "dev",
"prefer-stable": true
}
6 changes: 4 additions & 2 deletions config/role-js.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

// config for timedoor/RoleJs
return [

// Generator class to generate roles and permissions data
// You can use your own generator class, just make sure it
// implements the `GeneratorInterface` interface.
'generator' => timedoor\RoleJs\Generator\JeremyKenedyRoleGenerator::class,
];
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "laravel-role",
"version": "1.0.0",
"directories": {
"test": "tests"
},
"scripts": {
"test": "vitest",
"coverage": "vitest run --coverage"
},
"author": "Aryan Jaya",
"license": "MIT",
"devDependencies": {
"@vitest/coverage-c8": "^0.31.1",
"vitest": "^0.31.1"
}
}
Empty file removed resources/views/.gitkeep
Empty file.
19 changes: 0 additions & 19 deletions src/Commands/RoleJsCommand.php

This file was deleted.

86 changes: 86 additions & 0 deletions src/Commands/RoleJsGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace timedoor\RoleJs\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use timedoor\RoleJs\Generator\JeremyKenedyRoleGenerator;

class RoleJsGeneratorCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'role-js:generate
{path? : Path to the generated JavaScript file. Default: `resources/js/roles`.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a JavaScript file containing roles and permissions.';

protected $files;

public function __construct(Filesystem $files)
{
parent::__construct();

$this->files = $files;
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// Get the path to the generated JavaScript file.
$target = $this->argument('path') ?? 'resources/js/roles';
$path = base_path($target);

// Create the directory if it doesn't exist.
if (! $this->files->isDirectory($path)) {
$this->files->makeDirectory($path, 0755, true);
}

// Get the roles and permissions.
$generated = $this->generate();

// Generate the JavaScript file.
$this->files->put($path.'/data.ts', $generated);

$this->info("The {$target}/data.ts file has been generated.");

return static::SUCCESS;
}

protected function generate()
{
/** @var class-string $generatorClass */
$generatorClass = config('role-js.generator', JeremyKenedyRoleGenerator::class);

/** @var \timedoor\RoleJs\Generator\GeneratorInterface $generator */
$generator = new $generatorClass;

$role = $generator->getRoles();
$permissions = $generator->getPermissions();
$rolePermission = $generator->getRolePermissions();

return <<<TYPESCRIPT
const roles = {$role->toJson()} as const;
const permissions = {$permissions->toJson()} as const;
const rolePermission = {$rolePermission->toJson()} as const;
export {
roles,
permissions,
rolePermission,
};
TYPESCRIPT;
}
}
56 changes: 56 additions & 0 deletions src/Commands/RoleJsPublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace timedoor\RoleJs\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class RoleJsPublishCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'role-js:publish
{path? : Path to the publish JavaScript files. Default: `resources/js/roles`.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'publish a JavaScript files containing logic for roles and permissions.';

/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $file;

public function __construct(Filesystem $file)
{
parent::__construct();

$this->file = $file;
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// Get the path to the generated JavaScript file.
$path = $this->argument('path') ?? 'resources/js/roles';

// Copy directory from package to the specified path.
$this->file->copyDirectory(__DIR__.'/../../stubs/js/roles', base_path($path));

$this->info("The files has been published to {$path}.");

return self::SUCCESS;
}
}
16 changes: 0 additions & 16 deletions src/Facades/RoleJs.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Generator/GeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace timedoor\RoleJs\Generator;

interface GeneratorInterface
{
/**
* @return \Illuminate\Support\Collection<int, string>
*/
public function getRoles();

/**
* @return \Illuminate\Support\Collection<int, string>
*/
public function getPermissions();

/**
* @return \Illuminate\Support\Collection<string, string[]>
*/
public function getRolePermissions();
}
37 changes: 37 additions & 0 deletions src/Generator/JeremyKenedyRoleGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace timedoor\RoleJs\Generator;

class JeremyKenedyRoleGenerator implements GeneratorInterface
{
public function getRoles()
{
/** @var \Illuminate\Database\Eloquent\Model $roleClass */
$roleClass = config('roles.models.role');

return $roleClass::query()->pluck('slug');
}

public function getPermissions()
{
/** @var \Illuminate\Database\Eloquent\Model $permissionClass */
$permissionClass = config('roles.models.permission');

return $permissionClass::query()->pluck('slug');
}

public function getRolePermissions()
{
/** @var \Illuminate\Database\Eloquent\Model $roleClass */
$roleClass = config('roles.models.role');

$roles = $roleClass::with('permissions:id,slug')->get();

/** @var \Illuminate\Support\Collection<string, string[]> $rolePermissions */
$rolePermissions = $roles->mapWithKeys(function ($role) {
return [$role->getAttribute('slug') => $role->getAttribute('permissions')->pluck('slug')];
});

return $rolePermissions;
}
}
7 changes: 0 additions & 7 deletions src/RoleJs.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/RoleJsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use timedoor\RoleJs\Commands\RoleJsCommand;
use timedoor\RoleJs\Commands\RoleJsGeneratorCommand;
use timedoor\RoleJs\Commands\RoleJsPublishCommand;

class RoleJsServiceProvider extends PackageServiceProvider
{
Expand All @@ -19,7 +20,8 @@ public function configurePackage(Package $package): void
->name('laravel-role-js')
->hasConfigFile()
->hasCommands([
RoleJsCommand::class,
RoleJsGeneratorCommand::class,
RoleJsPublishCommand::class,
]);
}
}
9 changes: 9 additions & 0 deletions stubs/js/roles/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const roles = ["superadmin", "admin", "user"] as const;
const permissions = ["view.users", "create.users", "edit.users", "delete.users"] as const;
const rolePermission = { "superadmin": ["view.users", "create.users", "edit.users", "delete.users"], "admin": [] } as const;

export {
roles,
permissions,
rolePermission,
};
3 changes: 3 additions & 0 deletions stubs/js/roles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './type';
export * from './useRole';
export * from './data';
9 changes: 9 additions & 0 deletions stubs/js/roles/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { permissions, roles } from "./data";

export type RoleType = typeof roles[number];
export type PermissionType = typeof permissions[number];

export interface RolePermission {
role: RoleType | RoleType[];
permissions?: PermissionType[];
}

0 comments on commit 8a3c92f

Please sign in to comment.