diff --git a/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py b/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py index fd09e97d27..8ce0cf49d3 100644 --- a/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py +++ b/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py @@ -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) diff --git a/lms/djangoapps/django_comment_client/management/commands/show_permissions.py b/lms/djangoapps/django_comment_client/management/commands/show_permissions.py index 3f99bbaca0..ec3167aa0c 100644 --- a/lms/djangoapps/django_comment_client/management/commands/show_permissions.py +++ b/lms/djangoapps/django_comment_client/management/commands/show_permissions.py @@ -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: diff --git a/lms/djangoapps/django_comment_client/models.py b/lms/djangoapps/django_comment_client/models.py index 06bf8c0d6b..605a731517 100644 --- a/lms/djangoapps/django_comment_client/models.py +++ b/lms/djangoapps/django_comment_client/models.py @@ -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)) diff --git a/lms/djangoapps/django_comment_client/permissions.py b/lms/djangoapps/django_comment_client/permissions.py index eae67a8361..962f7954bb 100644 --- a/lms/djangoapps/django_comment_client/permissions.py +++ b/lms/djangoapps/django_comment_client/permissions.py @@ -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'], } diff --git a/lms/templates/discussion/user_profile.html b/lms/templates/discussion/user_profile.html index e616c0d003..01d9c66c61 100644 --- a/lms/templates/discussion/user_profile.html +++ b/lms/templates/discussion/user_profile.html @@ -1,4 +1,5 @@ <%! 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 @@