forked from inveniosoftware/invenio-users-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moderation: add self-action prevention
* The problem is that an admin could block his own account. With this change it is possible to prevent the admin from doing that. * Implement prevent_self_action decorator * Prevent self-block, deactivate, impersonate * Update tests for self-action prevention * closes <inveniosoftware/invenio-administration#203>
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# Copyright (C) 2024 KTH Royal Institute of Technology | ||
# | ||
# Invenio-Users-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Users decorators.""" | ||
from functools import wraps | ||
|
||
from flask import g | ||
from flask_resources import resource_requestctx | ||
from invenio_records_resources.resources.errors import PermissionDeniedError | ||
|
||
|
||
def prevent_self_action(): | ||
"""Decorator that prevents users from taking actions on their own account.""" | ||
|
||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
user_id = str(resource_requestctx.view_args["id"]) | ||
current_user_id = str(g.identity.id) | ||
|
||
if user_id == current_user_id: | ||
raise PermissionDeniedError() | ||
|
||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters