+
+
+def get_workbench(func):
+ """Decorator, passing in a workbench as kwarg which is cleaned up afterwards"""
+
+ @wraps(func)
+ def new_func(*args, **kwargs):
+ with mgg.workbench_manager.create() as workbench:
+ return func(*args, workbench=workbench, **kwargs)
+
+ return new_func
+
+
+def allow_registration(controller):
+ """ Decorator for if registration is enabled"""
+ @wraps(controller)
+ def wrapper(request, *args, **kwargs):
+ if not mgg.app_config["allow_registration"]:
+ messages.add_message(
+ request,
+ messages.WARNING,
+ _('Sorry, registration is disabled on this instance.'))
+ return redirect(request, "index")
+
+ return controller(request, *args, **kwargs)
+
+ return wrapper
+
+def allow_reporting(controller):
+ """ Decorator for if reporting is enabled"""
+ @wraps(controller)
+ def wrapper(request, *args, **kwargs):
+ if not mgg.app_config["allow_reporting"]:
+ messages.add_message(
+ request,
+ messages.WARNING,
+ _('Sorry, reporting is disabled on this instance.'))
+ return redirect(request, 'index')
+
+ return controller(request, *args, **kwargs)
+
+ return wrapper
+
+def get_optional_media_comment_by_id(controller):
+ """
+ Pass in a Comment based off of a url component. Because of this decor-
+ -ator's use in filing Reports, it has two valid outcomes.
+
+ :returns The view function being wrapped with kwarg `comment` set to
+ the Comment who's id is in the URL. If there is a
+ comment id in the URL and if it is valid.
+ :returns The view function being wrapped with kwarg `comment` set to
+ None. If there is no comment id in the URL.
+ :returns A 404 Error page, if there is a comment if in the URL and it
+ is invalid.
+ """
+ @wraps(controller)
+ def wrapper(request, *args, **kwargs):
+ if 'comment' in request.matchdict:
+ comment = Comment.query.filter_by(
+ id=request.matchdict['comment']
+ ).first()
+
+ if comment is None:
+ return render_404(request)
+
+ return controller(request, comment=comment, *args, **kwargs)
+ else:
+ return controller(request, comment=None, *args, **kwargs)
+ return wrapper
+
+
+def auth_enabled(controller):
+ """Decorator for if an auth plugin is enabled"""
+ @wraps(controller)
+ def wrapper(request, *args, **kwargs):
+ if not mgg.app.auth:
+ messages.add_message(
+ request,
+ messages.WARNING,
+ _('Sorry, authentication is disabled on this instance.'))
+ return redirect(request, 'index')
+
+ return controller(request, *args, **kwargs)
+
+ return wrapper
+
+def require_admin_or_moderator_login(controller):
+ """
+ Require a login from an administrator or a moderator.
+ """
+ @wraps(controller)
+ def new_controller_func(request, *args, **kwargs):
+ if request.user and \
+ not (request.user.has_privilege(u'admin')
+ or request.user.has_privilege(u'moderator')):
+
+ raise Forbidden()
+ elif not request.user:
+ next_url = urljoin(
+ request.urlgen('mediagoblin.auth.login',
+ qualified=True),
+ request.url)
+
+ return redirect(request, 'mediagoblin.auth.login',
+ next=next_url)
+
+ return controller(request, *args, **kwargs)
+
+ return new_controller_func
+
+
+
+def oauth_required(controller):
+ """ Used to wrap API endpoints where oauth is required """
+ @wraps(controller)
+ def wrapper(request, *args, **kwargs):
+ data = request.headers
+ authorization = decode_authorization_header(data)
+
+ if authorization == dict():
+ error = "Missing required parameter."
+ return json_response({"error": error}, status=400)
+
+
+ request_validator = GMGRequestValidator()
+ resource_endpoint = ResourceEndpoint(request_validator)
+ valid, r = resource_endpoint.validate_protected_resource_request(
+ uri=request.url,
+ http_method=request.method,
+ body=request.data,
+ headers=dict(request.headers),
+ )
+
+ if not valid:
+ error = "Invalid oauth parameter."
+ return json_response({"error": error}, status=400)
+
+ # Fill user if not already
+ token = authorization[u"oauth_token"]
+ request.access_token = AccessToken.query.filter_by(token=token).first()
+ if request.access_token is not None and request.user is None:
+ user_id = request.access_token.actor
+ request.user = LocalUser.query.filter_by(id=user_id).first()
+
+ return controller(request, *args, **kwargs)
+
+ return wrapper