Ejemplo n.º 1
0
    def seed(self, project):
        _, Session = project_engine(project)

        try:
            session = Session()

            if not session.query(Role).filter_by(name="admin").first():
                session.add(
                    Role(
                        name="admin",
                        description="Meltano Admin",
                        permissions=[
                            RolePermissions(type="view:design", context="*"),
                            RolePermissions(type="view:reports", context="*"),
                            RolePermissions(type="modify:acl", context="*"),
                        ],
                    )
                )

            if not session.query(Role).filter_by(name="regular").first():
                session.merge(Role(name="regular", description="Meltano User"))

            # add the universal permissions to Admin
            admin = session.query(Role).filter_by(name="admin").one()
            try:
                session.query(RolePermissions).filter_by(
                    role=admin, type="*", context="*"
                ).one()
            except sqlalchemy.orm.exc.NoResultFound:
                admin.permissions.append(RolePermissions(type="*", context="*"))

            session.commit()
        finally:
            session.close()
Ejemplo n.º 2
0
    def post(self):
        role, permission_type, context = self._parse_request()

        try:
            role = (Role.query.options(
                joinedload("permissions")).filter_by(name=role["name"]).one())
            perm = RolePermissions.query.filter_by(role=role,
                                                   type=permission_type,
                                                   context=context).first()

            if not perm:
                perm = RolePermissions(role=role,
                                       type=permission_type,
                                       context=context)
                role.permissions.append(perm)

            db.session.commit()
        except Exception as err:
            return str(err), 400

        return role, 200