2016-12-20 15:06:27 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2017-11-23 06:08:01 +00:00
|
|
|
import uuid
|
2018-11-23 02:25:35 +00:00
|
|
|
import base64
|
2019-04-15 06:37:45 +00:00
|
|
|
import string
|
|
|
|
import random
|
2021-04-25 10:13:41 +00:00
|
|
|
import datetime
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2017-01-26 12:24:50 +00:00
|
|
|
from django.conf import settings
|
2018-11-23 02:25:35 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
2021-10-18 10:41:41 +00:00
|
|
|
from django.contrib.auth.hashers import check_password
|
2018-11-23 02:25:35 +00:00
|
|
|
from django.core.cache import cache
|
2017-01-26 12:24:50 +00:00
|
|
|
from django.db import models
|
2019-10-25 11:20:28 +00:00
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.shortcuts import reverse
|
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
from orgs.utils import current_org
|
2021-01-17 04:08:21 +00:00
|
|
|
from orgs.models import OrganizationMember, Organization
|
2021-08-24 06:20:54 +00:00
|
|
|
from common.exceptions import JMSException
|
2021-03-01 10:40:07 +00:00
|
|
|
from common.utils import date_expired_default, get_logger, lazyproperty, random_string
|
2019-06-25 03:22:17 +00:00
|
|
|
from common import fields
|
2020-07-20 02:42:22 +00:00
|
|
|
from common.const import choices
|
2021-07-08 06:23:18 +00:00
|
|
|
from common.db.models import TextChoices
|
2021-08-24 06:20:54 +00:00
|
|
|
from users.exceptions import MFANotEnabled, PhoneNotSet
|
2019-12-16 08:53:29 +00:00
|
|
|
from ..signals import post_user_change_password
|
2016-12-20 16:43:52 +00:00
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
__all__ = ['User', 'UserPasswordHistory', 'MFAType']
|
2019-04-15 06:37:45 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
class MFAType(TextChoices):
|
|
|
|
OTP = 'otp', _('One-time password')
|
|
|
|
SMS_CODE = 'sms', _('SMS verify code')
|
|
|
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
class AuthMixin:
|
2021-04-25 10:13:41 +00:00
|
|
|
date_password_last_updated: datetime.datetime
|
|
|
|
is_local: bool
|
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
@property
|
|
|
|
def password_raw(self):
|
2016-12-20 16:43:52 +00:00
|
|
|
raise AttributeError('Password raw is not a readable attribute')
|
2016-12-20 15:06:27 +00:00
|
|
|
|
|
|
|
#: Use this attr to set user object password, example
|
|
|
|
#: user = User(username='example', password_raw='password', ...)
|
|
|
|
#: It's equal:
|
|
|
|
#: user = User(username='example', ...)
|
|
|
|
#: user.set_password('password')
|
|
|
|
@password_raw.setter
|
|
|
|
def password_raw(self, password_raw_):
|
|
|
|
self.set_password(password_raw_)
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
def set_password(self, raw_password):
|
2018-12-27 08:47:40 +00:00
|
|
|
if self.can_update_password():
|
2019-07-11 10:12:14 +00:00
|
|
|
self.date_password_last_updated = timezone.now()
|
2019-12-16 08:53:29 +00:00
|
|
|
post_user_change_password.send(self.__class__, user=self)
|
2019-07-11 10:12:14 +00:00
|
|
|
super().set_password(raw_password)
|
2018-12-27 08:47:40 +00:00
|
|
|
|
2020-05-28 09:47:18 +00:00
|
|
|
def set_public_key(self, public_key):
|
2020-06-04 09:02:43 +00:00
|
|
|
if self.can_update_ssh_key():
|
|
|
|
self.public_key = public_key
|
|
|
|
self.save()
|
2020-05-28 09:47:18 +00:00
|
|
|
|
2018-12-27 08:47:40 +00:00
|
|
|
def can_update_password(self):
|
|
|
|
return self.is_local
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2019-07-24 04:50:39 +00:00
|
|
|
def can_update_ssh_key(self):
|
2019-07-26 02:46:43 +00:00
|
|
|
return self.can_use_ssh_key_login()
|
|
|
|
|
2021-04-26 02:21:22 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_use_ssh_key_login():
|
|
|
|
return settings.TERMINAL_PUBLIC_KEY_AUTH
|
2019-07-24 04:50:39 +00:00
|
|
|
|
2021-04-28 09:03:20 +00:00
|
|
|
def is_history_password(self, password):
|
|
|
|
allow_history_password_count = settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT
|
|
|
|
history_passwords = self.history_passwords.all().order_by('-date_created')[:int(allow_history_password_count)]
|
|
|
|
|
|
|
|
for history_password in history_passwords:
|
|
|
|
if check_password(password, history_password.password):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-02-09 13:19:49 +00:00
|
|
|
def is_public_key_valid(self):
|
|
|
|
"""
|
|
|
|
Check if the user's ssh public key is valid.
|
|
|
|
This function is used in base.html.
|
|
|
|
"""
|
2019-06-25 03:22:17 +00:00
|
|
|
if self.public_key:
|
2017-02-09 13:19:49 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-12-17 12:09:25 +00:00
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
def public_key_obj(self):
|
|
|
|
class PubKey(object):
|
|
|
|
def __getattr__(self, item):
|
|
|
|
return ''
|
|
|
|
|
|
|
|
if self.public_key:
|
|
|
|
import sshpubkeys
|
|
|
|
try:
|
|
|
|
return sshpubkeys.SSHKey(self.public_key)
|
|
|
|
except (TabError, TypeError):
|
|
|
|
pass
|
|
|
|
return PubKey()
|
|
|
|
|
2020-05-28 09:47:18 +00:00
|
|
|
def get_public_key_comment(self):
|
|
|
|
return self.public_key_obj.comment
|
|
|
|
|
|
|
|
def get_public_key_hash_md5(self):
|
|
|
|
if not callable(self.public_key_obj.hash_md5):
|
|
|
|
return ''
|
2020-06-03 04:32:52 +00:00
|
|
|
try:
|
|
|
|
return self.public_key_obj.hash_md5()
|
|
|
|
except:
|
|
|
|
return ''
|
2020-05-28 09:47:18 +00:00
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
def reset_password(self, new_password):
|
|
|
|
self.set_password(new_password)
|
2021-04-28 11:25:30 +00:00
|
|
|
self.need_update_password = False
|
2019-07-12 07:43:36 +00:00
|
|
|
self.save()
|
2018-12-17 12:09:25 +00:00
|
|
|
|
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
def date_password_expired(self):
|
|
|
|
interval = settings.SECURITY_PASSWORD_EXPIRATION_TIME
|
|
|
|
date_expired = self.date_password_last_updated + timezone.timedelta(
|
|
|
|
days=int(interval))
|
|
|
|
return date_expired
|
2018-12-17 12:09:25 +00:00
|
|
|
|
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
def password_expired_remain_days(self):
|
|
|
|
date_remain = self.date_password_expired - timezone.now()
|
|
|
|
return date_remain.days
|
2018-12-17 12:09:25 +00:00
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
def password_has_expired(self):
|
|
|
|
if self.is_local and self.password_expired_remain_days < 0:
|
2016-12-20 15:06:27 +00:00
|
|
|
return True
|
2019-07-11 10:12:14 +00:00
|
|
|
return False
|
2016-12-20 15:06:27 +00:00
|
|
|
|
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
def password_will_expired(self):
|
2019-07-30 04:52:45 +00:00
|
|
|
if self.is_local and 0 <= self.password_expired_remain_days < 5:
|
2016-12-20 15:06:27 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-11-11 04:43:00 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_public_key_body(key):
|
|
|
|
for i in key.split():
|
|
|
|
if len(i) > 256:
|
|
|
|
return i
|
|
|
|
return key
|
|
|
|
|
|
|
|
def check_public_key(self, key):
|
|
|
|
if not self.public_key:
|
|
|
|
return False
|
|
|
|
key = self.get_public_key_body(key)
|
|
|
|
key_saved = self.get_public_key_body(self.public_key)
|
|
|
|
if key == key_saved:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
|
|
|
class RoleMixin:
|
2021-07-08 06:23:18 +00:00
|
|
|
class ROLE(TextChoices):
|
2020-08-14 09:09:23 +00:00
|
|
|
ADMIN = choices.ADMIN, _('System administrator')
|
|
|
|
AUDITOR = choices.AUDITOR, _('System auditor')
|
2020-08-19 13:44:20 +00:00
|
|
|
USER = choices.USER, _('User')
|
2020-07-20 02:42:22 +00:00
|
|
|
APP = 'App', _('Application')
|
|
|
|
|
|
|
|
role = ROLE.USER
|
|
|
|
|
|
|
|
@property
|
2020-08-07 05:53:17 +00:00
|
|
|
def role_display(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
return self.get_role_display()
|
2019-07-11 10:12:14 +00:00
|
|
|
|
2020-08-19 09:38:21 +00:00
|
|
|
@lazyproperty
|
|
|
|
def org_roles(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
from orgs.models import ROLE as ORG_ROLE
|
|
|
|
|
2021-03-02 06:57:48 +00:00
|
|
|
if current_org.is_root():
|
|
|
|
# root 组织, 取 User 本身的角色
|
2020-07-20 02:42:22 +00:00
|
|
|
if self.is_superuser:
|
2021-03-02 06:57:48 +00:00
|
|
|
roles = [ORG_ROLE.ADMIN]
|
|
|
|
elif self.is_super_auditor:
|
|
|
|
roles = [ORG_ROLE.AUDITOR]
|
2020-07-20 02:42:22 +00:00
|
|
|
else:
|
2021-03-02 06:57:48 +00:00
|
|
|
roles = [ORG_ROLE.USER]
|
|
|
|
else:
|
|
|
|
# 是真实组织, 取 OrganizationMember 中的角色
|
|
|
|
roles = [
|
2021-07-13 08:21:06 +00:00
|
|
|
getattr(ORG_ROLE, org_member.role.upper())
|
|
|
|
for org_member in self.m2m_org_members.all()
|
2021-03-02 06:57:48 +00:00
|
|
|
if org_member.org_id == current_org.id
|
|
|
|
]
|
|
|
|
roles.sort()
|
2020-08-19 09:38:21 +00:00
|
|
|
return roles
|
2020-07-20 02:42:22 +00:00
|
|
|
|
2020-08-19 09:38:21 +00:00
|
|
|
@lazyproperty
|
2020-08-19 09:59:25 +00:00
|
|
|
def org_roles_label_list(self):
|
2020-08-19 09:38:21 +00:00
|
|
|
from orgs.models import ROLE as ORG_ROLE
|
2021-07-13 08:21:06 +00:00
|
|
|
return [str(role.label) for role in self.org_roles if role in ORG_ROLE]
|
2020-08-19 09:59:25 +00:00
|
|
|
|
2021-08-17 08:01:27 +00:00
|
|
|
@lazyproperty
|
|
|
|
def org_roles_value_list(self):
|
|
|
|
from orgs.models import ROLE as ORG_ROLE
|
|
|
|
return [str(role.value) for role in self.org_roles if role in ORG_ROLE]
|
|
|
|
|
2020-08-19 09:59:25 +00:00
|
|
|
@lazyproperty
|
|
|
|
def org_role_display(self):
|
|
|
|
return ' | '.join(self.org_roles_label_list)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def total_role_display(self):
|
|
|
|
roles = list({self.role_display, *self.org_roles_label_list})
|
|
|
|
roles.sort()
|
|
|
|
return ' | '.join(roles)
|
2017-03-29 16:51:36 +00:00
|
|
|
|
2020-05-13 03:05:58 +00:00
|
|
|
def current_org_roles(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
from orgs.models import OrganizationMember, ROLE as ORG_ROLE
|
2021-03-02 06:57:48 +00:00
|
|
|
if current_org.is_root():
|
2020-07-20 02:42:22 +00:00
|
|
|
if self.is_superuser:
|
|
|
|
return [ORG_ROLE.ADMIN]
|
|
|
|
else:
|
|
|
|
return [ORG_ROLE.USER]
|
|
|
|
|
|
|
|
roles = list(set(OrganizationMember.objects.filter(
|
|
|
|
org_id=current_org.id, user=self
|
|
|
|
).values_list('role', flat=True)))
|
|
|
|
|
2020-05-13 03:05:58 +00:00
|
|
|
return roles
|
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
@property
|
|
|
|
def is_superuser(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
if self.role == self.ROLE.ADMIN:
|
2016-12-20 15:06:27 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@is_superuser.setter
|
|
|
|
def is_superuser(self, value):
|
|
|
|
if value is True:
|
2020-07-20 02:42:22 +00:00
|
|
|
self.role = self.ROLE.ADMIN
|
2016-12-20 15:06:27 +00:00
|
|
|
else:
|
2020-07-20 02:42:22 +00:00
|
|
|
self.role = self.ROLE.USER
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
@property
|
|
|
|
def is_super_auditor(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
return self.role == self.ROLE.AUDITOR
|
2019-09-12 10:56:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_common_user(self):
|
|
|
|
if self.is_org_admin:
|
|
|
|
return False
|
|
|
|
if self.is_org_auditor:
|
|
|
|
return False
|
|
|
|
if self.is_app:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_app(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
return self.role == self.ROLE.APP
|
2019-09-12 10:56:26 +00:00
|
|
|
|
2020-07-09 07:41:02 +00:00
|
|
|
@lazyproperty
|
|
|
|
def user_all_orgs(self):
|
|
|
|
from orgs.models import Organization
|
|
|
|
return Organization.get_user_all_orgs(self)
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def user_orgs(self):
|
|
|
|
from orgs.models import Organization
|
|
|
|
return Organization.get_user_user_orgs(self)
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2018-07-13 07:05:46 +00:00
|
|
|
def admin_orgs(self):
|
|
|
|
from orgs.models import Organization
|
|
|
|
return Organization.get_user_admin_orgs(self)
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def audit_orgs(self):
|
|
|
|
from orgs.models import Organization
|
|
|
|
return Organization.get_user_audit_orgs(self)
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def admin_or_audit_orgs(self):
|
|
|
|
from orgs.models import Organization
|
|
|
|
return Organization.get_user_admin_or_audit_orgs(self)
|
|
|
|
|
2019-11-19 02:26:01 +00:00
|
|
|
@lazyproperty
|
2018-07-13 07:05:46 +00:00
|
|
|
def is_org_admin(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
from orgs.models import ROLE as ORG_ROLE
|
|
|
|
if self.is_superuser or self.m2m_org_members.filter(role=ORG_ROLE.ADMIN).exists():
|
2018-07-13 07:05:46 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def is_org_auditor(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
from orgs.models import ROLE as ORG_ROLE
|
|
|
|
if self.is_super_auditor or self.m2m_org_members.filter(role=ORG_ROLE.AUDITOR).exists():
|
2019-09-12 10:56:26 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2019-06-19 02:47:26 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def can_admin_current_org(self):
|
|
|
|
return current_org.can_admin_by(self)
|
2019-07-01 03:22:05 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def can_audit_current_org(self):
|
|
|
|
return current_org.can_audit_by(self)
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-11-25 07:30:11 +00:00
|
|
|
def can_user_current_org(self):
|
2021-03-02 06:57:48 +00:00
|
|
|
return current_org.can_use_by(self)
|
2019-11-25 07:30:11 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@lazyproperty
|
2019-09-12 10:56:26 +00:00
|
|
|
def can_admin_or_audit_current_org(self):
|
|
|
|
return self.can_admin_current_org or self.can_audit_current_org
|
2016-12-25 15:10:53 +00:00
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
@property
|
|
|
|
def is_staff(self):
|
|
|
|
if self.is_authenticated and self.is_valid:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@is_staff.setter
|
|
|
|
def is_staff(self, value):
|
|
|
|
pass
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
@classmethod
|
|
|
|
def create_app_user(cls, name, comment):
|
|
|
|
app = cls.objects.create(
|
|
|
|
username=name, name=name, email='{}@local.domain'.format(name),
|
2020-07-20 02:42:22 +00:00
|
|
|
is_active=False, role=cls.ROLE.APP, comment=comment,
|
2019-07-11 10:12:14 +00:00
|
|
|
is_first_login=False, created_by='System'
|
|
|
|
)
|
|
|
|
access_key = app.create_access_key()
|
|
|
|
return app, access_key
|
2018-11-22 10:02:12 +00:00
|
|
|
|
2019-12-09 03:50:52 +00:00
|
|
|
def remove(self):
|
2021-03-02 06:57:48 +00:00
|
|
|
if current_org.is_root():
|
2019-12-09 03:50:52 +00:00
|
|
|
return
|
2021-01-17 04:08:21 +00:00
|
|
|
org = Organization.get_instance(current_org.id)
|
|
|
|
OrganizationMember.objects.remove_users(org, [self])
|
2019-12-09 03:50:52 +00:00
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@classmethod
|
|
|
|
def get_super_admins(cls):
|
|
|
|
return cls.objects.filter(role=cls.ROLE.ADMIN)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_org_admins(cls, org=None):
|
|
|
|
from orgs.models import Organization
|
|
|
|
if not isinstance(org, Organization):
|
|
|
|
org = current_org
|
|
|
|
org_admins = org.admins
|
|
|
|
return org_admins
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_super_and_org_admins(cls, org=None):
|
|
|
|
super_admins = cls.get_super_admins()
|
2021-08-25 11:02:50 +00:00
|
|
|
org_admins = cls.get_org_admins(org=org)
|
|
|
|
admins = org_admins | super_admins
|
|
|
|
return admins.distinct()
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2018-11-22 10:02:12 +00:00
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
class TokenMixin:
|
|
|
|
CACHE_KEY_USER_RESET_PASSWORD_PREFIX = "_KEY_USER_RESET_PASSWORD_{}"
|
|
|
|
email = ''
|
|
|
|
id = None
|
2016-12-20 15:06:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def private_token(self):
|
2019-08-21 12:27:21 +00:00
|
|
|
return self.create_private_token()
|
2019-03-05 11:47:14 +00:00
|
|
|
|
|
|
|
def create_private_token(self):
|
|
|
|
from authentication.models import PrivateToken
|
2019-08-21 12:27:21 +00:00
|
|
|
token, created = PrivateToken.objects.get_or_create(user=self)
|
2019-03-05 11:47:14 +00:00
|
|
|
return token
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
def delete_private_token(self):
|
|
|
|
from authentication.models import PrivateToken
|
|
|
|
PrivateToken.objects.filter(user=self).delete()
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
def refresh_private_token(self):
|
2019-08-21 12:27:21 +00:00
|
|
|
self.delete_private_token()
|
2019-03-05 11:47:14 +00:00
|
|
|
return self.create_private_token()
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def create_bearer_token(self, request=None):
|
|
|
|
expiration = settings.TOKEN_EXPIRATION or 3600
|
|
|
|
if request:
|
|
|
|
remote_addr = request.META.get('REMOTE_ADDR', '')
|
|
|
|
else:
|
|
|
|
remote_addr = '0.0.0.0'
|
|
|
|
if not isinstance(remote_addr, bytes):
|
|
|
|
remote_addr = remote_addr.encode("utf-8")
|
|
|
|
remote_addr = base64.b16encode(remote_addr) # .replace(b'=', '')
|
2019-03-04 12:45:57 +00:00
|
|
|
cache_key = '%s_%s' % (self.id, remote_addr)
|
|
|
|
token = cache.get(cache_key)
|
2018-11-23 02:25:35 +00:00
|
|
|
if not token:
|
2021-03-01 10:40:07 +00:00
|
|
|
token = random_string(36)
|
2019-02-19 02:29:25 +00:00
|
|
|
cache.set(token, self.id, expiration)
|
|
|
|
cache.set('%s_%s' % (self.id, remote_addr), token, expiration)
|
2019-08-21 12:27:21 +00:00
|
|
|
date_expired = timezone.now() + timezone.timedelta(seconds=expiration)
|
|
|
|
return token, date_expired
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def refresh_bearer_token(self, token):
|
|
|
|
pass
|
|
|
|
|
2018-01-09 05:10:28 +00:00
|
|
|
def create_access_key(self):
|
2019-03-04 09:07:51 +00:00
|
|
|
access_key = self.access_keys.create()
|
2018-01-09 05:10:28 +00:00
|
|
|
return access_key
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
@property
|
|
|
|
def access_key(self):
|
|
|
|
return self.access_keys.first()
|
2016-12-20 15:06:27 +00:00
|
|
|
|
|
|
|
def generate_reset_token(self):
|
2019-04-15 06:37:45 +00:00
|
|
|
letter = string.ascii_letters + string.digits
|
2019-06-25 03:22:17 +00:00
|
|
|
token = ''.join([random.choice(letter) for _ in range(50)])
|
2019-04-15 06:37:45 +00:00
|
|
|
self.set_cache(token)
|
|
|
|
return token
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate_reset_password_token(cls, token):
|
2020-09-29 07:31:45 +00:00
|
|
|
if not token:
|
|
|
|
return None
|
|
|
|
key = cls.CACHE_KEY_USER_RESET_PASSWORD_PREFIX.format(token)
|
|
|
|
value = cache.get(key)
|
|
|
|
if not value:
|
|
|
|
return None
|
2019-04-15 06:37:45 +00:00
|
|
|
try:
|
|
|
|
user_id = value.get('id', '')
|
|
|
|
email = value.get('email', '')
|
|
|
|
user = cls.objects.get(id=user_id, email=email)
|
2020-09-29 07:31:45 +00:00
|
|
|
return user
|
2019-04-15 06:37:45 +00:00
|
|
|
except (AttributeError, cls.DoesNotExist) as e:
|
|
|
|
logger.error(e, exc_info=True)
|
2020-09-29 07:31:45 +00:00
|
|
|
return None
|
2019-04-15 06:37:45 +00:00
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
def set_cache(self, token):
|
|
|
|
key = self.CACHE_KEY_USER_RESET_PASSWORD_PREFIX.format(token)
|
|
|
|
cache.set(key, {'id': self.id, 'email': self.email}, 3600)
|
|
|
|
|
2019-04-15 06:37:45 +00:00
|
|
|
@classmethod
|
|
|
|
def expired_reset_password_token(cls, token):
|
|
|
|
key = cls.CACHE_KEY_USER_RESET_PASSWORD_PREFIX.format(token)
|
|
|
|
cache.delete(key)
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
|
|
|
class MFAMixin:
|
2019-11-18 08:30:26 +00:00
|
|
|
mfa_level = 0
|
2019-07-11 10:12:14 +00:00
|
|
|
otp_secret_key = ''
|
2019-11-18 08:30:26 +00:00
|
|
|
MFA_LEVEL_CHOICES = (
|
2019-07-11 10:12:14 +00:00
|
|
|
(0, _('Disable')),
|
|
|
|
(1, _('Enable')),
|
|
|
|
(2, _("Force enable")),
|
|
|
|
)
|
2021-07-30 07:19:00 +00:00
|
|
|
is_org_admin: bool
|
2021-09-16 11:06:16 +00:00
|
|
|
username: str
|
2019-07-11 10:12:14 +00:00
|
|
|
|
2018-04-10 13:02:07 +00:00
|
|
|
@property
|
2019-11-18 08:30:26 +00:00
|
|
|
def mfa_enabled(self):
|
2021-07-30 07:19:00 +00:00
|
|
|
if self.mfa_force_enabled:
|
|
|
|
return True
|
|
|
|
return self.mfa_level > 0
|
2018-04-10 13:02:07 +00:00
|
|
|
|
2018-04-18 04:48:07 +00:00
|
|
|
@property
|
2019-11-18 08:30:26 +00:00
|
|
|
def mfa_force_enabled(self):
|
2021-07-30 07:19:00 +00:00
|
|
|
if settings.SECURITY_MFA_AUTH in [True, 1]:
|
|
|
|
return True
|
|
|
|
if settings.SECURITY_MFA_AUTH == 2 and self.is_org_admin:
|
2018-06-06 07:35:26 +00:00
|
|
|
return True
|
2019-11-18 08:30:26 +00:00
|
|
|
return self.mfa_level == 2
|
2018-04-18 04:48:07 +00:00
|
|
|
|
2019-11-18 08:30:26 +00:00
|
|
|
def enable_mfa(self):
|
|
|
|
if not self.mfa_level == 2:
|
|
|
|
self.mfa_level = 1
|
2018-04-10 13:04:56 +00:00
|
|
|
|
2019-11-18 08:30:26 +00:00
|
|
|
def force_enable_mfa(self):
|
|
|
|
self.mfa_level = 2
|
2018-04-10 13:04:56 +00:00
|
|
|
|
2019-11-18 08:30:26 +00:00
|
|
|
def disable_mfa(self):
|
|
|
|
self.mfa_level = 0
|
2018-04-19 03:13:11 +00:00
|
|
|
self.otp_secret_key = None
|
2018-04-10 13:02:07 +00:00
|
|
|
|
2019-11-18 10:12:03 +00:00
|
|
|
def reset_mfa(self):
|
|
|
|
if self.mfa_is_otp():
|
|
|
|
self.otp_secret_key = ''
|
|
|
|
|
2019-11-18 08:30:26 +00:00
|
|
|
@staticmethod
|
|
|
|
def mfa_is_otp():
|
2019-12-05 07:09:25 +00:00
|
|
|
if settings.OTP_IN_RADIUS:
|
2019-11-18 08:30:26 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-11-18 10:12:03 +00:00
|
|
|
def check_radius(self, code):
|
2019-11-11 12:10:49 +00:00
|
|
|
from authentication.backends.radius import RadiusBackend
|
|
|
|
backend = RadiusBackend()
|
|
|
|
user = backend.authenticate(None, username=self.username, password=code)
|
|
|
|
if user:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
def check_otp(self, code):
|
|
|
|
from ..utils import check_otp_code
|
2019-11-18 10:12:03 +00:00
|
|
|
return check_otp_code(self.otp_secret_key, code)
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
def check_mfa(self, code, mfa_type=MFAType.OTP):
|
2021-02-24 23:44:06 +00:00
|
|
|
if not self.mfa_enabled:
|
|
|
|
raise MFANotEnabled
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
if mfa_type == MFAType.OTP:
|
|
|
|
if settings.OTP_IN_RADIUS:
|
|
|
|
return self.check_radius(code)
|
|
|
|
else:
|
|
|
|
return self.check_otp(code)
|
|
|
|
elif mfa_type == MFAType.SMS_CODE:
|
|
|
|
return self.check_sms_code(code)
|
|
|
|
|
|
|
|
def get_supported_mfa_types(self):
|
|
|
|
methods = []
|
|
|
|
if self.otp_secret_key:
|
|
|
|
methods.append(MFAType.OTP)
|
2021-09-22 03:16:06 +00:00
|
|
|
if settings.XPACK_ENABLED and settings.SMS_ENABLED and self.phone:
|
2021-08-24 06:20:54 +00:00
|
|
|
methods.append(MFAType.SMS_CODE)
|
|
|
|
return methods
|
|
|
|
|
|
|
|
def check_sms_code(self, code):
|
|
|
|
from authentication.sms_verify_code import VerifyCodeUtil
|
|
|
|
|
|
|
|
if not self.phone:
|
|
|
|
raise PhoneNotSet
|
|
|
|
|
|
|
|
try:
|
|
|
|
util = VerifyCodeUtil(self.phone)
|
|
|
|
return util.verify(code)
|
|
|
|
except JMSException:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def send_sms_code(self):
|
|
|
|
from authentication.sms_verify_code import VerifyCodeUtil
|
|
|
|
|
|
|
|
if not self.phone:
|
|
|
|
raise PhoneNotSet
|
|
|
|
|
|
|
|
util = VerifyCodeUtil(self.phone)
|
|
|
|
util.touch()
|
|
|
|
return util.timeout
|
2019-11-05 10:46:29 +00:00
|
|
|
|
2019-11-18 08:30:26 +00:00
|
|
|
def mfa_enabled_but_not_set(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
if not self.mfa_enabled:
|
|
|
|
return False, None
|
2021-09-09 12:12:52 +00:00
|
|
|
|
|
|
|
if not self.mfa_is_otp():
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
if self.mfa_is_otp() and self.otp_secret_key:
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
if self.phone and settings.SMS_ENABLED and settings.XPACK_ENABLED:
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
return True, reverse('authentication:user-otp-enable-start')
|
2019-11-18 08:30:26 +00:00
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
class User(AuthMixin, TokenMixin, RoleMixin, MFAMixin, AbstractUser):
|
2020-12-09 10:43:13 +00:00
|
|
|
class Source(TextChoices):
|
|
|
|
local = 'local', _('Local')
|
|
|
|
ldap = 'ldap', 'LDAP/AD'
|
|
|
|
openid = 'openid', 'OpenID'
|
|
|
|
radius = 'radius', 'Radius'
|
|
|
|
cas = 'cas', 'CAS'
|
2016-12-20 17:17:36 +00:00
|
|
|
|
2021-02-26 09:33:11 +00:00
|
|
|
SOURCE_BACKEND_MAPPING = {
|
2021-03-24 11:01:35 +00:00
|
|
|
Source.local: [
|
|
|
|
settings.AUTH_BACKEND_MODEL, settings.AUTH_BACKEND_PUBKEY,
|
|
|
|
settings.AUTH_BACKEND_WECOM, settings.AUTH_BACKEND_DINGTALK,
|
|
|
|
],
|
2021-02-26 09:33:11 +00:00
|
|
|
Source.ldap: [settings.AUTH_BACKEND_LDAP],
|
|
|
|
Source.openid: [settings.AUTH_BACKEND_OIDC_PASSWORD, settings.AUTH_BACKEND_OIDC_CODE],
|
|
|
|
Source.radius: [settings.AUTH_BACKEND_RADIUS],
|
|
|
|
Source.cas: [settings.AUTH_BACKEND_CAS],
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
username = models.CharField(
|
|
|
|
max_length=128, unique=True, verbose_name=_('Username')
|
|
|
|
)
|
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
|
|
|
email = models.EmailField(
|
|
|
|
max_length=128, unique=True, verbose_name=_('Email')
|
|
|
|
)
|
|
|
|
groups = models.ManyToManyField(
|
|
|
|
'users.UserGroup', related_name='users',
|
|
|
|
blank=True, verbose_name=_('User group')
|
|
|
|
)
|
|
|
|
role = models.CharField(
|
2020-07-20 02:42:22 +00:00
|
|
|
choices=RoleMixin.ROLE.choices, default='User', max_length=10,
|
2019-07-11 10:12:14 +00:00
|
|
|
blank=True, verbose_name=_('Role')
|
|
|
|
)
|
|
|
|
avatar = models.ImageField(
|
|
|
|
upload_to="avatar", null=True, verbose_name=_('Avatar')
|
|
|
|
)
|
|
|
|
wechat = models.CharField(
|
|
|
|
max_length=128, blank=True, verbose_name=_('Wechat')
|
|
|
|
)
|
|
|
|
phone = models.CharField(
|
|
|
|
max_length=20, blank=True, null=True, verbose_name=_('Phone')
|
|
|
|
)
|
2019-11-18 08:30:26 +00:00
|
|
|
mfa_level = models.SmallIntegerField(
|
|
|
|
default=0, choices=MFAMixin.MFA_LEVEL_CHOICES, verbose_name=_('MFA')
|
2019-07-11 10:12:14 +00:00
|
|
|
)
|
|
|
|
otp_secret_key = fields.EncryptCharField(max_length=128, blank=True, null=True)
|
|
|
|
# Todo: Auto generate key, let user download
|
|
|
|
private_key = fields.EncryptTextField(
|
|
|
|
blank=True, null=True, verbose_name=_('Private key')
|
|
|
|
)
|
|
|
|
public_key = fields.EncryptTextField(
|
|
|
|
blank=True, null=True, verbose_name=_('Public key')
|
|
|
|
)
|
|
|
|
comment = models.TextField(
|
|
|
|
blank=True, null=True, verbose_name=_('Comment')
|
|
|
|
)
|
|
|
|
is_first_login = models.BooleanField(default=True)
|
|
|
|
date_expired = models.DateTimeField(
|
|
|
|
default=date_expired_default, blank=True, null=True,
|
|
|
|
db_index=True, verbose_name=_('Date expired')
|
|
|
|
)
|
|
|
|
created_by = models.CharField(
|
2019-08-21 12:27:21 +00:00
|
|
|
max_length=30, default='', blank=True, verbose_name=_('Created by')
|
2019-07-11 10:12:14 +00:00
|
|
|
)
|
|
|
|
source = models.CharField(
|
2021-02-26 09:33:11 +00:00
|
|
|
max_length=30, default=Source.local,
|
|
|
|
choices=Source.choices,
|
2021-06-04 03:38:00 +00:00
|
|
|
verbose_name=_('Source')
|
2019-07-11 10:12:14 +00:00
|
|
|
)
|
|
|
|
date_password_last_updated = models.DateTimeField(
|
|
|
|
auto_now_add=True, blank=True, null=True,
|
|
|
|
verbose_name=_('Date password last updated')
|
|
|
|
)
|
2021-05-22 09:00:01 +00:00
|
|
|
need_update_password = models.BooleanField(
|
|
|
|
default=False, verbose_name=_('Need update password')
|
|
|
|
)
|
2021-09-15 10:20:38 +00:00
|
|
|
wecom_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('WeCom'))
|
|
|
|
dingtalk_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('DingTalk'))
|
|
|
|
feishu_id = models.CharField(null=True, default=None, unique=True, max_length=128, verbose_name=_('FeiShu'))
|
2019-07-11 10:12:14 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{0.name}({0.username})'.format(self)
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
@classmethod
|
|
|
|
def get_group_ids_by_user_id(cls, user_id):
|
|
|
|
group_ids = cls.groups.through.objects.filter(user_id=user_id).distinct().values_list('usergroup_id', flat=True)
|
|
|
|
group_ids = list(group_ids)
|
|
|
|
return group_ids
|
2021-09-09 12:12:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def receive_backends(self):
|
|
|
|
return self.user_msg_subscription.receive_backends
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2021-03-24 11:01:35 +00:00
|
|
|
@property
|
|
|
|
def is_wecom_bound(self):
|
|
|
|
return bool(self.wecom_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_dingtalk_bound(self):
|
|
|
|
return bool(self.dingtalk_id)
|
|
|
|
|
2021-08-12 08:44:06 +00:00
|
|
|
@property
|
|
|
|
def is_feishu_bound(self):
|
|
|
|
return bool(self.feishu_id)
|
|
|
|
|
2021-09-16 10:59:15 +00:00
|
|
|
@property
|
|
|
|
def is_otp_secret_key_bound(self):
|
|
|
|
return bool(self.otp_secret_key)
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('users:user-detail', args=(self.id,))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def groups_display(self):
|
|
|
|
return ' '.join([group.name for group in self.groups.all()])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_display(self):
|
|
|
|
return self.get_source_display()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_expired(self):
|
|
|
|
if self.date_expired and self.date_expired < timezone.now():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
@property
|
|
|
|
def expired_remain_days(self):
|
|
|
|
date_remain = self.date_expired - timezone.now()
|
|
|
|
return date_remain.days
|
|
|
|
|
|
|
|
@property
|
|
|
|
def will_expired(self):
|
|
|
|
if 0 <= self.expired_remain_days < 5:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
@property
|
|
|
|
def is_valid(self):
|
|
|
|
if self.is_active and not self.is_expired:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_local(self):
|
2020-12-09 10:43:13 +00:00
|
|
|
return self.source == self.Source.local.value
|
2019-07-31 08:57:21 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def set_unprovide_attr_if_need(self):
|
2019-07-11 10:12:14 +00:00
|
|
|
if not self.name:
|
|
|
|
self.name = self.username
|
2020-03-12 08:24:38 +00:00
|
|
|
if not self.email or '@' not in self.email:
|
|
|
|
email = '{}@{}'.format(self.username, settings.EMAIL_SUFFIX)
|
|
|
|
if '@' in self.username:
|
|
|
|
email = self.username
|
|
|
|
self.email = email
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.set_unprovide_attr_if_need()
|
2019-07-11 10:12:14 +00:00
|
|
|
if self.username == 'admin':
|
|
|
|
self.role = 'Admin'
|
|
|
|
self.is_active = True
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
def is_member_of(self, user_group):
|
|
|
|
if user_group in self.groups.all():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def set_avatar(self, f):
|
|
|
|
self.avatar.save(self.username, f)
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
@classmethod
|
|
|
|
def get_avatar_url(cls, username):
|
|
|
|
user_default = settings.STATIC_URL + "img/avatar/user.png"
|
|
|
|
return user_default
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
def avatar_url(self):
|
|
|
|
admin_default = settings.STATIC_URL + "img/avatar/admin.png"
|
|
|
|
user_default = settings.STATIC_URL + "img/avatar/user.png"
|
|
|
|
if self.avatar:
|
|
|
|
return self.avatar.url
|
|
|
|
if self.is_superuser:
|
|
|
|
return admin_default
|
|
|
|
else:
|
|
|
|
return user_default
|
2016-12-20 15:06:27 +00:00
|
|
|
|
2021-04-09 07:01:22 +00:00
|
|
|
def unblock_login(self):
|
|
|
|
from users.utils import LoginBlockUtil, MFABlockUtils
|
|
|
|
LoginBlockUtil.unblock_user(self.username)
|
|
|
|
MFABlockUtils.unblock_user(self.username)
|
|
|
|
|
2021-03-11 12:09:23 +00:00
|
|
|
@property
|
|
|
|
def login_blocked(self):
|
2021-04-08 04:47:49 +00:00
|
|
|
from users.utils import LoginBlockUtil, MFABlockUtils
|
|
|
|
if LoginBlockUtil.is_user_block(self.username):
|
|
|
|
return True
|
|
|
|
if MFABlockUtils.is_user_block(self.username):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2021-03-11 12:09:23 +00:00
|
|
|
|
2017-11-23 06:08:01 +00:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
2016-12-20 16:43:52 +00:00
|
|
|
if self.pk == 1 or self.username == 'admin':
|
|
|
|
return
|
|
|
|
return super(User, self).delete()
|
|
|
|
|
2021-03-16 08:57:56 +00:00
|
|
|
@classmethod
|
|
|
|
def get_user_allowed_auth_backends(cls, username):
|
|
|
|
if not settings.ONLY_ALLOW_AUTH_FROM_SOURCE or not username:
|
|
|
|
# return settings.AUTHENTICATION_BACKENDS
|
|
|
|
return None
|
|
|
|
user = cls.objects.filter(username=username).first()
|
|
|
|
if not user:
|
|
|
|
return None
|
|
|
|
return user.get_allowed_auth_backends()
|
|
|
|
|
|
|
|
def get_allowed_auth_backends(self):
|
|
|
|
if not settings.ONLY_ALLOW_AUTH_FROM_SOURCE:
|
|
|
|
return None
|
|
|
|
return self.SOURCE_BACKEND_MAPPING.get(self.source, [])
|
|
|
|
|
2016-12-20 15:06:27 +00:00
|
|
|
class Meta:
|
2016-12-21 16:36:31 +00:00
|
|
|
ordering = ['username']
|
2018-03-05 04:59:14 +00:00
|
|
|
verbose_name = _("User")
|
2016-12-20 15:06:27 +00:00
|
|
|
|
|
|
|
#: Use this method initial user
|
|
|
|
@classmethod
|
|
|
|
def initial(cls):
|
2018-01-12 07:43:26 +00:00
|
|
|
from .group import UserGroup
|
2016-12-20 15:06:27 +00:00
|
|
|
user = cls(username='admin',
|
|
|
|
email='admin@jumpserver.org',
|
|
|
|
name=_('Administrator'),
|
|
|
|
password_raw='admin',
|
|
|
|
role='Admin',
|
|
|
|
comment=_('Administrator is the super user of system'),
|
|
|
|
created_by=_('System'))
|
|
|
|
user.save()
|
|
|
|
user.groups.add(UserGroup.initial())
|
|
|
|
|
2020-03-13 04:33:09 +00:00
|
|
|
def can_send_created_mail(self):
|
2020-12-09 10:43:13 +00:00
|
|
|
if self.email and self.source == self.Source.local.value:
|
2020-03-13 04:33:09 +00:00
|
|
|
return True
|
|
|
|
return False
|
2021-04-28 09:03:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserPasswordHistory(models.Model):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
password = models.CharField(max_length=128)
|
|
|
|
user = models.ForeignKey("users.User", related_name='history_passwords',
|
|
|
|
on_delete=models.CASCADE, verbose_name=_('User'))
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_("Date created"))
|
2021-05-17 11:32:12 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'{self.user} set at {self.date_created}'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.__str__()
|