def _post_process_tags(self): """Process possible after-effects of tags. This may include removing spreads, roles and such. Currently only spreads are supported.""" # Do nothing if the config doesn't have TAG_REWRITE or it's empty. # In the latter case you get very serious side-effects that will wipe # all spreads on all entities due to entity.list_all_with_spread()'s # way of handling an empty argument. if not hasattr(abcconf, "TAG_REWRITE") or not abcconf.TAG_REWRITE: return # Access Entity objects directly. from Cerebrum.Entity import EntitySpread es = EntitySpread(self.db) for row in es.list_all_with_spread( [int(s) for s in abcconf.TAG_REWRITE.values()]): if int(row['entity_id']) in self._spreads: # Entity found, check spreads in database if (not int(row['spread']) in self._spreads[int(row['entity_id'])]): es.clear() es.find(int(row['entity_id'])) es.delete_spread(int(row['spread'])) self.logger.info("Entity: '%d', removed spread '%s'" % (es.entity_id, int(row['spread']))) else: # Entity missing from file, remove all spreads es.clear() es.find(int(row['entity_id'])) es.delete_spread(int(row['spread'])) self.logger.info("Entity: '%d', removed spread '%s'" % (es.entity_id, int(row['spread'])))
def get(self): """List contexts""" args = self.context_search_filter.parse_args() filters = { key: value for (key, value) in args.items() if value is not None } entity_types = None if 'entity_types' in filters: etypes = [] for etype in filters['entity_types']: try: entity_type = db.const.EntityType(etype) int(entity_type) etypes.append(entity_type) except Errors.NotFoundError: abort(404, message='Unknown entity type for ' 'entity_types={}'.format(etype)) entity_types = etypes or None es = EntitySpread(db.connection) contexts = es.list_spreads(entity_types=entity_types) return contexts
def get_enabled_accounts(db): """ Get accounts with spreads. Accounts with spreads will not be considered `Account.is_deleted()` or `Account.is_reserved()`. """ es = EntitySpread(db) co = Factory.get('Constants')(db) return set(e_id for e_id, s_code in es.list_all_with_spread(entity_types=co.entity_account))
def make_spread_filter(db): """ :return callable: Returns a function that returns ``True`` iff the given account entity id has spreads. """ es = EntitySpread(db) co = Factory.get('Constants')(db) logger.debug("caching spreads...") cache = set(entity_id for entity_id, spread_code in es.list_all_with_spread( entity_types=co.entity_account)) logger.debug("done caching spreads") return lambda entity_id: entity_id in cache
def delete_common(entity_id, db): """Remove information from the database common to whichever entity we are deleting. """ # Remove spreads # Remove traits # Remove all permissions # Remove from all groups # Remove change_log entries const = Factory.get("Constants")() logger.debug("Deleting common parts for entity %s (id=%s)", fetch_name(entity_id, db), entity_id) es = EntitySpread(db) es.find(entity_id) logger.debug( "Deleting spreads: %s", ", ".join(str(const.Spread(x["spread"])) for x in es.get_spread())) for row in es.get_spread(): es.delete_spread(row["spread"]) et = EntityTrait(db) et.find(entity_id) logger.debug("Deleting traits: %s", ", ".join(str(x) for x in et.get_traits())) # copy(), since delete_trait and get_traits work on the same dict. This is # so silly. for trait_code in et.get_traits().copy(): et.delete_trait(trait_code) remove_target_permissions(entity_id, db) remove_permissions_on_target(entity_id, db) # Kill memberships group = Factory.get("Group")(db) for row in group.search(member_id=entity_id, filter_expired=False): group.clear() group.find(row["group_id"]) logger.debug("Removing %s as member of %s (id=%s)", entity_id, group.group_name, group.entity_id) group.remove_member(entity_id) # Kill change_log entries logger.debug("Cleaning change_log of references to %s", entity_id) # Kill change_log entries (this includes requests linked to this entity) for row in db.get_log_events(subject_entity=entity_id): db.remove_log_event(row["change_id"])
def get_entity_spreads(db, entity_id): """ Lookup any entity spreads on an entity. entity_id -> [<spread_code_str>, ...] """ # TODO: Include spreads in change_params, so that we don't have to look # them up? constants = Factory.get("Constants")(db) entity = EntitySpread(db) try: entity.find(int(entity_id)) return [six.text_type(constants.Spread(s['spread'])) for s in entity.get_spread()] except (NotFoundError, ValueError): pass return list()
def __init__(self, db, entity_types): es = EntitySpread(db) data = defaultdict(set) for e_id, s_code in es.list_all_with_spread(entity_types=entity_types): data[e_id].add(s_code) self.data = dict(data)