pierresilva/laravel-sentinel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pierresilva/laravel-sentinel

Composer 安装命令:

composer require pierresilva/laravel-sentinel

包简介

Laravel 5 Roles and Permissions

README 文档

README

Sentinel brings a simple and light-weight role-based permissions system to Laravel's built in Auth system.

Sentinel brings support for the following ACL structure:

  • Every user can have zero or more roles.
  • Every role can have zero or more permissions.

Permissions are then inherited to the user through the user's assigned roles.

Quick Installation

Begin by installing the package through Composer. The best way to do this is through your terminal via Composer itself:

composer require pierresilva/laravel-sentinel

Once this operation is complete, simply add the service provider to your project's config/app.php file and run the provided migrations against your database.

Service Provider

pierresilva\Sentinel\SentinelServiceProvider::class,

Facade

'Sentinel' => pierresilva\Sentinel\Facades\Sentinel::class,

Then open the Kernel.php file in app\Http\Kernel.php and add this to user it as middleware :

routeMiddleware

 'roles' => \pierresilva\Sentinel\Middleware\UserHasRole::class,
 'permissions' => \pierresilva\Sentinel\Middleware\UserHasPermission::class,

Migrations

You'll need to run the provided migrations against your database. Publish the migration files using the vendor:publish Artisan command and run migrate:

php artisan vendor:publish
php artisan migrate

Usage

Sentinel package comes bundled with a SentinelTrait file to be used within your User's Model file. This trait file provides all the necessary functions to tie your users in with roles and permissions.

Example User Model

<?php

namespace App;

use pierresilva\Sentinel\Traits\SentinelTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;
    use SentinelTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'gender', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

SentinelTrait

The following methods will become available from your User model.

  • isRole($roleSlug)
  • can($permission)
  • assignRole($roleId)
  • revokeRole($roleId)
  • revokeAllRoles()
  • syncRoles([$roleIds])

isRole($roleSlug)

Checks if the user is under the given role.

Auth::user()->isRole('administrator');

You may also use magic methods:

Auth::user()->isAdministrator();

can($permission)

Checks if the user has the given permission(s). You may pass either a string or an array of permissions to check for. In the case of an array, ALL permissions must be accountable for in order for this to return true.

Auth::user()->can('access.admin');

or

Auth::user()->can(['access.admin', 'view.users']);

assignRole($roleId)

Assign the given role to the user.

Auth::user()->assignRole(1);
revokeRole($roleId)

Revokes the given role from the user.

Auth::user()->revokeRole(1);

revokeAllRoles()

Revokes all roles from the user.

Auth::user()->revokeAllRoles();

syncRoles([$roleIds])

Syncs the given roles with the user. This will revoke any roles not supplied.

Auth::user()->syncRoles([1, 2, 3]);

Role Permissions

The bundled Role model has easy to use methods to manage and assign permissions.

  • can($permission)
  • getPermissions()
  • assignPermission($permissionId)
  • revokePermission($permissionId)
  • revokeAllPermissions()
  • syncPermissions([$permissionIds])

can($permission)

Checks if the role has the given permission(s). You may pass either a string or an array of permissions to check for. In the case of an array, ALL permissions must be accounted for in order for this to return true.

$role = Role::find(1);
return $role->can('access.admin');

getPermissions()

Retrieves an array of assigned permission slugs for the role.

$role = Role::find(1);
return $role->getPermissions();

assignPermission($permissionId)

Assigns the given permission to the role.

$role = Role::find(1);
$role->assignPermission(1);
$role->save();

revokePermission($permissionId)

Revokes the given permission from the role.

$role = Role::find(1);
$role->revokePermission(1);
$role->save();

revokeAllPermissions()

Revokes all permissions from the role.

$role = Role::find(1);
$role->revokeAllPermissions();
$role->save();

syncPermissions([$permissionIds])

Syncs the given permissions with the role. This will revoke any permissions not supplied.

$role = Role::find(1);
$role->syncPermissions([1, 2, 3]);
$role->save();

Facade Reference

Sentinel package comes with a handy Facade to make checking for roles and permissions easy within your code.

  • Sentinel::can($permissions)
  • Sentinel::canAtLeast($permissions)
  • Sentinel::isRole($role)

Sentinel::can($permissions)

Checks if the current user can perform the provided permissions.

Parameters

  • $permissions (array or string) Permission(s) to check for. Required. Returns Boolean Example
if (Sentinel::can('create.blog.post')) {
    // Do whatever
}
if (Sentinel::can(['access.admin', 'create.blog.post'])) {
    // Do whatever
}

Sentinel::canAtLeast($permissions)

Checks if the current user can at least perform one of the provided permissions.

Parameters

  • $permissions (array) Permissions to check for. Required. Returns Boolean Example
if (Sentinel::canAtLeast(['edit.blog.post', 'create.blog.post'])) {
    // Can at least do one of the tasks
}

Sentinel::isRole($role)

Checks if the current user has the given role.

Parameters

  • $role (string) Role (slug) to check for. Required. Returns Boolean Example
if (Sentinel::isRole('admin')) {
    // Do whatever
}

Template Helpers

Sentinel comes bundled with custom template methods to easily check for permissions and roles from within your view files for both Blade.

can($permissions)

Blade

@can('create.blog.post')
    Do whatever
@else
    Do something else
@endcan
@canatleast(['edit.blog.post', 'create.blog.post'])
    Do whatever
@else
    Do something else
@endcanatleast
@role('admin')
    Do whatever
@else
    Do something else
@endrole

pierresilva/laravel-sentinel 适用场景与选型建议

pierresilva/laravel-sentinel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「auth」 「laravel」 「roles」 「permissions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pierresilva/laravel-sentinel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pierresilva/laravel-sentinel 我们能提供哪些服务?
定制开发 / 二次开发

基于 pierresilva/laravel-sentinel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 141
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-11