blog/backend
SquidSpirit e6b41a768f
All checks were successful
Frontend CI / build (push) Successful in 1m13s
Deployment / deployment (release) Successful in 6m59s
BLOG-119 Restricted access to unpublished posts (#124)
### Description

This PR introduces an authorization layer for the post feature. It ensures that create, update, and read operations for posts are properly controlled based on user authentication status and post visibility (published vs. unpublished).

#### Key Changes:

* **Restricted Access to Unpublished Posts**:
    * Unauthenticated users can no longer access unpublished posts via the `GET /post/{id}` endpoint. Attempting to do so will now result in an `HTTP 401 Unauthorized` error.
    * The `get_all_post_info` endpoint is now aware of the user's authentication status to correctly filter posts.

* **Authentication Required for Modifications**:
    * Creating (`POST /post`) and updating (`PUT /post/{id}`) posts now requires an authenticated user. The `user_id` is passed from the web handler through the controller to the use cases.

* **New Error Type**:
    * A new `PostError::Unauthorized` variant has been added to handle access control failures gracefully.

* **API & Core Logic Updates**:
    * The `PostController`, use cases (`GetFullPostUseCase`, `GetAllPostInfoUseCase`, etc.), and web handlers have been updated to accept and process the `user_id`.
    * The `GetFullPostUseCase` now contains the primary logic to prevent unauthenticated access to draft posts.
    * OpenAPI (Utopia) documentation has been updated to reflect these new authorization rules.

### Package Changes

_No response_

### Screenshots

_No response_

### Reference

Resolves #119

### Checklist

- [x] A milestone is set
- [x] The related issuse has been linked to this branch

Reviewed-on: #124
Co-authored-by: SquidSpirit <squid@squidspirit.com>
Co-committed-by: SquidSpirit <squid@squidspirit.com>
2025-08-06 22:13:54 +08:00
..

Backend

Development

SQL Migration

  1. Install sqlx

    cargo install sqlx-cli
    
  2. Run migration

    sqlx migrate run
    

Run Project

  1. Prepare for sql schema setup

    cargo sqlx prepare --workspace
    
  2. Run the server

    RUST_LOG=debug cargo run
    
  3. (Optional) Hot restart

    1. Install watchexec

    2. Run the server with watchexec

      RUST_LOG=debug watchexec -e rs -r 'cargo run'