access.py 2.57 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
from django_comment_common.models import Role
14

15 16 17
from student.roles import (
    CourseBetaTesterRole,
    CourseInstructorRole,
cewing committed
18
    CourseCcxCoachRole,
19 20 21
    CourseStaffRole,
)

22

23 24
log = logging.getLogger(__name__)

25 26 27 28
ROLES = {
    'beta': CourseBetaTesterRole,
    'instructor': CourseInstructorRole,
    'staff': CourseStaffRole,
cewing committed
29
    'ccx_coach': CourseCcxCoachRole,
30 31
}

32

33
def list_with_level(course, level):
Miles Steele committed
34 35 36
    """
    List users who have 'level' access.

37
    `level` is in ['instructor', 'staff', 'beta'] for standard courses.
38 39
    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
40
    """
41
    return ROLES[level](course.id).users_with_role()
42 43


44 45 46 47
def allow_access(course, user, level):
    """
    Allow user access to course modification.

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


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

57
    `level` is one of ['instructor', 'staff', 'beta']
58 59 60 61
    """
    _change_access(course, user, level, 'revoke')


62
def _change_access(course, user, level, action):
63 64 65
    """
    Change access of user.

66
    `level` is one of ['instructor', 'staff', 'beta']
67
    action is one of ['allow', 'revoke']
68

69
    NOTE: will create a group if it does not yet exist.
70
    """
71

72
    try:
73
        role = ROLES[level](course.id)
74
    except KeyError:
75
        raise ValueError("unrecognized level '{}'".format(level))
76

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


85
def update_forum_role(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))