eflames/vault

Vault, Laravel 5 角色 & 权限

v0.1-alpha 2015-02-03 15:05 UTC

This package is auto-updated.

Last update: 2024-09-21 04:33:04 UTC


README

Project Status Build Status Scrutinizer Code Quality Total Downloads License

请先阅读

英文 这是 Rappasoft/Vault 的相同仓库,您可以在Rappasoft Vault找到它。此包仅创建用于支持在 Laravel 5.0 中创建的项目。如果您使用 Laravel 5.1,则需要下载原始版本!

西班牙语 这与 Rappasoft/Vault 的相同仓库,您可以在Rappasoft Vault找到它。此包仅创建用于支持在 Laravel 5.0 中创建的项目。如果您使用 Laravel 5.1,则需要下载原始版本!

Vault 是一个简单而强大的新 Laravel 5 框架访问控制系统。它包括一个后端用户界面来管理用户、角色和权限,以及它们之间的关系。

请注意,尽管此包完全工作,但它仍在开发中,代码库经常更改。当包稳定发布时,将创建 1.0 标签。

示例: Vault 用户索引 Vault 创建角色 Vault 编辑用户 Vault 角色索引

文档

先决条件

  • 此包假定您已安装了使用预包装认证库和功能的 Laravel 5。对于全新的项目,我建议使用我的Laravel 5 模板包并要求此库。
  • 用户模型必须启用软删除。
## Setup

In the require key of composer.json file add the following

"eflames/vault": "dev-master" 

Run the Composer update command

$ composer update 

In your config/app.php add the following to your $providers and $aliases array

'providers' => [ 'App\Providers\EventServiceProvider', 'App\Providers\RouteServiceProvider', ... 'Rappasoft\Vault\VaultServiceProvider', 'Illuminate\Html\HtmlServiceProvider', ],
'aliases' => [ 'App' => 'Illuminate\Support\Facades\App', ... 'Form' => 'Illuminate\Html\FormFacade', 'HTML' => 'Illuminate\Html\HtmlFacade' ],

The Vault Facade is loaded by the service provider by default.

Run the `vendor:publish` command
$ php artisan vendor:publish --provider="Rappasoft\Vault\VaultServiceProvider" 

This will publish the following files to your application:

  • app/config/vault.php config file
  • Vault Migration File
  • Vault Seed File (Will add the seed call to the end of your DatabaseSeeder.php class)
  • public/js/vault/*
  • public/css/vault/*

You can also publish individual assets by tag if need be:

$ php artisan vendor:publish --provider="Rappasoft\Vault\VaultServiceProvider" --tag="config" $ php artisan vendor:publish --provider="Rappasoft\Vault\VaultServiceProvider" --tag="migration" $ php artisan vendor:publish --provider="Rappasoft\Vault\VaultServiceProvider" --tag="seeder" $ php artisan vendor:publish --provider="Rappasoft\Vault\VaultServiceProvider" --tag="assets" 

You can also publish views, see configuration below.

Run the dumpautoload command

$ composer dumpautoload -o 

Run the migration command

$ php artisan migrate 
Add the `UserHasRole` trait to your User model:
<?php namespace App; ... use Illuminate\Database\Eloquent\SoftDeletes; use Rappasoft\Vault\Traits\UserHasRole; class User extends Model implements AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword, SoftDeletes, UserHasRole; }
Run the `seed` command
$ php artisan db:seed --class="VaultTableSeeder" 
Add the `route middleware` to your app/Http/Kernel.php file:
protected $routeMiddleware = [ 'auth' => 'App\Http\Middleware\Authenticate', 'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth', 'guest' => 'App\Http\Middleware\RedirectIfAuthenticated', ... 'vault.routeNeedsRole' => 'Rappasoft\Vault\Http\Middleware\RouteNeedsRole', 'vault.routeNeedsPermission' => 'Rappasoft\Vault\Http\Middleware\RouteNeedsPermission', 'vault.routeNeedsRoleOrPermission' => 'Rappasoft\Vault\Http\Middleware\RouteNeedsRoleOrPermission', ];

###就是这样!您现在应该能够访问https:///access/users以查看用户索引。

## Configuration ###Configuration File
/* * The company name used in the footer of the vault views. */ vault.general.company_name /* * Whether or not to load the vault views when the application loads. * Useful if you want to copy the vault routes into your own routes file to modify. */ vault.general.use_vault_routes /* * The namespaced route to the vault role */ vault.role /* * The namespaced route to the vault permission */ vault.permission /* * Used by Vault to save roles to the database. */ vault.roles_table /* * Used by Vault to save permissions to the database. */ vault.permissions_table /* * Used by Vault to save relationship between permissions and roles to the database. */ vault.permission_role_table /* * Used by Vault to save relationship between permissions and users to the database. * This table is only for permissions that belong directly to a specific user and not a role */ vault.permission_user_table /* * Used by Vault to save assigned roles to the database. */ vault.assigned_roles_table /* * Amount of users to show per page for pagination on users.index */ vault.users.default_per_page /* * The rules to validate the users password by when creating a new user */ vault.users.password_validation /* * Whether a role must contain a permission or can be used standalone (perhaps as a label) */ vault.roles.role_must_contain_permission /* * Whether or not the administrator role must possess every permission * Works in unison with permissions.permission_must_contain_role */ vault.roles.administrator_forced /* * Whether a permission must contain a role or can be used standalone * Works in unison with roles.administrator_forced * If a permission doesn't contain a role it can be assigned directly to a user */ vault.permissions.permission_must_contain_role /* * Validation overwrites, at time of validation uses these rules * Each must return an array even if a single rule */ vault.validation.users.create vault.validation.users.update
### Vault Views

