2023-06-08 10:04:07 +00:00
|
|
|
import os
|
2018-04-06 03:27:52 +00:00
|
|
|
import uuid
|
2023-10-18 07:50:38 +00:00
|
|
|
from datetime import timedelta
|
2023-09-21 06:02:09 +00:00
|
|
|
from importlib import import_module
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.conf import settings
|
2024-01-31 09:17:34 +00:00
|
|
|
from django.core.cache import caches
|
2018-04-06 03:27:52 +00:00
|
|
|
from django.db import models
|
2019-03-22 07:17:22 +00:00
|
|
|
from django.db.models import Q
|
2019-02-28 09:58:53 +00:00
|
|
|
from django.utils import timezone
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext, gettext_lazy as _
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
from common.db.encoder import ModelJSONFieldEncoder
|
2024-01-31 09:17:34 +00:00
|
|
|
from common.sessions.cache import user_session_manager
|
2023-10-10 09:39:41 +00:00
|
|
|
from common.utils import lazyproperty, i18n_trans
|
2023-02-17 11:58:43 +00:00
|
|
|
from ops.models import JobExecution
|
2021-08-04 03:28:51 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin, Organization
|
2019-10-29 04:27:05 +00:00
|
|
|
from orgs.utils import current_org
|
2023-06-08 10:04:07 +00:00
|
|
|
from terminal.models import default_storage
|
2022-11-10 06:44:23 +00:00
|
|
|
from .const import (
|
2022-11-11 07:11:10 +00:00
|
|
|
OperateChoices,
|
|
|
|
ActionChoices,
|
2023-02-07 00:52:48 +00:00
|
|
|
ActivityChoices,
|
2022-11-11 07:11:10 +00:00
|
|
|
LoginTypeChoices,
|
|
|
|
MFAChoices,
|
|
|
|
LoginStatusChoices,
|
2022-11-10 06:44:23 +00:00
|
|
|
)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2022-11-11 07:11:10 +00:00
|
|
|
"FTPLog",
|
|
|
|
"OperateLog",
|
2023-02-07 00:52:48 +00:00
|
|
|
"ActivityLog",
|
2022-11-11 07:11:10 +00:00
|
|
|
"PasswordChangeLog",
|
|
|
|
"UserLoginLog",
|
2023-09-15 09:15:15 +00:00
|
|
|
"JobLog",
|
|
|
|
"UserSession"
|
2018-09-03 03:24:25 +00:00
|
|
|
]
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2018-07-25 07:05:28 +00:00
|
|
|
|
2023-02-17 11:58:43 +00:00
|
|
|
class JobLog(JobExecution):
|
|
|
|
@property
|
|
|
|
def creator_name(self):
|
|
|
|
return self.creator.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
verbose_name = _("Job audit log")
|
|
|
|
|
|
|
|
|
2018-07-25 07:05:28 +00:00
|
|
|
class FTPLog(OrgModelMixin):
|
2023-06-08 10:04:07 +00:00
|
|
|
upload_to = 'FTP_FILES'
|
|
|
|
|
2018-04-06 03:27:52 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2022-11-11 07:11:10 +00:00
|
|
|
user = models.CharField(max_length=128, verbose_name=_("User"))
|
|
|
|
remote_addr = models.CharField(
|
|
|
|
max_length=128, verbose_name=_("Remote addr"), blank=True, null=True
|
|
|
|
)
|
2018-04-06 03:27:52 +00:00
|
|
|
asset = models.CharField(max_length=1024, verbose_name=_("Asset"))
|
2022-12-20 10:12:35 +00:00
|
|
|
account = models.CharField(max_length=128, verbose_name=_("Account"))
|
2022-11-11 07:11:10 +00:00
|
|
|
operate = models.CharField(
|
|
|
|
max_length=16, verbose_name=_("Operate"), choices=OperateChoices.choices
|
|
|
|
)
|
2018-04-06 03:27:52 +00:00
|
|
|
filename = models.CharField(max_length=1024, verbose_name=_("Filename"))
|
|
|
|
is_success = models.BooleanField(default=True, verbose_name=_("Success"))
|
2023-09-06 05:35:13 +00:00
|
|
|
date_start = models.DateTimeField(auto_now_add=True, verbose_name=_("Date start"), db_index=True)
|
2024-06-24 11:15:37 +00:00
|
|
|
has_file = models.BooleanField(default=False, verbose_name=_("Can Download"))
|
2023-06-08 10:04:07 +00:00
|
|
|
session = models.CharField(max_length=36, verbose_name=_("Session"), default=uuid.uuid4)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("File transfer log")
|
|
|
|
|
2023-06-08 10:04:07 +00:00
|
|
|
@property
|
|
|
|
def filepath(self):
|
|
|
|
return os.path.join(self.upload_to, self.date_start.strftime('%Y-%m-%d'), str(self.id))
|
|
|
|
|
|
|
|
def save_file_to_storage(self, file):
|
|
|
|
try:
|
|
|
|
name = default_storage.save(self.filepath, file)
|
|
|
|
except OSError as e:
|
|
|
|
return None, e
|
|
|
|
|
|
|
|
if settings.SERVER_REPLAY_STORAGE:
|
|
|
|
from .tasks import upload_ftp_file_to_external_storage
|
|
|
|
upload_ftp_file_to_external_storage.delay(str(self.id), file.name)
|
|
|
|
return name, None
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
class OperateLog(OrgModelMixin):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2022-11-11 07:11:10 +00:00
|
|
|
user = models.CharField(max_length=128, verbose_name=_("User"))
|
|
|
|
action = models.CharField(
|
|
|
|
max_length=16, choices=ActionChoices.choices, verbose_name=_("Action")
|
|
|
|
)
|
2018-09-03 03:24:25 +00:00
|
|
|
resource_type = models.CharField(max_length=64, verbose_name=_("Resource Type"))
|
|
|
|
resource = models.CharField(max_length=128, verbose_name=_("Resource"))
|
2023-01-16 11:02:09 +00:00
|
|
|
resource_id = models.CharField(
|
2023-02-01 07:06:10 +00:00
|
|
|
max_length=128, blank=True, default='', db_index=True,
|
2023-01-16 11:02:09 +00:00
|
|
|
verbose_name=_("Resource")
|
|
|
|
)
|
2019-12-05 07:09:25 +00:00
|
|
|
remote_addr = models.CharField(max_length=128, verbose_name=_("Remote addr"), blank=True, null=True)
|
2020-08-07 02:44:27 +00:00
|
|
|
datetime = models.DateTimeField(auto_now=True, verbose_name=_('Datetime'), db_index=True)
|
2023-01-17 04:43:07 +00:00
|
|
|
diff = models.JSONField(default=dict, encoder=ModelJSONFieldEncoder, null=True)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "<{}> {} <{}>".format(self.user, self.action, self.resource)
|
|
|
|
|
2022-01-18 04:25:28 +00:00
|
|
|
@lazyproperty
|
|
|
|
def resource_type_display(self):
|
|
|
|
return gettext(self.resource_type)
|
|
|
|
|
2021-08-04 03:28:51 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if current_org.is_root() and not self.org_id:
|
|
|
|
self.org_id = Organization.ROOT_ID
|
|
|
|
return super(OperateLog, self).save(*args, **kwargs)
|
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, d):
|
|
|
|
self = cls()
|
|
|
|
for k, v in d.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
return self
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_multi_dict(cls, l):
|
|
|
|
operate_logs = []
|
|
|
|
for d in l:
|
|
|
|
operate_log = cls.from_dict(d)
|
|
|
|
operate_logs.append(operate_log)
|
|
|
|
return operate_logs
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Operate log")
|
2023-01-16 11:02:09 +00:00
|
|
|
ordering = ('-datetime',)
|
2022-02-17 12:13:31 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2023-02-07 00:52:48 +00:00
|
|
|
class ActivityLog(OrgModelMixin):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
type = models.CharField(
|
|
|
|
choices=ActivityChoices.choices, max_length=2,
|
|
|
|
null=True, default=None, verbose_name=_("Activity type"),
|
|
|
|
)
|
|
|
|
resource_id = models.CharField(
|
|
|
|
max_length=36, blank=True, default='',
|
|
|
|
db_index=True, verbose_name=_("Resource")
|
|
|
|
)
|
|
|
|
datetime = models.DateTimeField(
|
|
|
|
auto_now=True, verbose_name=_('Datetime'), db_index=True
|
|
|
|
)
|
2023-02-17 09:14:53 +00:00
|
|
|
# 日志的描述信息
|
2023-02-07 00:52:48 +00:00
|
|
|
detail = models.TextField(default='', blank=True, verbose_name=_('Detail'))
|
2023-02-17 09:14:53 +00:00
|
|
|
# 详情ID, 结合 type 来使用, (实例ID 和 CeleryTaskID)
|
2023-02-08 03:32:05 +00:00
|
|
|
detail_id = models.CharField(
|
|
|
|
max_length=36, default=None, null=True, verbose_name=_('Detail ID')
|
2023-02-07 00:52:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Activity log")
|
|
|
|
ordering = ('-datetime',)
|
|
|
|
|
2023-10-10 09:39:41 +00:00
|
|
|
def __str__(self):
|
|
|
|
detail = i18n_trans(self.detail)
|
|
|
|
return "{} {}".format(detail, self.resource_id)
|
|
|
|
|
2023-02-07 00:52:48 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if current_org.is_root() and not self.org_id:
|
|
|
|
self.org_id = Organization.ROOT_ID
|
|
|
|
return super(ActivityLog, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
class PasswordChangeLog(models.Model):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2022-11-11 07:11:10 +00:00
|
|
|
user = models.CharField(max_length=128, verbose_name=_("User"))
|
2018-09-03 03:24:25 +00:00
|
|
|
change_by = models.CharField(max_length=128, verbose_name=_("Change by"))
|
2022-11-11 07:11:10 +00:00
|
|
|
remote_addr = models.CharField(
|
|
|
|
max_length=128, verbose_name=_("Remote addr"), blank=True, null=True
|
|
|
|
)
|
|
|
|
datetime = models.DateTimeField(auto_now=True, verbose_name=_("Datetime"))
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{} change {}'s password".format(self.change_by, self.user)
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
class Meta:
|
2022-11-11 07:11:10 +00:00
|
|
|
verbose_name = _("Password change log")
|
2022-02-17 12:13:31 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2019-02-28 09:58:53 +00:00
|
|
|
class UserLoginLog(models.Model):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2022-11-11 07:11:10 +00:00
|
|
|
username = models.CharField(max_length=128, verbose_name=_("Username"))
|
|
|
|
type = models.CharField(
|
|
|
|
choices=LoginTypeChoices.choices, max_length=2, verbose_name=_("Login type")
|
2019-02-28 09:58:53 +00:00
|
|
|
)
|
2023-05-29 11:45:55 +00:00
|
|
|
ip = models.GenericIPAddressField(verbose_name=_("Login IP"))
|
2022-11-11 07:11:10 +00:00
|
|
|
city = models.CharField(
|
|
|
|
max_length=254, blank=True, null=True, verbose_name=_("Login city")
|
2019-02-28 09:58:53 +00:00
|
|
|
)
|
2022-11-11 07:11:10 +00:00
|
|
|
user_agent = models.CharField(
|
|
|
|
max_length=254, blank=True, null=True, verbose_name=_("User agent")
|
|
|
|
)
|
|
|
|
mfa = models.SmallIntegerField(
|
|
|
|
default=MFAChoices.unknown, choices=MFAChoices.choices, verbose_name=_("MFA")
|
|
|
|
)
|
|
|
|
reason = models.CharField(
|
|
|
|
default="", max_length=128, blank=True, verbose_name=_("Reason")
|
|
|
|
)
|
2022-11-10 06:44:23 +00:00
|
|
|
status = models.BooleanField(
|
2022-11-11 07:11:10 +00:00
|
|
|
default=LoginStatusChoices.success,
|
|
|
|
choices=LoginStatusChoices.choices,
|
|
|
|
verbose_name=_("Status"),
|
|
|
|
)
|
2024-02-22 09:46:12 +00:00
|
|
|
datetime = models.DateTimeField(default=timezone.now, verbose_name=_("Login Date"), db_index=True)
|
2022-11-11 07:11:10 +00:00
|
|
|
backend = models.CharField(
|
2024-05-09 08:47:57 +00:00
|
|
|
max_length=32, default="", verbose_name=_("Auth backend")
|
2019-02-28 09:58:53 +00:00
|
|
|
)
|
|
|
|
|
2023-01-17 04:43:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
return '%s(%s)' % (self.username, self.city)
|
|
|
|
|
2022-01-18 04:25:28 +00:00
|
|
|
@property
|
|
|
|
def backend_display(self):
|
|
|
|
return gettext(self.backend)
|
|
|
|
|
2019-03-22 07:17:22 +00:00
|
|
|
@classmethod
|
2019-10-22 09:40:55 +00:00
|
|
|
def get_login_logs(cls, date_from=None, date_to=None, user=None, keyword=None):
|
2019-03-22 07:17:22 +00:00
|
|
|
login_logs = cls.objects.all()
|
2019-10-22 08:17:42 +00:00
|
|
|
if date_from and date_to:
|
2022-11-11 07:11:10 +00:00
|
|
|
date_from = "{} {}".format(date_from, "00:00:00")
|
|
|
|
date_to = "{} {}".format(date_to, "23:59:59")
|
2019-03-22 07:17:22 +00:00
|
|
|
login_logs = login_logs.filter(
|
2019-10-22 08:17:42 +00:00
|
|
|
datetime__gte=date_from, datetime__lte=date_to
|
2019-03-22 07:17:22 +00:00
|
|
|
)
|
|
|
|
if user:
|
|
|
|
login_logs = login_logs.filter(username=user)
|
|
|
|
if keyword:
|
|
|
|
login_logs = login_logs.filter(
|
2022-11-11 07:11:10 +00:00
|
|
|
Q(ip__contains=keyword)
|
|
|
|
| Q(city__contains=keyword)
|
|
|
|
| Q(username__contains=keyword)
|
2019-03-22 07:17:22 +00:00
|
|
|
)
|
2019-10-29 04:27:05 +00:00
|
|
|
if not current_org.is_root():
|
2022-11-11 07:11:10 +00:00
|
|
|
username_list = current_org.get_members().values_list("username", flat=True)
|
2019-10-29 04:27:05 +00:00
|
|
|
login_logs = login_logs.filter(username__in=username_list)
|
2019-03-22 07:17:22 +00:00
|
|
|
return login_logs
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
@property
|
|
|
|
def reason_display(self):
|
|
|
|
from authentication.errors import reason_choices, old_reason_choices
|
2022-11-11 07:11:10 +00:00
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
reason = reason_choices.get(self.reason)
|
|
|
|
if reason:
|
|
|
|
return reason
|
|
|
|
reason = old_reason_choices.get(self.reason, self.reason)
|
|
|
|
return reason
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
class Meta:
|
2022-11-11 07:11:10 +00:00
|
|
|
ordering = ["-datetime", "username"]
|
|
|
|
verbose_name = _("User login log")
|
2023-09-15 09:15:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserSession(models.Model):
|
2024-05-31 03:05:35 +00:00
|
|
|
_OPERATE_LOG_ACTION = {'delete': ActionChoices.finished}
|
|
|
|
|
2023-09-15 09:15:15 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
ip = models.GenericIPAddressField(verbose_name=_("Login IP"))
|
|
|
|
key = models.CharField(max_length=128, verbose_name=_("Session key"))
|
|
|
|
city = models.CharField(max_length=254, blank=True, null=True, verbose_name=_("Login city"))
|
|
|
|
user_agent = models.CharField(max_length=254, blank=True, null=True, verbose_name=_("User agent"))
|
|
|
|
type = models.CharField(choices=LoginTypeChoices.choices, max_length=2, verbose_name=_("Login type"))
|
2024-05-09 08:47:57 +00:00
|
|
|
backend = models.CharField(max_length=32, default="", verbose_name=_("Auth backend"))
|
2024-05-31 06:37:19 +00:00
|
|
|
date_created = models.DateTimeField(null=True, blank=True, verbose_name=_('Login date'))
|
2023-09-15 09:15:15 +00:00
|
|
|
user = models.ForeignKey(
|
|
|
|
'users.User', verbose_name=_('User'), related_name='sessions', on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s(%s)' % (self.user, self.ip)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def backend_display(self):
|
|
|
|
return gettext(self.backend)
|
|
|
|
|
2023-11-08 02:26:08 +00:00
|
|
|
@property
|
|
|
|
def is_active(self):
|
2024-01-31 09:17:34 +00:00
|
|
|
return user_session_manager.check_active(self.key)
|
2023-11-08 02:26:08 +00:00
|
|
|
|
2023-10-18 07:50:38 +00:00
|
|
|
@property
|
|
|
|
def date_expired(self):
|
|
|
|
session_store_cls = import_module(settings.SESSION_ENGINE).SessionStore
|
|
|
|
session_store = session_store_cls(session_key=self.key)
|
|
|
|
cache_key = session_store.cache_key
|
|
|
|
ttl = caches[settings.SESSION_CACHE_ALIAS].ttl(cache_key)
|
|
|
|
return timezone.now() + timedelta(seconds=ttl)
|
|
|
|
|
2023-09-15 09:15:15 +00:00
|
|
|
@classmethod
|
|
|
|
def clear_expired_sessions(cls):
|
2024-03-12 11:33:26 +00:00
|
|
|
keys = user_session_manager.get_keys()
|
2023-10-18 07:50:38 +00:00
|
|
|
cls.objects.exclude(key__in=keys).delete()
|
2023-09-15 09:15:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['-date_created']
|
|
|
|
verbose_name = _('User session')
|
|
|
|
permissions = [
|
2023-12-08 08:06:17 +00:00
|
|
|
('offline_usersession', _('Offline user session')),
|
2023-09-15 09:15:15 +00:00
|
|
|
]
|