lazycode/laravel-simple-permissions
Composer 安装命令:
composer require lazycode/laravel-simple-permissions
包简介
A Laravel package for handling roles and permissions.
README 文档
README
A simple Laravel package for handling roles and permissions. This package allows you to assign roles to users, manage permissions, and easily check for roles and permissions in your application.
Installation
To install the package, run the following command:
composer require lazycode/laravel-simple-permissions
Publish Configuration
After installing the package, publish the configuration file using:
php artisan vendor:publish --provider="Lazycode\Permissions\PermissionsServiceProvider" --tag=config
Run Migrations
Next, run the migrations to create the necessary tables in your database:
php artisan migrate
Usage
Setting Up the User Model
In your User model (usually located at app/Models/User.php), ensure you include the HasRolesAndPermissions trait:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use Lazycode\Permissions\Traits\HasRolesAndPermissions; // Ensure this is correct class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, HasRolesAndPermissions; // Your existing model code... }
Assigning Roles and Permissions
You can now use the package's functionality in your application. Here are some examples:
Assign a Role
To assign a role to a user:
$user = User::find(1); // Replace with the appropriate user ID $user->assignRole('Admin');
Get User's Role
To get the user's assigned role:
$role = $user->getRole();
Check User's Permissions
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:
use Illuminate\Database\Seeder; use Lazycode\Permissions\Models\Role; use Lazycode\Permissions\Models\Permission; use App\Models\User; class RolesAndPermissionsSeeder extends Seeder { public function run() { // Create permissions $editUserPermission = Permission::create(['name' => 'edit-user']); $addUserPermission = Permission::create(['name' => 'add-user']); $deleteUserPermission = Permission::create(['name' => 'delete-user']); // Create a role $adminRole = Role::create(['name' => 'Admin']); // Attach permissions to the role $adminRole->permissions()->attach([$editUserPermission->id, $addUserPermission->id, $deleteUserPermission->id]); // Create a user and assign the role $user = User::create(['name' => 'Admin User', 'email' => 'admin@example.com', 'password' => bcrypt('password')]); $user->assignRole('Admin'); } }
Database Schema
Ensure your database schema is set up correctly. The following tables should exist:
roles: To store roles.permissions: To store permissions.role_permission: Pivot table to manage many-to-many relationships between roles and permissions.users: Ensure there is arole_idcolumn in theuserstable to associate users with their roles.
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.
lazycode/laravel-simple-permissions 适用场景与选型建议
lazycode/laravel-simple-permissions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「laravel」 「permission」 「roles」 「permissions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lazycode/laravel-simple-permissions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lazycode/laravel-simple-permissions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lazycode/laravel-simple-permissions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
It's a barebone security class written on PHP
This package provides a flexible way to add Role-based Permissions to Laravel
Contao CMS integrity check for some files
A PHP security linter to detect insecure functions like var_dump, print_r, and other dangerous functions in your codebase
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-07