report: reports.Report = fastapi.Depends(get_report_from_path),
user: users.User = fastapi.Depends(require_current_user),
) -> fastapi.Response:
+ # Check for permissions
+ if not await report.has_perm(user, accept=data.accept):
+ raise fastapi.HTTPError(403, "You don't have permission to change this report")
+
+ # Close the report
await report.close(
closed_by = user,
accept = data.accept,
# Accepted?
accepted : bool = False
+ # Permissions check
+ async def has_perm(self, user, accept=False):
+ """
+ Returns True if the given user has permission to make changes to this report.
+ """
+ # Admins and moderators have all permissions
+ if user.is_admin() or user.is_moderator():
+ return True
+
+ # The owners have some permissions...
+ if self.reported_by == user.uid:
+ # Users cannot accept their own reports
+ if accept:
+ return False
+
+ # Otherwise users can manage their own reports
+ return True
+
+ # Everybody else has no permissions
+ return False
+
# Close!
async def close(self, closed_by=None, accept=True, update_stats=True):
Called when a moderator has made a decision
"""
# Prevent a report from being closed again
- if self.is_closed()
+ if self.is_closed():
raise RuntimeError("Report %s has already been closed" % self)
- # XXX Check for permissions
+ # Check for permissions
+ if not await self.has_perm(closed_by, accept=accept):
+ raise RuntimeError("%s does not have permission to change report %s" % (closed_by, self.id))
# Only the the user ID in the database
if isinstance(closed_by, users.User):
import email.message
import email.utils
+import functools
import ldap
import logging
import smtplib
"""
return self._get_string("mail")
+ @functools.cache
+ def is_admin(self):
+ """
+ Returns True if this user is an admin
+ """
+ return self in self.backend.users.admins
+
+ @functools.cache
+ def is_moderator(self):
+ """
+ Returns True if this user is a moderator
+ """
+ return self in self.backend.users.moderators
+
def sendmail(self, message, sender=None, headers=None):
"""
Sends the given message to this user