Skip to main content

[Thủ Thuật Drupal] Cách phân quyền trong module Drupal

Bạn đang tìm cách tạo phân quyền cho module mới trong Drupal 8/ Drupal 9 ? Trong Drupal, các quyền hiện được xác định trong file MODULENAME.permissions.yml thay vì sử dụng hook_permission().

Drupal có hai loại quyền mà chúng ta có thể khai báo:

- Tĩnh (Static) là các quyền không phụ thuộc vào bất kỳ thông tin nào trong hệ thống.

- Động (Dynamic) là các quyền được tạo nhanh chóng và dành riêng cho hệ thống (ví dụ: quyền cho từng loại node, vai trò người dùng, v.v.).

Drupal 8/9 Static Permissions

Ta khai báo một phân quyền tĩnh (static) trong file MODULENAME.permissions.yml của module bạn muốn tạo phân quyền

Xem code bên dưới:

static permission 1:
  title: 'First level permission'
  description: 'Gives static permission 1 to the users.'

static permission 2:
  title: 'Second level permission'
  description: 'Gives static permission 2 to the users.'
  restrict access: true

Tùy chọn restrict access sẽ hiển thị thông tin bổ sung trong phần quản trị, cảnh báo role này có tính bảo mật cần phải cẩn thận khi cấp quyền.

Bạn có thể vào đường dẫn "/admin/people/permissions " sẽ thấy được hai phân miền mới được thêm vào

Image
Tạo Phân Quyền Tĩnh trong Drupal 8/9

Drupal Dynamic Permissions

Trong Drupal có hỗ trợ tạo phân quyền động (Dynamic Permissions) thông qua một hàm định nghĩa cho phân quyền đó.

Bước 1 - Tạo callback định nghĩa quyền cho thông qua permission_callbacks của core hỗ trợ. Tạo một file /MODULENAME/MODULENAME.permissions.yml  thêm đoạn code 


permission_callbacks: 
   - \Drupal\mymodule\DynamicPermissions::permissions

Bước 2 - Tiếp theo là tọa file MODULENAME/src/DynamicPermissions.php:

<?php

namespace Drupal\mymodule;

use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Class DynamicPermissions
 * @package Drupal\mymodule
 */
class DynamicPermissions
{

  use StringTranslationTrait;

  /**
   * @return array
   */
  public function permissions()
  {
    $permissions = [];

    $count = 1;
    while ($count <= 5) {
      $permissions += [
        "mymodule permission $count" => [
          'title' => $this->t('mymodule permission @number', ['@number' => $count]),
          'description' => $this->t('This is a sample permission generated dynamically.'),
          'restrict access' => $count == 2 ? true : false,
        ],
      ];
      $count++;
    }
    return $permissions;
  }

}

Bước 3 - Sau đó vào trong "/admin/people/permissions" để kiểm tra

Image
Tạo Phân Quyền Drupal

Sử dụng custom permissions

 Ở đây có 2 cách để sử dụng:

- Trực tiếp trong code

- Kiểm tra truy cập qua route.

Trực tiếp trong Code

if (\Drupal::currentUser()->hasPermission('mymodule permission 2')) {

  // The "mymodule permission 2" has been granted to the current user.

}else{

  // The "mymodule permission 2" has NOT been granted to the current user.

}

Kiểm tra qua Route

Dưới khai báo Route bạn thêm _permission như sau:

mymodule_permissions.index:
  path: '/mymodule-permissions'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MyModuleController::index'
    _title: 'My Module Page Title'
  requirements:
    _permission: 'sample permission 2'