Skip to content

Commit

Permalink
changes regrading adding new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
subhobiswas committed Oct 8, 2024
1 parent 9c9a7dd commit a62b318
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ To assign a role to a user:

```php
$user = User::find(1); // Replace with the appropriate user ID
$user->assignRoleByName('Admin');
$user->assignRole('Admin');
```

#### Get User's Role
Expand All @@ -69,8 +69,31 @@ To check if a user has a specific permission:
if ($user->hasPermission('edit-user')) {
// The user has permission to edit a user
}

IN blade.php

@hasPermission('edit-user')
{{ show the content if user has this permission }}
@endhasPermission
```

### Functions List
`role`: Return the Role collection of modal.

`hasRole` `@hasRole` : Check Loggedin User has the role.

`hasAnyRole` `@hasAnyRole` : Checks if the user has any of the specified roles.

`permission`: Get the permission collection from permission modal.

`@hasPermission` `@haspermission`: Check Loggedin user has the permission.

`hasAnyPermission` `@hasAnyPermission`: Checks if the user has any of the specified permissions.

`hasAllPermission` `@hasAllPermission`: Verifies that the user has all the specified permissions.

`hasExactPermissions` `@hasExactPermissions`: Checks if the user has exactly the specified permissions.

## Seeding Roles and Permissions
You can create a seeder to start with some default roles and permissions. For example:

Expand All @@ -97,7 +120,7 @@ class RolesAndPermissionsSeeder extends Seeder

// Create a user and assign the role
$user = User::create(['name' => 'Admin User', 'email' => 'admin@example.com', 'password' => bcrypt('password')]);
$user->assignRoleByName('Admin');
$user->assignRole('Admin');
}
}
```
Expand All @@ -112,5 +135,3 @@ Ensure your database schema is set up correctly. The following tables should exi

## Conclusion
This package provides a simple way to manage user roles and permissions in your Laravel application. For further customization or additional features, feel free to contribute or reach out for support.


15 changes: 7 additions & 8 deletions src/PermissionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ public function boot(): void
};

// Role checks
$bladeCompiler->if('role', fn() => $bladeMethodWrapper('hasRole', ...func_get_args()));
$bladeCompiler->if('hasrole', fn() => $bladeMethodWrapper('hasRole', ...func_get_args()));
$bladeCompiler->if('hasanyrole', fn() => $bladeMethodWrapper('hasAnyRole', ...func_get_args()));
$bladeCompiler->if('hasRole', fn() => $bladeMethodWrapper('hasRole', ...func_get_args()));
$bladeCompiler->if('hasAnyRole', fn() => $bladeMethodWrapper('hasAnyRole', ...func_get_args()));

// Permission checks
$bladeCompiler->if('permission', fn() => $bladeMethodWrapper('hasPermission', ...func_get_args()));
$bladeCompiler->if('haspermission', fn() => $bladeMethodWrapper('hasPermission', ...func_get_args()));
$bladeCompiler->if('hasanypermission', fn() => $bladeMethodWrapper('hasAnyPermission', ...func_get_args()));
$bladeCompiler->if('hasallpermissions', fn() => $bladeMethodWrapper('hasAllPermissions', ...func_get_args()));
$bladeCompiler->if('hasexactpermissions', fn() => $bladeMethodWrapper('hasExactPermissions', ...func_get_args()));
$bladeCompiler->if('hasPermission', fn() => $bladeMethodWrapper('hasPermission', ...func_get_args()));
$bladeCompiler->if('hasAnyPermission', fn() => $bladeMethodWrapper('hasAnyPermission', ...func_get_args()));
$bladeCompiler->if('hasAllPermissions', fn() => $bladeMethodWrapper('hasAllPermissions', ...func_get_args()));
$bladeCompiler->if('hasExactPermissions', fn() => $bladeMethodWrapper('hasExactPermissions', ...func_get_args()));

// Ending directive for permissions
$bladeCompiler->directive('endunlesspermission', fn() => '<?php endif; ?>');
Expand All @@ -60,3 +58,4 @@ public function register(): void
$this->mergeConfigFrom(__DIR__ . '/config/permissions.php', 'permissions'); // Ensure this matches the config file
}
}

0 comments on commit a62b318

Please sign in to comment.