Example #1
0
def create_token(user):
    old_token_obj = Tokens.objects.filter(user=user).first()
    if old_token_obj:
        old_token_obj.delete()
    last_login = datetime.datetime.now()
    token = {
        'token': {
            'user': {
                'id': str(user.id),
                'name': user.name,
                'group': user.group.name,
                'role': user.group.role.name
            },
            'expires_at':
            timeutils.Changestr(last_login + datetime.timedelta(hours=2))
        }
    }
    X_Auth_Token = crypt.encodeToken(token)
    new_token_obj = Tokens(id=idutils.generate_id(),
                           user=user,
                           token=X_Auth_Token)
    new_token_obj.save()
    # update login time
    Users.objects.filter(id=user.id).update(**{'last_login': last_login})
    return token, X_Auth_Token
Example #2
0
class Tokens(models.Model):
    id = models.CharField(primary_key=True, default=idutils.generate_id(), max_length=32)
    user = models.ForeignKey(Users, on_delete=models.CASCADE)
    token = models.TextField()

    class Meta:
        db_table  = 'tokens'
Example #3
0
class Groups(models.Model):
    id = models.CharField(primary_key=True, default=idutils.generate_id(), max_length=32)
    name = models.CharField(max_length=32)
    role = models.ForeignKey(Roles, on_delete=models.CASCADE)
    metadata = models.TextField(default='{}')
    create_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table  = 'groups'
Example #4
0
class Ports(models.Model):
    id = models.CharField(primary_key=True,
                          default=idutils.generate_id(),
                          max_length=32)
    port = models.IntegerField(null=True)
    is_free = models.BooleanField(default=True)
    server = models.ForeignKey(Servers, on_delete=models.CASCADE)

    class Meta:
        db_table = 'ports'
Example #5
0
class Users(models.Model):
    id = models.CharField(primary_key=True, default=idutils.generate_id(), max_length=32)
    name = models.CharField(max_length=32)
    password = models.CharField(max_length=256)
    salt = models.CharField(max_length=16)
    email = models.EmailField(blank=True)
    group = models.ForeignKey(Groups, on_delete=models.CASCADE)
    metadata = models.TextField(default='{}')
    create_at = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(blank=True, null=True)

    class Meta:
        db_table  = 'users'
Example #6
0
class Dockers(models.Model):
    id = models.CharField(primary_key=True,
                          default=idutils.generate_id(),
                          max_length=32)
    ip = models.GenericIPAddressField(protocol='ipv4')
    name = models.CharField(max_length=256, null=True)
    status = models.CharField(max_length=16, null=True)
    description = models.TextField(null=True)
    owner = models.ForeignKey(Users, on_delete=models.CASCADE)  # the owner
    metadata = models.TextField(default='{}')
    create_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'dockers'
Example #7
0
class Servers(models.Model):
    id = models.CharField(primary_key=True,
                          default=idutils.generate_id(),
                          max_length=32)
    ip = models.GenericIPAddressField(protocol='ipv4')
    hostname = models.CharField(max_length=256, null=True)  # the server name
    salt = models.CharField(max_length=16)
    username = models.CharField(max_length=32)  # the system username
    password = models.CharField(max_length=256)
    status = models.CharField(max_length=16, null=True)
    description = models.TextField(null=True)
    version = models.CharField(max_length=32, null=True)
    cpu_number = models.IntegerField(null=True)
    memory_size = models.IntegerField(null=True)
    container_number = models.IntegerField(null=True)
    owner = models.ForeignKey(Users, on_delete=models.CASCADE)  # the owner
    metadata = models.TextField(default='{}')
    create_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'servers'
Example #8
0
def create_server(request, data):
    user_id, group, role = authutils.verify_request(request)
    data = data.get('server', None)
    if not data:
        LOG.error('params is wrong')
        raise DMException(error.PARAMS_ERROR)
    if not verify_data(data, ['ip', 'username', 'password'],
                       ['description', 'metadata']):
        LOG.error('params is wrong')
        raise DMException(error.PARAMS_ERROR)
    server = Servers.objects.filter(ip=data.get('ip')).first()
    if server:
        LOG.error('server exist')
        raise DMException(error.SERVER_EXIST)
    user = Users.objects.filter(id=user_id).first()
    kwargs = {}
    kwargs['id'] = idutils.generate_id()
    kwargs['ip'] = data['ip']
    kwargs['username'] = data['username']
    salt, password = crypt.encrypt(data['password'])
    kwargs['password'] = password
    kwargs['salt'] = salt
    kwargs['status'] = SERVER_STATUS.CREATING
    kwargs['owner'] = user
    if 'description' in data:
        kwargs['description'] = data['description']
    if 'metadata' in data:
        kwargs['metadata'] = json.dumps(data['metadata'])
    server = Servers(**kwargs)
    server.save()
    return {
        'server': {
            'id': server.id,
            'ip': server.ip,
            'status': server.status,
            'username': server.username,
            'owner': server.owner.id
        }
    }
Example #9
0
def create_user(request, data):
    user_id, group, role = authutils.verify_request(request)
    if role != ROLE.ADMIN:
        LOG.error('role is not admin')
        raise DMException(error.ADMIN_REQUIRE)
    data = data.get('user', None)
    if not data:
        LOG.error('params is wrong')
        raise DMException(error.PARAMS_ERROR)
    if not verify_data(data,['name','password','group'], ['email','metadata']):
        LOG.error('params is wrong')
        raise DMException(error.PARAMS_ERROR)
    if Users.objects.filter(name=data.get('name')).first():
        LOG.error('user already exist')
        raise DMException(error.USER_EXISTED)
    group = Groups.objects.filter(name=data.get('group')).first()
    if not group:
        LOG.error('group not exist')
        raise DMException(error.GROUP_NOTEXISTED)
    if group.role.name == ROLE.ADMIN:
        LOG.error('admin cannot be created')
        raise DMException(error.ADMIN_CANNOTCREATE)
    kwargs = {}
    kwargs['id'] = idutils.generate_id()
    kwargs['name'] = data['name']
    salt, password = crypt.encrypt(data['password'])
    kwargs['salt'] = salt
    kwargs['password'] = password
    if 'email' in data:
        if not verify_email(data['email']):
            LOG.error('email is wrong')
            raise DMException(error.PARAMS_ERROR)
        kwargs['email'] = data['email']
    kwargs['group'] = group
    if 'metadata' in data:
        kwargs['metadata'] = json.dumps(data['metadata'])
    user = Users(**kwargs)
    user.save()
    return {'user':{'id':user.id,'name':user.name,'group':user.group.name,'role':user.group.role.name}}