access.py 22.4 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
from functools import partial
7 8

from django.conf import settings
9
from django.contrib.auth.models import Group
10 11

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

16
from student.models import CourseEnrollmentAllowed
17
from external_auth.models import ExternalAuthMap
ichuang committed
18
from courseware.masquerade import is_masquerading_as_student
19
from django.utils.timezone import UTC
Your Name committed
20
from student.models import CourseEnrollment
21

22
DEBUG_ACCESS = False
23 24 25

log = logging.getLogger(__name__)

26 27 28 29 30 31 32 33

class CourseContextRequired(Exception):
    """
    Raised when a course_context is required to determine permissions
    """
    pass


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

39 40

def has_access(user, obj, action, course_context=None):
41 42 43 44 45 46 47
    """
    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
48
    - different access for instructor, staff, course staff, and students.
49 50 51

    user: a Django user object. May be anonymous.

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

    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.

60 61 62 63
    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'

64 65 66 67 68 69 70 71
    Returns a bool.  It is up to the caller to actually deny access in a way
    that makes sense in context.
    """
    # 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)

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

    # NOTE: any descriptor access checkers need to go above this
76
    if isinstance(obj, XModuleDescriptor):
77
        return _has_access_descriptor(user, obj, action, course_context)
78 79

    if isinstance(obj, XModule):
80
        return _has_access_xmodule(user, obj, action, course_context)
81 82

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

85
    if isinstance(obj, basestring):
86
        return _has_access_string(user, obj, action, course_context)
87

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

93 94

def get_access_group_name(obj, action):
95 96 97 98 99 100 101 102 103 104 105 106 107
    '''
    Returns group name for user group which has "action" access to the given object.

    Used in managing access lists.
    '''

    if isinstance(obj, CourseDescriptor):
        return _get_access_group_name_course_desc(obj, action)

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

109

110
# ================ Implementation helpers ================================
111 112 113 114 115 116 117
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
118
    'load_forum' -- can load and contribute to the forums (one access level for now)
119 120 121 122 123 124
    '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
125 126
        """
        Can this user load this course?
127 128

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

133 134 135 136
    def can_load_forum():
        """
        Can this user access the forums in this course?
        """
137 138 139 140
        return (can_load() and \
            (CourseEnrollment.is_enrolled(user, course.id) or \
                _has_staff_access_to_descriptor(user, course)
            ))
141

142 143
    def can_enroll():
        """
144 145 146 147 148 149 150 151 152
        First check if restriction of enrollment by login method is enabled, both
            globally and by the course.
        If it is, then the user must pass the criterion set by the course, e.g. that ExternalAuthMap 
            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
        
        (CourseEnrollmentAllowed always overrides)
153 154
        (staff can always enroll)
        """
155 156 157 158 159 160 161 162 163 164
        # if using registration method to restrict (say shibboleth)
        if settings.MITX_FEATURES.get('RESTRICT_ENROLL_BY_REG_METHOD') and course.enrollment_domain:
            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.
165

166
        now = datetime.now(UTC())
167 168 169
        start = course.enrollment_start
        end = course.enrollment_end

170
        if reg_method_ok and (start is None or now > start) and (end is None or now < end):
171
            # in enrollment period, so any user is allowed to enroll.
172
            debug("Allow: in enrollment period")
173 174
            return True

175
        # if user is in CourseEnrollmentAllowed with right course_id then can also enroll
176
        if user is not None and user.is_authenticated() and CourseEnrollmentAllowed:
177 178
            if CourseEnrollmentAllowed.objects.filter(email=user.email, course_id=course.id):
                return True
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        # 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.)
        if settings.MITX_FEATURES.get('ACCESS_REQUIRE_STAFF_FOR_COURSE'):
            # if this feature is on, only allow courses that have ispublic set to be
            # seen by non-staff
198
            if course.lms.ispublic:
199
                debug("Allow: ACCESS_REQUIRE_STAFF_FOR_COURSE and ispublic")
200 201 202 203 204 205 206
                return True
            return _has_staff_access_to_descriptor(user, course)

        return can_enroll() or can_load()

    checkers = {
        'load': can_load,
207
        'load_forum': can_load_forum,
208 209
        'enroll': can_enroll,
        'see_exists': see_exists,
210
        'staff': lambda: _has_staff_access_to_descriptor(user, course),
211
        'instructor': lambda: _has_instructor_access_to_descriptor(user, course),
212 213 214 215
        }

    return _dispatch(checkers, action, user, course)

216

217 218
def _get_access_group_name_course_desc(course, action):
    '''
219
    Return name of group which gives staff access to course.  Only understands action = 'staff' and 'instructor'
220
    '''
Calen Pennington committed
221
    if action == 'staff':
222
        return _course_staff_group_name(course.location)
Calen Pennington committed
223
    elif action == 'instructor':
224 225 226 227 228
        return _course_instructor_group_name(course.location)

    return []


229

230 231

def _has_access_error_desc(user, descriptor, action, course_context):
232 233 234 235 236 237 238 239
    """
    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():
240
        return _has_staff_access_to_descriptor(user, descriptor, course_context)
