Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
9c60bd63
Commit
9c60bd63
authored
5 years ago
by
Nimisha Asthagiri
Browse files
Options
Downloads
Patches
Plain Diff
Django2: student admin views permission fix
parent
18c5333a
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/djangoapps/student/admin.py
+48
-29
48 additions, 29 deletions
common/djangoapps/student/admin.py
common/djangoapps/student/tests/test_admin_views.py
+6
-0
6 additions, 0 deletions
common/djangoapps/student/tests/test_admin_views.py
with
54 additions
and
29 deletions
common/djangoapps/student/admin.py
+
48
−
29
View file @
9c60bd63
...
...
@@ -54,6 +54,32 @@ User = get_user_model() # pylint:disable=invalid-name
COURSE_ENROLLMENT_ADMIN_SWITCH
=
WaffleSwitch
(
STUDENT_WAFFLE_NAMESPACE
,
'
courseenrollment_admin
'
)
class
_Check
(
object
):
"""
A method decorator that pre-emptively returns false if a feature is disabled.
Otherwise, it returns the return value of the decorated method.
To use, add this decorator above a method and pass in a function that returns
a boolean indicating whether the feature is enabled.
Example:
@_Check.is_enabled(FEATURE_TOGGLE.is_enabled)
"""
@classmethod
def
is_enabled
(
cls
,
is_enabled_func
):
"""
See above docstring.
"""
def
inner
(
func
):
@wraps
(
func
)
def
decorator
(
*
args
,
**
kwargs
):
if
not
is_enabled_func
():
return
False
return
func
(
*
args
,
**
kwargs
)
return
decorator
return
inner
class
CourseAccessRoleForm
(
forms
.
ModelForm
):
"""
Form for adding new Course Access Roles view the Django Admin Panel.
"""
...
...
@@ -238,37 +264,40 @@ class CourseEnrollmentAdmin(admin.ModelAdmin):
def
queryset
(
self
,
request
):
return
super
(
CourseEnrollmentAdmin
,
self
).
queryset
(
request
).
select_related
(
'
user
'
)
def
has_permission
(
self
,
request
,
method
):
@_Check.is_enabled
(
COURSE_ENROLLMENT_ADMIN_SWITCH
.
is_enabled
)
def
has_view_permission
(
self
,
request
,
obj
=
None
):
"""
Returns True if
the given admin method is allowed
.
Returns True if
CourseEnrollment objects can be viewed via the admin view
.
"""
if
COURSE_ENROLLMENT_ADMIN_SWITCH
.
is_enabled
():
return
getattr
(
super
(
CourseEnrollmentAdmin
,
self
),
method
)(
request
)
return
False
return
super
(
CourseEnrollmentAdmin
,
self
).
has_view_permission
(
request
,
obj
)
# pylint: disable=no-member
@_Check.is_enabled
(
COURSE_ENROLLMENT_ADMIN_SWITCH
.
is_enabled
)
def
has_add_permission
(
self
,
request
):
"""
Returns True if CourseEnrollment objects can be added via the admin view.
"""
return
s
elf
.
has_permission
(
request
,
'
has_add_permission
'
)
return
s
uper
(
CourseEnrollmentAdmin
,
self
).
has_add_permission
(
request
)
@_Check.is_enabled
(
COURSE_ENROLLMENT_ADMIN_SWITCH
.
is_enabled
)
def
has_change_permission
(
self
,
request
,
obj
=
None
):
"""
Returns True if CourseEnrollment objects can be modified via the admin view.
"""
return
s
elf
.
has_permission
(
request
,
'
has_change_permission
'
)
return
s
uper
(
CourseEnrollmentAdmin
,
self
).
has_change_permission
(
request
,
obj
)
@_Check.is_enabled
(
COURSE_ENROLLMENT_ADMIN_SWITCH
.
is_enabled
)
def
has_delete_permission
(
self
,
request
,
obj
=
None
):
"""
Returns True if CourseEnrollment objects can be deleted via the admin view.
"""
return
s
elf
.
has_permission
(
request
,
'
has_delete_permission
'
)
return
s
uper
(
CourseEnrollmentAdmin
,
self
).
has_delete_permission
(
request
,
obj
)
@_Check.is_enabled
(
COURSE_ENROLLMENT_ADMIN_SWITCH
.
is_enabled
)
def
has_module_permission
(
self
,
request
):
"""
Returns True if links to the CourseEnrollment admin view can be displayed.
"""
return
s
elf
.
has_permission
(
request
,
'
has_module_permission
'
)
return
s
uper
(
CourseEnrollmentAdmin
,
self
).
has_module_permission
(
request
)
class
UserProfileInline
(
admin
.
StackedInline
):
...
...
@@ -351,45 +380,35 @@ class LoginFailuresAdmin(admin.ModelAdmin):
actions
=
[
'
unlock_student_accounts
'
]
change_form_template
=
'
admin/student/loginfailures/change_form_template.html
'
class
_Feature
(
object
):
@_Check.is_enabled
(
LoginFailures
.
is_feature_enabled
)
def
has_module_permission
(
self
,
request
):
"""
Inner feature class to implement decorator
.
Only enabled if feature is enabled
.
"""
@classmethod
def
is_enabled
(
cls
,
func
):
"""
Check if feature is enabled.
"""
@wraps
(
func
)
def
decorator
(
*
args
,
**
kwargs
):
"""
Decorator class to return
"""
if
not
LoginFailures
.
is_feature_enabled
():
return
False
return
func
(
*
args
,
**
kwargs
)
return
decorator
return
super
(
LoginFailuresAdmin
,
self
).
has_module_permission
(
request
)
@_
Feat
ure.is_enabled
def
has_
module
_permission
(
self
,
request
):
@_
Check.is_enabled
(
LoginFail
ure
s
.
is_
feature_
enabled
)
def
has_
view
_permission
(
self
,
request
,
obj
=
None
):
"""
Only enabled if feature is enabled.
"""
return
super
(
LoginFailuresAdmin
,
self
).
has_
module
_permission
(
request
)
return
super
(
LoginFailuresAdmin
,
self
).
has_
view
_permission
(
request
,
obj
)
# pylint: disable=no-member
@_
Feat
ure.is_enabled
@_
Check.is_enabled
(
LoginFail
ure
s
.
is_
feature_
enabled
)
def
has_delete_permission
(
self
,
request
,
obj
=
None
):
"""
Only enabled if feature is enabled.
"""
return
super
(
LoginFailuresAdmin
,
self
).
has_delete_permission
(
request
,
obj
)
@_
Feat
ure.is_enabled
@_
Check.is_enabled
(
LoginFail
ure
s
.
is_
feature_
enabled
)
def
has_change_permission
(
self
,
request
,
obj
=
None
):
"""
Only enabled if feature is enabled.
"""
return
super
(
LoginFailuresAdmin
,
self
).
has_change_permission
(
request
,
obj
)
@_
Feat
ure.is_enabled
@_
Check.is_enabled
(
LoginFail
ure
s
.
is_
feature_
enabled
)
def
has_add_permission
(
self
,
request
):
"""
Only enabled if feature is enabled.
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/student/tests/test_admin_views.py
+
6
−
0
View file @
9c60bd63
...
...
@@ -347,6 +347,12 @@ class LoginFailuresAdminTest(TestCase):
)
self
.
assertEqual
(
str
(
LoginFailures
.
objects
.
get
(
user
=
self
.
user2
)),
'
Zażółć gęślą jaźń: 2 - -
'
)
@override_settings
(
FEATURES
=
{
'
ENABLE_MAX_FAILED_LOGIN_ATTEMPTS
'
:
True
})
def
test_feature_enabled
(
self
):
url
=
reverse
(
'
admin:student_loginfailures_changelist
'
)
response
=
self
.
client
.
get
(
url
)
self
.
assertEqual
(
response
.
status_code
,
200
)
@ddt.data
(
reverse
(
'
admin:student_loginfailures_changelist
'
),
reverse
(
'
admin:student_loginfailures_add
'
),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment