access.py 17.1 KB
Newer Older
1 2 3 4
"""This file contains (or should), all access control logic for the courseware.
Ideally, it will be the only place that needs to know about any special settings
like DISABLE_START_DATES"""
import logging
5
from datetime import datetime, timedelta
6 7

from django.conf import settings
Ned Batchelder committed
8
from django.contrib.auth.models import AnonymousUser
9 10

from xmodule.course_module import CourseDescriptor
11
from xmodule.error_module import ErrorDescriptor
12
from xmodule.modulestore import Location
13 14 15
from xmodule.x_module import XModule

from xblock.core import XBlock
16

17
from student.models import CourseEnrollmentAllowed
18
from external_auth.models import ExternalAuthMap
ichuang committed
19
from courseware.masquerade import is_masquerading_as_student
20
from django.utils.timezone import UTC
Your Name committed
21
from student.models import CourseEnrollment
22
from student.roles import (
23 24 25
    GlobalStaff, CourseStaffRole, CourseInstructorRole,
    OrgStaffRole, OrgInstructorRole, CourseBetaTesterRole
)
26
DEBUG_ACCESS = False
27 28 29

log = logging.getLogger(__name__)

30

31 32 33 34 35
def debug(*args, **kwargs):
    # to avoid overly verbose output, this is off by default
    if DEBUG_ACCESS:
        log.debug(*args, **kwargs)

36 37

def has_access(user, obj, action, course_context=None):
38 39 40 41 42 43 44
    """
    Check whether a user has the access to do action on obj.  Handles any magic
    switching based on various settings.

    Things this module understands:
    - start dates for modules
    - DISABLE_START_DATES
45
    - different access for instructor, staff, course staff, and students.
46

47 48
    user: a Django user object. May be anonymous. If none is passed,
                    anonymous is assumed
49

50 51
    obj: The object to check access for.  A module, descriptor, location, or
                    certain special strings (e.g. 'global')
52 53 54 55 56 57

    action: A string specifying the action that the client is trying to perform.

    actions depend on the obj type, but include e.g. 'enroll' for courses.  See the
    type-specific functions below for the known actions for that type.

58 59 60 61
    course_context: A course_id specifying which course run this access is for.
        Required when accessing anything other than a CourseDescriptor, 'global',
        or a location with category 'course'

62 63 64
    Returns a bool.  It is up to the caller to actually deny access in a way
    that makes sense in context.
    """
65 66 67 68
    # Just in case user is passed in as None, make them anonymous
    if not user:
        user = AnonymousUser()

69 70 71 72 73
    # delegate the work to type-specific functions.
    # (start with more specific types, then get more general)
    if isinstance(obj, CourseDescriptor):
        return _has_access_course_desc(user, obj, action)

74
    if isinstance(obj, ErrorDescriptor):
75
        return _has_access_error_desc(user, obj, action, course_context)
76

77
    if isinstance(obj, XModule):
78
        return _has_access_xmodule(user, obj, action, course_context)
79

80 81 82 83
    # NOTE: any descriptor access checkers need to go above this
    if isinstance(obj, XBlock):
        return _has_access_descriptor(user, obj, action, course_context)

84
    if isinstance(obj, Location):
85
        return _has_access_location(user, obj, action, course_context)
86

87
    if isinstance(obj, basestring):
88
        return _has_access_string(user, obj, action, course_context)
89

90 91
    # Passing an unknown object here is a coding error, so rather than
    # returning a default, complain.
92
    raise TypeError("Unknown object type in has_access(): '{0}'"
93 94
                    .format(type(obj)))

95 96

# ================ Implementation helpers ================================
97 98 99 100 101 102 103
def _has_access_course_desc(user, course, action):
    """
    Check if user has access to a course descriptor.

    Valid actions:

    'load' -- load the courseware, see inside the course
104
    'load_forum' -- can load and contribute to the forums (one access level for now)
105 106 107 108 109 110
    'enroll' -- enroll.  Checks for enrollment window,
                  ACCESS_REQUIRE_STAFF_FOR_COURSE,
    'see_exists' -- can see that the course exists.
    'staff' -- staff access to course.
    """
    def can_load():
Calen Pennington committed
111 112
        """
        Can this user load this course?
113 114

        NOTE: this is not checking whether user is actually enrolled in the course.
Calen Pennington committed
115
        """