241 242 243 244 245 246 247 248 249

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

    return _dispatch(checkers, action, user, descriptor)


250
def _has_access_descriptor(user, descriptor, action, course_context=None):
251 252 253 254 255 256 257 258 259 260 261 262
    """
    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():
263 264 265 266 267 268
        """
        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.
        """
269
        # If start dates are off, can always load
270
        if settings.MITX_FEATURES['DISABLE_START_DATES'] and not is_masquerading_as_student(user):
271
            debug("Allow: DISABLE_START_DATES")
272 273 274
            return True

        # Check start date
275
        if descriptor.lms.start is not None:
276
            now = datetime.now(UTC())
277
            effective_start = _adjust_start_date_for_beta_testers(user, descriptor)
278
            if now > effective_start:
279
                # after start date, everyone can see it
280
                debug("Allow: now > effective start date")
281 282
                return True
            # otherwise, need staff access
283
            return _has_staff_access_to_descriptor(user, descriptor, course_context)
284 285

        # No start date, so can always load.
286
        debug("Allow: no start date")
287 288 289 290
        return True

    checkers = {
        'load': can_load,
291
        'staff': lambda: _has_staff_access_to_descriptor(user, descriptor, course_context)
292 293 294 295 296
        }

    return _dispatch(checkers, action, user, descriptor)


297
def _has_access_xmodule(user, xmodule, action, course_context):
298 299 300 301 302 303 304
    """
    Check if user has access to this xmodule.

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


308
def _has_access_location(user, location, action, course_context):
309 310 311 312 313 314 315 316 317 318 319 320 321
    """
    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 = {
322
        'staff': lambda: _has_staff_access_to_location(user, location, course_context)
323 324 325 326 327
        }

    return _dispatch(checkers, action, user, location)


328
def _has_access_string(user, perm, action, course_context):
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    """
    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
        return _has_global_staff_access(user)

    checkers = {
        'staff': check_staff
        }

    return _dispatch(checkers, action, user, perm)


352 353 354 355 356 357 358 359 360 361 362 363
#####  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',
364 365 366
              user,
              obj.location.url() if isinstance(obj, XModuleDescriptor) else str(obj)[:60],
              action)
367 368
        return result

369
    raise ValueError("Unknown action for object type '{0}': '{1}'".format(
370 371
        type(obj), action))

372 373

def _does_course_group_name_exist(name):
374 375
    return len(Group.objects.filter(name=name)) > 0

376

377
def _course_org_staff_group_name(location, course_context=None):
378
    """
379 380
    Get the name of the staff group for an organization which corresponds
    to the organization in the course id.
381

382
    location: something that can passed to Location
383 384 385
    course_context: A course_id that specifies the course run in which
                    the location occurs.
                    Required if location doesn't have category 'course'
386

387
    """
388
    loc = Location(location)
389 390 391 392 393 394 395 396
    if loc.category == 'course':
        course_id = loc.course_id
    else:
        if course_context is None:
            raise CourseContextRequired()
        course_id = course_context
    return 'staff_%s' % course_id.split('/')[0]

397

398
def group_names_for(role, location, course_context=None):
399
    """Returns the group names for a given role with this location. Plural
David Ormsbee committed
400
    because it will return both the name we expect now as well as the legacy
401
    group name we support for backwards compatibility. This should not check
David Ormsbee committed
402
    the DB for existence of a group (like some of its callers do) because that's
403 404
    a DB roundtrip, and we expect this might be invoked many times as we crawl
    an XModule tree."""
405
    loc = Location(location)
406
    legacy_group_name = '{0}_{1}'.format(role, loc.course)
407

408 409 410 411 412 413 414
    if loc.category == 'course':
        course_id = loc.course_id
    else:
        if course_context is None:
            raise CourseContextRequired()
        course_id = course_context

415
    group_name = '{0}_{1}'.format(role, course_id)
416

417
    return [group_name, legacy_group_name]
418

419 420 421 422
group_names_for_staff = partial(group_names_for, 'staff')
group_names_for_instructor = partial(group_names_for, 'instructor')

def _course_staff_group_name(location, course_context=None):
423
    """
424
    Get the name of the staff group for a location in the context of a course run.
425

426 427 428 429 430 431
    location: something that can passed to Location
    course_context: A course_id that specifies the course run in which the location occurs.
        Required if location doesn't have category 'course'

    cdodge: We're changing the name convention of the group to better epxress different runs of courses by
    using course_id rather than just the course number. So first check to see if the group name exists
432
    """
433 434
    loc = Location(location)
    group_name, legacy_group_name = group_names_for_staff(location, course_context)
435

436 437
    if _does_course_group_name_exist(legacy_group_name):
        return legacy_group_name
438

439
    return group_name
440

John Jarvis committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
def _course_org_instructor_group_name(location, course_context=None):
    """
    Get the name of the instructor group for an organization which corresponds
    to the organization in the course id.

    location: something that can passed to Location
    course_context: A course_id that specifies the course run in which
                    the location occurs.
                    Required if location doesn't have category 'course'

    """
    loc = Location(location)
    if loc.category == 'course':
        course_id = loc.course_id
    else:
        if course_context is None:
            raise CourseContextRequired()
        course_id = course_context
    return 'instructor_%s' % course_id.split('/')[0]
