Add private token and change user group

pull/530/head
ibuler 2016-12-22 00:36:31 +08:00
parent 875aaa0029
commit 5b4ce709af
14 changed files with 109 additions and 53 deletions

View File

@ -73,7 +73,7 @@ class Asset(models.Model):
def __unicode__(self): def __unicode__(self):
return '%(ip)s:%(port)s' % {'ip': self.ip, 'port': self.port} return '%(ip)s:%(port)s' % {'ip': self.ip, 'port': self.port}
__str__ = __unicode__() __str__ = __unicode__
@property @property
def is_valid(self): def is_valid(self):
@ -88,7 +88,6 @@ class Asset(models.Model):
pass pass
class Meta: class Meta:
db_table = 'asset'
unique_together = ('ip', 'port') unique_together = ('ip', 'port')
@classmethod @classmethod
@ -126,4 +125,4 @@ class Tag(models.Model):
__str__ = __unicode__ __str__ = __unicode__
class Meta: class Meta:
db_table = 'tag' ordering = ['name']

View File

@ -25,7 +25,7 @@ class AssetGroup(models.Model):
return self.name return self.name
class Meta: class Meta:
db_table = 'asset_group' ordering = ['name']
@classmethod @classmethod
def initial(cls): def initial(cls):

View File

@ -34,7 +34,7 @@ class IDC(models.Model):
return cls.objects.get_or_create(name=_('Default'), created_by=_('System'), comment=_('Default IDC'))[0] return cls.objects.get_or_create(name=_('Default'), created_by=_('System'), comment=_('Default IDC'))[0]
class Meta: class Meta:
db_table = 'idc' ordering = ['name']
@classmethod @classmethod
def generate_fake(cls, count=100): def generate_fake(cls, count=100):

View File

@ -37,6 +37,8 @@ class AdminUser(models.Model):
def __unicode__(self): def __unicode__(self):
return self.name return self.name
__str__ = __unicode__
@property @property
def password(self): def password(self):
return signer.unsign(self._password) return signer.unsign(self._password)
@ -66,7 +68,7 @@ class AdminUser(models.Model):
return self.assets.count() return self.assets.count()
class Meta: class Meta:
db_table = 'admin_user' ordering = ['name']
@classmethod @classmethod
def generate_fake(cls, count=100): def generate_fake(cls, count=100):
@ -161,7 +163,7 @@ class SystemUser(models.Model):
return self.asset_groups.count() return self.asset_groups.count()
class Meta: class Meta:
db_table = 'system_user' ordering = ['name']
@classmethod @classmethod
def generate_fake(cls, count=100): def generate_fake(cls, count=100):

View File

