[Thực hành] Phân quyền dựa trên vai trò
NỘI DUNG BÀI VIẾT
Mục tiêu
Hiểu và thực hiện phân quyền dựa trên vai trò trong ứng dụng laravel
Mô tả
Bất kể hệ thống website nào cũng có người dùng và đi kèm với nó là việc xác thực (authentication) và phân quyền (authorization) với từng người dùng. Việc phân quyền cho người dùng để đảm bảo các vấn đề về bảo mật và tuân thủ các chính sách từng hệ thống. Ví dụ như có những người dùng được phép chỉnh sửa hoặc xóa một tài nguyên nhưng cũng có những người dùng chỉ được phép đọc thông tin.
Ở bài này chúng ta sẽ sử dụng Laravel Gate để phân quyền người dùng với 2 role admin và user bình thường
- role user sau khi đăng nhập chỉ được xem view page_guest.blade.php
- role admin được xem page page_guest.blade.php và page_admin.blade.php
Hướng dẫn thực hiện
Phần 1: Chuẩn bị dữ liệu
Bước 1: Tạo mới project
composer create-project --prefer-dist laravel/laravel authorization
Bước 2: Thêm role cho bảng user
Thêm trường admin vào file “databases/migrations/2014_10_12_000000_create_users_table.php”
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->string('admin'); $table->rememberToken(); $table->timestamps(); }); }
Bước 3: Tạo người dùng sử dụng seeding
php artisan make:seeder UsersTableSeeder
Trong file “databases/seeds/UsersTableSeeder.php” tạo 2 user với 2 role khác nhau.
<?php use Illuminate\Database\Seeder; use App\User; class UsersTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { //tao moi admin $user = new User(); $user->name = "adminUser"; $user->email = "[email protected]"; $user->password = bcrypt("admin"); $user->admin = 1; $user->save(); //tao user binh thuong $user = new User(); $user->name = "user"; $user->email = "[email protected]"; $user->password = bcrypt("user"); $user->admin = 0; $user->save(); } }
Bước 4: Cấu hình database và chạy migrate
- Cấu hình database trong file .env
- Chạy migrate
Bước 5: Sử dụng authentication trong laravel
Chạy câu lệnh sau trong thư mục gốc của project
php artisan make:auth
Khởi chạy ứng dụng và login với user vừa tạo.
php artisan serve
Truy cập vào đường dẫn : http://localhost:8000 xem kết quả
Phần 2: Phân quyền dựa theo role
Bước 1: Tạo 2 view: page_guest.blade.php và page_admin.blade.php
File “resources/layouts/page_guest.blade.php” :
<?php echo "xin chao guest"; File "resources/layouts/page_admin.blade.php" : <?php echo "xin chao admin";
Bước 2: Định nghĩa các hành động
Gates là Closures xác định xem người dùng có được phép thực hiện một hành động cụ thể hay không (ở đây chúng ta thực việc check trường admin) và thường được định nghĩa trong App\Providers\AuthServiceProvider:
public function boot() { $this->registerPolicies(); Gate::define('view-page-admin', function ($user) { if ($user->admin == "1") { return true; } return false; }); Gate::define('view-page-guest', function ($user) { if ($user->admin == "1" || $user->admin == "0") { return true; } return false; }); }
Bước 3: Khai báo Gate cho hành động của user trong Controller
public function userCan($action, $option = NULL) { $user = Auth::user(); return Gate::forUser($user)->allows($action, $option); }
Bước 4: Thực hiện hành động phân quyền trong HomeController
Trong UserController tạo 2 action showPageGuest() và showPageAdmin()
public function showPageGuest() { if (!$this->userCan('view-page-guest')) { abort('403', __('Bạn không có quyền thực hiện thao tác này')); } return view("layouts.page_guest"); } public function showPageAdmin() { if (!$this->userCan('view-page-admin')) { abort('403', __('Bạn không có quyền thực hiện thao tác này')); } return view("layouts.page_admin"); }
Bước 4: Tạo route gọi đến action tương ứng
Trong file web.php tạo 2 route gọi đến 2 action tương ứng
Route::get('/page-guest', 'HomeController@showPageGuest'); Route::get('/page-admin', 'HomeController@showPageAdmin');
Đăng nhập với 2 user vừa tạo sau đó truy cập vào các route vừa tạo để xem kết quả
Mã nguồn tham khảo: https://github.com/codegym-vn/laravel-authentication-gat
Leave a Reply