~homeworkprod/byceps

38df9baaa80057758e02417862bfcf91bd962039 — Jochen Kupperschmidt 9 months ago ea7e3ea
Move `permission_required` decorator to `byceps.util.views`
34 files changed, 65 insertions(+), 83 deletions(-)

M byceps/blueprints/admin/attendance/views.py
M byceps/blueprints/admin/authorization/views.py
M byceps/blueprints/admin/board/views.py
M byceps/blueprints/admin/brand/views.py
M byceps/blueprints/admin/consent/views.py
M byceps/blueprints/admin/dashboard/views.py
M byceps/blueprints/admin/jobs/views.py
M byceps/blueprints/admin/more/views.py
M byceps/blueprints/admin/news/views.py
M byceps/blueprints/admin/newsletter/views.py
M byceps/blueprints/admin/orga/views.py
M byceps/blueprints/admin/orga_presence/views.py
M byceps/blueprints/admin/orga_team/views.py
M byceps/blueprints/admin/party/views.py
M byceps/blueprints/admin/seating/views.py
M byceps/blueprints/admin/shop/article/views.py
M byceps/blueprints/admin/shop/email/views.py
M byceps/blueprints/admin/shop/order/views.py
M byceps/blueprints/admin/shop/shipping/views.py
M byceps/blueprints/admin/shop/shop/views.py
M byceps/blueprints/admin/shop/storefront/views.py
M byceps/blueprints/admin/site/views.py
M byceps/blueprints/admin/snippet/views.py
M byceps/blueprints/admin/terms/views.py
M byceps/blueprints/admin/ticketing/checkin/views.py
M byceps/blueprints/admin/ticketing/views.py
M byceps/blueprints/admin/tourney/views.py
M byceps/blueprints/admin/user/views.py
M byceps/blueprints/admin/user_badge/views.py
M byceps/blueprints/admin/webhook/views.py
D byceps/blueprints/common/authorization/decorators.py
M byceps/blueprints/site/board/views_posting.py
M byceps/blueprints/site/board/views_topic.py
M byceps/util/views.py
M byceps/blueprints/admin/attendance/views.py => byceps/blueprints/admin/attendance/views.py +1 -1
@@ 14,8 14,8 @@ from ....services.ticketing import attendance_service
from ....services.user import service as user_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from ..core.authorization import AdminPermission

M byceps/blueprints/admin/authorization/views.py => byceps/blueprints/admin/authorization/views.py +1 -1
@@ 12,8 12,8 @@ from ....services.authorization import service as authorization_service
from ....services.user import service as user_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import RolePermission

M byceps/blueprints/admin/board/views.py => byceps/blueprints/admin/board/views.py +1 -2
@@ 22,9 22,8 @@ from ....services.brand import service as brand_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to, respond_no_content
from ....util.views import permission_required, redirect_to, respond_no_content

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry
from ...site.board.authorization import BoardPermission


M byceps/blueprints/admin/brand/views.py => byceps/blueprints/admin/brand/views.py +1 -2
@@ 18,9 18,8 @@ from ....services.party import service as party_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to
from ....util.views import permission_required, redirect_to

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import BrandPermission

M byceps/blueprints/admin/consent/views.py => byceps/blueprints/admin/consent/views.py +1 -1
@@ 9,8 9,8 @@ byceps.blueprints.admin.consent.views
from ....services.consent import subject_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import ConsentPermission

M byceps/blueprints/admin/dashboard/views.py => byceps/blueprints/admin/dashboard/views.py +1 -1
@@ 31,8 31,8 @@ from ....services.ticketing import ticket_service
from ....services.user import stats_service as user_stats_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from ..user.service import get_users_created_since

M byceps/blueprints/admin/jobs/views.py => byceps/blueprints/admin/jobs/views.py +1 -1
@@ 8,8 8,8 @@ byceps.blueprints.admin.jobs.views

from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import JobsPermission

M byceps/blueprints/admin/more/views.py => byceps/blueprints/admin/more/views.py +1 -2
@@ 13,8 13,7 @@ from ....services.party import service as party_service
from ....services.site import service as site_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated

from ...common.authorization.decorators import permission_required
from ....util.views import permission_required

from ..core.authorization import AdminPermission


M byceps/blueprints/admin/news/views.py => byceps/blueprints/admin/news/views.py +1 -2
@@ 25,9 25,8 @@ from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.iterables import pairwise
from ....util.views import redirect_to, respond_no_content
from ....util.views import permission_required, redirect_to, respond_no_content

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import NewsChannelPermission, NewsItemPermission

M byceps/blueprints/admin/newsletter/views.py => byceps/blueprints/admin/newsletter/views.py +1 -2
@@ 16,9 16,8 @@ from ....services.newsletter.transfer.models import List
from ....services.newsletter.types import SubscriptionState
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import jsonified, textified
from ....util.views import jsonified, permission_required, textified

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import NewsletterPermission

