jumpserver/apps/assets/models/account.py

67 lines
2.0 KiB
Python
Raw Normal View History

2022-07-12 02:54:23 +00:00
from django.db import models
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
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'))
2022-07-12 02:54:23 +00:00
history = HistoricalRecords()
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