Esempio n. 1
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit(Request $request, $id)
 {
     //
     $oGroup = Group::findOrFail($id);
     // 需要更新的组
     $powers = Group::get_group_power($id);
     // 组拥有的权限
     $oAllPowers = Power::select(['id', 'category', 'name'])->get();
     // 全部的权限
     // 按分类分开
     $aCategoryPowers = array();
     foreach ($oAllPowers as $k => $v) {
         $aCategoryPowers[$v->category][] = ['id' => $v->id, 'name' => $v->name];
     }
     if ($request->isMethod('get')) {
         return view('admin.group.edit', ['group' => $oGroup, 'power_categorys' => $aCategoryPowers, 'powers' => $powers]);
     }
     // post
     $sGroupName = $request->input('groupname', $oGroup->groupname);
     $aGroupPower = $request->input('powers', array());
     $aAllPowers = array_column($oAllPowers->toArray(), 'id');
     try {
         $j = count($aGroupPower);
         for ($i = 0; $i < $j; $i++) {
             $aGroupPower[$i] = intval($aGroupPower[$i]);
             if (!in_array($aGroupPower[$i], $aAllPowers)) {
                 // 权限不存在
                 unset($aGroupPower[$i]);
             }
         }
     } catch (Exception $e) {
         App::abort(404);
     }
     // 超级管理员的权限永远是所有,无法更改的
     // 只有名字可以修改
     if ($oGroup->id != 1) {
         Group::update_powers($id, $aGroupPower);
     }
     $oGroup->save();
     session()->flash('msg_success', '修改成功');
     return back();
 }