116
        # delegate to generic descriptor check to check start dates
117
        return _has_access_descriptor(user, course, 'load')
118

119 120 121 122
    def can_load_forum():
        """
        Can this user access the forums in this course?
        """
123 124 125 126
        return (can_load() and \
            (CourseEnrollment.is_enrolled(user, course.id) or \
                _has_staff_access_to_descriptor(user, course)
            ))
127

128 129
    def can_enroll():
        """
130 131
        First check if restriction of enrollment by login method is enabled, both
            globally and by the course.
Calen Pennington committed
132
        If it is, then the user must pass the criterion set by the course, e.g. that ExternalAuthMap
133 134 135 136
            was set by 'shib:https://idp.stanford.edu/", in addition to requirements below.
        Rest of requirements:
        Enrollment can only happen in the course enrollment period, if one exists.
            or
Calen Pennington committed
137

138
        (CourseEnrollmentAllowed always overrides)
139 140
        (staff can always enroll)
        """
141
        # if using registration method to restrict (say shibboleth)
142
        if settings.FEATURES.get('RESTRICT_ENROLL_BY_REG_METHOD') and course.enrollment_domain:
143 144 145 146 147 148 149 150
            if user is not None and user.is_authenticated() and \
                ExternalAuthMap.objects.filter(user=user, external_domain=course.enrollment_domain):
                debug("Allow: external_auth of " + course.enrollment_domain)
                reg_method_ok = True
            else:
                reg_method_ok = False
        else:
            reg_method_ok = True #if not using this access check, it's always OK.
151

152
        now = datetime.now(UTC())
153 154 155
        start = course.enrollment_start
        end = course.enrollment_end

156
        if reg_method_ok and (start is None or now > start) and (end is None or now < end):
157
            # in enrollment period, so any user is allowed to enroll.
158
            debug("Allow: in enrollment period")
159 160
            return True

161
        # if user is in CourseEnrollmentAllowed with right course_id then can also enroll
162
        if user is not None and user.is_authenticated() and CourseEnrollmentAllowed:
163 164
            if CourseEnrollmentAllowed.objects.filter(email=user.email, course_id=course.id):
                return True
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        # otherwise, need staff access
        return _has_staff_access_to_descriptor(user, course)

    def see_exists():
        """
        Can see if can enroll, but also if can load it: if user enrolled in a course and now
        it's past the enrollment period, they should still see it.

        TODO (vshnayder): This means that courses with limited enrollment periods will not appear
        to non-staff visitors after the enrollment period is over.  If this is not what we want, will
        need to change this logic.
        """
        # VS[compat] -- this setting should go away once all courses have
        # properly configured enrollment_start times (if course should be
        # staff-only, set enrollment_start far in the future.)
181
        if settings.FEATURES.get('ACCESS_REQUIRE_STAFF_FOR_COURSE'):
182 183
            # if this feature is on, only allow courses that have ispublic set to be
            # seen by non-staff
Calen Pennington committed
184
            if course.ispublic:
185
                debug("Allow: ACCESS_REQUIRE_STAFF_FOR_COURSE and ispublic")
186 187 188 189 190 191 192
                return True
            return _has_staff_access_to_descriptor(user, course)

        return can_enroll() or can_load()

    checkers = {
        'load': can_load,
193
        'load_forum': can_load_forum,
194 195
        'enroll': can_enroll,
        'see_exists': see_exists,
196
        'staff': lambda: _has_staff_access_to_descriptor(user, course),
197
        'instructor': lambda: _has_instructor_access_to_descriptor(user, course),
198 199 200 201
        }

    return _dispatch(checkers, action, user, course)

202 203

def _has_access_error_desc(user, descriptor, action, course_context):
204 205 206 207 208 209 210 211
    """
    Only staff should see error descriptors.

    Valid actions:
    'load' -- load this descriptor, showing it to the user.
    'staff' -- staff access to descriptor.
    """
    def check_for_staff():
212
        return _has_staff_access_to_descriptor(user, descriptor, course_context)
213 214 215 216 217 218 219 220 221

    checkers = {
        'load': check_for_staff,
        'staff': check_for_staff
        }

    return _dispatch(checkers, action, user, descriptor)


