access.py 15.8 KB
Newer Older
1 2 3 4 5 6 7 8
"""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
import time

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
DEBUG_ACCESS = False
17 18 19

log = logging.getLogger(__name__)

20 21 22 23 24 25 26 27

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


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

33 34

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

    user: a Django user object. May be anonymous.

    obj: The object to check access for.  For now, a module or descriptor.

    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.

53 54 55 56
    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'

57 58 59 60 61 62 63 64
    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)

65
    if isinstance(obj, ErrorDescriptor):
66
        return _has_access_error_desc(user, obj, action, course_context)
67 68

    # NOTE: any descriptor access checkers need to go above this
69
    if isinstance(obj, XModuleDescriptor):
70
        return _has_access_descriptor(user, obj, action, course_context)
71 72

    if isinstance(obj, XModule):
73
        return _has_access_xmodule(user, obj, action, course_context)
74 75

    if isinstance(obj, Location):
76
        return _has_access_location(user, obj, action, course_context)
77

78
    if isinstance(obj, basestring):
79
        return _has_access_string(user, obj, action, course_context)
80

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

86 87

def get_access_group_name(obj, action):
88 89 90 91 92 93 94 95 96 97 98 99 100
    '''
    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)))
101

102

103
# ================ Implementation helpers ================================
104 105 106 107 108 109 110 111 112 113 114 115 116
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
    '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
117 118
        """
        Can this user load this course?
119 120

        NOTE: this is not checking whether user is actually enrolled in the course.
Calen Pennington committed
121
        """
122
        # delegate to generic descriptor check to check start dates
123
        return _has_access_descriptor(user, course, 'load')
124 125 126 127 128 129 130 131 132 133 134 135 136

    def can_enroll():
        """
        If the course has an enrollment period, check whether we are in it.
        (staff can always enroll)
        """

        now = time.gmtime()
        start = course.enrollment_start
        end = course.enrollment_end

        if (start is None or now > start) and (end is None or now < end):
            # in enrollment period, so any user is allowed to enroll.
137
            debug("Allow: in enrollment period")
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            return True

        # 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
            if course.metadata.get('ispublic'):
159
                debug("Allow: ACCESS_REQUIRE_STAFF_FOR_COURSE and ispublic")
160 161 162 163 164 165 166 167 168
                return True
            return _has_staff_access_to_descriptor(user, course)

        return can_enroll() or can_load()

    checkers = {
        'load': can_load,
        'enroll': can_enroll,
        'see_exists': see_exists,
169
        'staff': lambda: _has_staff_access_to_descriptor(user, course),
170
        'instructor': lambda: _has_instructor_access_to_descriptor(user, course),
171 172 173 174
        }

    return _dispatch(checkers, action, user, course)

175

176 177 178 179
def _get_access_group_name_course_desc(course, action):
    '''
    Return name of group which gives staff access to course.  Only understands action = 'staff'
    '''
180
    if not action == 'staff':
181 182
        return []
    return _course_staff_group_name(course.location)
183

184 185

def _has_access_error_desc(user, descriptor, action, course_context):
186 187 188 189 190 191 192 193
    """
    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():
194
        return _has_staff_access_to_descriptor(user, descriptor, course_context)
195 196 197 198 199 200 201 202 203

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

    return _dispatch(checkers, action, user, descriptor)


204
def _has_access_descriptor(user, descriptor, action, course_context=None):
205 206 207 208 209 210 211 212 213 214 215 216
    """
    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():
217 218 219 220 221 222
        """
        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.
        """
223 224
        # If start dates are off, can always load
        if settings.MITX_FEATURES['DISABLE_START_DATES']:
225
            debug("Allow: DISABLE_START_DATES")
226 227 228 229 230 231 232
            return True

        # Check start date
        if descriptor.start is not None:
            now = time.gmtime()
            if now > descriptor.start:
                # after start date, everyone can see it
233
                debug("Allow: now > start date")
234 235
                return True
            # otherwise, need staff access
236
            return _has_staff_access_to_descriptor(user, descriptor, course_context)
237 238

        # No start date, so can always load.
239
        debug("Allow: no start date")
240 241 242 243
        return True

    checkers = {
        'load': can_load,
244
        'staff': lambda: _has_staff_access_to_descriptor(user, descriptor, course_context)
245 246 247 248 249
        }

    return _dispatch(checkers, action, user, descriptor)


250
def _has_access_xmodule(user, xmodule, action, course_context):
251 252 253 254 255 256 257
    """
    Check if user has access to this xmodule.

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