@ -60,7 +60,6 @@ INSTALLED_APPS = [
'common.apps.CommonConfig', 'common.apps.CommonConfig',
'terminal.apps.TerminalConfig', 'terminal.apps.TerminalConfig',
'rest_framework', 'rest_framework',
'rest_framework.authtoken',
'bootstrapform', 'bootstrapform',
'captcha', 'captcha',
'django.contrib.auth', 'django.contrib.auth',

View File

@ -14,6 +14,18 @@ from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrTerminalUser, User from .hands import IsSuperUserOrTerminalUser, User
class TerminalRegister(ListCreateAPIView):
queryset = Terminal.objects.all()
serializer_class = TerminalSerializer
permission_classes = (AllowAny,)
def create(self, request, *args, **kwargs):
name = signer.unsign(request.data.get('name', ''))
remote_addr = request.Meta.get('REMOTE_ADDR')
serializer = self.serializer_class({'name': name, 'remote_addr': remote_addr})
class TerminalViewSet(viewsets.ModelViewSet): class TerminalViewSet(viewsets.ModelViewSet):
queryset = Terminal.objects.all() queryset = Terminal.objects.all()

View File

@ -10,7 +10,7 @@ from .models import Terminal
class TerminalForm(forms.ModelForm): class TerminalForm(forms.ModelForm):
class Meta: class Meta:
model = Terminal model = Terminal
fields = ['name', 'ip', 'is_active', 'type', 'url', 'comment'] fields = ['name', 'remote_addr', 'type', 'url', 'comment']
help_texts = { help_texts = {
'url': 'Example: ssh://192.168.1.1:22 or http://jms.jumpserver.org, that user login' 'url': 'Example: ssh://192.168.1.1:22 or http://jms.jumpserver.org, that user login'
} }

View File

@ -8,20 +8,42 @@ from users.models import User
class Terminal(models.Model): class Terminal(models.Model):
TYPE_CHOICES = ( TYPE_CHOICES = (
('S', 'SSH Terminal'), ('SSH', 'SSH Terminal'),
('WT', 'Web Terminal') ('Web', 'Web Terminal')
) )
name = models.CharField(max_length=30, unique=True, verbose_name=_('Name')) name = models.CharField(max_length=30, unique=True, verbose_name=_('Name'))
ip = models.GenericIPAddressField(verbose_name=_('From ip')) remote_addr = models.GenericIPAddressField(verbose_name=_('Remote address'), blank=True, null=True)
is_active = models.BooleanField(default=False, verbose_name=_('Is active'))
is_bound_ip = models.BooleanField(default=False, verbose_name=_('Is bound ip'))
type = models.CharField(choices=TYPE_CHOICES, max_length=2, verbose_name=_('Terminal type')) type = models.CharField(choices=TYPE_CHOICES, max_length=2, verbose_name=_('Terminal type'))
user = models.OneToOneField(User, verbose_name='Application user', null=True)
url = models.CharField(max_length=100, verbose_name=_('URL to login')) url = models.CharField(max_length=100, verbose_name=_('URL to login'))
date_created = models.DateTimeField(auto_now_add=True) date_created = models.DateTimeField(auto_now_add=True)
comment = models.TextField(blank=True, verbose_name=_('Comment')) comment = models.TextField(blank=True, verbose_name=_('Comment'))
def is_valid(self): @property
return self.is_active and self.is_accepted def is_active(self):
if self.user and self.user.is_active:
return True
return False
@is_active.setter
def is_active(self, active):
if self.user:
self.user.is_active = active
self.user.save()
@property
def is_accepted(self):
if self.user:
return True
else:
return False
@is_accepted.setter
def is_accepted(self, accepted):
if accepted:
user = User.create_app_user(name=self.name, comment=self.comment)
self.user = user
self.save()
@property @property
def is_superuser(self): def is_superuser(self):
@ -31,9 +53,14 @@ class Terminal(models.Model):
def is_terminal(self): def is_terminal(self):
return True return True
def __unicode__(self):
active = 'Active' if self.user and self.user.is_active else 'Disabled'
return '%s: %s' % (self.name, active)
__str__ = __unicode__
class Meta: class Meta:
db_table = 'terminal' db_table = 'terminal'
ordering = ['is_active']
class TerminalHeatbeat(models.Model): class TerminalHeatbeat(models.Model):

View File

@ -4,6 +4,4 @@
from .group import * from .group import *
from .user import * from .user import *
from .access_key import * from .authentication import *

View File

@ -1,23 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import uuid
from django.db import models
from . import User
def get_uuid_string():
return uuid.uuid4().__str__()
class AccessKey(models.Model):
id = models.UUIDField(verbose_name='AccessKeyID', primary_key=True, default=get_uuid_string, editable=False)
secret = models.UUIDField(verbose_name='AccessKeySecret', default=get_uuid_string, editable=False)
user = models.ForeignKey(User, verbose_name='User')
def __unicode__(self):
return self.id
__str__ = __unicode__

View File

@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import uuid
from django.db import models
from django.utils.translation import ugettext_lazy as _
from rest_framework.authtoken.models import Token
from . import User
__all__ = ['AccessKey']
def get_uuid_string():
return uuid.uuid4().__str__()
class AccessKey(models.Model):
id = models.UUIDField(verbose_name='AccessKeyID', primary_key=True,
default=uuid.uuid4, editable=False)
secret = models.UUIDField(verbose_name='AccessKeySecret',
default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, verbose_name='User', related_name='access_key')
def get_id(self):
return str(self.id)
def get_secret(self):
return str(self.secret)
def __unicode__(self):
return str(self.id)
__str__ = __unicode__
class PrivateToken(Token):
"""Inherit from auth token, otherwise migration is boring"""
class Meta:
verbose_name = _('Private Token')

View File

@ -5,6 +5,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import models, IntegrityError from django.db import models, IntegrityError
from django.contrib.auth.models import Group
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.utils import signer, date_expired_default from common.utils import signer, date_expired_default
@ -13,8 +14,7 @@ from common.mixins import NoDeleteModelMixin
__all__ = ['UserGroup'] __all__ = ['UserGroup']
class UserGroup(NoDeleteModelMixin): class UserGroup(NoDeleteModelMixin, Group):
name = models.CharField(max_length=100, unique=True, verbose_name=_('Name'))
comment = models.TextField(blank=True, verbose_name=_('Comment')) comment = models.TextField(blank=True, verbose_name=_('Comment'))
date_created = models.DateTimeField(auto_now_add=True) date_created = models.DateTimeField(auto_now_add=True)
created_by = models.CharField(max_length=100) created_by = models.CharField(max_length=100)
@ -34,7 +34,7 @@ class UserGroup(NoDeleteModelMixin):
return True return True
class Meta: class Meta:
db_table = 'user_group' ordering = ['name']
@classmethod @classmethod
def initial(cls): def initial(cls):

View File

@ -15,7 +15,7 @@ from django.shortcuts import reverse
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from common.utils import signer, date_expired_default from common.utils import signer, date_expired_default
from . import UserGroup, AccessKey from . import UserGroup
__all__ = ['User'] __all__ = ['User']
@ -178,11 +178,12 @@ class User(AbstractUser):
@classmethod @classmethod
def create_app_user(cls, name, comment): def create_app_user(cls, name, comment):
domain_name = settings.DOMAIN_NAME or 'jumpserver.org' from . import AccessKey
domain_name = settings.CONFIG.DOMAIN_NAME or 'jumpserver.org'
app = cls.objects.create(username=name, name=name, email='%s@%s' % (name, domain_name), app = cls.objects.create(username=name, name=name, email='%s@%s' % (name, domain_name),
role='App', enable_otp=False, comment=comment, is_first_login=False, role='App', enable_otp=False, comment=comment, is_first_login=False,
created_by='System') created_by='System')
AccessKey.object.create(user=app) AccessKey.objects.create(user=app)
return app return app
@classmethod @classmethod
@ -207,7 +208,7 @@ class User(AbstractUser):
return super(User, self).delete() return super(User, self).delete()
class Meta: class Meta:
db_table = 'user' ordering = ['username']
#: Use this method initial user #: Use this method initial user
@classmethod @classmethod

View File

@ -7,10 +7,11 @@ from . import User, UserGroup
def initial_model(): def initial_model():
for cls in [User, UserGroup]: for cls in [User, UserGroup]:
if getattr(cls, 'initial'):
cls.initial() cls.initial()
def generate_fake(): def generate_fake():
for cls in [User, UserGroup]: for cls in [User, UserGroup]:
if getattr(cls, 'generate_fake'):
cls.generate_fake() cls.generate_fake()