272 lines
8.3 KiB
PHP
Executable File
272 lines
8.3 KiB
PHP
Executable File
<?php
|
||
namespace app\model;
|
||
|
||
class AuthRule extends Base
|
||
{
|
||
//根据权限IDs获取权限列表
|
||
public static function getListByRuleIDs($ruleIds)
|
||
{
|
||
$items = self::wherein('id',$ruleIds)
|
||
//->where('status', 1)
|
||
->order('sort', 'asc')
|
||
->select()
|
||
->toArray();
|
||
return self::getChildren($items);
|
||
}
|
||
|
||
//角色权限 + 基本权限
|
||
public static function getAuthListByRuleIDs($groupId)
|
||
{
|
||
$order = ['sort'=>'asc'];
|
||
if ($groupId == 1) {
|
||
$items = self::order($order)->select()->toArray();
|
||
} else {
|
||
$group = AuthGroup::getById($groupId);
|
||
$rulesArr = [];
|
||
if ($group && !empty($group['rules'])) {
|
||
$rulesArr = array_filter(explode(',', $group['rules']));
|
||
}
|
||
if (count($rulesArr) >0) {
|
||
$items = self::wherein('id', $rulesArr)->whereOr('is_base', 1)->order($order)->select()->toArray();
|
||
} else {
|
||
$items = self::where('is_base', 1)->order($order)->select()->toArray();
|
||
}
|
||
}
|
||
$levelItems = self::getChildren($items);
|
||
$levelIds = self::getChildrenIds($items);
|
||
// 独立写入不在连续分层中的权限,连续分层权限可用于菜单显示
|
||
foreach ($items as $item) {
|
||
if(!in_array($item['id'], $levelIds)) {
|
||
$levelItems[] = $item;
|
||
}
|
||
}
|
||
return $levelItems;
|
||
}
|
||
|
||
//根据上级ID获取下级权限列表
|
||
public static function getListTree($forMenu = 0)
|
||
{
|
||
$items = self::getList($forMenu);
|
||
return self::getChildren($items);
|
||
}
|
||
|
||
//根据上级ID获取下级权限列表
|
||
public static function getListByParentId($parentId)
|
||
{
|
||
return self::where('parent_id', $parentId)
|
||
->order('sort', 'asc')
|
||
->select()
|
||
->toArray();
|
||
}
|
||
|
||
/**
|
||
* @param array $items
|
||
* @param integer $parent_id
|
||
* @return array
|
||
*/
|
||
public static function getChildren($items, $parentId = 0)
|
||
{
|
||
$data = [];
|
||
foreach($items as $item){
|
||
if($item['parent_id'] == $parentId){
|
||
$childern = self::getChildren($items, $item['id']);
|
||
if(!empty($childern)){
|
||
$item['hasChildren'] = true;
|
||
$item['children'] = $childern;
|
||
}
|
||
$data[] = $item;
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
public static function getChildrenIds($items, $parentId = 0)
|
||
{
|
||
$data = [];
|
||
foreach($items as $item){
|
||
if($item['parent_id'] == $parentId){
|
||
$childrenIds = self::getChildrenIds($items, $item['id']);
|
||
if(!empty($childrenIds)){
|
||
$data = array_merge($data, $childrenIds);
|
||
}
|
||
$data[] = $item['id'];
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* @param integer $forMenu 大于0表示获取可显示的权限
|
||
* @return void
|
||
*/
|
||
private static function getList($forMenu = 0)
|
||
{
|
||
if($forMenu){
|
||
return self::where('status', 1)->order('sort', 'asc')->select()->toArray();
|
||
}else{
|
||
return self::order('sort', 'asc')->select()->toArray();
|
||
}
|
||
}
|
||
|
||
//获取可显示权限
|
||
public static function getTopListForDisplay()
|
||
{
|
||
return self::where('status', 1)
|
||
->where('parent_id', 0)
|
||
->order('sort', 'asc')
|
||
->select()
|
||
->toArray();
|
||
}
|
||
|
||
//根据两个名字获取权限
|
||
public static function getByTwoName($name, $otherName)
|
||
{
|
||
return self::whereOr('name',$name)
|
||
->whereOr('name',$otherName)
|
||
->findOrEmpty()
|
||
->toArray();
|
||
}
|
||
|
||
//根据name获取权限
|
||
public static function getByName($name)
|
||
{
|
||
return self::where('name', $name)
|
||
->findOrEmpty()
|
||
->toArray();
|
||
}
|
||
|
||
public static function onAfterInsert($rule)
|
||
{
|
||
$rule->sort = $rule->id;
|
||
$rule->save();
|
||
}
|
||
|
||
/**
|
||
* 该情况适合无限级菜单分组显示 (当前系统仅支持2级权限,因此推荐使用getListTree 方法)
|
||
* @param int $forMenu 是否只获取可显示菜单权限
|
||
*/
|
||
public static function getListTreeSort($forMenu = 0)
|
||
{
|
||
$items = self::getList($forMenu);
|
||
return self::groupSort($items, 0, 1, true, true);
|
||
}
|
||
|
||
/**
|
||
* 分组排序(不拆分为子集)
|
||
*
|
||
* @param $items 数据源
|
||
* @param integer $pid
|
||
* @param integer $level
|
||
* @param bool $clear 是否释放局部变量(外部调用时必须先释放,避免数据被累加;内部不用,需要累加)
|
||
* @param bool $isArr 传入的$items是否为数组,默认否(数据集)
|
||
* @param string $levelSpare 分层符
|
||
* @return void
|
||
*/
|
||
public static function groupSort($items,$pid = 0, $level = 1, $clear = true, $isArr = false, $levelSpare = '_')
|
||
{
|
||
static $data = [];
|
||
if ($clear) {
|
||
$data = [];
|
||
static $data = [];
|
||
}
|
||
if(!empty($levelSpare) && $level > 1) {
|
||
$levelSpare = str_repeat($levelSpare, $level-1);
|
||
}
|
||
if (count($items) > 0) {
|
||
if ($isArr) {
|
||
foreach ($items as $key => $item) {
|
||
if ($item['parent_id'] == $pid) {
|
||
$item['level'] = $level;
|
||
$item['title'] = $levelSpare.$item['title'];
|
||
$data[] = $item;
|
||
self::groupSort($items, $item['id'], $level+1, false, $isArr);
|
||
}
|
||
}
|
||
} else {
|
||
foreach ($items as $key => $item) {
|
||
if ($item->parent_id == $pid) {
|
||
$item->level = $level;
|
||
$item->title = $levelSpare.$item->title;
|
||
$data[] = $item;
|
||
self::groupSort($items, $item->id, $level+1, false, $isArr);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 树形排序 (拆分子集)
|
||
*
|
||
* @param Collection $items 数据集(非数组)
|
||
* @param integer $pid
|
||
* @param integer $level
|
||
* @return void
|
||
*/
|
||
public static function treeSort($items,$pid = 0, $level = 1)
|
||
{
|
||
$data = [];
|
||
if (count($items) > 0) {
|
||
foreach ($items as $key => $item) {
|
||
if ($item->parent_id == $pid) {
|
||
$item->level = $level;
|
||
$children = self::treeSort($items, $item->id, $level+1);
|
||
$item->children = $children;
|
||
$data[] = $item;
|
||
}
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 查询用户权限(登陆时更具角色更新为最新权限)
|
||
* 可显示权限和所有权限 角色权限 + 基本权限
|
||
*
|
||
* @param boolean $groupId 角色ID,超级管理员(对应group_id = 1)
|
||
* @return void
|
||
*/
|
||
public static function userRolesList($groupId = 1)
|
||
{
|
||
$userRoles = [];
|
||
$items = null;
|
||
$order = ['sort'=>'asc'];
|
||
if ($groupId == 1) {
|
||
$items = self::order($order)->select();
|
||
} else {
|
||
$group = AuthGroup::getById($groupId);
|
||
$rulesArr = [];
|
||
if ($group && !empty($group['rules'])) {
|
||
$rulesArr = array_filter(explode(',', $group['rules']));
|
||
}
|
||
if (count($rulesArr) >0) {
|
||
$items = self::wherein('id', $rulesArr)->whereOr('is_base', 1)->order($order)->select();
|
||
} else {
|
||
$items = self::where('is_base', 1)->order($order)->select();
|
||
}
|
||
}
|
||
if (empty($items) || $items->isEmpty()) {
|
||
return $userRoles;
|
||
}
|
||
|
||
$allRulesId = [];
|
||
$displayRules = [];
|
||
$displayRulesId = [];
|
||
foreach ($items as $key => $item) {
|
||
$allRulesId[] = $item->id;
|
||
if ($item->status > 0) {
|
||
$displayRules[] = $item;
|
||
$displayRulesId[] = $item->id;
|
||
}
|
||
}
|
||
|
||
$userRoles['allRules'] = $items;
|
||
$userRoles['allRulesId'] = $allRulesId;
|
||
$userRoles['displayRules'] = $displayRules;
|
||
$userRoles['displayRulesId'] = $displayRulesId;
|
||
return $userRoles;
|
||
}
|
||
|
||
}
|