rocket: User sign up guard forwards over failures When no user can be found, the User implementation of FromRequest was failing, which is a mistake. This meant that no other routes were tried and thus assigning policies to routes failed. This changes fixes that by forwarding instead. To be frank, this is over forwarding, and can still be improved on.

zj
Oct 7, 2021, 2:35 PM
Y52KSJEFIX7KIL3MWH65WKHKXIX34T3VUSBVH2SBJ4ZJHM767J4AC

Dependencies

  • [2] E4OBIIUB templates: Fix navigation mocks Cleans up the directory structure for templates and fixes the navigation bugs in the applications too. Includes a context! macro which is lifted from upstream, but is currently unreleased. When rocket_dyn_templates is updates, it could be DRY'ed out.
  • [3] RJ75MX6Y routes: Move user routes to controllers module The last routes to move, the src directory now only has a main.rs file and directories.
  • [4] KFVJ3KMW frontend: Introduce navigation bar Minor changes to the front-end mostly, to allow users to register, sign in, and sign out. The sign out route is changed to a GET endpoint, as links in HTML cannot DELETE.

Change contents

  • replacement in templates/landing.html.tera at line 11
    [2.2713][2.2713:2814]()
    <a class="btn btn-primary btn-lg ps-4 gap-3" href="/users/sign_up" role="button">Sign up</a>
    [2.2713]
    [2.2814]
    <a class="btn btn-primary btn-lg ps-4 gap-3" href="/users/new" role="button">Sign up</a>
  • edit in src/controllers/users.rs at line 13
    [3.327][3.327:362]()
    use rocket::outcome::try_outcome;
  • replacement in src/controllers/users.rs at line 20
    [3.518][3.518:735]()
    async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
    let db = try_outcome!(request.guard::<&State<Database>>().await);
    // &State<Database>>().await);
    [3.518]
    [3.735]
    async fn from_request(request: &'r Request<'_>) -> request::Outcome<User, ()> {
    let db: &State<Database> = match request.guard().await {
    Outcome::Success(db) => db,
    _ => return Outcome::Failure((Status::Unauthorized, ())),
    };
  • replacement in src/controllers/users.rs at line 29
    [3.868][3.868:941]()
    None => return Outcome::Failure((Status::Unauthorized, ())),
    [3.868]
    [3.941]
    None => return Outcome::Forward(()),
  • replacement in src/controllers/users.rs at line 34
    [3.1041][3.1041:1110]()
    Err(_e) => Outcome::Failure((Status::Unauthorized, ())),
    [3.1041]
    [3.1110]
    Err(_e) => Outcome::Forward(()), // TODO this is a failure
  • edit in src/controllers/users.rs at line 39
    [3.1129]
    [3.1129]
    #[get("/users/new", rank = 1)]
    fn new_already_user(_user: User) -> Redirect {
    Redirect::to("/")
    }
  • edit in src/controllers/users.rs at line 47
    [2.4280][3.1237:1340](),[3.1237][3.1237:1340]()
    }
    #[get("/users/new", rank = 1)]
    fn new_already_user(_user: User) -> Redirect {
    Redirect::to("/")