Exemple #1
0
 def load_fixtures(self):
     self.private_coll = Collection.create({
         'foreign_id': 'test_private',
         'label': "Private Collection",
         'category': 'grey'
     })
     self._banana = Entity.create(
         {
             'schema': 'Person',
             'properties': {
                 'name': ['Banana'],
             }
         }, self.private_coll)
     user = Role.by_foreign_id(Role.SYSTEM_USER)
     Permission.grant(self.private_coll, user, True, False)
     self.public_coll = Collection.create({
         'foreign_id': 'test_public',
         'label': "Public Collection",
         'category': 'news'
     })
     self._kwazulu = Entity.create(
         {
             'schema': 'Company',
             'properties': {
                 'name': ['KwaZulu'],
                 'alias': ['kwazulu']
             }
         }, self.public_coll)
     visitor = Role.by_foreign_id(Role.SYSTEM_GUEST)
     Permission.grant(self.public_coll, visitor, True, False)
     db.session.commit()
     samples = read_entities(self.get_fixture_path('samples.ijson'))
     index_entities(self.private_coll, samples)
     process_collection(self.public_coll, ingest=False, reset=True)
     process_collection(self.private_coll, ingest=False, reset=True)
Exemple #2
0
    def load_fixtures(self):
        self.admin = self.create_user(foreign_id='admin', is_admin=True)
        self.private_coll = self.create_collection(
            foreign_id='test_private',
            label="Private Collection",
            category='grey',
            creator=self.admin
        )
        self._banana = self.create_entity({
            'schema': 'Person',
            'properties': {
                'name': ['Banana'],
                'birthDate': '1970-08-21'
            }
        }, self.private_coll)
        self._banana2 = self.create_entity({
            'schema': 'Person',
            'properties': {
                'name': ['Banana'],
                'birthDate': '1970-03-21'
            }
        }, self.private_coll)
        self._banana3 = self.create_entity({
            'schema': 'Person',
            'properties': {
                'name': ['Banana'],
                'birthDate': '1970-05-21'
            }
        }, self.private_coll)
        user = Role.by_foreign_id(Role.SYSTEM_USER)
        Permission.grant(self.private_coll, user, True, False)
        self.public_coll = self.create_collection(
            foreign_id='test_public',
            label="Public Collection",
            category='news',
            creator=self.admin
        )
        self._kwazulu = self.create_entity({
            'schema': 'Company',
            'properties': {
                'name': ['KwaZulu'],
                'alias': ['kwazulu']
            }
        }, self.public_coll)
        visitor = Role.by_foreign_id(Role.SYSTEM_GUEST)
        Permission.grant(self.public_coll, visitor, True, False)
        db.session.commit()

        aggregator = get_aggregator(self.public_coll)
        aggregator.delete()
        aggregator.close()
        reindex_collection(self.public_coll, sync=True)

        aggregator = get_aggregator(self.private_coll)
        aggregator.delete()
        for sample in read_entities(self.get_fixture_path('samples.ijson')):
            aggregator.put(sample, fragment='sample')
        aggregator.close()
        reindex_collection(self.private_coll, sync=True)
Exemple #3
0
def get_public_roles():
    app = current_app._get_current_object()
    if not hasattr(app, '_public_roles') or not len(app._public_roles):
        roles = [
            Role.by_foreign_id(Role.SYSTEM_GUEST),
            Role.by_foreign_id(Role.SYSTEM_USER)
        ]
        app._public_roles = [r.id for r in roles if r is not None]
    return app._public_roles
Exemple #4
0
def get_public_roles():
    app = current_app._get_current_object()
    if not hasattr(app, '_public_roles') or not len(app._public_roles):
        roles = [
            Role.by_foreign_id(Role.SYSTEM_GUEST),
            Role.by_foreign_id(Role.SYSTEM_USER)
        ]
        app._public_roles = [r.id for r in roles if r is not None]
    return app._public_roles