By default the package works without publishing its views. But if you wanted to publish the vault views to your application to take full control, run the vault:views command:

$ php artisan vault:views 
### Vault Routes

If you do not want vault to use its default routes file you can duplicate it and set the vault.general.use_vault_routes configuration to false and it will not load by default.

### Utilizing the `status` property

If would would like to enable enabled/disabled users you can simply do a check wherever you are logging in your user:

if ($user->status == 0) return Redirect::back()->withMessage("Your account is currently disabled");
## Applying the Route Middleware

Laravel 5 is trying to steer away from the filters.php file and more towards using middleware. Here is an example right from the vault routes file of a group of routes that requires the Administrator role:

Route::group([ 'middleware' => 'vault.routeNeedsRole', 'role' => ['Administrator'], 'redirect' => '/', 'with' => ['error', 'You do not have access to do that.'] ], function() { Route::group(['prefix' => 'access'], function () { /*User Management*/ Route::resource('users', '\Rappasoft\Vault\Http\Controllers\UserController', ['except' => ['show']]); }); });

The above code checks to see if the currently authenticated user has the role Administrator, if not redirects to / with a session variable that has a key of message and value of You do not have access to do that.

The following middleware ships with the vault package:

  • vault.routeNeedsRole
  • vault.routeNeedsPermission
  • vault.routeNeedsRoleOrPermission
## Route Parameters
  • middleware => The middleware name, you can change them in your app/Http/Kernel.php file.
  • role => A string of one role or an array of roles by name.
  • permission => A string of one permission or an array of permissions by name.
  • needsAll => A boolean, false by default, that states whether or not all of the specified roles/permissions are required to authenticate.
  • with => Sends a session flash on failure. Array with 2 items, first is session key, second is value.
  • redirect => Redirect to a url if authentication fails.
  • redirectRoute => Redirect to a route if authentication fails.
  • redirectAction => Redirect to an action if authentication fails.

If no redirect is specified a response('Unauthorized', 401); will be thrown.

## Create Your Own Middleware

If you would like to create your own middleware, the following methods are available.

/**  * Checks if the user has a Role by its name.  * @param string $name  * @return bool */ Vault::hasRole($role); /**  * Checks to see if the user has an array of roles, and whether or not all must return true to authenticate  * @param array $roles  * @param boolean $needsAll  * @return bool */ Vault::hasRoles($roles, $needsAll); /**  * Check if user has a permission by its name.  * @param string $permission.  * @return bool */ Vault::can($permission); /**  * Check an array of permissions and whether or not all are required to continue  * @param array $permissions  * @param boolean $needsAll  * @return bool */ Vault::canMultiple($permissions, $needsAll);

Vault:: by default uses the currently authenticated user. You can also do:

$user->hasRole($role); $user->hasRoles($roles, $needsAll); $user->can($permission); $user->canMultiple($permissions, $needsAll);
### VaultRoute trait

If you would like to take advantage of the methods used by Vault's route handler, you can use it:

use Rappasoft\Vault\Traits\VaultRoute 

Which will give you methods in your middleware to grab route assets. You can then add methods to your middleware to grab assets that vault doesn't grab by default and take advantage of them.

## Blade Extensions

Vault comes with @blade extensions to help you show and hide data by role or permission without clogging up your code with unwanted if statements:

@role('User') This content will only show if the authenticated user has the `User` role. @endrole @permission('can_view_this_content') This content will only show if the authenticated user is somehow associated with the `can_view_this_content` permission. @endpermission

Currently each call only supports one role or permission, however they can be nested.

If you want to show or hide a specific section you can do so in your layout files the same way:

@role('User') @section('special_content') @endrole @permission('can_view_this_content') @section('special_content') @endpermission
## License

Vault is free software distributed under the terms of the MIT license.

## Additional information

如有任何问题,请在此处报告