261
def _has_access_location(user, location, action, course_context):
262 263 264 265 266 267 268 269 270 271 272 273 274
    """
    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 = {
275
        'staff': lambda: _has_staff_access_to_location(user, location, course_context)
276 277 278 279 280
        }

    return _dispatch(checkers, action, user, location)


281
def _has_access_string(user, perm, action, course_context):
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    """
    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)


305 306 307 308 309 310 311 312 313 314 315 316
#####  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',
317 318 319
              user,
              obj.location.url() if isinstance(obj, XModuleDescriptor) else str(obj)[:60],
              action)
320 321
        return result

322
    raise ValueError("Unknown action for object type '{0}': '{1}'".format(
323 324
        type(obj), action))

325 326

def _does_course_group_name_exist(name):
327 328
    return len(Group.objects.filter(name=name)) > 0

329

330
def _course_staff_group_name(location, course_context=None):
331
    """
332
    Get the name of the staff group for a location in the context of a course run.
333

334 335 336 337 338
    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
339
    using course_id rather than just the course number. So first check to see if the group name exists
340
    """
341 342 343 344 345
    loc = Location(location)
    legacy_name = 'staff_%s' % loc.course
    if _does_course_group_name_exist(legacy_name):
        return legacy_name

346 347 348 349 350 351 352 353
    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
354

355

356
def _course_instructor_group_name(location, course_context=None):
357
    """
358
    Get the name of the instructor group for a location, in the context of a course run.
359 360 361
    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.
362 363
    course_context: A course_id that specifies the course run in which the location occurs.
        Required if location doesn't have category 'course'
364

365
    cdodge: We're changing the name convention of the group to better epxress different runs of courses by
366
    using course_id rather than just the course number. So first check to see if the group name exists
367
    """
368 369 370 371
    loc = Location(location)
    legacy_name = 'instructor_%s' % loc.course
    if _does_course_group_name_exist(legacy_name):
        return legacy_name
372 373 374 375 376 377 378 379 380

    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
381

382

383 384 385 386 387 388 389 390 391
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


392 393
def _has_instructor_access_to_location(user, location, course_context=None):
    return _has_access_to_location(user, location, 'instructor', course_context)
394 395


396 397
def _has_staff_access_to_location(user, location, course_context=None):
    return _has_access_to_location(user, location, 'staff', course_context)
398

399

400
def _has_access_to_location(user, location, access_level, course_context):
401 402 403 404 405 406
    '''
    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.
407 408 409 410 411

    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.
412 413
    location = location
    access_level = string, either "staff" or "instructor"
414 415
    '''
    if user is None or (not user.is_authenticated()):
416
        debug("Deny: no user or anon user")
417 418
        return False
    if user.is_staff:
419
        debug("Allow: user.is_staff")
420 421 422
        return True

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

    if access_level == 'staff':
426
        staff_group = _course_staff_group_name(location, course_context)
427 428 429
        if staff_group in user_groups:
            debug("Allow: user in group %s", staff_group)
            return True
430 431 432
        debug("Deny: user not in group %s", staff_group)

    if access_level == 'instructor' or access_level == 'staff': 	# instructors get staff privileges
433
        instructor_group = _course_instructor_group_name(location, course_context)
434 435 436 437 438 439 440 441
        if instructor_group in user_groups:
            debug("Allow: user in group %s", instructor_group)
            return True
        debug("Deny: user not in group %s", instructor_group)

    else:
        log.debug("Error in access._has_access_to_location access_level=%s unknown" % access_level)

442 443
    return False

444

445 446 447
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)
448
    return _has_staff_access_to_location(user, loc, course_id)
449 450


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

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

459 460

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

464
    descriptor: something that has a location attribute
465
    """
466
    return _has_staff_access_to_location(user, descriptor.location, course_context)