models.py 3.06 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
3
4
5
6
from django.conf import settings
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save
Lukas Burgey's avatar
Lukas Burgey committed
7
8
9


class User(AbstractUser):
10
11
12
13
14
15
16
17
18
19
    TYPE_CHOICES = (
            ('apiclient', 'API-Client'),
            ('oidcuser', 'OIDC User'),
            ('admin', 'Admin'),
            )
    user_type = models.CharField(
            max_length=20,
            choices=TYPE_CHOICES,
            default='oidcuser',
            )
Lukas Burgey's avatar
Lukas Burgey committed
20
21
22
23
    sub = models.CharField(max_length=150, blank=True, null=True)
    password = models.CharField(max_length=150, blank=True, null=True)


24
25
26
27
28
29
30
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        if instance.user_type == 'apiclient':
            Token.objects.create(user=instance)


Lukas Burgey's avatar
Lukas Burgey committed
31
32
33
34
35
36
37
38
39
40
41
42
def construct_user(user_info):
    return User(
            sub=user_info['sub'],
            name=user_info['name'],
            first_name=user_info['given_name'],
            last_name=user_info['family_name'],
            email=user_info['email'],
            username=user_info['email'],
            )


class Site(models.Model):
43
44
45
46
47
    client = models.OneToOneField(
            User,
            related_name='site',
            blank=True,
            null=True)
Lukas Burgey's avatar
Lukas Burgey committed
48
49
50
51
52
53
54
55
56
57
58
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)

    def __str__(self):
        return self.name


class Service(models.Model):
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)
    site = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
59
60
            Site,
            related_name='services')
Lukas Burgey's avatar
Lukas Burgey committed
61
    groups = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
62
63
64
            Group,
            related_name='services',
            blank=True)
Lukas Burgey's avatar
Lukas Burgey committed
65
66
67
68
69
70
71
72
73

    def __str__(self):
        return self.name + '@' + self.site.name


class SSHPublicKey(models.Model):
    name = models.CharField(max_length=150, unique=True)
    key = models.TextField(max_length=1000)
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
74
75
            User,
            related_name='ssh_keys')
Lukas Burgey's avatar
Lukas Burgey committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    def __str__(self):
        return self.name


class State(object):
    def __init__(self, user):

        if user.is_authenticated:
            self.services = Service.objects.filter(
                    groups__user=user
                    )
        else:
            self.services = []
Lukas Burgey's avatar
Lukas Burgey committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111


class Deployment(models.Model):
    user = models.ForeignKey(
            User,
            related_name='deployments',
            on_delete=models.CASCADE,
            )
    service = models.ForeignKey(
            Service,
            related_name='deployments',
            on_delete=models.CASCADE,
            )
    # SET_NULL: we allow credentials to be deleted after deployment
    ssh_keys = models.ManyToManyField(
            SSHPublicKey,
            related_name='deployments',
            blank=True,
            )

    def __str__(self):
        return str(self.user) + '@' + str(self.service)