通過(guò)創(chuàng)建一個(gè)小程序表,添加字段admin_id,
admin添加兩個(gè)管理員,權(quán)限都為超級(jí)管理員的下級(jí)管理員。
新建小程序數(shù)據(jù),分別為用戶名admin 和 admin2各添加了兩個(gè)小程序
數(shù)據(jù)限制模式為auth,則admin和admin2可以互相看到對(duì)方同級(jí)數(shù)據(jù),并不是只看到自己的和下級(jí)的。
數(shù)據(jù)限制模式為personal,則admin和admin2只可以看到自己
crud類里面的auth限制里面為獲取權(quán)限范圍內(nèi)所有管理員ID
AppletController
/**
* 開(kāi)啟auth數(shù)據(jù)限制
* @var string
*/
protected $dataLimit = 'personal';
/**
* 以admin_id為數(shù)據(jù)限制字段
* @var string
*/
protected $dataLimitField = 'admin_id';
CRUD
/**
* 查詢前置
* @param Request $request
* @return array
* @throws BusinessException
*/
protected function selectInput(Request $request): array
{
$field = $request->get('field');
$order = $request->get('order', 'asc');
$format = $request->get('format', 'normal');
$limit = (int)$request->get('limit', $format === 'tree' ? 1000 : 10);
$limit = $limit <= 0 ? 10 : $limit;
$order = $order === 'asc' ? 'asc' : 'desc';
$where = $request->get();
$page = (int)$request->get('page');
$page = $page > 0 ? $page : 1;
$table = config('plugin.admin.database.connections.mysql.prefix') . $this->model->getTable();
$allow_column = Util::db()->select("desc `$table`");
if (!$allow_column) {
throw new BusinessException('表不存在');
}
$allow_column = array_column($allow_column, 'Field', 'Field');
if (!in_array($field, $allow_column)) {
$field = null;
}
foreach ($where as $column => $value) {
if (
$value === '' || !isset($allow_column[$column]) ||
is_array($value) && (empty($value) || !in_array($value[0], ['null', 'not null']) && !isset($value[1]))
) {
unset($where[$column]);
}
}
// 按照數(shù)據(jù)限制字段返回?cái)?shù)據(jù)
if ($this->dataLimit === 'personal') {
$where[$this->dataLimitField] = admin_id();
} elseif ($this->dataLimit === 'auth') {
$primary_key = $this->model->getKeyName();
if (!Auth::isSupperAdmin() && (!isset($where[$primary_key]) || $this->dataLimitField != $primary_key)) {
$where[$this->dataLimitField] = ['in', Auth::getScopeAdminIds(true)];
}
}
return [$where, $format, $limit, $field, $order, $page];
}
修改數(shù)據(jù)限制模式 無(wú)痕窗口登陸 兩個(gè)管理員賬號(hào)
Mac系統(tǒng) 環(huán)境為PHP8.2.10
如果是超級(jí)管理員就返回所有的,不是就只返回自己的。
權(quán)限這塊沒(méi)做判斷。
// 按照數(shù)據(jù)限制字段返回?cái)?shù)據(jù)
if ($this->dataLimit === 'personal') {
$where[$this->dataLimitField] = admin_id();
} elseif ($this->dataLimit === 'auth') {
if (Auth::isSupperAdmin()) {
$where[$this->dataLimitField] = ['in', Auth::getScopeAdminIds(true)];
} else {
$where[$this->dataLimitField] = admin_id();
}
// $primary_key = $this->model->getKeyName();
// if (!Auth::isSupperAdmin() && (!isset($where[$primary_key]) || $this->dataLimitField != $primary_key)) {
// $where[$this->dataLimitField] = ['in', Auth::getScopeAdminIds(true)];
// }
}
return [$where, $format, $limit, $field, $order, $page];