M byceps/blueprints/admin/orga/views.py => byceps/blueprints/admin/orga/views.py +6 -2
@@ 19,9 19,13 @@ from ....util.export import serialize_to_csv
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to, respond_no_content, textified
from ....util.views import (
    permission_required,
    redirect_to,
    respond_no_content,
    textified,
)

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from ..orga_team.authorization import OrgaTeamPermission

M byceps/blueprints/admin/orga_presence/views.py => byceps/blueprints/admin/orga_presence/views.py +1 -1
@@ 22,8 22,8 @@ from ....services.party import service as party_service
from ....services.user.transfer.models import User
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import OrgaPresencePermission

M byceps/blueprints/admin/orga_team/views.py => byceps/blueprints/admin/orga_team/views.py +1 -2
@@ 14,9 14,8 @@ from ....services.user import service as user_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to, respond_no_content
from ....util.views import permission_required, redirect_to, respond_no_content

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import OrgaTeamPermission

M byceps/blueprints/admin/party/views.py => byceps/blueprints/admin/party/views.py +1 -2
@@ 25,9 25,8 @@ from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.iterables import partition
from ....util.templatefilters import local_tz_to_utc, utc_to_local_tz
from ....util.views import redirect_to
from ....util.views import permission_required, redirect_to

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import PartyPermission

M byceps/blueprints/admin/seating/views.py => byceps/blueprints/admin/seating/views.py +1 -1
@@ 19,8 19,8 @@ from ....services.ticketing import (
)
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import SeatingPermission

M byceps/blueprints/admin/shop/article/views.py => byceps/blueprints/admin/shop/article/views.py +1 -2
@@ 28,9 28,8 @@ from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_error, flash_success
from .....util.framework.templating import templated
from .....util.templatefilters import local_tz_to_utc, utc_to_local_tz
from .....util.views import redirect_to, respond_no_content
from .....util.views import permission_required, redirect_to, respond_no_content

from ....common.authorization.decorators import permission_required
from ....common.authorization.registry import permission_registry

from .authorization import ShopArticlePermission