222
def _has_access_descriptor(user, descriptor, action, course_context=None):
223 224 225 226 227 228 229 230 231 232 233 234
    """
    Check if user has access to this descriptor.

    Valid actions:
    'load' -- load this descriptor, showing it to the user.
    'staff' -- staff access to descriptor.

    NOTE: This is the fallback logic for descriptors that don't have custom policy
    (e.g. courses).  If you call this method directly instead of going through
    has_access(), it will not do the right thing.
    """
    def can_load():
235 236 237 238 239 240
        """
        NOTE: This does not check that the student is enrolled in the course
        that contains this module.  We may or may not want to allow non-enrolled
        students to see modules.  If not, views should check the course, so we
        don't have to hit the enrollments table on every module load.
        """
241
        # If start dates are off, can always load
242
        if settings.FEATURES['DISABLE_START_DATES'] and not is_masquerading_as_student(user):
243
            debug("Allow: DISABLE_START_DATES")
244 245 246
            return True

        # Check start date
247
        if 'detached' not in descriptor._class_tags and descriptor.start is not None:
248
            now = datetime.now(UTC())
249 250 251 252 253
            effective_start = _adjust_start_date_for_beta_testers(
                user,
                descriptor,
                course_context=course_context
            )
254
            if now > effective_start:
255
                # after start date, everyone can see it
256
                debug("Allow: now > effective start date")
257 258
                return True
            # otherwise, need staff access
259
            return _has_staff_access_to_descriptor(user, descriptor, course_context)
260 261

        # No start date, so can always load.
262
        debug("Allow: no start date")
263 264 265 266
        return True

    checkers = {
        'load': can_load,
267
        'staff': lambda: _has_staff_access_to_descriptor(user, descriptor, course_context)
268 269 270 271 272
        }

    return _dispatch(checkers, action, user, descriptor)


273
def _has_access_xmodule(user, xmodule, action, course_context):
274 275 276 277 278 279 280
    """
    Check if user has access to this xmodule.

    Valid actions:
      - same as the valid actions for xmodule.descriptor
    """
    # Delegate to the descriptor
281
    return has_access(user, xmodule.descriptor, action, course_context)
282 283


284
def _has_access_location(user, location, action, course_context):
285 286 287 288 289 290 291 292 293 294 295 296 297
    """
    Check if user has access to this location.

    Valid actions:
    'staff' : True if the user has staff access to this location

    NOTE: if you add other actions, make sure that

     has_access(user, location, action) == has_access(user, get_item(location), action)

    And in general, prefer checking access on loaded items, rather than locations.
    """
    checkers = {
298
        'staff': lambda: _has_staff_access_to_location(user, location, course_context)
299 300 301 302 303
        }

    return _dispatch(checkers, action, user, location)


304
def _has_access_string(user, perm, action, course_context):
305 306 307 308 309 310 311 312 313 314 315 316 317 318
    """
    Check if user has certain special access, specified as string.  Valid strings:

    'global'

    Valid actions:

    'staff' -- global staff access.
    """

    def check_staff():
        if perm != 'global':
            debug("Deny: invalid permission '%s'", perm)
            return False
319
        return GlobalStaff().has_user(user)
320 321 322

    checkers = {
        'staff': check_staff
323
    }
324 325 326 327

    return _dispatch(checkers, action, user, perm)


328 329 330 331 332 333 334 335 336 337 338 339
#####  Internal helper methods below

def _dispatch(table, action, user, obj):
    """
    Helper: call table[action], raising a nice pretty error if there is no such key.

    user and object passed in only for error messages and debugging
    """
    if action in table:
        result = table[action]()
        debug("%s user %s, object %s, action %s",
              'ALLOWED' if result else 'DENIED',
340
              user,
341
              obj.location.url() if isinstance(obj, XBlock) else str(obj)[:60],
342
              action)
343 344
        return result

345
    raise ValueError(u"Unknown action for object type '{0}': '{1}'".format(
346 347
        type(obj), action))

348

