public static function get_auth($type, $auth, $permission, $object)
 {
     $fields_own = Authorization::select("field", "own")->where("auth", "=", $auth)->where($type, ">=", $permission)->where($type, "!=", 0)->where("object", "=", $object);
     $data = array("fields" => $fields_own->pluck('field')->all(), "own" => $fields_own->pluck('own')->first());
     return $data;
 }
 public function removePermission(Requests\PermissionRequest $request)
 {
     $this->middleware('auth');
     if (Gate::denies('manage_institutions')) {
         abort(403);
     }
     if (Gate::denies('update_institution', Institution::findOrFail($request->institution_id))) {
         abort(403);
     }
     Authorization::where('user_id', $request->user_id)->where('institution_id', $request->institution_id)->where('role_id', $request->role_id)->detach();
 }
 static function hasManyThrough($where, $join, $through, $foreign, $local)
 {
     $auth = self::auth();
     if (isset($auth)) {
         if (is_object($where)) {
             if (isset($where->permission)) {
                 $permission = $where->permission;
             } else {
                 $permission = 0;
             }
         } else {
             throw new \RuntimeException('Only object.');
         }
         $auth = Authorization::get_auth("show", $auth, $permission, get_join_name($join));
         $select = $auth['fields'];
         $own = $auth['own'];
         if (count($select) == 0) {
             abort(403);
         }
         $objects = $where->first()->hasManyThrough($join, $through, $foreign, $local)->select($select);
         if ($where->permission == 1) {
             if ($own != null) {
                 $objects->where($own, "=", Auth::user()->id);
             }
         }
         $objects->permission = $permission;
         return $objects;
     }
 }