M byceps/blueprints/admin/shop/email/views.py => byceps/blueprints/admin/shop/email/views.py +1 -1
@@ 18,8 18,8 @@ from .....services.shop.order.email import (
from .....services.shop.shop import service as shop_service
from .....util.framework.blueprint import create_blueprint
from .....util.framework.templating import templated
from .....util.views import permission_required

from ....common.authorization.decorators import permission_required
from ....common.authorization.registry import permission_registry

from ..shop.authorization import ShopPermission

M byceps/blueprints/admin/shop/order/views.py => byceps/blueprints/admin/shop/order/views.py +1 -2
@@ 23,9 23,8 @@ from .....signals import shop as shop_signals
from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_error, flash_notice, flash_success
from .....util.framework.templating import templated
from .....util.views import redirect_to, respond_no_content
from .....util.views import permission_required, redirect_to, respond_no_content

from ....common.authorization.decorators import permission_required
from ....common.authorization.registry import permission_registry

from ..shop.authorization import ShopPermission

M byceps/blueprints/admin/shop/shipping/views.py => byceps/blueprints/admin/shop/shipping/views.py +1 -2
@@ 13,8 13,7 @@ from .....services.shop.shipping import service as shipping_service
from .....services.shop.shop import service as shop_service
from .....util.framework.blueprint import create_blueprint
from .....util.framework.templating import templated

from ....common.authorization.decorators import permission_required
from .....util.views import permission_required

from ..order.authorization import ShopOrderPermission


M byceps/blueprints/admin/shop/shop/views.py => byceps/blueprints/admin/shop/shop/views.py +5 -2
@@ 15,9 15,12 @@ from .....services.shop.shop import service as shop_service
from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_success
from .....util.framework.templating import templated
from .....util.views import redirect_to, respond_no_content_with_location
from .....util.views import (
    permission_required,
    redirect_to,
    respond_no_content_with_location,
)

from ....common.authorization.decorators import permission_required
from ....common.authorization.registry import permission_registry

from .authorization import ShopPermission

M byceps/blueprints/admin/shop/storefront/views.py => byceps/blueprints/admin/shop/storefront/views.py +1 -3
@@ 16,9 16,7 @@ from .....services.shop.storefront import service as storefront_service
from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_error, flash_success
from .....util.framework.templating import templated
from .....util.views import redirect_to

from ....common.authorization.decorators import permission_required
from .....util.views import permission_required, redirect_to

from ..shop.authorization import ShopPermission


M byceps/blueprints/admin/site/views.py => byceps/blueprints/admin/site/views.py +1 -2
@@ 26,9 26,8 @@ from ....services.site.transfer.models import Site, SiteWithBrand
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to
from ....util.views import permission_required, redirect_to

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import SitePermission

M byceps/blueprints/admin/snippet/views.py => byceps/blueprints/admin/snippet/views.py +1 -1
@@ 20,12 20,12 @@ from ....util.framework.flash import flash_error, flash_success
from ....util.iterables import pairwise
from ....util.framework.templating import templated
from ....util.views import (
    permission_required,
    redirect_to,
    respond_no_content,
    respond_no_content_with_location,
)

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry
from ...site.snippet.templating import get_snippet_context


M byceps/blueprints/admin/terms/views.py => byceps/blueprints/admin/terms/views.py +1 -1
@@ 14,8 14,8 @@ from ....services.terms import version_service as terms_version_service
from ....services.user import service as user_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import TermsPermission

M byceps/blueprints/admin/ticketing/checkin/views.py => byceps/blueprints/admin/ticketing/checkin/views.py +1 -2
@@ 14,8 14,7 @@ from .....services.party import service as party_service
from .....services.ticketing import ticket_service
from .....util.framework.blueprint import create_blueprint
from .....util.framework.templating import templated

from ....common.authorization.decorators import permission_required
from .....util.views import permission_required

from ...ticketing.authorization import TicketingPermission
from ...user import service as user_blueprint_service

M byceps/blueprints/admin/ticketing/views.py => byceps/blueprints/admin/ticketing/views.py +1 -2
@@ 21,9 21,8 @@ from ....signals import ticketing as ticketing_signals
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_notice, flash_success
from ....util.framework.templating import templated
from ....util.views import respond_no_content
from ....util.views import permission_required, respond_no_content

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import TicketingPermission

M byceps/blueprints/admin/tourney/views.py => byceps/blueprints/admin/tourney/views.py +1 -2
@@ 13,9 13,8 @@ from ....services.tourney import category_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to, respond_no_content
from ....util.views import permission_required, redirect_to, respond_no_content

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import TourneyCategoryPermission

M byceps/blueprints/admin/user/views.py => byceps/blueprints/admin/user/views.py +1 -2
@@ 30,9 30,8 @@ from ....signals import user as user_signals
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to, respond_no_content
from ....util.views import permission_required, redirect_to, respond_no_content

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from ..authorization.authorization import RolePermission

M byceps/blueprints/admin/user_badge/views.py => byceps/blueprints/admin/user_badge/views.py +1 -2
@@ 15,9 15,8 @@ from ....signals import user_badge as user_badge_signals
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to
from ....util.views import permission_required, redirect_to

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import UserBadgePermission

M byceps/blueprints/admin/webhook/views.py => byceps/blueprints/admin/webhook/views.py +1 -1
@@ 9,8 9,8 @@ byceps.blueprints.admin.webhook.views
from ....services.webhooks import service as webhook_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ...common.authorization.decorators import permission_required
from ...common.authorization.registry import permission_registry

from .authorization import WebhookPermission

D byceps/blueprints/common/authorization/decorators.py => byceps/blueprints/common/authorization/decorators.py +0 -26
@@ 1,26 0,0 @@
"""
byceps.blueprints.common.authorization.decorators
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from functools import wraps

from flask import abort, g


def permission_required(permission):
    """Ensure the current user has the given permission."""

    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if not g.current_user.has_permission(permission):
                abort(403)
            return func(*args, **kwargs)

        return wrapper

    return decorator

M byceps/blueprints/site/board/views_posting.py => byceps/blueprints/site/board/views_posting.py +1 -3
@@ 20,9 20,7 @@ from ....services.user import service as user_service
from ....signals import board as board_signals
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import respond_no_content_with_location

from ...common.authorization.decorators import permission_required
from ....util.views import permission_required, respond_no_content_with_location

from .authorization import BoardPermission, BoardPostingPermission
from .blueprint import blueprint

M byceps/blueprints/site/board/views_topic.py => byceps/blueprints/site/board/views_topic.py +1 -3
@@ 22,9 22,7 @@ from ....services.user import service as user_service
from ....signals import board as board_signals
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import respond_no_content_with_location

from ...common.authorization.decorators import permission_required
from ....util.views import permission_required, respond_no_content_with_location

from .authorization import BoardPermission, BoardTopicPermission
from .blueprint import blueprint

M byceps/util/views.py => byceps/util/views.py +24 -1
@@ 10,7 10,15 @@ View utilities.

from functools import wraps

from flask import g, jsonify, redirect, Response, stream_with_context, url_for
from flask import (
    abort,
    g,
    jsonify,
    redirect,
    Response,
    stream_with_context,
    url_for,
)

from .framework.flash import flash_notice



@@ 28,6 36,21 @@ def login_required(func):
    return wrapper


def permission_required(permission):
    """Ensure the current user has the given permission."""

    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if not g.current_user.has_permission(permission):
                abort(403)
            return func(*args, **kwargs)

        return wrapper

    return decorator


def create_empty_json_response(status):
    """Create a JSON response with the given status code and an empty
    object as its content.