示例#1
0
 public function role_edit(Request $request)
 {
     $this->validate($request, array('role' => 'required'));
     $permissions = array_filter(explode(',', $request->input('permissions')));
     $error_msg_role = 'The role has already existed!';
     if (!$request->input('id')) {
         if (Role::where('role', $request->input('role'))->first()) {
             return redirect()->back()->withErrors($error_msg_role);
         }
         $lastInsert = Role::create($request->all());
         $insertRoleId = $lastInsert->id;
     } else {
         $self = Role::find($request->input('id'));
         if ($self->role != $request->input('role') && Role::where('role', $request->input('role'))->first()) {
             return redirect()->back()->withErrors($error_msg_role);
         }
         $insertRoleId = $request->input('id');
         Role::find($insertRoleId)->update($request->all());
         PermissionRole::where('role_id', $insertRoleId)->delete();
     }
     foreach ($permissions as $one) {
         PermissionRole::create(array('permission_id' => $one, 'role_id' => $insertRoleId));
     }
     return redirect()->back();
 }
 public function assignRolePermission($role_id)
 {
     try {
         $permissions = Input::get('permissions');
         $inputs = Input::all();
         $permission_ids = PermissionRole::where('role_id', $role_id)->lists('permission_id')->toArray();
         foreach ($permissions as $key => $permission_id) {
             if (in_array($permission_id, $permission_ids) && $inputs["permission_status_{$permission_id}"] == 'off') {
                 //Turn off permission.
                 PermissionRole::where('role_id', $role_id)->where('permission_id', $permission_id)->delete();
             } elseif (!in_array($permission_id, $permission_ids) && $inputs["permission_status_{$permission_id}"] == 'on') {
                 $new_assign_permission = ['role_id' => $role_id, 'permission_id' => $permission_id, 'order' => 1];
                 PermissionRole::create($new_assign_permission);
             }
         }
         return Redirect::to("/crm/role/{$role_id}")->withMessage(Generate::success_message('Success', 'Assigned Successfully'));
     } catch (Exception $e) {
         return Redirect::to("/crm/role/{$role_id}")->withMessage(Generate::error_message('Fail', 'Failed to assign permission.'));
     }
 }