access.py 2.95 KB
Newer Older
1 2 3 4 5
"""
Access control operations for use by instructor APIs.

Does not include any access control, be sure to check access before calling.

Miles Steele committed
6
TO DO sync instructor and staff flags
7 8 9 10 11
    e.g. should these be possible?
        {instructor: true, staff: false}
        {instructor: true, staff: true}
"""

12
import logging
13 14 15 16
from django.contrib.auth.models import Group
from courseware.access import (get_access_group_name,
                               course_beta_test_group_name)
from django_comment_common.models import Role
17

18 19
log = logging.getLogger(__name__)

20

21
def list_with_level(course, level):
Miles Steele committed
22 23 24
    """
    List users who have 'level' access.

25
    `level` is in ['instructor', 'staff', 'beta'] for standard courses.
26 27
    There could be other levels specific to the course.
    If there is no Group for that course-level, returns an empty list
Miles Steele committed
28
    """
29
    if level == 'beta':
Miles Steele committed
30 31 32 33
        grpname = course_beta_test_group_name(course.location)
    else:
        grpname = get_access_group_name(course, level)

34 35 36
    try:
        return Group.objects.get(name=grpname).user_set.all()
    except Group.DoesNotExist:
37
        log.info("list_with_level called with non-existant group named {}".format(grpname))
38 39 40
        return []


41 42 43 44
def allow_access(course, user, level):
    """
    Allow user access to course modification.

45
    `level` is one of ['instructor', 'staff', 'beta']
46 47 48 49 50 51 52 53
    """
    _change_access(course, user, level, 'allow')


def revoke_access(course, user, level):
    """
    Revoke access from user to course modification.

54
    `level` is one of ['instructor', 'staff', 'beta']
55 56 57 58
    """
    _change_access(course, user, level, 'revoke')


59
def _change_access(course, user, level, action):
60 61 62
    """
    Change access of user.

63
    `level` is one of ['instructor', 'staff', 'beta']
64
    action is one of ['allow', 'revoke']
65

66
    NOTE: will create a group if it does not yet exist.
67
    """
68

69
    if level == 'beta':
Miles Steele committed
70
        grpname = course_beta_test_group_name(course.location)
71
    elif level in ['instructor', 'staff']:
72
        grpname = get_access_group_name(course, level)
73 74
    else:
        raise ValueError("unrecognized level '{}'".format(level))
75 76
    group, _ = Group.objects.get_or_create(name=grpname)

77
    if action == 'allow':
78
        user.groups.add(group)
79
    elif action == 'revoke':
80 81
        user.groups.remove(group)
    else:
82
        raise ValueError("unrecognized action '{}'".format(action))
Miles Steele committed
83 84


85
def update_forum_role_membership(course_id, user, rolename, action):
Miles Steele committed
86 87 88
    """
    Change forum access of user.

89
    `rolename` is one of [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA]
90
    `action` is one of ['allow', 'revoke']
Miles Steele committed
91

92
    if `action` is bad, raises ValueError
93
    if `rolename` does not exist, raises Role.DoesNotExist
Miles Steele committed
94 95 96
    """
    role = Role.objects.get(course_id=course_id, name=rolename)

97
    if action == 'allow':
Miles Steele committed
98
        role.users.add(user)
99
    elif action == 'revoke':
Miles Steele committed
100 101
        role.users.remove(user)
    else:
102
        raise ValueError("unrecognized action '{}'".format(action))