Beispiel #1
0
class Group(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    Group represent link between users and printers, single group has
    M:N users and M:N printers
    """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))

    user = association_proxy("group_user", "user")

    def __init__(self, name=None):
        self.name = name

    def __repr__(self):
        return '<Group %r>' % self.name

    def editable(self, user: User):
        """Returns true if group is editable by given user else false"""
        from octoprint_dashboard.model import GroupUser
        if user.superadmin:
            return True

        role = GroupUser.query.join(User).join(Group).filter(
            User.id == user.id, Group.id == self.id).scalar().role
        if role == 'admin':
            return True

        return False
Beispiel #2
0
class Config(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    There has to be only one record of Config in database otherwise application wouldn't work
    """
    id = db.Column(db.Integer, primary_key=True)
    secret = db.Column(db.String(80))
    client_refresh = db.Column(db.Integer)
    server_refresh = db.Column(db.Integer)
    oauth_client_id = db.Column(db.String)
    oauth_client_secret = db.Column(db.String)
    oauth_redirect_uri = db.Column(db.String)

    def __init__(self, secret, client_refresh, server_refresh, oauth_client_id,
                 oauth_client_secret, oauth_redirect_uri):
        self.secret = secret
        self.client_refresh = client_refresh
        self.server_refresh = server_refresh
        self.oauth_client_id = oauth_client_id
        self.oauth_client_secret = oauth_client_secret
        self.oauth_redirect_uri = oauth_redirect_uri

    def __repr__(self):
        return '<Config %r>' % self.id
Beispiel #3
0
class GroupUser(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    GroupUser represents link between Groups and Users.
    This class is used because every relation has role attribute
    """
    __tablename__ = 'group_user'

    group_id = db.Column(db.Integer, db.ForeignKey('group.id'), primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    role = db.Column(db.String(80), nullable=False, default="user")

    group = db.relationship("Group", backref=db.backref("group_user", lazy="dynamic", cascade='all, delete-orphan'))
    user = db.relationship("User", backref=db.backref("group_user", lazy="dynamic", cascade='all, delete-orphan'))

    def __init__(self, group=None, user=None, role="user"):
        self.group = group
        self.user = user
        self.role = role

    def __repr__(self):
        return '<GroupUser %r>' % self.role
class Printer(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    Printer represent single OctoPrint instance
    """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    apikey = db.Column(db.String(80))
    url = db.Column(db.String(80))
    group = db.relationship(Group,
                            secondary=printer_group,
                            backref=db.backref('printer', lazy='dynamic'),
                            lazy="dynamic")

    states = {}

    def __init__(self, name, apikey, url):
        self.name = name
        self.apikey = apikey
        self.url = url

    def __repr__(self):
        return '<Printer %r>' % self.name

    def __hash__(self):
        return hash(self.id)

    def set_state(self, state):
        """
        Sets state instance variable
        Used by PrinterStatusApi
        """
        self.state = state
        return self
Beispiel #5
0
class Config(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    There has to be only one record of Config in database otherwise application wouldn't work
    """
    OAUTH_CVUT = "oauth_cvut"
    NONE = "none"

    AUTH_CHOICES = (
        (OAUTH_CVUT, "ČVUT - OAuth"),
        (NONE, "None")
    )

    id = db.Column(db.Integer, primary_key=True)
    secret = db.Column(db.String(80))
    auth = db.Column(utils.ChoiceType(choices=AUTH_CHOICES), default=NONE, nullable=False)
    oauth_client_id = db.Column(db.String)
    oauth_client_secret = db.Column(db.String)
    oauth_redirect_uri = db.Column(db.String)

    def __init__(self, secret, auth, oauth_client_id, oauth_client_secret, oauth_redirect_uri):
        self.secret = secret
        self.auth = auth
        self.oauth_client_id = oauth_client_id
        self.oauth_client_secret = oauth_client_secret
        self.oauth_redirect_uri = oauth_redirect_uri

    def __repr__(self):
        return '<Config %r>' % self.id

    def load(self):
        return {
            "SECRET": self.secret,
            "AUTH": self.auth,
            "OAUTH_CLIENT_ID": self.oauth_client_id,
            "OAUTH_CLIENT_SECRET": self.oauth_client_secret,
            "OAUTH_REDIRECT_URI": self.oauth_redirect_uri,
            "OCTO_CONF": True
        }
class User(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    User represents human being, logged into app at least once
    """
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    access_token = db.Column(db.String(80))
    refresh_token = db.Column(db.String(80))
    superadmin = db.Column(db.Boolean, default=False)

    group = association_proxy("group_user", "group")

    def __init__(self,
                 username=None,
                 access_token=None,
                 refresh_token=None,
                 superadmin=False):
        self.username = username
        self.access_token = access_token
        self.refresh_token = refresh_token
        self.superadmin = superadmin

    def __repr__(self):
        return '<User %r>' % self.username

    @staticmethod
    def upsert(username, access_token, refresh_token):
        """
        Creates user with parameters in database or updates access and refresh token of user
        """
        user = User.query.filter_by(username=username).scalar()
        if user is None:
            user = User(username, access_token, refresh_token)
            db.session.add(user)
        else:
            user.access_token = access_token
            user.refresh_token = refresh_token
        db.session.commit()
        return user

    @staticmethod
    def upsert_superadmin(username):
        """
        Makes user superadmin
        """
        user = User.query.filter_by(username=username).scalar()
        if user is None:
            user = User(username, None, None)
            user.superadmin = True
            db.session.add(user)
        else:
            user.superadmin = True
        db.session.commit()

    def get_accessible_printers(self):
        """
        Returns all printers accessible to user with admin role
        """
        from octoprint_dashboard.model import Printer, Group, GroupUser
        if self.superadmin:
            printers = Printer.query.all()
        else:
            printers = Printer.query.join(Printer.group).join(
                Group.group_user).filter(User.id == self.id,
                                         GroupUser.role == "admin").all()

        return printers

    def get_accessible_printers_id(self, printer_ids):
        """
        Returns printers of given ids accessible to user with admin role
        """
        from octoprint_dashboard.model import Printer, Group, GroupUser
        if self.superadmin:
            printers = Printer.query.filter(Printer.id.in_(printer_ids)).all()
        else:
            printers = Printer.query.filter(Printer.id.in_(printer_ids)) \
                .join(Printer.group).join(Group.group_user) \
                .filter(User.id == self.id, GroupUser.role == "admin").all()

        return printers

    def get_accessible_printer_id(self, printer_id):
        """
        Returns printer of given id accessible to user with admin role or None
        """
        from octoprint_dashboard.model import Printer, Group, GroupUser
        if self.superadmin:
            printer = Printer.query.get(printer_id)
        else:
            printer = Printer.query.filter(Printer.id == printer_id) \
                .join(Printer.group).join(Group.group_user) \
                .filter(User.id == self.id, GroupUser.role == "admin").scalar()

        return printer

    def get_printer_id(self, printer_id):
        """
        Returns printer of given id accessible to user or None
        """
        from octoprint_dashboard.model import Printer, Group
        if self.superadmin:
            printer = Printer.query.get(printer_id)
        else:
            printer = Printer.query.filter(Printer.id == printer_id) \
                .join(Printer.group).join(Group.group_user) \
                .filter(User.id == self.id).scalar()

        return printer

    def get_editable_groups(self):
        """
        Returns groups accessible to user with admin role
        """
        from octoprint_dashboard.model import Group, GroupUser

        if self.superadmin:
            groups = Group.query.all()
        else:
            groups = Group.query.join(Group.group_user).join(
                GroupUser.user).filter(User.id == self.id).filter(
                    GroupUser.role == "admin").all()

        return groups

    def get_editable_group_id(self, id):
        """
        Returns groups of given id accessible to user with admin role
        """
        from octoprint_dashboard.model import Group, GroupUser

        if self.superadmin:
            group = Group.query.get(id)
        else:
            group = Group.query.join(Group.group_user).join(
                GroupUser.user).filter(User.id == self.id).filter(
                    GroupUser.role == "admin").filter(Group.id == id).scalar()

        return group

    def get_groups(self):
        """
        Returns groups accessible to user
        """
        from octoprint_dashboard.model import Group, GroupUser

        if self.superadmin:
            groups = Group.query.all()
        else:
            groups = Group.query.join(Group.group_user).join(
                GroupUser.user).filter(User.id == self.id).all()

        return groups
from octoprint_dashboard.app import db
from octoprint_dashboard.model import Group
"""M:N association table"""
printer_group = db.Table(
    'printer_group',
    db.Column('printer_id', db.Integer, db.ForeignKey('printer.id')),
    db.Column('group_id', db.Integer, db.ForeignKey('group.id')))


class Printer(db.Model):
    """
    Instance of this class equals single record of config in database
    Class behaves like repository of Config records

    Printer represent single OctoPrint instance
    """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    apikey = db.Column(db.String(80))
    url = db.Column(db.String(80))
    group = db.relationship(Group,
                            secondary=printer_group,
                            backref=db.backref('printer', lazy='dynamic'),
                            lazy="dynamic")

    states = {}

    def __init__(self, name, apikey, url):
        self.name = name
        self.apikey = apikey
        self.url = url