460

461

462
def _course_instructor_group_name(location, course_context=None):
463
    """
464
    Get the name of the instructor group for a location, in the context of a course run.
465 466 467
    A course instructor has all staff privileges, but also can manage list of course staff (add, remove, list).

    location: something that can passed to Location.
468 469
    course_context: A course_id that specifies the course run in which the location occurs.
        Required if location doesn't have category 'course'
470

471
    cdodge: We're changing the name convention of the group to better epxress different runs of courses by
472
    using course_id rather than just the course number. So first check to see if the group name exists
473
    """
474
    loc = Location(location)
475
    group_name, legacy_group_name = group_names_for_instructor(location, course_context)
476

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    if _does_course_group_name_exist(legacy_group_name):
        return legacy_group_name

    return group_name

def course_beta_test_group_name(location):
    """
    Get the name of the beta tester group for a location.  Right now, that's
    beta_testers_COURSE.

    location: something that can passed to Location.
    """
    return 'beta_testers_{0}'.format(Location(location).course)

# nosetests thinks that anything with _test_ in the name is a test.
# Correct this (https://nose.readthedocs.org/en/latest/finding_tests.html)
course_beta_test_group_name.__test__ = False
494

495

496

497 498 499 500 501 502 503 504 505
def _has_global_staff_access(user):
    if user.is_staff:
        debug("Allow: user.is_staff")
        return True
    else:
        debug("Deny: not user.is_staff")
        return False


506 507 508 509 510 511 512 513 514 515 516
def _adjust_start_date_for_beta_testers(user, descriptor):
    """
    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:
517
        A datetime.  Either the same as start, or earlier for beta testers.
518 519 520 521 522 523 524

    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.
525 526 527

    NOTE: If testing manually, make sure MITX_FEATURES['DISABLE_START_DATES'] = False
    in envs/dev.py!
528
    """
Calen Pennington committed
529
    if descriptor.lms.days_early_for_beta is None:
530
        # bail early if no beta testing is set up
531
        return descriptor.lms.start
532 533 534

    user_groups = [g.name for g in user.groups.all()]

535
    beta_group = course_beta_test_group_name(descriptor.location)
536 537
    if beta_group in user_groups:
        debug("Adjust start time: user in group %s", beta_group)
Calen Pennington committed
538
        delta = timedelta(descriptor.lms.days_early_for_beta)
539
        effective = descriptor.lms.start - delta
540
        return effective
541

Calen Pennington committed
542
    return descriptor.lms.start
543

Calen Pennington committed
544

545 546
def _has_instructor_access_to_location(user, location, course_context=None):
    return _has_access_to_location(user, location, 'instructor', course_context)
547 548


549 550
def _has_staff_access_to_location(user, location, course_context=None):
    return _has_access_to_location(user, location, 'staff', course_context)
551

552

553
def _has_access_to_location(user, location, access_level, course_context):
554 555 556 557 558 559
    '''
    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.
560 561 562 563 564

    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.
565 566
    location = location
    access_level = string, either "staff" or "instructor"
567 568
    '''
    if user is None or (not user.is_authenticated()):
569
        debug("Deny: no user or anon user")
570
        return False
571 572 573 574

    if is_masquerading_as_student(user):
        return False

575
    if user.is_staff:
576
        debug("Allow: user.is_staff")
577 578 579
        return True

    # If not global staff, is the user in the Auth group for this class?
580
    user_groups = [g.name for g in user.groups.all()]
581 582

    if access_level == 'staff':
583
        staff_groups = group_names_for_staff(location, course_context) + \
584
                       [_course_org_staff_group_name(location, course_context)]
585 586 587 588 589
        for staff_group in staff_groups:
            if staff_group in user_groups:
                debug("Allow: user in group %s", staff_group)
                return True
        debug("Deny: user not in groups %s", staff_groups)
590

591
    if access_level == 'instructor' or access_level == 'staff':  # instructors get staff privileges
592
        instructor_groups = group_names_for_instructor(location, course_context) + \
593
                            [_course_org_instructor_group_name(location, course_context)]
594 595 596 597 598
        for instructor_group in instructor_groups:
            if instructor_group in user_groups:
                debug("Allow: user in group %s", instructor_group)
                return True
        debug("Deny: user not in groups %s", instructor_groups)
599 600
    else:
        log.debug("Error in access._has_access_to_location access_level=%s unknown" % access_level)
601 602
    return False

603

604 605 606
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)
607
    return _has_staff_access_to_location(user, loc, course_id)
608 609


610
def _has_instructor_access_to_descriptor(user, descriptor, course_context=None):
611 612 613 614 615
    """Helper method that checks whether the user has staff access to
    the course of the location.

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

618 619

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

623
    descriptor: something that has a location attribute
624
    """
625
    return _has_staff_access_to_location(user, descriptor.location, course_context)