Commit 283847a4 by Rocky Duan

added administrator role

parent 90d3dc37
......@@ -7,6 +7,7 @@ class Command(BaseCommand):
help = 'Seed default permisssions and roles'
def handle(self, *args, **options):
administrator_role = Role.objects.get_or_create(name="Administrator", course_id="MITx/6.002x/2012_Fall")[0]
moderator_role = Role.objects.get_or_create(name="Moderator", course_id="MITx/6.002x/2012_Fall")[0]
student_role = Role.objects.get_or_create(name="Student", course_id="MITx/6.002x/2012_Fall")[0]
......@@ -19,4 +20,9 @@ class Command(BaseCommand):
"endorse_comment", "delete_comment"]:
moderator_role.add_permission(per)
for per in ["manage_moderator"]:
administrator_role.add_permission(per)
moderator_role.inherit_permissions(student_role)
administrator_role.inherit_permissions(moderator_role)
......@@ -8,6 +8,7 @@ class Command(BaseCommand):
help = "Show a user's roles and permissions"
def handle(self, *args, **options):
print args
if len(args) != 1:
raise CommandError("The number of arguments does not match. ")
try:
......
......@@ -11,7 +11,8 @@ class Role(models.Model):
def __unicode__(self):
return self.name + " for " + (self.course_id if self.course_id else "all courses")
def inherit_permissions(self, role):
def inherit_permissions(self, role): # TODO the name of this method is a little bit confusing,
# since it's one-off and doesn't handle inheritance later
if role.course_id and role.course_id != self.course_id:
logging.warning("%s cannot inheret permissions from %s due to course_id inconsistency" %
(self, role))
......
......@@ -17,7 +17,6 @@ def assign_default_role(sender, instance, **kwargs):
logging.info("assign_default_role: adding %s as %s" % (instance.user, role))
instance.user.roles.add(role)
def has_permission(user, permission, course_id=None):
# if user.permissions.filter(name=permission).exists():
# return True
......@@ -30,10 +29,16 @@ def has_permission(user, permission, course_id=None):
CONDITIONS = ['is_open', 'is_author']
def check_condition(user, condition, course_id, data):
def check_open(user, condition, course_id, data):
return not data['content']['closed']
try:
return data and not data['content']['closed']
except KeyError:
return False
def check_author(user, condition, course_id, data):
return data['content']['user_id'] == str(user.id)
try:
return data and data['content']['user_id'] == str(user.id)
except KeyError:
return False
handlers = {
'is_open' : check_open,
......@@ -67,26 +72,27 @@ def check_conditions_permissions(user, permissions, course_id, **kwargs):
VIEW_PERMISSIONS = {
'update_thread' : ['edit_content', ['update_thread', 'is_open', 'author']],
# 'create_comment' : [["create_comment", "is_open"]],
'create_comment' : ["create_comment"],
'delete_thread' : ['delete_thread'],
'update_comment' : ['edit_content', ['update_comment', 'is_open', 'author']],
'endorse_comment' : ['endorse_comment'],
'openclose_thread' : ['openclose_thread'],
'create_sub_comment': [['create_sub_comment', 'is_open']],
'delete_comment' : ['delete_comment'],
'vote_for_comment' : [['vote', 'is_open']],
'undo_vote_for_comment': [['unvote', 'is_open']],
'vote_for_thread' : [['vote', 'is_open']],
'undo_vote_for_thread': [['unvote', 'is_open']],
'follow_thread' : ['follow_thread'],
'follow_commentable': ['follow_commentable'],
'follow_user' : ['follow_user'],
'unfollow_thread' : ['unfollow_thread'],
'unfollow_commentable': ['unfollow_commentable'],
'unfollow_user' : ['unfollow_user'],
'create_thread' : ['create_thread'],
'update_thread' : ['edit_content', ['update_thread', 'is_open', 'author']],
# 'create_comment' : [["create_comment", "is_open"]],
'create_comment' : ["create_comment"],
'delete_thread' : ['delete_thread'],
'update_comment' : ['edit_content', ['update_comment', 'is_open', 'author']],
'endorse_comment' : ['endorse_comment'],
'openclose_thread' : ['openclose_thread'],
'create_sub_comment' : [['create_sub_comment', 'is_open']],
'delete_comment' : ['delete_comment'],
'vote_for_comment' : [['vote', 'is_open']],
'undo_vote_for_comment' : [['unvote', 'is_open']],
'vote_for_thread' : [['vote', 'is_open']],
'undo_vote_for_thread' : [['unvote', 'is_open']],
'follow_thread' : ['follow_thread'],
'follow_commentable' : ['follow_commentable'],
'follow_user' : ['follow_user'],
'unfollow_thread' : ['unfollow_thread'],
'unfollow_commentable' : ['unfollow_commentable'],
'unfollow_user' : ['unfollow_user'],
'create_thread' : ['create_thread'],
'update_moderator_status' : ['manage_moderator'],
}
......
<%! from django_comment_client.utils import pluralize %>
<%! from django_comment_client.permissions import has_permission, check_permissions_by_view %>
<%inherit file="../main.html" />
<%namespace name='static' file='../static_content.html'/>
......@@ -19,13 +20,24 @@
<div class="course-wrapper">
<section aria-label="User Profile" class="user-profile">
<nav>
<%
role_names = sorted(map(lambda x: x.name, django_user.roles.all()))
%>
<article class="sidebar-module discussion-sidebar">
<div class="sidebar-username">${user.username}</div>
<div class="sidebar-username">${django_user.username}</div>
<div class="sidebar-user-roles">
${", ".join(role_names)}
</div>
<div class="sidebar-threads-count">${discussion_user['threads_count']} ${pluralize('discussion', discussion_user['threads_count'])} started</div>
<div class="sidebar-comments-count">${discussion_user['comments_count']} ${pluralize('comment', discussion_user['comments_count'])}</div>
<a href="#" class="sidebar-promote-moderator-button">Promote to Moderator</a>
<a href="#" class="sidebar-revoke-moderator-button">Revoke Moderator provileges</a>
% if check_permissions_by_view(user, course.id, content=None, name='update_moderator_status'):
% if "Moderator" in role_names:
<a href="javascript:void(0)" class="sidebar-revoke-moderator-button">Revoke Moderator provileges</a>
% else:
<a href="javascript:void(0)" class="sidebar-promote-moderator-button">Promote to Moderator</a>
% endif
% endif
</article>
</nav>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment