• BaiJiangJie's avatar
    [Feature] 添加功能 RemoteApp (#2706) · 1eca5179
    BaiJiangJie authored
    * [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修改代码风格
    1eca5179
remote_app.py 2.46 KB
# coding: utf-8
#

import uuid
from django.db import models
from django.utils.translation import ugettext_lazy as _

from orgs.mixins import OrgModelMixin
from common.fields.model import EncryptJsonDictTextField

from .. import const


__all__ = [
    'RemoteApp',
]


class RemoteApp(OrgModelMixin):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    name = models.CharField(max_length=128, verbose_name=_('Name'))
    asset = models.ForeignKey(
        'assets.Asset', on_delete=models.CASCADE, verbose_name=_('Asset')
    )
    system_user = models.ForeignKey(
        'assets.SystemUser', on_delete=models.CASCADE,
        verbose_name=_('System user')
    )
    type = models.CharField(
        default=const.REMOTE_APP_TYPE_CHROME,
        choices=const.REMOTE_APP_TYPE_CHOICES,
        max_length=128, verbose_name=_('App type')
    )
    path = models.CharField(
        max_length=128, blank=False, null=False,
        verbose_name=_('App path')
    )
    params = EncryptJsonDictTextField(
        max_length=4096, default={}, blank=True, null=True,
        verbose_name=_('Parameters')
    )
    created_by = models.CharField(
        max_length=32, null=True, blank=True, verbose_name=_('Created by')
    )
    date_created = models.DateTimeField(
        auto_now_add=True, null=True, blank=True, verbose_name=_('Date created')
    )
    comment = models.TextField(
        max_length=128, default='', blank=True, verbose_name=_('Comment')
    )

    class Meta:
        verbose_name = _("RemoteApp")
        unique_together = [('org_id', 'name')]
        ordering = ('name', )

    def __str__(self):
        return self.name

    @property
    def parameters(self):
        """
        返回Guacamole需要的RemoteApp配置参数信息中的parameters参数
        """
        _parameters = list()
        _parameters.append(self.type)
        path = '\"%s\"' % self.path
        _parameters.append(path)
        for field in const.REMOTE_APP_TYPE_MAP_FIELDS[self.type]:
            value = self.params.get(field['name'])
            if value is None:
                continue
            _parameters.append(value)
        _parameters = ' '.join(_parameters)
        return _parameters

    @property
    def asset_info(self):
        return {
            'id': self.asset.id,
            'hostname': self.asset.hostname
        }

    @property
    def system_user_info(self):
        return {
            'id': self.system_user.id,
            'name': self.system_user.name
        }