簡體   English   中英

如何限制店主訪問商店頁面?

[英]How to limit access to a store page to the store owner?

我創建了一個自定義模塊來創建/store/ID/tasks頁面

https://www.drupal.org/project/commerce

如何限制店主訪問此頁面?

如果當前用戶是商店 ID 76 的所有者,他可以訪問此頁面:

/store/76/tasks

但如果他去另一家商店,他一定是拒絕了訪問:

/store/89/tasks

https://git.drupalcode.org/sandbox/zenimagine-3076032

task_notify/task_notify.routing.yml

task_notify.store_page.tasks:
  path: '/store/{store}/tasks'
  defaults:
    _controller: '\Drupal\task_notify\Controller\TaskNotifyStoreController::Tasks'
    _title: 'Liste des tâches'
  requirements:
    _custom_access: '\Drupal\task_notify\Controller\TaskNotifyStoreController::taskAccess'

task_notify/src/Controller/TaskNotifyStoreController.php

<?php

namespace Drupal\task_notify\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\commerce_store\Entity\StoreInterface;

class TaskNotifyStoreController extends ControllerBase {

  public function Tasks() {
    return [
      '#theme' => 'task_notify_store_template',
    ];
  }

  public function taskAccess(StoreInterface $store, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $result = $store->access('edit', $account, TRUE);
    return $return_as_object ? $result : $result->isAllowed();
  }

}

僅當當前用戶可以編輯商店(站點管理員和商店所有者)時,該頁面才應該可以訪問。

模塊代碼中的訪問必須與此視圖中的條件相同:

https://i.stack.imgur.com/ZfUMo.png

我受到以下兩個文件的啟發:

https://git.drupalcode.org/project/commerce_marketplace/-/blob/8.x-1.x/src/Plugin/Action/MarketplaceIncreaseStoreLimitByOne.php

https://git.drupalcode.org/project/commerce_marketplace/-/blob/8.x-1.x/src/Plugin/Action/MarketplaceMarkAsDefault.php

在這種情況下,我們可以告訴 Drupal {store} 是一個實體,它將加載 object。 所以我們不必在 Controller function 中這樣做。

所以你的路由文件可以包含“參數”設置來做到這一點。

task_notify.store_page.tasks:
  path: '/store/{store}/tasks'
  defaults:
    _controller: '\Drupal\task_notify\Controller\TaskNotifyStoreController::Tasks'
    _title: 'Liste des tâches'
  requirements:
    _custom_access: '\Drupal\task_notify\Controller\TaskNotifyStoreController::taskAccess'
  options:
    parameters:
      store:
        type: entity:commerce_store

現在您的 controller function 可以訪問該 object。

  public function Tasks(StoreInterface $store) { ...

根據我的經驗,access() 方法並非如此(至少在我們在這里使用類型提示參數時)。 你得到一個字符串,所以你必須手動加載商店。

  public function taskAccess(string $store, AccountInterface $account) {
    $store = \Drupal\commerce_store\Entity\Store::load($store);
    // Check store owner against current user.
    if ($store->access('edit', $account)) {
      return AccessResult::allowed();
    }
    else {
      return AccessResult::forbidden();
    }

此外,我們現在需要在路由文件中定義 $account,因為我們正在使用類型提示參數(我認為)。 因此,將其添加到選項中:。

task_notify.store_page.tasks:
  path: '/store/{store}/tasks'
  defaults:
    _controller: '\Drupal\task_notify\Controller\TaskNotifyStoreController::Tasks'
    _title: 'Liste des tâches'
  requirements:
    _custom_access: '\Drupal\task_notify\Controller\TaskNotifyStoreController::taskAccess'
  options:
    parameters:
      store:
        type: entity:commerce_store
      account: \Drupal\Core\Session\AccountProxy

$account 是我們可以通過這種方式輸入提示的幾個特殊參數之一。 更多信息: https://www.drupal.org/docs/8/api/routing-system/access-checking-on-routes/advanced-route-access-checking

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM