2017-11-29 11:27:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-04-13 10:20:29 +00:00
|
|
|
from datetime import datetime
|
2017-11-29 11:27:04 +00:00
|
|
|
import uuid
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2021-12-24 09:23:09 +00:00
|
|
|
from common.utils.common import lazyproperty
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2017-11-29 11:27:04 +00:00
|
|
|
|
2018-07-25 07:13:53 +00:00
|
|
|
|
|
|
|
class AbstractSessionCommand(OrgModelMixin):
|
2020-03-12 08:24:38 +00:00
|
|
|
RISK_LEVEL_ORDINARY = 0
|
|
|
|
RISK_LEVEL_DANGEROUS = 5
|
|
|
|
RISK_LEVEL_CHOICES = (
|
|
|
|
(RISK_LEVEL_ORDINARY, _('Ordinary')),
|
|
|
|
(RISK_LEVEL_DANGEROUS, _('Dangerous')),
|
|
|
|
)
|
2017-11-29 11:27:04 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-12-29 18:29:29 +00:00
|
|
|
user = models.CharField(max_length=64, db_index=True, verbose_name=_("User"))
|
|
|
|
asset = models.CharField(max_length=128, db_index=True, verbose_name=_("Asset"))
|
2022-12-05 05:27:51 +00:00
|
|
|
account = models.CharField(max_length=64, db_index=True, verbose_name=_("Account"))
|
2017-11-29 11:27:04 +00:00
|
|
|
input = models.CharField(max_length=128, db_index=True, verbose_name=_("Input"))
|
2017-12-03 16:21:26 +00:00
|
|
|
output = models.CharField(max_length=1024, blank=True, verbose_name=_("Output"))
|
2017-11-29 11:27:04 +00:00
|
|
|
session = models.CharField(max_length=36, db_index=True, verbose_name=_("Session"))
|
2020-03-12 08:24:38 +00:00
|
|
|
risk_level = models.SmallIntegerField(default=RISK_LEVEL_ORDINARY, choices=RISK_LEVEL_CHOICES, db_index=True, verbose_name=_("Risk level"))
|
2017-11-29 11:27:04 +00:00
|
|
|
timestamp = models.IntegerField(db_index=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2022-04-13 10:20:29 +00:00
|
|
|
@lazyproperty
|
|
|
|
def timestamp_display(self):
|
|
|
|
return datetime.fromtimestamp(self.timestamp)
|
|
|
|
|
2021-12-24 09:23:09 +00:00
|
|
|
@lazyproperty
|
|
|
|
def remote_addr(self):
|
|
|
|
from terminal.models import Session
|
|
|
|
session = Session.objects.filter(id=self.session).first()
|
|
|
|
if session:
|
|
|
|
return session.remote_addr
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
2020-10-27 03:35:31 +00:00
|
|
|
@classmethod
|
|
|
|
def get_risk_level_str(cls, risk_level):
|
|
|
|
risk_mapper = dict(cls.RISK_LEVEL_CHOICES)
|
|
|
|
return risk_mapper.get(risk_level)
|
|
|
|
|
2018-01-21 09:27:27 +00:00
|
|
|
def to_dict(self):
|
|
|
|
d = {}
|
|
|
|
for field in self._meta.fields:
|
|
|
|
d[field.name] = getattr(self, field.name)
|
|
|
|
return d
|
|
|
|
|
2017-11-29 11:27:04 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.input
|