Exemple #5
0
    def load_fixtures(self):
        self.admin = self.create_user(foreign_id='admin', is_admin=True)
        self.private_coll = self.create_collection(foreign_id='test_private',
                                                   label="Private Collection",
                                                   category='grey',
                                                   casefile=False,
                                                   creator=self.admin)
        self._banana = Entity.create(
            {
                'schema': 'Person',
                'properties': {
                    'name': ['Banana'],
                }
            }, self.private_coll)
        user = Role.by_foreign_id(Role.SYSTEM_USER)
        Permission.grant(self.private_coll, user, True, False)
        self.public_coll = self.create_collection(foreign_id='test_public',
                                                  label="Public Collection",
                                                  category='news',
                                                  casefile=False,
                                                  creator=self.admin)
        self._kwazulu = Entity.create(
            {
                'schema': 'Company',
                'properties': {
                    'name': ['KwaZulu'],
                    'alias': ['kwazulu']
                }
            }, self.public_coll)
        visitor = Role.by_foreign_id(Role.SYSTEM_GUEST)
        Permission.grant(self.public_coll, visitor, True, False)
        db.session.commit()

        drop_aggregator(self.public_coll)
        stage = get_stage(self.public_coll, OP_PROCESS)
        process_collection(stage, self.public_coll, ingest=False, sync=True)

        aggregator = get_aggregator(self.private_coll)
        aggregator.delete()
        stage = get_stage(self.private_coll, OP_PROCESS)
        for sample in read_entities(self.get_fixture_path('samples.ijson')):
            aggregator.put(sample, fragment='sample')
            index_aggregate(stage,
                            self.private_coll,
                            entity_id=sample.id,
                            sync=True)
        aggregator.close()
        process_collection(stage, self.private_coll, ingest=False, sync=True)
Exemple #6
0
def publish(foreign_id):
    """Make a collection visible to all users."""
    collection = get_collection(foreign_id)
    role = Role.by_foreign_id(Role.SYSTEM_GUEST)
    editor = Role.load_cli_user()
    update_permission(role, collection, True, False, editor_id=editor.id)
    update_collection(collection)
Exemple #7
0
def bulk_load(config):
    """Bulk load entities from a CSV file or SQL database.

    This is done by mapping the rows in the source data to entities and links
    which can be understood by the entity index.
    """
    for foreign_id, data in config.items():
        collection = Collection.by_foreign_id(foreign_id)
        if collection is None:
            collection = Collection.create({
                'foreign_id': foreign_id,
                'managed': True,
                'label': data.get('label') or foreign_id,
                'summary': data.get('summary'),
                'category': data.get('category'),
            })

        for role_fk in dict_list(data, 'roles', 'role'):
            role = Role.by_foreign_id(role_fk)
            if role is not None:
                Permission.grant(collection, role, True, False)
            else:
                log.warning("Could not find role: %s", role_fk)

        db.session.commit()
        update_collection(collection)

        for query in dict_list(data, 'queries', 'query'):
            load_query(collection, query)
Exemple #8
0
def handle_oauth(provider, oauth_token):
    from aleph.model import Role

    token = provider.parse_id_token(oauth_token)
    if token is None:
        return None
    name = token.get("name", token.get("given_name"))
    email = token.get("email", token.get("upn"))
    role_id = "%s:%s" % (settings.OAUTH_HANDLER, token.get("sub", email))
    role = Role.by_foreign_id(role_id)
    if settings.OAUTH_MIGRATE_SUB and role is None:
        role = Role.by_email(email)
        if role is not None:
            role.foreign_id = role_id
            role.update({"name": name})
    if role is None:
        role = Role.load_or_create(role_id, Role.USER, name, email=email)
    if not role.is_actor:
        return None
    role.clear_roles()

    for group in _get_groups(provider, oauth_token, token):
        if group == settings.OAUTH_ADMIN_GROUP:
            role.is_admin = True
            continue
        foreign_id = "group:%s" % group
        group_role = Role.load_or_create(foreign_id, Role.GROUP, group)
        role.add_role(group_role)
        log.debug("User %r is member of %r", role, group_role)
    return role
Exemple #9
0
def publish(foreign_id):
    """Make a collection visible to all users."""
    collection = Collection.by_foreign_id(foreign_id)
    if collection is None:
        raise ValueError("No such collection: %r" % foreign_id)
    role = Role.by_foreign_id(Role.SYSTEM_GUEST)
    update_permission(role, collection, True, False)
    db.session.commit()