349
def _adjust_start_date_for_beta_testers(user, descriptor, course_context=None):
350 351 352 353 354 355 356 357 358 359
    """
    If user is in a beta test group, adjust the start date by the appropriate number of
    days.

    Arguments:
       user: A django user.  May be anonymous.
       descriptor: the XModuleDescriptor the user is trying to get access to, with a
       non-None start date.

    Returns:
360
        A datetime.  Either the same as start, or earlier for beta testers.
361 362 363 364 365 366 367

    NOTE: number of days to adjust should be cached to avoid looking it up thousands of
    times per query.

    NOTE: For now, this function assumes that the descriptor's location is in the course
    the user is looking at.  Once we have proper usages and definitions per the XBlock
    design, this should use the course the usage is in.
368

369
    NOTE: If testing manually, make sure FEATURES['DISABLE_START_DATES'] = False
370
    in envs/dev.py!
371
    """
Calen Pennington committed
372
    if descriptor.days_early_for_beta is None:
373
        # bail early if no beta testing is set up
Calen Pennington committed
374
        return descriptor.start
375

376
    if CourseBetaTesterRole(descriptor.location, course_context=course_context).has_user(user):
377
        debug("Adjust start time: user in beta role for %s", descriptor)
Calen Pennington committed
378 379
        delta = timedelta(descriptor.days_early_for_beta)
        effective = descriptor.start - delta
380
        return effective
381

Calen Pennington committed
382
    return descriptor.start
383

Calen Pennington committed
384

385 386
def _has_instructor_access_to_location(user, location, course_context=None):
    return _has_access_to_location(user, location, 'instructor', course_context)
387 388


389 390
def _has_staff_access_to_location(user, location, course_context=None):
    return _has_access_to_location(user, location, 'staff', course_context)
391

392

393
def _has_access_to_location(user, location, access_level, course_context):
394 395 396 397 398 399
    '''
    Returns True if the given user has access_level (= staff or
    instructor) access to a location.  For now this is equivalent to
    having staff / instructor access to the course location.course.

    This means that user is in the staff_* group or instructor_* group, or is an overall admin.
400 401 402 403 404

    TODO (vshnayder): this needs to be changed to allow per-course_id permissions, not per-course
    (e.g. staff in 2012 is different from 2013, but maybe some people always have access)

    course is a string: the course field of the location being accessed.
405 406
    location = location
    access_level = string, either "staff" or "instructor"
407 408
    '''
    if user is None or (not user.is_authenticated()):
409
        debug("Deny: no user or anon user")
410
        return False
411 412 413 414

    if is_masquerading_as_student(user):
        return False

415
    if GlobalStaff().has_user(user):
416
        debug("Allow: user.is_staff")
417 418
        return True

419 420 421 422
    if access_level not in ('staff', 'instructor'):
        log.debug("Error in access._has_access_to_location access_level=%s unknown", access_level)
        debug("Deny: unknown access level")
        return False
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    staff_access = (
        CourseStaffRole(location, course_context).has_user(user) or
        OrgStaffRole(location).has_user(user)
    )

    if staff_access and access_level == 'staff':
        debug("Allow: user has course staff access")
        return True

    instructor_access = (
        CourseInstructorRole(location, course_context).has_user(user) or
        OrgInstructorRole(location).has_user(user)
    )

    if instructor_access and access_level in ('staff', 'instructor'):
        debug("Allow: user has course instructor access")
        return True

    debug("Deny: user did not have correct access")
443 444
    return False

445

446 447 448
def _has_staff_access_to_course_id(user, course_id):
    """Helper method that takes a course_id instead of a course name"""
    loc = CourseDescriptor.id_to_location(course_id)
449
    return _has_staff_access_to_location(user, loc, course_id)
450 451


452
def _has_instructor_access_to_descriptor(user, descriptor, course_context=None):
453 454 455 456 457
    """Helper method that checks whether the user has staff access to
    the course of the location.

    descriptor: something that has a location attribute
    """
458
    return _has_instructor_access_to_location(user, descriptor.location, course_context)
459

460 461

def _has_staff_access_to_descriptor(user, descriptor, course_context=None):
462 463 464
    """Helper method that checks whether the user has staff access to
    the course of the location.

465
    descriptor: something that has a location attribute
466
    """
467
    return _has_staff_access_to_location(user, descriptor.location, course_context)
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484


def get_user_role(user, course_id):
    """
    Return corresponding string if user has staff, instructor or student
    course role in LMS.
    """
    from courseware.courses import get_course
    course = get_course(course_id)
    if is_masquerading_as_student(user):
        return 'student'
    elif has_access(user, course, 'instructor'):
        return 'instructor'
    elif has_access(user, course, 'staff'):
        return 'staff'
    else:
        return 'student'