mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-10-22 16:31:33 +00:00
* [Feature] RemoteApp添加Model * [Feature] RemoteApp添加ViewSet API * [Feature] RemoteApp添加获取connection-info API * [Feature] Perms模块修改目录结构 * [Feature] RemoteAppPermission添加Model * [Feature] RemoteAppPermission添加ViewSet API * [Feature] RemoteAppPermission添加用户/用户组获取被授权的RemoteApp API * [Feature] RemoteAppPermission添加校验用户对RemoteApp的权限 API * [Feature] RemoteAppPermission添加获取用户授权的RemoteApp树 API * [Feature] RemoteAppPermission添加<添加/移除>所授权的<用户/RemoteApp> API * [Feature] RemoteApp添加创建、更新、详情、删除、用户RemoteApp等页面 * [Feature] RemoteAppPermission添加创建、更新、详情、删除、授权用户、授权RemoteApp等页面 * [Feature] RemoteApp从assets模块迁移到新添加的applications模块 * [Feature] RemoteApp/RemoteAppPermission添加迁移文件 * [Feature] RemoteApp/RemoteAppPermission修改小细节 * [Feature] RemoteApp/RemoteAppPermission修改小细节2 * [Feature] RemoteApp/RemoteAppPermission修改小细节3 * [Feature] RemoteApp更新迁移文件 * [Feature] RemoteApp/RemoteAppPermission添加翻译信息 * [Feature] RemoteApp/RemoteAppPermission删除迁移文件 * [Feature] RemoteApp/RemoteAppPermission添加迁移文件 * [Feature] RemoteApp/RemoteAppPermission修改代码风格
136 lines
3.3 KiB
Python
136 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
import json
|
|
from django.db import models
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from ..utils import get_signer
|
|
|
|
|
|
__all__ = [
|
|
'JsonMixin', 'JsonDictMixin', 'JsonListMixin', 'JsonTypeMixin',
|
|
'JsonCharField', 'JsonTextField', 'JsonListCharField', 'JsonListTextField',
|
|
'JsonDictCharField', 'JsonDictTextField', 'EncryptCharField',
|
|
'EncryptTextField', 'EncryptMixin', 'EncryptJsonDictTextField',
|
|
]
|
|
signer = get_signer()
|
|
|
|
|
|
class JsonMixin:
|
|
tp = None
|
|
|
|
@staticmethod
|
|
def json_decode(data):
|
|
try:
|
|
return json.loads(data)
|
|
except (TypeError, json.JSONDecodeError):
|
|
return None
|
|
|
|
@staticmethod
|
|
def json_encode(data):
|
|
return json.dumps(data)
|
|
|
|
def from_db_value(self, value, expression, connection, context):
|
|
if value is None:
|
|
return value
|
|
return self.json_decode(value)
|
|
|
|
def to_python(self, value):
|
|
if value is None:
|
|
return value
|
|
|
|
if not isinstance(value, str) or not value.startswith('"'):
|
|
return value
|
|
else:
|
|
return self.json_decode(value)
|
|
|
|
def get_prep_value(self, value):
|
|
if value is None:
|
|
return value
|
|
return self.json_encode(value)
|
|
|
|
|
|
class JsonTypeMixin(JsonMixin):
|
|
tp = dict
|
|
|
|
def from_db_value(self, value, expression, connection, context):
|
|
value = super().from_db_value(value, expression, connection, context)
|
|
if not isinstance(value, self.tp):
|
|
value = self.tp()
|
|
return value
|
|
|
|
def to_python(self, value):
|
|
data = super().to_python(value)
|
|
if not isinstance(data, self.tp):
|
|
data = self.tp()
|
|
return data
|
|
|
|
def get_prep_value(self, value):
|
|
if not isinstance(value, self.tp):
|
|
value = self.tp()
|
|
return self.json_encode(value)
|
|
|
|
|
|
class JsonDictMixin(JsonTypeMixin):
|
|
tp = dict
|
|
|
|
|
|
class JsonDictCharField(JsonDictMixin, models.CharField):
|
|
description = _("Marshal dict data to char field")
|
|
|
|
|
|
class JsonDictTextField(JsonDictMixin, models.TextField):
|
|
description = _("Marshal dict data to text field")
|
|
|
|
|
|
class JsonListMixin(JsonTypeMixin):
|
|
tp = list
|
|
|
|
|
|
class JsonStrListMixin(JsonListMixin):
|
|
pass
|
|
|
|
|
|
class JsonListCharField(JsonListMixin, models.CharField):
|
|
description = _("Marshal list data to char field")
|
|
|
|
|
|
class JsonListTextField(JsonListMixin, models.TextField):
|
|
description = _("Marshal list data to text field")
|
|
|
|
|
|
class JsonCharField(JsonMixin, models.CharField):
|
|
description = _("Marshal data to char field")
|
|
|
|
|
|
class JsonTextField(JsonMixin, models.TextField):
|
|
description = _("Marshal data to text field")
|
|
|
|
|
|
class EncryptMixin:
|
|
def from_db_value(self, value, expression, connection, context):
|
|
if value is not None:
|
|
return signer.unsign(value)
|
|
return None
|
|
|
|
def get_prep_value(self, value):
|
|
if value is None:
|
|
return value
|
|
return signer.sign(value)
|
|
|
|
|
|
class EncryptTextField(EncryptMixin, models.TextField):
|
|
description = _("Encrypt field using Secret Key")
|
|
|
|
|
|
class EncryptCharField(EncryptMixin, models.CharField):
|
|
def __init__(self, *args, **kwargs):
|
|
kwargs['max_length'] = 2048
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
class EncryptJsonDictTextField(EncryptMixin, JsonDictTextField):
|
|
pass
|
|
|
|
|