access.py 2.5 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
from student.roles import CourseBetaTesterRole, CourseInstructorRole, CourseStaffRole
16

17 18
log = logging.getLogger(__name__)

19 20 21 22 23 24
ROLES = {
    'beta': CourseBetaTesterRole,
    'instructor': CourseInstructorRole,
    'staff': CourseStaffRole,
}

25

26
def list_with_level(course, level):
Miles Steele committed
27 28 29
    """
    List users who have 'level' access.

30
    `level` is in ['instructor', 'staff', 'beta'] for standard courses.
31 32
    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
33
    """
34
    return ROLES[level](course.id).users_with_role()
35 36


37 38 39 40
def allow_access(course, user, level):
    """
    Allow user access to course modification.

41
    `level` is one of ['instructor', 'staff', 'beta']
42 43 44 45 46 47 48 49
    """
    _change_access(course, user, level, 'allow')


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

50
    `level` is one of ['instructor', 'staff', 'beta']
51 52 53 54
    """
    _change_access(course, user, level, 'revoke')


55
def _change_access(course, user, level, action):
56 57 58
    """
    Change access of user.

59
    `level` is one of ['instructor', 'staff', 'beta']
60
    action is one of ['allow', 'revoke']
61

62
    NOTE: will create a group if it does not yet exist.
63
    """
64

65
    try:
66
        role = ROLES[level](course.id)
67
    except KeyError:
68
        raise ValueError("unrecognized level '{}'".format(level))
69

70
    if action == 'allow':
71
        role.add_users(user)
72
    elif action == 'revoke':
73
        role.remove_users(user)
74
    else:
75
        raise ValueError("unrecognized action '{}'".format(action))
Miles Steele committed
76 77


78
def update_forum_role(course_id, user, rolename, action):
Miles Steele committed
79 80 81
    """
    Change forum access of user.

82
    `rolename` is one of [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA]
83
    `action` is one of ['allow', 'revoke']
Miles Steele committed
84

85
    if `action` is bad, raises ValueError
86
    if `rolename` does not exist, raises Role.DoesNotExist
Miles Steele committed
87 88 89
    """
    role = Role.objects.get(course_id=course_id, name=rolename)

90
    if action == 'allow':
Miles Steele committed
91
        role.users.add(user)
92
    elif action == 'revoke':
Miles Steele committed
93 94
        role.users.remove(user)
    else:
95
        raise ValueError("unrecognized action '{}'".format(action))