generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from aryanjaya/update
Add Generator Logic & Test
- Loading branch information
Showing
29 changed files
with
691 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ phpstan.neon | |
testbench.yaml | ||
vendor | ||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './type'; | ||
export * from './useRole'; | ||
export * from './data'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
Oops, something went wrong.