예제 #1
0
def add_culinary(session: Session, auth: Auth, culinary_name: str,
                 description: str, location: str, img_url: str,
                 authorization: http.Header):
    ba = "".join(authorization.split())
    decode = base64.b64decode(ba[5:]).decode('utf-8')
    username, password = decode.split(':')

    isPublisher = session.query(Publisher).filter_by(
        username=username).first() and session.query(Publisher).filter_by(
            password=password).first()
    query = session.query(Publisher).filter_by(
        username=auth.get_user_id()).first()

    if isPublisher:
        if query:
            addCulinary = Culinary(publisher=auth.get_user_id(),
                                   culinary_name=culinary_name,
                                   description=description,
                                   location=location,
                                   img_url=img_url)
            session.add(addCulinary)
            session.commit()
            return {'message': 'success add culinary'}
        else:
            return {'message': 'error add culinary'}
    else:
        return {'message': 'not authorized'}
예제 #2
0
def get_token(session: Session, auth: Auth, authorization: http.Header):
    if authorization is None:
        return {'message': 'not authorization and please login'}

    ba = "".join(authorization.split())
    decode = base64.b64decode(ba[5:]).decode('utf-8')
    username, password = decode.split(':')

    query_admin = session.query(Admin).filter_by(
        username=auth.get_user_id()).first()
    query_publisher = session.query(Publisher).filter_by(
        username=auth.get_user_id()).first()

    if query_admin:
        if query_admin.password == password:
            return {
                'username': auth.get_user_id(),
                'user_id': query_admin.id,
                'basic_token': ba[5:],
            }
        else:
            return {'message': 'auth Password wrong !'}
    elif query_publisher:
        if query_publisher.password == password:
            return {
                'username': auth.get_user_id(),
                'user_id': query_publisher.id,
                'token': ba[5:],
            }
        else:
            return {'message': 'auth Password wrong !'}

    else:
        return {'message': 'error authorization'}
def submit_annotation(
    message_id,
    annotation: Annotation,
    repository: repo.Repository,
    auth: Auth,
) -> Submission:
    document = repository.get_document(message_id)
    annotation = document.update_annotation(
        annotation['program'],
        auth.get_user_id(),
    )
    annotation_url = reverse_url(
        'retrieve_annotation',
        message_id=annotation.message_id,
        revision=annotation.revision,
    )
    return Response(
        Submission(annotation_url=annotation_url),
        # This should be SEE_OTHER, but apistar misrenders the response. See
        # <https://github.com/encode/apistar/issues/317>.
        status=HTTPStatus.OK,
        headers={
            'Location': annotation_url,
        },
    )
예제 #4
0
async def create_project(data: Project._scheme, auth: Auth, session: Session):
    data.pop('id')
    obj = Project(**data)
    obj.user_id = auth.get_user_id()
    session.add(obj)
    session.commit()
    return http.Response(obj.render(), status=201)
예제 #5
0
    def has_permission(self, auth: Auth, router: Router, path: http.Path,
                       method: http.Method):
        if not auth.is_authenticated():
            return False

        _, kwargs = router.lookup(path, method)
        scopes_required = {
            scope.format(**kwargs)
            for scope in self.scopes_required
        }
        scopes_given = set(auth.token['scope'].split())
        return scopes_required <= scopes_given
예제 #6
0
def submit_original(
    original: http.Body,
    content_type: http.Header,
    metadata: meta.MergedMetadata,
    repository: repo.Repository,
    auth: Auth,
) -> Submission:
    revision = repository.submit(
        original,
        content_type,
        metadata.message_id,
        metadata.date,
        metadata.subject,
        auth.get_user_id(),
    )
    download_url = reverse_url(
        'retrieve_revision',
        message_id=revision.message_id,
        revision=revision.revision,
    )
    annotation_url = reverse_url(
        'submit_annotation',
        message_id=revision.message_id,
    )
    return Response(
        Submission(
            message_id=revision.message_id,
            download_url=download_url,
            annotation_url=annotation_url,
        ),
        # This should be SEE_OTHER, but apistar misrenders the response. See
        # <https://github.com/encode/apistar/issues/317>.
        status=HTTPStatus.OK,
        headers={
            'Location': download_url,
        },
    )
 def has_permission(self, auth: Auth):
     return auth.is_authenticated() and auth.token is None
 def has_permission(self, auth: Auth):
     return auth.is_authenticated()
예제 #9
0
def me(auth: Auth):
    return {
        "is_authenticated": auth.is_authenticated(),
        "username": auth.get_display_name(),
    }
예제 #10
0
def get_auth(auth: Auth):
    return {
        'user_id': auth.get_user_id(),
        'display_name': auth.get_display_name(),
        'is_authenticated': auth.is_authenticated()
    }
예제 #11
0
 def has_permission(self, auth: Auth):
     if not auth.is_authenticated():
         return False
     return bool(auth.user.is_guest)
예제 #12
0
def display_user(auth: Auth):
    return {
        'is_authenticated': auth.is_authenticated(),
        'user': auth.get_display_name(),
    }