Add a decorator that checks for course-level permissions
This commit is contained in:
@@ -249,6 +249,28 @@ def require_level(level):
|
||||
return decorator
|
||||
|
||||
|
||||
def require_course_permission(permission):
|
||||
"""
|
||||
Decorator with argument that requires a specific permission of the requesting
|
||||
user. If the requirement is not satisfied, returns an
|
||||
HttpResponseForbidden (403).
|
||||
|
||||
Assumes that request is in args[0].
|
||||
Assumes that course_id is in kwargs['course_id'].
|
||||
"""
|
||||
def decorator(func): # pylint: disable=missing-docstring
|
||||
def wrapped(*args, **kwargs):
|
||||
request = args[0]
|
||||
course = get_course_by_id(CourseKey.from_string(kwargs['course_id']))
|
||||
|
||||
if request.user.has_perm(permission, course):
|
||||
return func(*args, **kwargs)
|
||||
else:
|
||||
return HttpResponseForbidden()
|
||||
return wrapped
|
||||
return decorator
|
||||
|
||||
|
||||
def require_sales_admin(func):
|
||||
"""
|
||||
Decorator for checking sales administrator access before executing an HTTP endpoint. This decorator
|
||||
|
||||
Reference in New Issue
Block a user