我已经使用Drupal很多年了(从Drupal 6开始),直到最近--在一个大项目的背景下--我才转向D8。到目前为止,至少可以说,与D8合作是一种非常愉快的经历。然而,我已经准备好路上会有几块石头。我想请求你帮我移除一个大的.
简而言之,情况是这样的:我正在创建一种learning管理系统 (LMS),它使用lessons、courses和classes。每堂课都属于一门课程,但每门课程可能属于或不属于零级、一门或多门课程。我正在考虑使用简单的节点引用来构造这个结构:
现在,我想出售对每种内容类型的访问权,我的问题是:如果用户购买了对其中一个类的访问,她还应该能够访问这个类引用的所有课程(和课程)。因此,在检查单个课程的访问权限时,系统不仅需要检查用户是否购买了该课程,还需要检查用户是否有权访问包含此课程的类。
我第一次考虑用模块节点访问节点引用来解决这个问题,但这似乎正好相反(而且没有D8版本)。以参考方式取得也一样。(总的来说,我看过的大多数访问控制模块似乎还没有为D8做好准备。)但是基于规则的访问控制呢?还是团体?
有人能提出建议吗?
任何帮助都将不胜感激。提前谢谢。
发布于 2018-05-29 23:39:46
为什么不去自定义访问,主要是将你的“类”或项目与人们购买的产品联系起来--你可以在产品上创建一个实体引用到节点或类或者其他什么.
你可以做如下的事情
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_node_access().
*/
function mymodule_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account){
// Note thes checks do not work on admin (user 1). and clear cache each time.
$type = $node->bundle();
if ($type === 'your_content_type') {
if ($account->id() !== 0) {
$user = $account->getAccount();
$uid = $user->id();
// Ok we know is trying to access and the node type.
$storage = \Drupal::entityTypeManager()->getStorage('commerce_order');
$orders = $storage->loadByProperties(['uid' => $uid]);
if (count($orders) != 0) {
$has_bought = FALSE;
// CUSTOM FIND Your Corse .
$corse_type = $node->field_corse_type->value;
foreach ($orders as $order) {
$order_items = $order->order_items->referencedEntities();
foreach ($order_items as $item) {
// Do better than this lol ...
// dump($item);
if ($item->title->value == 'SOME Course' && $corse_type == 'SOMETHING RELATED TO PRODUCT') {
$has_bought = TRUE;
}
}
}
// final check.
if ($has_bought === TRUE) {
return AccessResult::isAllowed();
}
else{
// Kick them. as not bought.
return AccessResult::forbidden();
}
}
else {
// Kick them. as not paid.
return AccessResult::forbidden();
}
}
else {
// kick them.
return AccessResult::forbidden();
}
}
else {
return AccessResult::neutral();
}
}https://drupal.stackexchange.com/questions/262382
复制相似问题