承接 acoustep/entrust-gui 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

acoustep/entrust-gui

Composer 安装命令:

composer require acoustep/entrust-gui

包简介

A GUI for the Entrust package.

README 文档

README

Code Climate Build Status Latest Stable Version Total Downloads Latest Unstable Version License ko-fi

Entrust GUI is a Admin Interface that makes the administration of users, roles and permissions easier for the Entrust package.

This package is currently not for handling authentication, authorisation or registration of users.

User Panel Preview

Installation

For Laravel 5.2 - 5.7 see the 5.2 branch for installation details.

For Laravel 5.8+ see the 5.8 branch for installation details.

Add the package to your composer.json file

"acoustep/entrust-gui": "dev-master"

Add the service provider to your config/app.php file

Acoustep\EntrustGui\EntrustGuiServiceProvider::class,

Add the Entrust Alias to your config/app.php file as well.

'Entrust'   => Zizaco\Entrust\EntrustFacade::class,

Publish the configuration file(s)

php artisan vendor:publish --tag="config"

If you haven't already set up Entrust then make the migration file and run the migration.

php artisan entrust:migration
php artisan migrate

Entrust GUI uses esensi/model which means you can set your validation rules in your models.

To generate User, Role and Permission models run the entrust-gui:models command.

php artisan entrust-gui:models

See the manually creating models section if you prefer to adjust your current model files.

By default, all three files are published into the app_path() directory. You can specify the files separately and the location

Add the Entrust GUI middleware to app\Http\Kernal.php. This middleware will allow users with the role admin (case sensitive) to access Entrust GUI and deny other users.

protected $routeMiddleware = [
  // ...
  'entrust-gui.admin' => \Acoustep\EntrustGui\Http\Middleware\AdminAuth::class,
];

At this point you're all good to go. See Getting Started for how to use the package.

Getting Started

Accessing Entrust GUI

By default all routes are prefixed with /entrust-gui.

  • Users: /entrust-gui/users
  • Roles: /entrust-gui/roles
  • Permissions: /entrust-gui/permissions

You can change this prefix by editing route-prefix in config/entrust-gui.php.

'route-prefix' => 'admin'

Pointing your app to /entrust-gui/users will redirect you to /auth/login if you are not logged in as admin using the default entrust-gui.admin middleware.

If you have not set up Laravel authentication you will see a NotFoundHttpException exception. See the Laravel Authentication documentation for setting up the Login system in Laravel 5.1.

Middleware

By default Entrust GUI uses entrust-gui.admin for middleware. This allows logged in users with the admin role to access it.

You can change the middleware in config/entrust-gui.php in the middleware setting.

If you wish to test out the system without middleware then go to config/entrust-gui.php and set middleware to null.

'middleware' => null,

If you want to change the name of the role that has access to the admin middleware, update middleware-role in the configuration file.

"middleware-role" => 'sudo-admin',

Layout

To use your own layout override the layout key in config/entrust-gui.php with the template you wish to use.

"layout" => "app", // located in views/app.blade.php

Each template yields to heading and content so make sure your new layout has those sections.

<html>
<head>
  <meta charset="UTF-8">
  <title>title</title>
</head>
<body>
  <h1>@yield('heading')</h1>
  @include('entrust-gui::partials.notifications')
  @yield('content')
</body>
</html>

Editing Templates

To edit the template files you first need to publish them

php artisan vendor:publish --tag="views"

All files are then stored in the resources/views/vendor/entrust-gui directory.

Routes

You may wish to link to pages in your own templates. EntrustGUI follows Laravel's Route::resource method with the exception of show.

route('entrust-gui::users.index')
route('entrust-gui::users.create')
route('entrust-gui::users.destroy', $id)
route('entrust-gui::users.update', $id)
route('entrust-gui::users.edit', $id)

Events

The following event classes are available:

  • UserCreatedEvent, UserDeletedEvent, UserUpdatedEvent.
  • RoleCreatedEvent, RoleDeletedEvent, RoleUpdatedEvent.
  • PermissionCreatedEvent, PermissionDeletedEvent, PermissionUpdatedEvent.

Example Event Listener

<?php

namespace App\Listeners;

use Acoustep\EntrustGui\Events\UserCreatedEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;

class UserCreatedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserCreatedEvent  $event
     * @return void
     */
    public function handle(UserCreatedEvent $event)
    {
      Log::info('created: '.$event->user->email);
    }
}

Add the listeners you need to use to app/Providers/EventServiceProvider.php.

protected $listen = [
  'Acoustep\EntrustGui\Events\UserCreatedEvent' => [
    'App\Listeners\UserCreatedListener',
  ],
  'Acoustep\EntrustGui\Events\UserUpdatedEvent' => [
    'App\Listeners\UserUpdatedListener',
  ],
  'Acoustep\EntrustGui\Events\UserDeletedEvent' => [
    'App\Listeners\UserDeletedListener',
  ],
  'Acoustep\EntrustGui\Events\RoleCreatedEvent' => [
    'App\Listeners\RoleCreatedListener',
  ],
  'Acoustep\EntrustGui\Events\RoleUpdatedEvent' => [
    'App\Listeners\RoleUpdatedListener',
  ],
  'Acoustep\EntrustGui\Events\RoleDeletedEvent' => [
    'App\Listeners\RoleDeletedListener',
  ],
  'Acoustep\EntrustGui\Events\PermissionCreatedEvent' => [
    'App\Listeners\PermissionCreatedListener',
  ],
  'Acoustep\EntrustGui\Events\PermissionUpdatedEvent' => [
    'App\Listeners\PermissionUpdatedListener',
  ],
  'Acoustep\EntrustGui\Events\PermissionDeletedEvent' => [
    'App\Listeners\PermissionDeletedListener',
  ]
];

