예제 #1
0
def get_api_user(
        security: IdEncodingHelper = depends(IdEncodingHelper),
        user_manager: UserManager = depends(UserManager),
        key: Optional[str] = Query(None),
        x_api_key: Optional[str] = Header(None),
        run_as:
    Optional[EncodedDatabaseIdField] = Header(
        default=None,
        title='Run as User',
        description=
        ('The user ID that will be used to effectively make this API call. '
         'Only admins and designated users can make API calls on behalf of other users.'
         ))) -> Optional[User]:
    api_key = key or x_api_key
    if not api_key:
        return None
    user = user_manager.by_api_key(api_key=api_key)
    if run_as:
        if user_manager.user_can_do_run_as(user):
            try:
                decoded_run_as_id = security.decode_id(run_as)
            except Exception:
                raise UserInvalidRunAsException
            return user_manager.by_id(decoded_run_as_id)
        else:
            raise UserCannotRunAsException
    return user
예제 #2
0
def get_api_user(security: IdEncodingHelper = depends(IdEncodingHelper),
                 user_manager: UserManager = depends(UserManager),
                 key: Optional[str] = Query(None),
                 x_api_key: Optional[str] = Header(None),
                 run_as: Optional[EncodedDatabaseIdField] = Header(
                     None, title='Run as User',
                     description='Admins and ')) -> Optional[User]:
    api_key = key or x_api_key
    if not api_key:
        return None
    user = user_manager.by_api_key(api_key=api_key)
    if run_as:
        if user_manager.user_can_do_run_as(user):
            try:
                decoded_run_as_id = security.decode_id(run_as)
            except Exception:
                raise UserInvalidRunAsException
            return user_manager.by_id(decoded_run_as_id)
        else:
            raise UserCannotRunAsException
    return user