2016-12-20 16:43:52 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import logging
2022-09-06 11:57:03 +00:00
import uuid
from common . db import fields
2017-03-06 13:05:00 +00:00
from django . db import models
2022-09-06 11:57:03 +00:00
from django . utils . translation import gettext_lazy as _
2018-10-16 08:47:47 +00:00
from django . core . validators import MinValueValidator , MaxValueValidator
2016-12-20 16:43:52 +00:00
2022-09-06 11:57:03 +00:00
from orgs . mixins . models import OrgModelMixin
2016-12-20 16:43:52 +00:00
2022-08-03 07:58:06 +00:00
__all__ = [ ' SystemUser ' ]
2017-12-11 09:08:43 +00:00
logger = logging . getLogger ( __name__ )
2016-12-20 16:43:52 +00:00
2022-09-06 11:57:03 +00:00
class SystemUser ( OrgModelMixin ) :
2021-07-08 06:23:18 +00:00
LOGIN_AUTO = ' auto '
LOGIN_MANUAL = ' manual '
LOGIN_MODE_CHOICES = (
2022-08-03 07:58:06 +00:00
( LOGIN_AUTO , _ ( ' Automatic managed ' ) ) ,
2021-07-08 06:23:18 +00:00
( LOGIN_MANUAL , _ ( ' Manually input ' ) )
)
2022-02-21 08:24:03 +00:00
class Type ( models . TextChoices ) :
2021-07-08 06:23:18 +00:00
common = ' common ' , _ ( ' Common user ' )
admin = ' admin ' , _ ( ' Admin user ' )
2022-09-06 11:57:03 +00:00
id = models . UUIDField ( default = uuid . uuid4 , primary_key = True )
name = models . CharField ( max_length = 128 , verbose_name = _ ( ' Name ' ) )
username = models . CharField ( max_length = 128 , blank = True , verbose_name = _ ( ' Username ' ) , db_index = True )
password = fields . EncryptCharField ( max_length = 256 , blank = True , null = True , verbose_name = _ ( ' Password ' ) )
private_key = fields . EncryptTextField ( blank = True , null = True , verbose_name = _ ( ' SSH private key ' ) )
public_key = fields . EncryptTextField ( blank = True , null = True , verbose_name = _ ( ' SSH public key ' ) )
token = models . TextField ( default = ' ' , verbose_name = _ ( ' Token ' ) )
comment = models . TextField ( blank = True , verbose_name = _ ( ' Comment ' ) )
date_created = models . DateTimeField ( auto_now_add = True , verbose_name = _ ( " Date created " ) )
date_updated = models . DateTimeField ( auto_now = True , verbose_name = _ ( " Date updated " ) )
created_by = models . CharField ( max_length = 128 , null = True , verbose_name = _ ( ' Created by ' ) )
2021-07-08 06:23:18 +00:00
username_same_with_user = models . BooleanField ( default = False , verbose_name = _ ( " Username same with user " ) )
2022-07-15 10:03:32 +00:00
type = models . CharField ( max_length = 16 , choices = Type . choices , default = Type . common , verbose_name = _ ( ' Type ' ) )
2022-08-03 07:58:06 +00:00
priority = models . IntegerField ( default = 81 , verbose_name = _ ( " Priority " ) , help_text = _ ( " 1-100, the lower the value will be match first " ) , validators = [ MinValueValidator ( 1 ) , MaxValueValidator ( 100 ) ] )
2022-08-04 02:44:11 +00:00
protocol = models . CharField ( max_length = 16 , default = ' ssh ' , verbose_name = _ ( ' Protocol ' ) )
2021-07-08 06:23:18 +00:00
auto_push = models . BooleanField ( default = True , verbose_name = _ ( ' Auto push ' ) )
sudo = models . TextField ( default = ' /bin/whoami ' , verbose_name = _ ( ' Sudo ' ) )
shell = models . CharField ( max_length = 64 , default = ' /bin/bash ' , verbose_name = _ ( ' Shell ' ) )
2022-08-03 07:58:06 +00:00
login_mode = models . CharField ( choices = LOGIN_MODE_CHOICES , default = LOGIN_AUTO , max_length = 10 , verbose_name = _ ( ' Login mode ' ) )
2021-07-08 06:23:18 +00:00
sftp_root = models . CharField ( default = ' tmp ' , max_length = 128 , verbose_name = _ ( " SFTP Root " ) )
home = models . CharField ( max_length = 4096 , default = ' ' , verbose_name = _ ( ' Home ' ) , blank = True )
system_groups = models . CharField ( default = ' ' , max_length = 4096 , verbose_name = _ ( ' System groups ' ) , blank = True )
ad_domain = models . CharField ( default = ' ' , max_length = 256 )
2021-11-05 08:11:29 +00:00
# linux su 命令 (switch user)
2021-11-05 08:39:08 +00:00
su_enabled = models . BooleanField ( default = False , verbose_name = _ ( ' User switch ' ) )
2021-11-05 08:11:29 +00:00
su_from = models . ForeignKey ( ' self ' , on_delete = models . SET_NULL , related_name = ' su_to ' , null = True , verbose_name = _ ( " Switch from " ) )
2022-09-01 06:46:31 +00:00
privileged = None
2021-07-08 06:23:18 +00:00
2016-12-20 16:43:52 +00:00
class Meta :
2016-12-21 16:36:31 +00:00
ordering = [ ' name ' ]
2018-07-20 02:54:16 +00:00
unique_together = [ ( ' name ' , ' org_id ' ) ]
2018-01-05 09:57:02 +00:00
verbose_name = _ ( " System user " )
2022-03-04 02:16:21 +00:00
permissions = [
( ' match_systemuser ' , _ ( ' Can match system user ' ) ) ,
]