后端获取登录用户权限
/**
* 获取用户菜单属性
* @return array
*/
public function getMenusAttribute()
{
$user = Auth::user();
if ($user->admin) {
$permissions = DB::table('permissions')
->select('id', 'dependency', 'name', 'icon', 'type', 'router', 'status')
->where('type', 'menu')
->get()->groupBy('dependency');
} else {
/**
* 获取人员所在部门的权限
*/
$departments = DB::table('users')->select('permissions.*')
->leftJoin('department_user', 'users.id', '=', 'department_user.user_id')
->join('departments', 'department_user.department_id', '=', 'departments.id')
->leftJoin('department_permission', 'departments.id', '=', 'department_permission.department_id')
->join('permissions', 'department_permission.permission_id', '=', 'permissions.id')
->where('users.id', $user->id)
->get();
/**
* 获取人员所在岗位的权限
*/
$positions = DB::table('users')->select('permissions.*')
->leftJoin('position_user', 'users.id', '=', 'position_user.user_id')
->join('positions', 'position_user.position_id', '=', 'positions.id')
->leftJoin('position_permission', 'positions.id', '=', 'position_permission.position_id')
->join('permissions', 'position_permission.permission_id', '=', 'permissions.id')
->where('users.id', $user->id)
->get();
/**
* 获取人员拥有的特殊权限
*/
$users = DB::table('users')->select('permissions.*')
->leftJoin('user_permission', 'users.id', '=', 'user_id')
->join('permissions', 'user_permission.permission_id', '=', 'permissions.id')
->where('users.id', $user->id)
->get();
/**
* 将三种权限按依赖关系分组并根据ID排序
*/
$permissions = collect(array($departments, $positions, $users))
->collapse()
->sortBy('id')
->groupBy('dependency');
}
$menus = [];
/**
* 将Collection按照菜单结构添加到$menus数组中
*/
foreach ($permissions as $key => $value) {
if ($key == 0) {
foreach ($value as $item) {
$menus[$item->id] = $item;
}
} else {
$menus[$key]->children = $value;
}
}
return $menus;
}