Editing Translations

Run the publish translation command

php artisan vendor:publish --tag="translations"

Translations are then published to resources/lang/vendor/entrust-gui.

Adding Password Confirmation Field to Users

Update your User model to the following:

<?php namespace App;

use Esensi\Model\Contracts\HashingModelInterface;
use Esensi\Model\Contracts\PurgingModelInterface;
use Esensi\Model\Contracts\ValidatingModelInterface;
use Esensi\Model\Traits\HashingModelTrait;
use Esensi\Model\Traits\PurgingModelTrait;
use Esensi\Model\Traits\ValidatingModelTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract, ValidatingModelInterface, HashingModelInterface, PurgingModelInterface
{
    use Authenticatable, CanResetPassword, ValidatingModelTrait, EntrustUserTrait, PurgingModelTrait, HashingModelTrait;

    protected $throwValidationExceptions = true;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

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

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    protected $purgeable = [
        'password_confirmation',
    ];

    protected $hashable = ['password'];

    protected $rulesets = [

        'creating' => [
            'email'      => 'required|email|unique:users',
            'password'   => 'required|confirmed',
        ],

        'updating' => [
            'email'      => 'required|email|unique:users',
            'password'   => 'confirmed',
        ],
    ];

}

Update config/entrust-gui.php

'confirmable' => true,

Generating Models Command Options

Generating a specific model

php artisan entrust-gui:models User

Changing the model directory destination

php artisan entrust-gui:models --path=new/path

Skipping confirmation prompts for overwriting existing files

php artisan entrust-gui:models --force

Manually Creating Models

Here are User, Role and Permission models. Make sure these parameters are and traits are included for the package to work as intended.

app/User.php

<?php namespace App;

use Esensi\Model\Contracts\HashingModelInterface;
use Esensi\Model\Contracts\ValidatingModelInterface;
use Esensi\Model\Traits\HashingModelTrait;
use Esensi\Model\Traits\ValidatingModelTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract, ValidatingModelInterface, HashingModelInterface
{
    use Authenticatable, CanResetPassword, ValidatingModelTrait, EntrustUserTrait, HashingModelTrait;

    protected $throwValidationExceptions = true;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

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

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    protected $hashable = ['password'];

    protected $rulesets = [

        'creating' => [
            'email'      => 'required|email|unique:users',
            'password'   => 'required',
        ],

        'updating' => [
            'email'      => 'required|email|unique:users',
            'password'   => '',
        ],
    ];

}

app/Role.php

<?php namespace App;

use Esensi\Model\Contracts\ValidatingModelInterface;
use Esensi\Model\Traits\ValidatingModelTrait;
use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole implements ValidatingModelInterface
{
  use ValidatingModelTrait;

  protected $throwValidationExceptions = true;

  protected $fillable = [
    'name',
    'display_name',
    'description',
  ];

  protected $rules = [
    'name'      => 'required|unique:roles',
    'display_name'      => 'required|unique:roles',
  ];
}

app/Permission.php

<?php namespace App;

use Esensi\Model\Contracts\ValidatingModelInterface;
use Esensi\Model\Traits\ValidatingModelTrait;
use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission implements ValidatingModelInterface
{
  use ValidatingModelTrait;

  protected $throwValidationExceptions = true;

  protected $fillable = [
    'name',
    'display_name',
    'description',
  ];

  protected $rules = [
    'name'      => 'required|unique:permissions',
  ];
}

Upgrade Guide / Breaking Changes

0.6

Includes a new config key, "unauthorized-url" which lets you set the redirection if a user is not authorized. If this key is not found, it will use the old url, /auth/login. The default is set to /auth/login to match Laravel 5.1's documentation on the authentication routes.

0.3.* to 0.4.0

Starting from 0.4.0 Entrust GUI switches from dwightwatson/validating to esensi/model.

Hashing passwords has moved from the package to the User model.

Update your User model to the one in the latest documentation.

Add 'confirmable' => false, to your configuration file.

If you intend to use the confirmable option and have already published the views add the following to your resources/views/vendor/entrust-gui/users/partials/form.blade.php template

@if(Config::get('entrust-gui.confirmable') === true)
<div class="form-group">
    <label for="password">Confirm Password</label>
    <input type="password" class="form-control" id="password_confirmation" placeholder="Confirm Password" name="password_confirmation">
</div>
@endif

Support Entrust GUI

Entrust GUI is maintained entirely in my own time. If you'd like to help, please feel free to open an issue with feature requests or proposals. If you'd like to buy me a coffee to say thanks, check out my ko-fi page.

To do

  • Advanced middleware configuration
  • More testing
  • More documentation

acoustep/entrust-gui 适用场景与选型建议

acoustep/entrust-gui 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.1k 次下载、GitHub Stars 达 110, 最近一次更新时间为 2015 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 acoustep/entrust-gui 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 26.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 114
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 110
  • Watchers: 7
  • Forks: 20
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-09