outposts: fix permissions not being updated when providers are modified

This commit is contained in:
Jens Langhammer 2020-09-14 15:41:02 +02:00
parent 7ea5c22b6c
commit 6a669992a8
2 changed files with 11 additions and 14 deletions

View File

@ -104,25 +104,22 @@ class Outpost(models.Model):
return datetime.fromtimestamp(value) return datetime.fromtimestamp(value)
return None return None
def _create_user(self) -> User: @property
"""Create user and assign permissions for all required objects""" def user(self) -> User:
"""Get/create user with access to all required objects"""
users = User.objects.filter(username=f"pb-outpost-{self.uuid.hex}")
if not users.exists():
user: User = User.objects.create(username=f"pb-outpost-{self.uuid.hex}") user: User = User.objects.create(username=f"pb-outpost-{self.uuid.hex}")
user.set_unusable_password() user.set_unusable_password()
user.save() user.save()
else:
user = users.first()
for model in self.get_required_objects(): for model in self.get_required_objects():
assign_perm( assign_perm(
f"{model._meta.app_label}.view_{model._meta.model_name}", user, model f"{model._meta.app_label}.view_{model._meta.model_name}", user, model
) )
return user return user
@property
def user(self) -> User:
"""Get/create user with access to all required objects"""
user = User.objects.filter(username=f"pb-outpost-{self.uuid.hex}")
if user.exists():
return user.first()
return self._create_user()
@property @property
def token(self) -> Token: def token(self) -> Token:
"""Get/create token for auto-generated user""" """Get/create token for auto-generated user"""

View File

@ -41,7 +41,7 @@ def outpost_send_update(model_class: str, model_pk: Any):
"""Send outpost update to all registered outposts, irregardless to which passbook """Send outpost update to all registered outposts, irregardless to which passbook
instance they are connected""" instance they are connected"""
model = path_to_class(model_class) model = path_to_class(model_class)
outpost_model: OutpostModel = model.objects.get(model_pk) outpost_model: OutpostModel = model.objects.get(pk=model_pk)
for outpost in outpost_model.outpost_set.all(): for outpost in outpost_model.outpost_set.all():
channel_layer = get_channel_layer() channel_layer = get_channel_layer()
for channel in outpost.channels: for channel in outpost.channels: