misp-modules/website/app/db_class/db.py

120 lines
3.8 KiB
Python
Raw Normal View History

import json
2024-02-29 10:15:16 +01:00
from .. import db, login_manager
from flask_login import UserMixin, AnonymousUserMixin
2024-02-07 14:39:19 +01:00
class Module(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String, index=True, unique=True)
description = db.Column(db.String)
is_active = db.Column(db.Boolean, default=True)
request_on_query = db.Column(db.Boolean, default=False)
2024-02-12 15:33:47 +01:00
input_attr = db.Column(db.String)
2024-02-07 14:39:19 +01:00
def to_json(self):
json_dict = {
"id": self.id,
"name": self.name,
"description": self.description,
"is_active": self.is_active,
2024-02-12 15:33:47 +01:00
"request_on_query": self.request_on_query,
"input_attr": self.input_attr
2024-02-07 14:39:19 +01:00
}
return json_dict
class Session_db(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
uuid = db.Column(db.String(36), index=True, unique=True)
modules_list = db.Column(db.String)
2024-02-07 14:39:19 +01:00
query_enter = db.Column(db.String)
input_query = db.Column(db.String)
config_module=db.Column(db.String)
result=db.Column(db.String)
nb_errors = db.Column(db.Integer, index=True)
2024-02-09 11:13:14 +01:00
query_date = db.Column(db.DateTime, index=True)
2024-02-07 14:39:19 +01:00
def to_json(self):
json_dict = {
"id": self.id,
"uuid": self.uuid,
"modules": json.loads(self.modules_list),
"query_enter": json.loads(self.query_enter),
"input_query": self.input_query,
"config_module": json.loads(self.config_module),
"result": json.loads(self.result),
"nb_errors": self.nb_errors,
2024-02-19 10:27:48 +01:00
"query_date": self.query_date.strftime('%Y-%m-%d %H:%M')
}
return json_dict
def history_json(self):
json_dict = {
"uuid": self.uuid,
"modules": json.loads(self.modules_list),
"query": json.loads(self.query_enter),
"input": self.input_query,
2024-02-19 10:27:48 +01:00
"query_date": self.query_date.strftime('%Y-%m-%d %H:%M')
}
return json_dict
2024-02-07 14:39:19 +01:00
class History(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
session_id = db.Column(db.Integer, index=True)
class History_Tree(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
session_uuid = db.Column(db.String(36), index=True)
tree = db.Column(db.String)
2024-02-07 14:39:19 +01:00
class Config(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String, index=True, unique=True)
class Module_Config(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
module_id = db.Column(db.Integer, index=True)
config_id = db.Column(db.Integer, index=True)
value = db.Column(db.String, index=True)
2024-02-29 10:15:16 +01:00
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
first_name = db.Column(db.String(64), index=True)
last_name = db.Column(db.String(64), index=True)
email = db.Column(db.String(64), unique=True, index=True)
def to_json(self):
return {
"id": self.id,
"first_name": self.first_name,
"last_name": self.last_name,
"email": self.email
}
class ExternalTools(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(64), index=True)
url = db.Column(db.String)
is_active = db.Column(db.Boolean)
def to_json(self):
return {
"id": self.id,
"url": self.url,
"name": self.name,
"is_active": self.is_active
}
2024-02-29 10:15:16 +01:00
class AnonymousUser(AnonymousUserMixin):
def is_admin(self):
return False
def read_only(self):
return True
login_manager.anonymous_user = AnonymousUser
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))