access.py 3.02 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_comment_common.models import Role
from lms.djangoapps.instructor.enrollment import enroll_email, get_email_params
from student.roles import CourseBetaTesterRole, CourseCcxCoachRole, CourseInstructorRole, CourseStaffRole
17

18 19
log = logging.getLogger(__name__)

20 21 22 23
ROLES = {
    'beta': CourseBetaTesterRole,
    'instructor': CourseInstructorRole,
    'staff': CourseStaffRole,
cewing committed
24
    'ccx_coach': CourseCcxCoachRole,
25 26
}

27

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

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


39
def allow_access(course, user, level, send_email=True):
40 41 42
    """
    Allow user access to course modification.

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


48
def revoke_access(course, user, level, send_email=True):
49 50 51
    """
    Revoke access from user to course modification.

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


57
def _change_access(course, user, level, action, send_email=True):
58 59 60
    """
    Change access of user.

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

64
    NOTE: will create a group if it does not yet exist.
65
    """
66

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

72
    if action == 'allow':
73 74 75 76 77 78
        if level == 'ccx_coach':
            email_params = get_email_params(course, True)
            enroll_email(
                course_id=course.id,
                student_email=user.email,
                auto_enroll=True,
79
                email_students=send_email,
80 81
                email_params=email_params,
            )
82
        role.add_users(user)
83
    elif action == 'revoke':
84
        role.remove_users(user)
85
    else:
86
        raise ValueError("unrecognized action '{}'".format(action))
Miles Steele committed
87 88


89
def update_forum_role(course_id, user, rolename, action):
Miles Steele committed
90 91 92
    """
    Change forum access of user.

93
    `rolename` is one of [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA]
94
    `action` is one of ['allow', 'revoke']
Miles Steele committed
95

96
    if `action` is bad, raises ValueError
97
    if `rolename` does not exist, raises Role.DoesNotExist
Miles Steele committed
98 99 100
    """
    role = Role.objects.get(course_id=course_id, name=rolename)

101
    if action == 'allow':
Miles Steele committed
102
        role.users.add(user)
103
    elif action == 'revoke':
Miles Steele committed
104 105
        role.users.remove(user)
    else:
106
        raise ValueError("unrecognized action '{}'".format(action))