Пример #1
0
def get_course_or_global_permissions(
) -> JSONResponse[t.Union[GlobalPermMap, t.Mapping[int, CoursePermMap]]]:
    """Get all the global :class:`.psef.models.Permission` or the value of a
    permission in all courses of the currently logged in
    :class:`.psef.models.User`

    .. :quickref: Permission; Get global permissions or all the course
        permissions for the current user.

    :qparam str type: The type of permissions to get. This can be ``global`` or
        ``course``.
    :qparam str permission: The permissions to get when getting course
        permissions. You can pass this parameter multiple times to get multiple
        permissions. DEPRECATED: This option is deprecated, as it is preferred
        that you simply get all permissions for a course.

    :returns: The returning object depends on the given ``type``. If it was
        ``global`` a mapping between permissions name and a boolean indicating
        if the currently logged in user has this permissions is returned.

        If it was ``course`` such a mapping is returned for every course the
        user is enrolled in. So it is a mapping between course ids and
        permission mapping. The permissions given as ``permission`` query
        parameter are the only ones that are present in the permission
        map. When no ``permission`` query is given all course permissions are
        returned.
    """
    ensure_keys_in_dict(request.args, [('type', str)])
    permission_type = t.cast(str, request.args['type']).lower()

    if permission_type == 'global':
        return jsonify(GPerm.create_map(current_user.get_all_permissions()))
    elif permission_type == 'course' and 'permission' in request.args:
        add_deprecate_warning(
            'Requesting a subset of course permissions is deprecated')
        # Make sure at least one permission is present
        ensure_keys_in_dict(request.args, [('permission', str)])
        perm_names = t.cast(t.List[str], request.args.getlist('permission'))
        return jsonify({
            course_id: CPerm.create_map(v)
            for course_id, v in current_user.get_permissions_in_courses(
                [CPerm.get_by_name(p) for p in perm_names]).items()
        })
    elif permission_type == 'course':
        return jsonify({
            course_id: CPerm.create_map(v)
            for course_id, v in
            current_user.get_all_permissions_in_courses().items()
        })
    else:
        raise APIException(
            'Invalid permission type given',
            f'The given type "{permission_type}" is not "global" or "course"',
            APICodes.INVALID_PARAM,
            400,
        )
Пример #2
0
def get_permissions_for_course(
    course_id: int, ) -> JSONResponse[t.Mapping[str, bool]]:
    """Get all the course :class:`.models.Permission` of the currently logged
    in :class:`.models.User`

    .. :quickref: Course; Get all the course permissions for the current user.

    :param int course_id: The id of the course of which the permissions should
        be retrieved.
    :returns: A mapping between the permission name and a boolean indicating if
        the currently logged in user has this permission.
    """
    course = helpers.get_or_404(models.Course, course_id)
    return jsonify(current_user.get_all_permissions(course))
Пример #3
0
def get_course_permissions(
) -> JSONResponse[t.Union[_PermMap, t.Mapping[int, _PermMap]]]:
    """Get all the global :class:`.psef.models.Permission` or the value of a
    permission in all courses of the currently logged in
    :class:`.psef.models.User`

    .. :quickref: Permission; Get global permissions or all the course
        permissions for the current user.

    :qparam str type: The type of permissions to get. This can be ``global`` or
        ``course``.
    :qparam str permission: The permissions to get when getting course
        permissions. You can pass this parameter multiple times to get multiple
        permissions.

    :returns: The returning object depends on the given ``type``. If it was
        ``global`` a mapping between permissions name and a boolean indicating
        if the currently logged in user has this permissions is returned.

        If it was ``course`` such a mapping is returned for every course the
        user is enrolled in. So it is a mapping between course ids and
        permission mapping. The permissions given as ``permission`` query
        parameter are the only ones that are present in the permission map.
    """
    ensure_keys_in_dict(request.args, [('type', str)])
    permission_type = t.cast(str, request.args['type']).lower()

    if permission_type == 'global':
        return jsonify(current_user.get_all_permissions())
    elif permission_type == 'course':
        # Make sure at least one permission is present
        ensure_keys_in_dict(request.args, [('permission', str)])
        perms = t.cast(t.List[str], request.args.getlist('permission'))
        return jsonify(current_user.get_permissions_in_courses(perms))
    else:
        raise APIException(
            'Invalid permission type given',
            f'The given type "{permission_type}" is not "global" or "course"',
            APICodes.INVALID_PARAM,
            400,
        )
Пример #4
0
def self_information(
) -> t.Union[JSONResponse[t.Union[models.User, t.MutableMapping[str, t.Any], t.
                                  Mapping[int, str]]],
             ExtendedJSONResponse[t.Union[models.User, t.MutableMapping[str, t.
                                                                        Any]]],
             ]:
    """Get the info of the currently logged in :class:`.models.User`.

    .. :quickref: User; Get information about the currently logged in user.

    :query type: If this is ``roles`` a mapping between course_id and role name
        will be returned, if this is ``extended`` the result of
        :py:meth:`.models.User.__extended_to_json__()` will be returned. If
        this is something else or not present the result of
        :py:meth:`.models.User.__to_json__()` will be returned.
    :query with_permissions: Setting this to true will add the key
        ``permissions`` to the user. The value will be a mapping indicating
        which global permissions this user has.
    :returns: A response containing the JSON serialized user

    :raises PermissionException: If there is no logged in user. (NOT_LOGGED_IN)
    """
    args = request.args
    if args.get('type') == 'roles':
        return jsonify(
            {
                role.course_id: role.name
                for role in current_user.courses.values()
            }
        )

    elif helpers.extended_requested() or args.get('type') == 'extended':
        obj = current_user.__extended_to_json__()
        if request_arg_true('with_permissions'):
            obj['permissions'] = GPerm.create_map(
                current_user.get_all_permissions()
            )
        return extended_jsonify(obj)
    return jsonify(current_user)