ricardoriogo/permiso
Composer 安装命令:
composer require ricardoriogo/permiso
包简介
Simple Laravel 5 package to add Role-based Permissions
关键字:
README 文档
README
A simple Laravel 5 package for Role-based permissions.
Instalation
1. Add to composer.json
In the require key of composer.json file add the following:
"ricardoriogo/permiso": "dev-master"
Run composer update command.
$ composer update
2. Register Service Provider
In config/app.php add Riogo\Permiso\PermisoServiceProvider to the end of $providers array.
'providers' => array( 'App\Providers\EventServiceProvider', 'App\Providers\RouteServiceProvider', ... 'Riogo\Permiso\PermisoServiceProvider', ),
3. Change Auth configuration
In config/auth.php change the driver configuration to permiso.
Permiso will use App\User model by default. You will need to change model configuration on config/auth.php if you're using another model for authentication.
4. Add UserRoleTrait to your auth model
In your auth model add the UserRoleTrait trait. By default App\User is the model used for authentication.
class User extends Model implements AuthenticatableContract, CanResetPasswordContract { use Riogo\Permiso\UserRolesTrait ... }
5. Run Migrations
To create the migration file for roles and permissions tables use the command:
$ php artisan permiso:migration
This will create a migration file on database/migrations.
Then use migrate command.
$ php artisan migrate
How to use
Permiso extends Auth class capabilities for checking if authenticated user have especific roles or permissions.
Checking for a Role
To check for a role you have to use hasRole() method.
if (Auth::hasRole('admin')) { // Actions for this Role }
You can use the alias method is() too.
if (Auth::is('admin')) { // Actions for this Role }
Checking for multiple Roles
It's possible check for multiple roles, passing an array with the roles or a string with comma separated values.
// Using an array if (Auth::hasRole(['admin', 'member'])) { // Actions for this Roles } // Same result with string if (Auth::hasRole('admin, member')) { // Actions for this Roles }
It will return true if user have one or more of this roles.
If you want to check if user have all roles use the method checkAll() before hasRole().
// Will return true if user have admin and member roles. if (Auth::checkAll()->hasRole(['admin', 'member'])) { // Actions for this Roles }
Checking for a Permission
All uses of role are applicable in permissions using hasPermission() ou your alias can().
if (Auth::hasPermission('users.list')) { // Actions for this Permission } if (Auth::checkAll()->can('users.delete, users.create')) { // Actions for this Permissions }
Variant of is() method
For check one role it's possible to use a variant of is(), it use a magic method to define a role to check. See the examples:
Auth::isAdmin()will return true if user have admin role.Auth::isMember()will return true if user have member role.Auth::isRoleWithLongName()will return true if user have role_with_long_name role.
Configuration
If you will use your own models for Role and Permission or change the default tables names, publish the configuration file using
$ php artisan vendor:publish --provider="Riogo\Permiso\PermisoServiceProvider"
And change all configuration needed in config/permiso.php.
ricardoriogo/permiso 适用场景与选型建议
ricardoriogo/permiso 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 318 次下载、GitHub Stars 达 10, 最近一次更新时间为 2015 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auth」 「acl」 「laravel」 「permission」 「roles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ricardoriogo/permiso 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ricardoriogo/permiso 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ricardoriogo/permiso 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Email Toolkit Plugin for CakePHP
This package provides a flexible way to add Role-based Permissions to Laravel
AclManager plugin for CakePHP 3.x
统计信息
- 总下载量: 318
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-15