2018-04-06 03:27:52 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.db import models
|
2019-03-22 07:17:22 +00:00
|
|
|
from django.db.models import Q
|
2022-01-18 04:25:28 +00:00
|
|
|
from django.utils.translation import gettext, ugettext_lazy as _
|
2019-02-28 09:58:53 +00:00
|
|
|
from django.utils import timezone
|
2022-01-18 04:25:28 +00:00
|
|
|
from common.utils import lazyproperty
|
2018-04-06 03:27:52 +00:00
|
|
|
|
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
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'FTPLog', 'OperateLog', 'PasswordChangeLog', 'UserLoginLog',
|
|
|
|
]
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2018-07-25 07:05:28 +00:00
|
|
|
|
|
|
|
class FTPLog(OrgModelMixin):
|
2020-06-24 08:17:58 +00:00
|
|
|
OPERATE_DELETE = 'Delete'
|
|
|
|
OPERATE_UPLOAD = 'Upload'
|
|
|
|
OPERATE_DOWNLOAD = 'Download'
|
|
|
|
OPERATE_RMDIR = 'Rmdir'
|
|
|
|
OPERATE_RENAME = 'Rename'
|
|
|
|
OPERATE_MKDIR = 'Mkdir'
|
|
|
|
OPERATE_SYMLINK = 'Symlink'
|
|
|
|
|
|
|
|
OPERATE_CHOICES = (
|
|
|
|
(OPERATE_DELETE, _('Delete')),
|
|
|
|
(OPERATE_UPLOAD, _('Upload')),
|
|
|
|
(OPERATE_DOWNLOAD, _('Download')),
|
|
|
|
(OPERATE_RMDIR, _('Rmdir')),
|
|
|
|
(OPERATE_RENAME, _('Rename')),
|
|
|
|
(OPERATE_MKDIR, _('Mkdir')),
|
|
|
|
(OPERATE_SYMLINK, _('Symlink'))
|
|
|
|
)
|
|
|
|
|
2018-04-06 03:27:52 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
user = models.CharField(max_length=128, verbose_name=_('User'))
|
2019-12-05 07:09:25 +00:00
|
|
|
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"))
|
|
|
|
system_user = models.CharField(max_length=128, verbose_name=_("System user"))
|
2020-06-24 08:17:58 +00:00
|
|
|
operate = models.CharField(max_length=16, verbose_name=_("Operate"), choices=OPERATE_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"))
|
2020-05-09 03:28:39 +00:00
|
|
|
date_start = models.DateTimeField(auto_now_add=True, verbose_name=_('Date start'))
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("File transfer log")
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
class OperateLog(OrgModelMixin):
|
|
|
|
ACTION_CREATE = 'create'
|
|
|
|
ACTION_UPDATE = 'update'
|
|
|
|
ACTION_DELETE = 'delete'
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
(ACTION_CREATE, _("Create")),
|
|
|
|
(ACTION_UPDATE, _("Update")),
|
|
|
|
(ACTION_DELETE, _("Delete"))
|
|
|
|
)
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
user = models.CharField(max_length=128, verbose_name=_('User'))
|
|
|
|
action = models.CharField(max_length=16, choices=ACTION_CHOICES, verbose_name=_("Action"))
|
|
|
|
resource_type = models.CharField(max_length=64, verbose_name=_("Resource Type"))
|
|
|
|
resource = models.CharField(max_length=128, 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)
|
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-02-17 12:13:31 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Operate log")
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
class PasswordChangeLog(models.Model):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
user = models.CharField(max_length=128, verbose_name=_('User'))
|
|
|
|
change_by = models.CharField(max_length=128, verbose_name=_("Change by"))
|
2019-12-05 07:09:25 +00:00
|
|
|
remote_addr = models.CharField(max_length=128, verbose_name=_("Remote addr"), blank=True, null=True)
|
2020-05-09 03:28:39 +00:00
|
|
|
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:
|
|
|
|
verbose_name = _('Password change log')
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2019-02-28 09:58:53 +00:00
|
|
|
class UserLoginLog(models.Model):
|
|
|
|
LOGIN_TYPE_CHOICE = (
|
|
|
|
('W', 'Web'),
|
|
|
|
('T', 'Terminal'),
|
2021-04-14 06:45:15 +00:00
|
|
|
('U', 'Unknown'),
|
2019-02-28 09:58:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
MFA_DISABLED = 0
|
|
|
|
MFA_ENABLED = 1
|
|
|
|
MFA_UNKNOWN = 2
|
|
|
|
|
|
|
|
MFA_CHOICE = (
|
|
|
|
(MFA_DISABLED, _('Disabled')),
|
|
|
|
(MFA_ENABLED, _('Enabled')),
|
|
|
|
(MFA_UNKNOWN, _('-')),
|
|
|
|
)
|
|
|
|
|
|
|
|
STATUS_CHOICE = (
|
|
|
|
(True, _('Success')),
|
|
|
|
(False, _('Failed'))
|
|
|
|
)
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
username = models.CharField(max_length=128, verbose_name=_('Username'))
|
|
|
|
type = models.CharField(choices=LOGIN_TYPE_CHOICE, max_length=2, verbose_name=_('Login type'))
|
|
|
|
ip = models.GenericIPAddressField(verbose_name=_('Login ip'))
|
|
|
|
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'))
|
|
|
|
mfa = models.SmallIntegerField(default=MFA_UNKNOWN, choices=MFA_CHOICE, verbose_name=_('MFA'))
|
2019-08-21 12:27:21 +00:00
|
|
|
reason = models.CharField(default='', max_length=128, blank=True, verbose_name=_('Reason'))
|
2019-02-28 09:58:53 +00:00
|
|
|
status = models.BooleanField(max_length=2, default=True, choices=STATUS_CHOICE, verbose_name=_('Status'))
|
|
|
|
datetime = models.DateTimeField(default=timezone.now, verbose_name=_('Date login'))
|
2020-12-11 07:48:31 +00:00
|
|
|
backend = models.CharField(max_length=32, default='', verbose_name=_('Authentication backend'))
|
2019-02-28 09:58:53 +00:00
|
|
|
|
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:
|
2019-10-22 09:40:55 +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(
|
|
|
|
Q(ip__contains=keyword) |
|
|
|
|
Q(city__contains=keyword) |
|
|
|
|
Q(username__contains=keyword)
|
|
|
|
)
|
2019-10-29 04:27:05 +00:00
|
|
|
if not current_org.is_root():
|
2020-07-20 02:42:22 +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
|
|
|
|
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:
|
2019-02-28 09:58:53 +00:00
|
|
|
ordering = ['-datetime', 'username']
|
2022-02-17 12:13:31 +00:00
|
|
|
verbose_name = _('User login log')
|