2020-02-19 18:53:43 +00:00
|
|
|
# Copyright © 2017 Tom Hacohen
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, version 3.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-02-20 10:02:59 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2020-05-27 13:03:16 +00:00
|
|
|
from django.db import models, transaction
|
2020-02-19 18:53:43 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.validators import RegexValidator
|
2020-05-27 09:13:54 +00:00
|
|
|
from django.db.models import Q
|
2020-02-19 18:53:43 +00:00
|
|
|
from django.utils.functional import cached_property
|
2020-05-26 15:52:44 +00:00
|
|
|
from django.utils.crypto import get_random_string
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-07-13 14:08:36 +00:00
|
|
|
from . import app_settings
|
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-06-26 09:13:50 +00:00
|
|
|
UidValidator = RegexValidator(regex=r'^[a-zA-Z0-9\-_]{20,}$', message='Not a valid UID')
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Collection(models.Model):
|
2020-06-23 09:55:28 +00:00
|
|
|
main_item = models.ForeignKey('CollectionItem', related_name='parent', null=True, on_delete=models.SET_NULL)
|
2020-02-19 18:53:43 +00:00
|
|
|
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.uid
|
|
|
|
|
2020-04-16 08:28:49 +00:00
|
|
|
@cached_property
|
2020-06-23 09:55:28 +00:00
|
|
|
def uid(self):
|
|
|
|
return self.main_item.uid
|
2020-04-16 08:28:49 +00:00
|
|
|
|
2020-05-27 06:39:31 +00:00
|
|
|
@property
|
2020-03-12 19:02:27 +00:00
|
|
|
def content(self):
|
2020-04-16 08:28:49 +00:00
|
|
|
return self.main_item.content
|
2020-03-12 19:02:27 +00:00
|
|
|
|
2020-05-26 15:14:39 +00:00
|
|
|
@property
|
2020-05-27 07:09:45 +00:00
|
|
|
def etag(self):
|
2020-05-27 06:39:31 +00:00
|
|
|
return self.content.uid
|
2020-05-19 12:33:10 +00:00
|
|
|
|
|
|
|
@cached_property
|
2020-05-27 07:09:45 +00:00
|
|
|
def stoken(self):
|
2020-05-27 09:13:54 +00:00
|
|
|
stoken = Stoken.objects.filter(
|
|
|
|
Q(collectionitemrevision__item__collection=self) | Q(collectionmember__collection=self)
|
|
|
|
).order_by('id').last()
|
2020-04-16 13:33:16 +00:00
|
|
|
|
2020-05-27 09:13:54 +00:00
|
|
|
if stoken is None:
|
|
|
|
raise Exception('stoken is None. Should never happen')
|
|
|
|
|
|
|
|
return stoken.uid
|
2020-04-16 13:33:16 +00:00
|
|
|
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
class CollectionItem(models.Model):
|
2020-06-23 10:00:51 +00:00
|
|
|
uid = models.CharField(db_index=True, blank=False,
|
2020-05-26 13:26:57 +00:00
|
|
|
max_length=43, validators=[UidValidator])
|
2020-02-20 10:02:59 +00:00
|
|
|
collection = models.ForeignKey(Collection, related_name='items', on_delete=models.CASCADE)
|
2020-03-10 15:49:23 +00:00
|
|
|
version = models.PositiveSmallIntegerField()
|
2020-03-12 19:02:27 +00:00
|
|
|
encryptionKey = models.BinaryField(editable=True, blank=False, null=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('uid', 'collection')
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-03-12 19:02:27 +00:00
|
|
|
return '{} {}'.format(self.uid, self.collection.uid)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-02-20 11:56:16 +00:00
|
|
|
@cached_property
|
|
|
|
def content(self):
|
2020-02-20 20:41:39 +00:00
|
|
|
return self.revisions.get(current=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-05-26 15:14:39 +00:00
|
|
|
@property
|
2020-05-27 07:09:45 +00:00
|
|
|
def etag(self):
|
2020-05-27 06:39:31 +00:00
|
|
|
return self.content.uid
|
2020-04-19 14:32:40 +00:00
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-02-20 12:42:35 +00:00
|
|
|
def chunk_directory_path(instance, filename):
|
2020-07-13 14:08:36 +00:00
|
|
|
custom_func = app_settings.CHUNK_PATH_FUNC
|
|
|
|
if custom_func is not None:
|
|
|
|
return custom_func(instance, filename)
|
|
|
|
|
2020-08-04 12:19:45 +00:00
|
|
|
col = instance.collection
|
2020-02-20 12:42:35 +00:00
|
|
|
user_id = col.owner.id
|
2020-08-04 12:37:07 +00:00
|
|
|
uid_prefix = instance.uid[:2]
|
|
|
|
uid_rest = instance.uid[2:]
|
|
|
|
return Path('user_{}'.format(user_id), col.uid, uid_prefix, uid_rest)
|
2020-02-20 12:42:35 +00:00
|
|
|
|
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
class CollectionItemChunk(models.Model):
|
|
|
|
uid = models.CharField(db_index=True, blank=False, null=False,
|
2020-06-26 08:05:01 +00:00
|
|
|
max_length=60, validators=[UidValidator])
|
2020-08-04 12:19:45 +00:00
|
|
|
collection = models.ForeignKey(Collection, related_name='chunks', on_delete=models.CASCADE)
|
2020-02-20 12:48:19 +00:00
|
|
|
chunkFile = models.FileField(upload_to=chunk_directory_path, max_length=150, unique=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.uid
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-08-04 10:42:28 +00:00
|
|
|
class Meta:
|
2020-08-04 12:19:45 +00:00
|
|
|
unique_together = ('collection', 'uid')
|
2020-08-04 10:42:28 +00:00
|
|
|
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-05-26 15:52:44 +00:00
|
|
|
def generate_stoken_uid():
|
|
|
|
return get_random_string(32, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_')
|
|
|
|
|
|
|
|
|
|
|
|
class Stoken(models.Model):
|
|
|
|
uid = models.CharField(db_index=True, unique=True, blank=False, null=False, default=generate_stoken_uid,
|
2020-06-26 09:13:50 +00:00
|
|
|
max_length=43, validators=[UidValidator])
|
2020-05-26 15:52:44 +00:00
|
|
|
|
|
|
|
|
2020-02-20 20:41:39 +00:00
|
|
|
class CollectionItemRevision(models.Model):
|
2020-05-26 15:52:44 +00:00
|
|
|
stoken = models.OneToOneField(Stoken, on_delete=models.PROTECT)
|
2020-03-12 13:52:36 +00:00
|
|
|
uid = models.CharField(db_index=True, unique=True, blank=False, null=False,
|
2020-06-26 09:13:50 +00:00
|
|
|
max_length=43, validators=[UidValidator])
|
2020-02-20 20:41:39 +00:00
|
|
|
item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE)
|
2020-03-17 20:10:33 +00:00
|
|
|
meta = models.BinaryField(editable=True, blank=False, null=False)
|
2020-02-26 18:04:26 +00:00
|
|
|
current = models.BooleanField(db_index=True, default=True, null=True)
|
2020-02-26 14:55:47 +00:00
|
|
|
deleted = models.BooleanField(default=False)
|
2020-02-20 11:56:16 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('item', 'current')
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-03-12 13:52:36 +00:00
|
|
|
return '{} {} current={}'.format(self.uid, self.item.uid, self.current)
|
2020-02-26 18:38:07 +00:00
|
|
|
|
|
|
|
|
2020-04-15 14:35:51 +00:00
|
|
|
class RevisionChunkRelation(models.Model):
|
|
|
|
chunk = models.ForeignKey(CollectionItemChunk, related_name='revisions_relation', on_delete=models.CASCADE)
|
|
|
|
revision = models.ForeignKey(CollectionItemRevision, related_name='chunks_relation', on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id', )
|
|
|
|
|
|
|
|
|
2020-09-07 08:02:40 +00:00
|
|
|
class AccessLevels(models.IntegerChoices):
|
|
|
|
READ_ONLY = 0
|
|
|
|
ADMIN = 1
|
|
|
|
READ_WRITE = 2
|
2020-05-20 10:48:46 +00:00
|
|
|
|
2020-02-26 18:38:07 +00:00
|
|
|
|
2020-05-20 10:48:46 +00:00
|
|
|
class CollectionMember(models.Model):
|
2020-05-27 07:52:27 +00:00
|
|
|
stoken = models.OneToOneField(Stoken, on_delete=models.PROTECT, null=True)
|
2020-02-26 18:38:07 +00:00
|
|
|
collection = models.ForeignKey(Collection, related_name='members', on_delete=models.CASCADE)
|
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
|
|
encryptionKey = models.BinaryField(editable=True, blank=False, null=False)
|
2020-09-07 08:02:40 +00:00
|
|
|
accessLevel = models.IntegerField(
|
2020-02-26 18:38:07 +00:00
|
|
|
choices=AccessLevels.choices,
|
|
|
|
default=AccessLevels.READ_ONLY,
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'collection')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{} {}'.format(self.collection.uid, self.user)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-05-27 13:03:16 +00:00
|
|
|
def revoke(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
CollectionMemberRemoved.objects.update_or_create(
|
|
|
|
collection=self.collection, user=self.user,
|
|
|
|
defaults={
|
|
|
|
'stoken': Stoken.objects.create(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.delete()
|
|
|
|
|
|
|
|
|
|
|
|
class CollectionMemberRemoved(models.Model):
|
|
|
|
stoken = models.OneToOneField(Stoken, on_delete=models.PROTECT, null=True)
|
|
|
|
collection = models.ForeignKey(Collection, related_name='removed_members', on_delete=models.CASCADE)
|
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'collection')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{} {}'.format(self.collection.uid, self.user)
|
|
|
|
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-05-20 11:30:09 +00:00
|
|
|
class CollectionInvitation(models.Model):
|
|
|
|
uid = models.CharField(db_index=True, blank=False, null=False,
|
2020-06-26 09:13:50 +00:00
|
|
|
max_length=43, validators=[UidValidator])
|
2020-05-20 11:30:09 +00:00
|
|
|
version = models.PositiveSmallIntegerField(default=1)
|
|
|
|
fromMember = models.ForeignKey(CollectionMember, on_delete=models.CASCADE)
|
|
|
|
# FIXME: make sure to delete all invitations for the same collection once one is accepted
|
2020-05-20 12:15:24 +00:00
|
|
|
# Make sure to not allow invitations if already a member
|
2020-05-20 11:30:09 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='incoming_invitations', on_delete=models.CASCADE)
|
|
|
|
signedEncryptionKey = models.BinaryField(editable=False, blank=False, null=False)
|
2020-09-07 08:02:40 +00:00
|
|
|
accessLevel = models.IntegerField(
|
2020-05-20 11:30:09 +00:00
|
|
|
choices=AccessLevels.choices,
|
|
|
|
default=AccessLevels.READ_ONLY,
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'fromMember')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{} {}'.format(self.fromMember.collection.uid, self.user)
|
|
|
|
|
2020-05-20 12:15:24 +00:00
|
|
|
@cached_property
|
|
|
|
def collection(self):
|
|
|
|
return self.fromMember.collection
|
|
|
|
|
2020-05-20 11:30:09 +00:00
|
|
|
|
2020-05-14 10:43:49 +00:00
|
|
|
class UserInfo(models.Model):
|
|
|
|
owner = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)
|
|
|
|
version = models.PositiveSmallIntegerField(default=1)
|
2020-05-26 10:23:45 +00:00
|
|
|
loginPubkey = models.BinaryField(editable=True, blank=False, null=False)
|
2020-05-26 10:44:40 +00:00
|
|
|
pubkey = models.BinaryField(editable=True, blank=False, null=False)
|
2020-06-02 15:56:23 +00:00
|
|
|
encryptedContent = models.BinaryField(editable=True, blank=False, null=False)
|
2020-05-14 10:43:49 +00:00
|
|
|
salt = models.BinaryField(editable=True, blank=False, null=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "UserInfo<{}>".format(self.owner)
|