我是CakePHP的新手,但是我已经使用PHP已有一段时间了。我正在尝试创建一个提供用户访问级别(ACL)的助手。
到目前为止,这是我的ACLHelper.php
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\ORM\TableRegistry;
class ACLHelper extends Helper{
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id]);
return $acl->first();
}
}
我以这种方式调用此函数
<?= $this->ACL->getACL($user->id, 'is_items', '4') ?>
这是输出
{ "is_items": "4" }
我需要的是如果字段的值等于或大于提供给函数的$level
的值,则返回true或false的函数。现在,如果我这样做:
<?= $this->ACL->getACL($user->id, 'is_items', '4')->is_item ?>
它将仅返回值。我的问题是我不想两次指定该字段。
在此先感谢您的帮助
答案 0 :(得分:0)
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
// Get the first ACL record right here
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id])->first();
// Compare the requested field against the provided level
return $acl->$acl_field >= $level;
}