laravel×°ÖÃȨÏÞÖÎÀí
laravelÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄ¹¤¾ßºÍ×é¼þ£¬¿ÉÒÔ´ó´óÌá¸ß¿ª·¢Ð§ÂÊ¡£ÔÚ¿ª·¢Ó¦ÓóÌÐòµÄÀú³ÌÖУ¬Ðí¶àʱ¼äÐèÒª¾ÙÐÐÓû§È¨ÏÞÖÎÀí¡£laravelÌṩÁ˺ÜÊÇÀû±ãµÄȨÏÞÖÎÀí¹¦Ð§£¬¿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËÙºÍÇå¾²µØʵÏÖȨÏÞ¿ØÖÆ¡£
±¾ÎĽ«´ÓÒÔϼ¸¸ö·½ÃæÏÈÈÝLaravelȨÏÞÖÎÀíµÄ×°ÖúÍÉèÖãº
×°ÖÃLaravelȨÏÞÖÎÀí×é¼þ
Êý¾Ý¿âǨáã
Óû§ÈÏÖ¤
½ÇÉ«ºÍȨÏÞÖÎÀí
ÖÐÐļþ
·Óɱ£»¤
Ò»¡¢×°ÖÃLaravelȨÏÞÖÎÀí×é¼þ
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýcomposer×°ÖÃspatie/laravel-permission×é¼þÀ´ÊµÏÖȨÏÞÖÎÀí¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔÔÚÏîÄ¿µÄ¸ùĿ¼ÏÂÖ´ÐÐÒÔÏÂÏÂÁîÀ´×°ÖÃÕâ¸ö×é¼þ£º
composer require spatie/laravel-permission
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³ÉÖ®ºó£¬ÎÒÃÇÐèÒªÔÚconfig/app.phpÎļþÖÐÌí¼ÓÕâ¸ö×é¼þµÄЧÀÍÌṩÕߣº
'providers' => [ // ... SpatiePermissionPermissionServiceProvider::class, ],
µÇ¼ºó¸´ÖÆ
ͬʱ£¬ÔÚͳһ¸öÎļþÖÐÌí¼ÓÕâ¸ö×é¼þµÄÃÅÃ棺
'aliases' => [ // ... 'Permission' => SpatiePermissionFacadesPermission::class, 'Role' => SpatiePermissionFacadesRole::class, ],
µÇ¼ºó¸´ÖÆ
¶þ¡¢Êý¾Ý¿âǨáã
ÔÚ×°ÖÃÍê×é¼þÒÔºó£¬ÎÒÃÇÐèÒªÔËÐÐÊý¾Ý¿âǨáãÒÔ½¨ÉèȨÏÞÏà¹ØµÄ±í¡£ÎÒÃÇ¿ÉÒÔÔÚʹÓÃartisanÏÂÁîÌìÉúÊý¾Ý¿âǨáãÎļþ£º
php artisan make:migration create_permission_tables
µÇ¼ºó¸´ÖÆ
È»ºó£¬·¿ªÌìÉúµÄǨáãÎļþ£¬Ìí¼ÓÒÔÏ´úÂ룺
class CreatePermissionTables extends Migration { public function up() { Schema::create('permissions', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('guard_name'); $table->timestamps(); }); Schema::create('roles', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('guard_name'); $table->timestamps(); }); Schema::create('model_has_roles', function (Blueprint $table) { $table->integer('role_id')->unsigned(); $table->morphs('model'); $table->string('model_type')->nullable(); $table->string('guard_name'); $table->foreign('role_id')->references('id')->on('roles') ->onDelete('cascade'); $table->primary(['role_id', 'model_id', 'model_type']); }); Schema::create('model_has_permissions', function (Blueprint $table) { $table->integer('permission_id')->unsigned(); $table->morphs('model'); $table->string('model_type')->nullable(); $table->string('guard_name'); $table->foreign('permission_id')->references('id')->on('permissions') ->onDelete('cascade'); $table->primary(['permission_id', 'model_id', 'model_type']); }); Schema::create('role_has_permissions', function (Blueprint $table) { $table->integer('permission_id')->unsigned(); $table->integer('role_id')->unsigned(); $table->string('guard_name'); $table->foreign('permission_id')->references('id')->on('permissions') ->onDelete('cascade'); $table->foreign('role_id')->references('id')->on('roles') ->onDelete('cascade'); $table->primary(['permission_id', 'role_id']); }); } public function down() { Schema::dropIfExists('permissions'); Schema::dropIfExists('roles'); Schema::dropIfExists('model_has_roles'); Schema::dropIfExists('model_has_permissions'); Schema::dropIfExists('role_has_permissions'); } }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃǾͿÉÒÔÔËÐÐǨáãÏÂÁîÁË£º
php artisan migrate
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬Ïà¹ØµÄ±í¾Í»á±»½¨Éèµ½Êý¾Ý¿âÖС£
Èý¡¢Óû§ÈÏÖ¤
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÔÚÓ¦ÓÃÖÐʵÏÖÓû§ÈÏÖ¤¹¦Ð§¡£LaravelÒѾΪÎÒÃÇÌṩÁ˺ÜÀû±ãµÄÓû§ÈÏ֤ϵͳ£¬ÎÒÃÇÖ»ÐèÒªÔËÐÐÒÔÏÂÏÂÁ
php artisan make:auth
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áÌìÉúÒ»¸ö°üÀ¨Óû§µÇ¼¡¢×¢²á¡¢ÐÞ¸ÄÃÜÂëµÈ¹¦Ð§µÄÒ³Ãæ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÕâЩ²Ù×÷À´½¨ÉèºÍÖÎÀíÓû§¡£
ËÄ¡¢½ÇÉ«ºÍȨÏÞÖÎÀí
ÔÚLaravelȨÏÞÖÎÀíÖУ¬½ÇÉ«ºÍȨÏÞÊǺÜÊÇÖ÷ÒªµÄ¿´·¨¡£ÎÒÃÇ¿ÉÒÔͨ¹ý½ÇÉ«ºÍȨÏÞÀ´½ç˵Óû§µÄ»á¼û¿ØÖƹæÔò¡£
½¨Éè½ÇÉ«
ÎÒÃÇ¿ÉÒÔʹÓÃRoleÃÅÃæÀ´½¨Éè½ÇÉ«¡£ÀýÈ磺
use SpatiePermissionModelsRole; $role = Role::create(['name' => 'admin']);
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫½¨ÉèÒ»¸öÃûΪ¡°admin¡±µÄ½ÇÉ«¡£
½¨ÉèȨÏÞ
ͬÑùµÄ£¬ÎÒÃÇ¿ÉÒÔʹÓÃPermissionÃÅÃæÀ´½¨ÉèȨÏÞ£º
use SpatiePermissionModelsPermission; $permission = Permission::create(['name' => 'create posts']);
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫½¨ÉèÒ»¸öÃûΪ¡°create posts¡±µÄȨÏÞ¡£
¸ø½ÇÉ«ÊÚȨ
ÏÖÔÚÎÒÃÇÓÐÁ˽ÇÉ«ºÍȨÏÞ£¬ÎÒÃÇ»¹ÐèÒª½«È¨ÏÞÊÚÓè¸ø½ÇÉ«¡£ÎÒÃÇ¿ÉÒÔʹÓýÇÉ«µÄgivePermissionToÒªÁìÀ´Íê³É£º
$role = Role::findByName('admin'); $permission = Permission::findByName('create posts'); $role->givePermissionTo($permission);
µÇ¼ºó¸´ÖÆ
¼ì²éÓû§ÊÇ·ñ¾ßÓÐȨÏÞ
ÏÖÔÚÎÒÃÇÒѾ½ç˵Á˽ÇÉ«ºÍȨÏÞ£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelȨÏÞÖÎÀíÌṩµÄcanÒªÁìÀ´¼ì²éÓû§ÊÇ·ñ¾ßÓÐȨÏÞ¡£ÀýÈ磺
$user->can('create posts');
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫·µ»ØÒ»¸ö²¼¶ûÖµ£¬ÌåÏÖÄ¿½ñÓû§ÊÇ·ñ¾ßÓС°create posts¡±È¨ÏÞ¡£
¼ì²éÓû§ÊÇ·ñ¾ßÓнÇÉ«
ͬÑùµÄ£¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃhasRoleÒªÁìÀ´¼ì²éÓû§ÊÇ·ñ¾ßÓÐij¸ö½ÇÉ«¡£ÀýÈ磺
$user->hasRole('admin');
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫·µ»ØÒ»¸ö²¼¶ûÖµ£¬ÌåÏÖÄ¿½ñÓû§ÊÇ·ñ¾ßÓС°admin¡±½ÇÉ«¡£
Îå¡¢ÖÐÐļþ
ÎÒÃÇ¿ÉÒÔʹÓÃLaravelµÄÖÐÐļþÀ´±£»¤ÎÒÃǵÄ·ÓɺͿØÖÆÆ÷£¬´Ó¶øʵÏÖȨÏÞ¿ØÖÆ¡£ÏÂÃæÊÇʾÀý´úÂ룺
Route::group([ 'middleware' => ['role:admin'], ], function () { Route::get('/admin', function () { // }); }); Route::group([ 'middleware' => ['permission:create posts'], ], function () { Route::get('/new-post', function () { // }); });
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫±£»¤¡°/admin¡±ºÍ¡°/new-post¡±Â·ÓÉ£¬Ö»ÔÊÐíÓµÓС°admin¡±½ÇÉ«ºÍ¡°create posts¡±È¨ÏÞµÄÓû§»á¼û¡£
Áù¡¢Â·Óɱ£»¤
×îºó£¬ÎÒÃÇÐèÒª±£»¤ÎÒÃǵÄ·ÓɺͿØÖÆÆ÷¡£ÎÒÃÇ¿ÉÒÔʹÓÃcanºÍauthorizeÒªÁìÀ´ÊµÏÖ¡£
public function store(Request $request) { $this->authorize('create', Post::class); // ... } public function edit(Request $request, Post $post) { if (! $request->user()->can('edit', $post)) { abort(403); } // ... }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫±£»¤storeºÍeditÒªÁ죬ֻÔÊÐíÓµÓС°create¡±ºÍ¡°edit¡±µÄȨÏÞµÄÓû§»á¼û¡£
×ܽá
×ܵÄÀ´Ëµ£¬LaravelµÄȨÏÞÖÎÀíºÜÊÇÀû±ãºÍÇå¾²¡£ÎÒÃÇ¿ÉÒÔͨ¹ý×°ÖÃspatie/laravel-permission×é¼þÀ´ÊµÏÖȨÏÞ¿ØÖÆ£¬²¢Ê¹ÓÃLaravelÌṩµÄÐí¶àÒªÁìºÍ¹¦Ð§À´ÖÎÀí½ÇÉ«ºÍȨÏÞ¡£Í¨¹ýÖÐÐļþºÍ·Óɱ£»¤£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ±£»¤ÎÒÃǵÄÓ¦ÓóÌÐò£¬²¢ÏÞÖÆÓû§µÄ»á¼ûȨÏÞ¡£
ÒÔÉϾÍÊÇlaravel×°ÖÃȨÏÞÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