PHP code example of webrek / laravel-mongo-permission
1. Go to this page and download the library: Download webrek/laravel-mongo-permission 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/ */
webrek / laravel-mongo-permission example snippets
use Webrek\MongoPermission\Models\Permission;
use Webrek\MongoPermission\Models\Role;
use Webrek\MongoPermission\Traits\HasRoles;
class User extends Authenticatable {
use HasRoles;
}
Permission::create(['name' => 'edit articles']);
$role = Role::create(['name' => 'editor']);
$role->givePermissionTo('edit articles');
$user->assignRole('editor');
$user->hasPermissionTo('edit articles'); // true
// On the user (via the HasRoles / HasPermissions traits)
$user->roles(); // Collection<Role>
$user->permissions(); // Collection<Permission> direct grants
// On a role
$role->permissions(); // Collection<Permission>
$role->users(); // users holding the role
// (matches both flat ["id"] and structured role_ids)
// On a permission
$permission->roles(); // Collection<Role> that grant it
$user->assignRoleUntil('admin', now()->addHours(2));
$user->givePermissionToUntil('publish posts', now()->addDays(7));
$user->hasRole('admin'); // true for two hours
$user->hasPermissionTo('publish posts'); // true for seven days
// After the expiry passes:
$user->hasRole('admin'); // false
use Webrek\MongoPermission\Testing\MongoPermissionAssertions;
class FooTest extends TestCase
{
use MongoPermissionAssertions;
public function test_admin_can_edit(): void
{
$this->assertUserHasRole($user, 'admin');
$this->assertUserHasPermission($user, 'edit articles');
$this->assertUserHasDirectPermission($user, 'publish');
$this->assertRoleHasPermission($role, 'view');
}
}