Exemplo n.º 1
0
class Permission(db.Model):
    __tablename__ = 'permission'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), nullable=False)
    resource_id = db.Column(db.Integer, db.ForeignKey('resource.id'))

    def __init__(self, name):
        self.name = name
Exemplo n.º 2
0
class ScheduledWorkflow(db.Model):
    """A SqlAlchemy table representing a workflow scheduled for execution

    Attributes:
        id (int): The primary key
        workflow_id (UUID): The id of the workflow scheduled for execution
    """
    __tablename__ = 'scheduled_workflow'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    workflow_id = db.Column(UUIDType(binary=False), nullable=False)
    task_id = db.Column(db.Integer, db.ForeignKey('scheduled_task.id'))
Exemplo n.º 3
0
class MessageHistory(db.Model):
    """A Flask-SqlAlchemy table which contains entries related to the history of a message

    Attributes:
        id (int): The primary key
        action (MessageAction): The action taken
        timestamp (datetime): The timestamp of the action
        user_id (int): The ID of the user who took the action
        username (str): The username of the user who took the action

    Args:
        user (User): The user who took the action
        action (MessageAction): The action taken
    """
    __tablename__ = 'message_history'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    action = db.Column(db.Enum(MessageAction, name='message_action'))
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    user_id = db.Column(db.Integer)
    username = db.Column(db.String)
    message_id = db.Column(db.Integer, db.ForeignKey('message.id'))

    def __init__(self, user, action):
        self.action = action
        self.user_id = user.id
        self.username = user.username

    def as_json(self):
        """gets a JSON representation of the message history entry

        Returns:
            dict: The JSON representation of the message history entry
        """
        return {
            'action': self.action.name,
            'user_id': self.user_id,
            'username': self.username,
            'id': self.id,
            'timestamp': utc_as_rfc_datetime(self.timestamp)
        }
Exemplo n.º 4
0
class Resource(db.Model, TrackModificationsMixIn):
    __tablename__ = 'resource'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), nullable=False)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    permissions = db.relationship('Permission', backref=db.backref('resource'), cascade='all, delete-orphan')

    def __init__(self, name, permissions):
        """Initializes a new Resource object, which is a type of Resource that a Role may have access to.

        Args:
            name (str): Name of the Resource object.
            permissions (list[str]): List of permissions ("create", "read", "update", "delete", "execute")
                for the Resource
        """
        self.name = name
        self.set_permissions(permissions)

    def set_permissions(self, new_permissions):
        """Adds the given list of permissions to the Resource object.

        Args:
            new_permissions (list|set[str]): A list of permission names with which the Resource will be associated.
                These permissions must be in the set ["create", "read", "update", "delete", "execute"].
        """
        self.permissions = []
        new_permission_names = set(new_permissions)
        self.permissions.extend([Permission(permission) for permission in new_permission_names])

    def as_json(self, with_roles=False):
        """Returns the dictionary representation of the Resource object.

        Args:
            with_roles (bool, optional): Boolean to determine whether or not to include Role objects associated with the
                Resource in the JSON representation. Defaults to False.
        """
        out = {'id': self.id, 'name': self.name, 'permissions': [permission.name for permission in self.permissions]}
        if with_roles:
            out["role"] = self.role.name
        return out
Exemplo n.º 5
0
class MessageHistory(db.Model):
    __tablename__ = 'message_history'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    action = db.Column(db.Enum(MessageAction))
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    user_id = db.Column(db.Integer)
    username = db.Column(db.String)
    message_id = db.Column(db.Integer, db.ForeignKey('message.id'))

    def __init__(self, user, action):
        self.action = action
        self.user_id = user.id
        self.username = user.username

    def as_json(self):
        return {
            'action': self.action.name,
            'user_id': self.user_id,
            'username': self.username,
            'id': self.id,
            'timestamp': utc_as_rfc_datetime(self.timestamp)
        }
Exemplo n.º 6
0
import logging
from datetime import datetime

from passlib.hash import pbkdf2_sha512
from sqlalchemy.ext.hybrid import hybrid_property

from walkoff.extensions import db
from walkoff.helpers import utc_as_rfc_datetime
from walkoff.serverdb.mixins import TrackModificationsMixIn
from walkoff.serverdb.role import Role

logger = logging.getLogger(__name__)

user_roles_association = db.Table(
    'user_roles_association',
    db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')))


class User(db.Model, TrackModificationsMixIn):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    roles = db.relationship('Role',
                            secondary=user_roles_association,
                            backref=db.backref('users', lazy='dynamic'))
    username = db.Column(db.String(80), unique=True, nullable=False)
    _password = db.Column('password', db.String(255), nullable=False)
    active = db.Column(db.Boolean, default=True)
    last_login_at = db.Column(db.DateTime)
    current_login_at = db.Column(db.DateTime)
    last_login_ip = db.Column(db.String(45))
Exemplo n.º 7
0
import json
from datetime import datetime

from sqlalchemy_utils import UUIDType

from walkoff.extensions import db
from walkoff.helpers import utc_as_rfc_datetime
from walkoff.messaging import MessageAction

user_messages_association = db.Table(
    'user_messages', db.Column('user_id', db.Integer,
                               db.ForeignKey('user.id')),
    db.Column('message_id', db.Integer, db.ForeignKey('message.id')))


class Message(db.Model):
    __tablename__ = 'message'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    subject = db.Column(db.String())
    body = db.Column(db.String(), nullable=False)
    users = db.relationship('User',
                            secondary=user_messages_association,
                            backref=db.backref('messages', lazy='dynamic'))
    workflow_execution_id = db.Column(UUIDType(binary=False), nullable=False)
    requires_reauth = db.Column(db.Boolean, default=False)
    requires_response = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    history = db.relationship('MessageHistory', backref='message', lazy=True)

    def __init__(self,
Exemplo n.º 8
0
class ScheduledWorkflow(db.Model):
    __tablename__ = 'scheduled_workflow'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    workflow_id = db.Column(UUIDType(), nullable=False)
    task_id = db.Column(db.Integer, db.ForeignKey('scheduled_task.id'))