/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (!Auth::check()) { Session::flash('msg', '请先登录'); return redirect('admin/login'); } $user = Auth::user(); $currentAction = Route::currentRouteAction(); $actions = []; if ($user->is_admin) { // Cache::forget('actions'); $actions = ActionModel::where('pid', 0)->with('children')->get(); // $actions = Cache::remember('actions',24*60,function(){ // return ActionModel::where('pid', 0)->with('children')->get(); // }); } else { $user->load('role.actions.children'); $actions = $this->buildTree($user->role->actions); if (!$this->checkPermission($currentAction, $actions)) { Session::flash('msg', '对不起,你没有权限访问该资源'); return redirect('admin/login'); } } View::share('actions', $actions); View::share('user_name', $user->user_name); return $next($request); }
/** * @param $id * @return array */ private function parentIds($id) { $pids = array($id); $info = ActionModel::find($id); if ($info && $info->pid != 0) { $return = $this->parentIds($info->pid); if ($return) { $pids = array_merge($pids, $return); } } return $pids; }
private function action_seed($left_bar, $pid = 0) { foreach ($left_bar as $val) { $model = ActionModel::create(array('action_name' => $val['action_name'], 'pid' => $pid, 'action' => $val['action'], 'action_namespace' => $val['action_namespace'], 'action_class' => $val['action_class'], 'action_method' => $val['action_method'], 'created' => time(), 'prefix_class' => $val['prefix_class'])); if (isset($val['children']) && $val['children']) { $this->action_seed($val['children'], $model->id); } if ($val['action_class'] == 'Test1Controller' || $val['action_name'] == '测试模块1') { $this->role1->actions()->save($model); } if ($val['action_class'] == 'Test2Controller' || $val['action_name'] == '测试模块2') { $this->role2->actions()->save($model); } if ($val['action_class'] == 'HomeController') { $this->role1->actions()->save($model); $this->role2->actions()->save($model); } } }