Skip to content

Commit

Permalink
Artisan publish-permission changes (#14)
Browse files Browse the repository at this point in the history
It now does only the thing automatically that the command says.

For migration you need to add `--migration` and `--seed`.
  • Loading branch information
eramitgupta authored Oct 20, 2024
2 parents 123ee85 + 1a459c6 commit 9cca615
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,17 @@ Once the database is configured, publish the required migration and model files
php artisan erag:publish-permission
```

This command will:
This command will publish the required migrations:

- Publish and run the required migrations.
- Automatically run the seeder to set up roles and permissions in your database.
```bash
php artisan erag:publish-permission --migrate
```

If you want to run the published migrations and seed the datbase you cann add `--migrate` and `--seed` respectively. Then the command will automatically run the migrations and the seeder to set up roles and permissions in your database.

```bash
php artisan erag:publish-permission --migrate --seed
```

## Step 6: Using Role-Based Permissions

Expand Down Expand Up @@ -161,6 +168,7 @@ getRoles();
To protect routes based on roles and permissions, you can use the provided middleware. For example, to allow only users with the `user` role and `create-user` permission:

```php

Route::group(['middleware' => ['role:user,user-create']], function () {
// Protected routes go here
});
Expand Down Expand Up @@ -240,7 +248,7 @@ class RolePermissionSeeder extends Seeder
{
$roles = [
'admin' => ['post-create', 'post-edit', 'post-delete', 'post-update'],
'user' => ['user-create', 'user-edit', 'user-delete', 'user-update'],
'user' => ['user-create, 'user-edit', 'user-delete', 'user-update'],
];

foreach ($roles as $roleName => $permissionNames) {
Expand Down Expand Up @@ -269,6 +277,7 @@ class RolePermissionSeeder extends Seeder
'email' => '[email protected]',
'password' => Hash::make('user'),
'roles' => ['user'],
'permissions' => ['user-create],
'permissions' => ['user-create'],
],
];
Expand Down
45 changes: 24 additions & 21 deletions src/Commands/PublishPermissionMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class PublishPermissionMigrations extends Command
{
protected $signature = 'erag:publish-permission';
protected $signature = 'erag:publish-permission {--migrate} {--seed}';

protected $description = 'Publish the Permission and Role migration files and models';

Expand All @@ -24,13 +24,15 @@ public function handle(): void
'--force' => true,
]);

$this->info('Running migrations...');
$exitCode = $this->call('migrate', ['--force' => true]);
if ($this->option('migrate')) {
$this->info('Running migrations...');
$exitCode = $this->call('migrate', ['--force' => true]);

if ($exitCode === 0) {
$this->info('Migrations completed successfully.');
} else {
$this->error('Migration process encountered errors.');
if ($exitCode === 0) {
$this->info('Migrations completed successfully.');
} else {
$this->error('Migration process encountered errors.');
}
}

$this->info('Publishing seeder...');
Expand All @@ -39,21 +41,22 @@ public function handle(): void
'--force' => true,
]);

$this->info('Running seeder...');
try {
$exitCode = $this->call('db:seed', [
'--class' => 'RolePermissionSeeder',
'--force' => true,
]);

if ($exitCode === 0) {
$this->info('Seeder completed successfully.');
} else {
$this->error('Seeder process encountered errors.');
if ($this->option('seed')) {
$this->info('Running seeder...');
try {
$exitCode = $this->call('db:seed', [
'--class' => 'RolePermissionSeeder',
'--force' => true,
]);

if ($exitCode === 0) {
$this->info('Seeder completed successfully.');
} else {
$this->error('Seeder process encountered errors.');
}
} catch (\Exception $e) {
$this->error('Error running seeder: '.$e->getMessage());
}
} catch (\Exception $e) {
$this->error('Error running seeder: '.$e->getMessage());
}

}
}

0 comments on commit 9cca615

Please sign in to comment.