PHP code example of ngarak-dev / permissions-ui-wrapper
1. Go to this page and download the library: Download ngarak-dev/permissions-ui-wrapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ngarak-dev / permissions-ui-wrapper example snippets
'seeder' => [
// Whether to clear existing permissions and roles before seeding
'clear_before_seeding' => false,
// Whether to create random permissions in addition to the defined ones
'create_random_permissions' => false,
// Whether to create random roles in addition to the defined ones
'create_random_roles' => false,
// If set, this user ID will automatically be assigned the Super Admin role
'super_admin_user_id' => null,
],
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
// Check if a user has a specific permission
if ($user->hasPermissionTo('edit articles')) {
// ...
}
// Check if a user has any of the permissions
if ($user->hasAnyPermission(['edit articles', 'publish articles'])) {
// ...
}
// Check if a user has all permissions
if ($user->hasAllPermissions(['edit articles', 'publish articles'])) {
// ...
}
Route::group(['middleware' => ['permission:publish articles']], function () {
// Routes accessible only to users with the 'publish articles' permission
});
Route::group(['middleware' => ['role:admin']], function () {
// Routes accessible only to users with the 'admin' role
});
Route::group(['middleware' => ['role_or_permission:admin|edit articles']], function () {
// Routes accessible to users with either 'admin' role or 'edit articles' permission
});
// Assign a role to a user
$user->assignRole('writer');
// Assign multiple roles
$user->assignRole(['writer', 'admin']);
// Alternative syntax
$user->assignRole('writer', 'admin');
// Remove a role from a user
$user->removeRole('writer');
// Remove multiple roles
$user->removeRole(['writer', 'admin']);
// Remove all roles and assign the given roles
$user->syncRoles(['writer', 'admin']);
// Check if a user has a role
if ($user->hasRole('writer')) {
// ...
}
// Check if a user has any of the roles
if ($user->hasAnyRole(['writer', 'admin'])) {
// ...
}
// Check if a user has all roles
if ($user->hasAllRoles(['writer', 'admin'])) {
// ...
}
'permission_groups' => [
'user' => [
'label' => 'User Management',
'description' => 'Permissions related to user management',
],
// Add more groups...
],
// Creating a role with a custom guard
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'api']);
// Assigning a role with a custom guard
$user->assignRole('admin', 'api');
'permission_groups' => [
'user' => [
'label' => 'User Management',
'description' => 'Permissions related to user management',
],
// Add more groups...
],
bash
php artisan permissions-ui:install --force
bash
# Set an existing user as super user
php artisan permissions-ui:super-user {userId}
# Create a new user as super user
php artisan permissions-ui:super-user --create