'Gets a list of all course enrollments for a user.
Returns a list for the currently logged in user, or for the user named by the \'user\' GET
parameter. If the username does not match that of the currently logged in user, only
courses for which the currently logged in user has the Staff or Admin role are listed.
As a result, a course team member can find out which of his or her own courses a particular
learner is enrolled in.
Only the Staff or Admin role (granted on the Django administrative console as the staff
or instructor permission) in individual courses gives the requesting user access to
enrollment data. Permissions granted at the organizational level do not give a user
access to enrollment data for all of that organization\'s courses.
Users who have the global staff permission can access all enrollment data for all
courses.'

@method_decorator(ensure_csrf_cookie_cross_domain)
def get(self, request):

    username = request.GET.get('user', request.user.username)
    try:
        enrollment_data = api.get_enrollments(username)
    except CourseEnrollmentError:
        return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u"An    error    occurred    while    retrieving    enrollments    for    user    '{username}'".format(username=username)})
    if ((username == request.user.username) or GlobalStaff().has_user(request.user) or self.has_api_key_permissions(request)):
        return Response(enrollment_data)
    filtered_data = []
    for enrollment in enrollment_data:
        course_key = CourseKey.from_string(enrollment['course_details']['course_id'])
        if user_has_role(request.user, CourseStaffRole(course_key)):
            filtered_data.append(enrollment)
    return Response(filtered_data)
