/** * 检查权限 */ public function power_check() { /** * var_dump(request()->method()); * string 'GET' (length=3) * var_dump(Route::currentRouteAction()); * string 'App\Http\Controllers\Admin\GroupController@get_list' (length=51) */ // route $route = Route::currentRouteAction(); $temp_r = explode('@', $route); $temp_ctl = explode('\\', $temp_r[0]); // controller acction method $controller = end($temp_ctl); // controller $action = end($temp_r); // action $method = request()->method(); // method // delete temp var unset($temp_r); unset($temp_ctl); $oAdmin = Admin::findOrFail(session('admin_id')); $oGroups = AdminGroup::whereIn('id', json_decode($oAdmin->groups))->get(); $oPowers = Power::where('controller', $controller)->where('action', $action)->get(); $aPowers = array_column($oPowers->toArray(), 'method', 'id'); /** * 选出继续操作需要的权限ID */ $iNeedPower = null; // id foreach ($aPowers as $key => $value) { if ($value === $method || $value === '') { $iNeedPower = $key; break; } } if ($iNeedPower === null) { // 没有此权限记录 return False; } /** * 检查用户所属的组中有没有拥有这种权限的组 */ $flag = False; foreach ($oGroups as $oGroup) { $powers = json_decode($oGroup->power); $powers = empty($powers) ? array() : $powers; if (in_array($iNeedPower, $powers)) { $flag = True; break; } } return $flag; }
public static function get_groups_info($adminid = null) { $adminid = $adminid ? $adminid : session('admin_id'); $aGroups = self::get_groups($adminid); return AdminGroup::whereIn('id', $aGroups)->get()->toArray(); }