Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow users to redact their own events #262

Merged
merged 3 commits into from
Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from synapse.api.constants import EventTypes, Membership, JoinRules
from synapse.api.errors import AuthError, Codes, SynapseError
from synapse.util.logutils import log_function
from synapse.types import UserID
from synapse.types import UserID, EventID

import logging

Expand Down Expand Up @@ -91,7 +91,7 @@ def check(self, event, auth_events):
self._check_power_levels(event, auth_events)

if event.type == EventTypes.Redaction:
self._check_redaction(event, auth_events)
self.check_redaction(event, auth_events)

logger.debug("Allowing! %s", event)
except AuthError as e:
Expand Down Expand Up @@ -541,16 +541,35 @@ def _can_send_event(self, event, auth_events):

return True

def _check_redaction(self, event, auth_events):
def check_redaction(self, event, auth_events):
"""Check whether the event sender is allowed to redact the target event.

Returns:
True if the the sender is allowed to redact the target event if the
target event was created by them.
False if the sender is allowed to redact the target event with no
further checks.

Raises:
AuthError if the event sender is definitely not allowed to redact
the target event.
"""
user_level = self._get_user_power_level(event.user_id, auth_events)

redact_level = self._get_named_level(auth_events, "redact", 50)

if user_level < redact_level:
raise AuthError(
403,
"You don't have permission to redact events"
)
if user_level > redact_level:
return False

redacter_domain = EventID.from_string(event.event_id).domain
redactee_domain = EventID.from_string(event.redacts).domain
if redacter_domain == redactee_domain:
return True

raise AuthError(
403,
"You don't have permission to redact events"
)

def _check_power_levels(self, event, auth_events):
user_list = event.content.get("users", {})
Expand Down
17 changes: 16 additions & 1 deletion synapse/handlers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from twisted.internet import defer

from synapse.api.errors import LimitExceededError, SynapseError
from synapse.api.errors import LimitExceededError, SynapseError, AuthError
from synapse.crypto.event_signing import add_hashes_and_signatures
from synapse.api.constants import Membership, EventTypes
from synapse.types import UserID, RoomAlias
Expand Down Expand Up @@ -146,6 +146,21 @@ def handle_new_client_event(self, event, context, extra_destinations=[],
returned_invite.signatures
)

if event.type == EventTypes.Redaction:
if self.auth.check_redaction(event, auth_events=context.current_state):
original_event = yield self.store.get_event(
event.redacts,
check_redacted=False,
get_prev_content=False,
allow_rejected=False,
allow_none=False
)
if event.user_id != original_event.user_id:
raise AuthError(
403,
"You don't have permission to redact events"
)

destinations = set(extra_destinations)
for k, s in context.current_state.items():
try:
Expand Down