User creation
This commit is contained in:
+6
-1
@@ -6,6 +6,7 @@ extern crate diesel;
|
||||
extern crate dotenv;
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
extern crate bcrypt;
|
||||
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
@@ -48,7 +49,11 @@ fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![
|
||||
routes::instance::configure,
|
||||
routes::instance::post_config
|
||||
routes::instance::post_config,
|
||||
|
||||
routes::user::details,
|
||||
routes::user::new,
|
||||
routes::user::create,
|
||||
])
|
||||
.manage(init_pool())
|
||||
.attach(Template::fairing())
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod instance;
|
||||
pub mod user;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
use diesel;
|
||||
use diesel::{ QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection };
|
||||
use schema::users;
|
||||
|
||||
#[derive(Queryable, Identifiable)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
pub display_name: String,
|
||||
pub outbox_url: String,
|
||||
pub inbox_url: String,
|
||||
pub is_admin: bool,
|
||||
pub summary: String,
|
||||
pub email: Option<String>,
|
||||
pub hashed_password: Option<String>,
|
||||
pub instance_id: i32
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "users"]
|
||||
pub struct NewUser {
|
||||
pub username: String,
|
||||
pub display_name: String,
|
||||
pub outbox_url: String,
|
||||
pub inbox_url: String,
|
||||
pub is_admin: bool,
|
||||
pub summary: String,
|
||||
pub email: Option<String>,
|
||||
pub hashed_password: Option<String>,
|
||||
pub instance_id: i32
|
||||
}
|
||||
|
||||
impl User {
|
||||
fn grant_admin_rights() {}
|
||||
|
||||
pub fn insert (conn: &PgConnection, new: NewUser) -> User {
|
||||
diesel::insert_into(users::table)
|
||||
.values(new)
|
||||
.get_result(conn)
|
||||
.expect("Error saving new instance")
|
||||
}
|
||||
|
||||
pub fn compute_outbox(user: String, hostname: String) -> String {
|
||||
format!("https://{}/@/{}/outbox", hostname, user)
|
||||
}
|
||||
|
||||
pub fn compute_inbox(user: String, hostname: String) -> String {
|
||||
format!("https://{}/@/{}/inbox", hostname, user)
|
||||
}
|
||||
|
||||
fn get () {}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod instance;
|
||||
pub mod user;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
use rocket::request::Form;
|
||||
use rocket::response::Redirect;
|
||||
use rocket_contrib::Template;
|
||||
use std::collections::HashMap;
|
||||
use bcrypt::{hash, DEFAULT_COST};
|
||||
|
||||
use db_conn::DbConn;
|
||||
use models::user::*;
|
||||
use models::instance::Instance;
|
||||
|
||||
|
||||
#[get("/@/<name>")]
|
||||
fn details(name: String) {
|
||||
|
||||
}
|
||||
|
||||
#[get("/users/new")]
|
||||
fn new() -> Template {
|
||||
Template::render("users/new", HashMap::<String, i32>::new())
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct NewUserForm {
|
||||
username: String,
|
||||
email: String,
|
||||
password: String,
|
||||
password_confirmation: String
|
||||
}
|
||||
|
||||
#[post("/users/new", data = "<data>")]
|
||||
fn create(conn: DbConn, data: Form<NewUserForm>) -> Redirect {
|
||||
let inst = Instance::get_local(&*conn).unwrap();
|
||||
let form = data.get();
|
||||
|
||||
if form.password == form.password_confirmation {
|
||||
User::insert(&*conn, NewUser {
|
||||
username: form.username.to_string(),
|
||||
display_name: form.username.to_string(),
|
||||
outbox_url: User::compute_outbox(form.username.to_string(), inst.public_domain.to_string()),
|
||||
inbox_url: User::compute_inbox(form.username.to_string(), inst.public_domain.to_string()),
|
||||
is_admin: !inst.has_admin(&*conn),
|
||||
summary: String::from(""),
|
||||
email: Some(form.email.to_string()),
|
||||
hashed_password: Some(hash(form.password.as_str(), DEFAULT_COST).unwrap()),
|
||||
instance_id: inst.id
|
||||
});
|
||||
}
|
||||
|
||||
Redirect::to(format!("/@/{}", data.get().username).as_str())
|
||||
}
|
||||
@@ -8,3 +8,25 @@ table! {
|
||||
blocked -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Int4,
|
||||
username -> Varchar,
|
||||
display_name -> Varchar,
|
||||
outbox_url -> Varchar,
|
||||
inbox_url -> Varchar,
|
||||
is_admin -> Bool,
|
||||
summary -> Text,
|
||||
email -> Nullable<Text>,
|
||||
hashed_password -> Nullable<Text>,
|
||||
instance_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(users -> instances (instance_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
instances,
|
||||
users,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user