def _filter_authorized_instances(self, instances, api): if not instances: return None user = authentication.get_user(token=authentication.get_token()) valid_instances = [ i for i in instances if authentication.has_access( i.name, abort=False, user=user, api=api) ] if not valid_instances: authentication.abort_request(user) return valid_instances
def _filter_authorized_instances(self, instances, api): if not instances: return None # During the period database is not accessible, all the instances are valid for the user. if not can_connect_to_database(): return instances user = authentication.get_user(token=authentication.get_token()) valid_instances = [ i for i in instances if authentication.has_access( i.name, abort=False, user=user, api=api) ] if not valid_instances: context = 'User has no access to any instance' authentication.abort_request(user, context) return valid_instances
def get_instances(self, name=None, lon=None, lat=None, object_id=None, api='ALL'): available_instances = [] if name: if name in self.instances: available_instances = [self.instances[name]] elif lon and lat: available_instances = [self.instances[k] for k in self._all_keys_of_coord(lon, lat)] elif object_id: available_instances = [self.instances[k] for k in self._all_keys_of_id(object_id)] else: available_instances = self.instances.values() valid_instances = self._filter_authorized_instances(available_instances, api) if available_instances and not valid_instances: #user doesn't have access to any of the instances authentication.abort_request(user=authentication.get_user()) else: return valid_instances
def get_regions(self, region_str=None, lon=None, lat=None, object_id=None, api='ALL', only_one=False): available_regions = [] if region_str and self.region_exists(region_str): available_regions = [region_str] elif lon and lat: available_regions = self._all_keys_of_coord(lon, lat) elif object_id: available_regions = self._all_keys_of_id(object_id) else: available_regions = self.instances.keys() valid_regions = self._filter_authorized_instances(available_regions, api) if valid_regions: return choose_best_instance(valid_regions) if only_one else valid_regions elif available_regions: authentication.abort_request(user=authentication.get_user()) raise RegionNotFound(region=region_str, lon=lon, lat=lat, object_id=object_id)