如何在内置 Laravel 的身份验证之上构建权限系统?

How to build a privileges system on the top of the built in Laravel's Authentication?

我需要构建某种类型的权限系统,以便我可以控制谁可以在我的应用程序上执行什么操作。我在网上看到很多现成的软件包,但是 none 脱颖而出,我可以利用它来满足我的需求,而无需对 permissions/templates.

进行硬编码

我的想法是创建一个中间件,它将针对登录使用进行身份验证。如果我有以下路线

Route::get('accounts', array(
    'as' => 'accounts_index_path',
    'uses' => 'AccountController@index')
);

Route::get('account/create', array(
    'as' => 'account_create_path',
    'uses' => 'AccountController@create')
);

Route::post('account/store', array(
    'as' => 'account_store_path',
    'uses' => 'AccountController@store')
);

Route::get('account/{account}', array(
    'as' => 'account_show_path',
    'uses' => 'AccountController@show')
)->where('account', '[0-9]+');

Route::get('account/{account}/edit', array(
    'as' => 'account_edit_path',
    'uses' => 'AccountController@edit')
)->where('account', '[0-9]+');

Route::put('account/{account}/update', array(
    'as' => 'account_update_path',
    'uses' => 'AccountController@update')
)->where('account', '[0-9]+');


Route::delete('account/{account}', array(
    'as' => 'account_destroy_path',
    'uses' => 'AccountController@destroy')
)->where('account', '[0-9]+');

理论上,我应该能够让用户访问以下路由名称 "account_store_path"、“'account_create_path”、"accounts_index_path" ...

也许,如果我不想让用户访问特定的路由,那么我不会给他们权限。

理论上这应该可行,但我想拥有更强大的系统,我可以在其中授予用户编辑记录但不是所有字段的权限。例如,我希望具有 "manager" 角色的用户能够更改帐户名称、帐户所有者,但我不希望具有 "standard" 角色的用户编辑这些字段,但我想要标准用户能够更新备注字段和其他字段。

澄清一下,我知道我可能需要创建一个具有所有允许权限的模板,然后将用途分配给正确的模板。

当我有权决定谁可以更新什么时,我需要帮助来尝试为这种特权系统设计最佳方法。

问题 我如何 allow/disallow 用户更新某些字段而不更新其他字段?

可以在 Laravel 中构建角色。有一个视频教程可以在用户身份验证的基础上构建您自己的角色系统。遵循此 link https://laracasts.com/lessons/users-and-roles 这是为 Laravel 4.2 完成的,但稍作改动,您应该能够为 Laravel 5.x 完成。我已经在 4.2、5.0 和 5.1 上完成了它的工作。完成后,您可以创建管理员、超级用户、更新用户等组。根据您的权限结构,为不同的组赋予不同的权限。

关于此主题的一些 link 阅读:

https://laracasts.com/discuss/channels/laravel/which-package-is-best-for-roles-permissions/?page=2
https://laracasts.com/discuss/channels/general-discussion/users-and-roles-1


编辑
我已经更详细地回答了另一个问题,也可以看看它:
Laravel 5.1 multiple authentication