jumpserver/apps/assets/models/account.py

87 lines
2.7 KiB
Python
Raw Normal View History

2022-07-12 02:54:23 +00:00
from django.db import models
from django.db.models import Q
2022-07-12 02:54:23 +00:00
from django.utils.translation import gettext_lazy as _
from simple_history.models import HistoricalRecords
2022-09-06 11:57:03 +00:00
from common.utils import lazyproperty
from .base import BaseAccount
2022-07-12 02:54:23 +00:00
2022-08-19 10:49:00 +00:00
__all__ = ['Account', 'AccountTemplate']
2022-07-12 02:54:23 +00:00
class AccountHistoricalRecords(HistoricalRecords):
def __init__(self, *args, **kwargs):
self.included_fields = kwargs.pop('included_fields', None)
super().__init__(*args, **kwargs)
def fields_included(self, model):
fields = []
for field in model._meta.fields:
if self.included_fields is None:
if field.name not in self.excluded_fields:
fields.append(field)
else:
if field.name in self.included_fields:
fields.append(field)
return fields
2022-09-06 11:57:03 +00:00
class Account(BaseAccount):
class InnerAccount(models.TextChoices):
INPUT = '@INPUT', '@INPUT'
USER = '@USER', '@USER'
2022-09-13 06:06:25 +00:00
asset = models.ForeignKey(
'assets.Asset', related_name='accounts',
on_delete=models.CASCADE, verbose_name=_('Asset')
)
su_from = models.ForeignKey(
'assets.Account', related_name='su_to', null=True,
on_delete=models.SET_NULL, verbose_name=_("Su from")
)
2022-07-15 10:57:52 +00:00
version = models.IntegerField(default=0, verbose_name=_('Version'))
history = AccountHistoricalRecords(
included_fields=['id', 'secret_type', 'secret']
)
2022-07-12 02:54:23 +00:00
class Meta:
verbose_name = _('Account')
2022-09-13 13:07:20 +00:00
unique_together = [
2022-09-20 05:54:25 +00:00
('username', 'asset', 'secret_type'),
2022-09-13 13:07:20 +00:00
('name', 'asset'),
]
2022-07-12 02:54:23 +00:00
permissions = [
2022-07-27 08:51:39 +00:00
('view_accountsecret', _('Can view asset account secret')),
('change_accountsecret', _('Can change asset account secret')),
('view_historyaccount', _('Can view asset history account')),
('view_historyaccountsecret', _('Can view asset history account secret')),
2022-07-12 02:54:23 +00:00
]
2022-07-13 08:36:49 +00:00
2022-09-06 11:57:03 +00:00
@lazyproperty
2022-09-23 10:59:19 +00:00
def platform(self):
return self.asset.platform
2022-09-06 11:57:03 +00:00
2022-07-13 08:36:49 +00:00
def __str__(self):
2022-08-11 07:45:03 +00:00
return '{}@{}'.format(self.username, self.asset.name)
2022-08-19 10:49:00 +00:00
@classmethod
def get_input_account(cls):
""" @INPUT 手动登录的账号(any) """
return cls(name=cls.InnerAccount.INPUT.value, username='')
@classmethod
def get_user_account(cls, username):
""" @USER 动态用户的账号(self) """
return cls(name=cls.InnerAccount.USER.value, username=username)
2022-08-19 10:49:00 +00:00
2022-09-06 11:57:03 +00:00
class AccountTemplate(BaseAccount):
2022-08-19 10:49:00 +00:00
class Meta:
verbose_name = _('Account template')
2022-09-06 11:57:03 +00:00
unique_together = (
('name', 'org_id'),
)
2022-08-19 10:49:00 +00:00
def __str__(self):
2022-09-06 11:57:03 +00:00
return self.username