Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permission checks to markNotificationAsRead + deleteNotification #1654

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@
from sqlalchemy import func, and_, or_

from dataall.modules.notifications.db import notification_models as models
from dataall.base.db import paginate
from dataall.base.db import paginate, exceptions
from dataall.base.context import get_context
from functools import wraps


class NotificationAccess:
@staticmethod
def is_recipient(f):
@wraps(f)
def wrapper(*args, **kwds):
uri = kwds.get('notificationUri')
if not uri:
raise KeyError(f"{f.__name__} doesn't have parameter uri.")
context = get_context()
with context.db_engine.scoped_session() as session:
notification = session.query(models.Notification).get(uri)
if notification and (notification.recipient in context.groups + [context.username]):
return f(*args, **kwds)
else:
raise exceptions.UnauthorizedOperation(
action='UPDATE NOTIFICATION',
message=f'User {context.username} is not the recipient user/group of the notification {uri}',
)

return wrapper


class NotificationRepository:
Expand Down Expand Up @@ -88,13 +112,15 @@ def count_deleted_notifications(session, username, groups):
return int(count)

@staticmethod
@NotificationAccess.is_recipient
def read_notification(session, notificationUri):
notification = session.query(models.Notification).get(notificationUri)
notification.is_read = True
session.commit()
return True

@staticmethod
@NotificationAccess.is_recipient
def delete_notification(session, notificationUri):
notification = session.query(models.Notification).get(notificationUri)
if notification:
Expand Down
Loading