Exemple #10
0
def publish(foreign_id):
    """Make a collection visible to all users."""
    collection = Collection.by_foreign_id(foreign_id)
    if collection is None:
        raise ValueError("No such collection: %r" % foreign_id)
    role = Role.by_foreign_id(Role.SYSTEM_GUEST)
    editor = Role.load_cli_user()
    update_permission(role, collection, True, False, editor_id=editor.id)
    update_collection(collection)
Exemple #11
0
 def grant_foreign(cls, resource, foreign_id, read, write):
     from aleph.model import Source, Collection, Role
     role = Role.by_foreign_id(foreign_id)
     if role is None:
         return
     if isinstance(resource, Source):
         cls.grant_resource(cls.SOURCE, resource.id, role, read, write)
     if isinstance(resource, Collection):
         cls.grant_resource(cls.COLLECTION, resource.id, role, read, write)
Exemple #12
0
 def grant_foreign(cls, resource, foreign_id, read, write):
     from aleph.model import Source, Collection, Role
     role = Role.by_foreign_id(foreign_id)
     if role is None:
         return
     if isinstance(resource, Source):
         cls.grant_resource(cls.SOURCE, resource.id, role, read, write)
     if isinstance(resource, Collection):
         cls.grant_resource(cls.COLLECTION, resource.id, role, read, write)
Exemple #13
0
 def grant_foreign(cls, resource, foreign_id, read, write):
     from aleph.model import Source, Watchlist, Role
     role = Role.by_foreign_id(foreign_id)
     if role is None:
         return
     if isinstance(resource, Source):
         cls.grant_resource(cls.SOURCE, resource.id, role, read, write)
     if isinstance(resource, Watchlist):
         cls.grant_resource(cls.WATCHLIST, resource.id, role, read, write)
Exemple #14
0
def deleterole(foreign_id):  # noqa
    """Hard-delete a role (user, or group) from the database."""
    role = Role.by_foreign_id(foreign_id, deleted=True)
    if role is None:
        raise click.BadParameter("No such role: %r" % foreign_id)
    delete_role(role)
Exemple #15
0
 def grant_publish(self, collection):
     visitor = Role.by_foreign_id(Role.SYSTEM_GUEST)
     self.grant(collection, visitor, True, False)
Exemple #16
0
    def load_fixtures(self):
        self.admin = self.create_user(foreign_id="admin", is_admin=True)
        self.private_coll = self.create_collection(
            foreign_id="test_private",
            label="Private Collection",
            category="grey",
            creator=self.admin,
        )
        self._banana = self.create_entity(
            {
                "schema": "Person",
                "properties": {
                    "name": ["Banana"],
                    "birthDate": "1970-08-21"
                },
            },
            self.private_coll,
        )
        self._banana2 = self.create_entity(
            {
                "schema": "Person",
                "properties": {
                    "name": ["Banana"],
                    "birthDate": "1970-03-21"
                },
            },
            self.private_coll,
        )
        self._banana3 = self.create_entity(
            {
                "schema": "Person",
                "properties": {
                    "name": ["Banana ba Nana"],
                    "birthDate": "1969-05-21",
                    "deathDate": "1972-04-23",
                },
            },
            self.private_coll,
        )
        user = Role.by_foreign_id(Role.SYSTEM_USER)
        Permission.grant(self.private_coll, user, True, False)
        self.public_coll = self.create_collection(
            foreign_id="test_public",
            label="Public Collection",
            category="news",
            creator=self.admin,
        )
        self._kwazulu = self.create_entity(
            {
                "schema": "Company",
                "properties": {
                    "name": ["KwaZulu"],
                    "alias": ["kwazulu"]
                },
            },
            self.public_coll,
        )
        visitor = Role.by_foreign_id(Role.SYSTEM_GUEST)
        Permission.grant(self.public_coll, visitor, True, False)
        db.session.commit()

        aggregator = get_aggregator(self.public_coll)
        aggregator.delete()
        aggregator.close()
        reindex_collection(self.public_coll, sync=True)

        aggregator = get_aggregator(self.private_coll)
        aggregator.delete()
        for sample in read_entities(self.get_fixture_path("samples.ijson")):
            aggregator.put(sample, fragment="sample")
        aggregator.close()
        reindex_collection(self.private_coll, sync=True)
Exemple #17
0
 def grant_publish(self, collection):
     visitor = Role.by_foreign_id(Role.SYSTEM_GUEST)
     self.grant(collection, visitor, True, False)