Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance user model #24

Merged
merged 23 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
serde_urlencoded = "0.6"
diesel = { version = "1.4", features = ["postgres", "r2d2"] }
diesel = { version = "1.4", features = ["postgres", "r2d2", "chrono"] }
diesel-derive-enum = { version = "1", features = ["postgres"] }
diesel_migrations = "1.4"
tempfile = "3.1"
maplit = "1.0"
Expand All @@ -28,6 +29,6 @@ parking_lot = { version = "0.11", features = ["nightly"] }
thiserror = "1.0.20"

[dependencies.rocket_contrib]
version="0.4.2"
version = "0.4.2"
default-features = false
features = ["json", "diesel_postgres_pool", "serve"]
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ The name is open for discussion.
with `cargo install diesel_cli`.

4. Create the development and testing database with
`diesel database reset --database-url "postgresql://zauth:zauth@localhost/zauth"`
and
`diesel database reset --database-url "postgresql://zauth:zauth@localhost/zauth_test"`.
```shell script
diesel database reset --database-url "postgresql://zauth:zauth@localhost/zauth"
diesel database reset --database-url "postgresql://zauth:zauth@localhost/zauth_test"
```
This will also run the migrations.

5. You can start the server with `cargo run`.
Expand All @@ -27,7 +28,7 @@ The name is open for discussion.
```
ZAUTH_ADMIN_PASSWORD=admin cargo run
```
The server should then run on[localhost:8000](http://localhost:8000) and create
The server should then run on [localhost:8000](http://localhost:8000) and create
an admin user with password 'admin'.

You can now start developing! A good way to start is to look at the routes defined in the [controllers](./src/controllers/).
2 changes: 2 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
# see diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
import_types = ["diesel::sql_types::*", "crate::user_state::*"] # <- note the extra import
38 changes: 27 additions & 11 deletions migrations/2019-10-30-155718_schema/up.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
admin BOOLEAN NOT NULL DEFAULT false
CREATE TYPE user_state AS ENUM ('pending', 'active', 'disabled');

CREATE TABLE users
(
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
hashed_password VARCHAR(255) NOT NULL,
admin BOOLEAN NOT NULL DEFAULT false,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
ssh_key TEXT,
state user_state NOT NULL DEFAULT 'pending',
last_login TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE clients (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
secret VARCHAR(255) NOT NULL,
needs_grant BOOLEAN NOT NULL DEFAULT false,
redirect_uri_list TEXT NOT NULL DEFAULT ''
CREATE INDEX ix_users_username ON users (username);
CREATE INDEX ix_users_email ON users (email);
mcbloch marked this conversation as resolved.
Show resolved Hide resolved


CREATE TABLE clients
(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
secret VARCHAR(255) NOT NULL,
needs_grant BOOLEAN NOT NULL DEFAULT false,
redirect_uri_list TEXT NOT NULL DEFAULT '',
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
2 changes: 1 addition & 1 deletion src/controllers/sessions_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn create_session(
Ok(Either::Right(template! {
"session/login.html";
state: String = form.state.unwrap(),
error: Option<String> = Some(String::from("Usernae or password incorrect")),
error: Option<String> = Some(String::from("Username or password incorrect")),
}))
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,24 @@ fn assemble(rocket: Rocket) -> Rocket {
.or_else(|_e| {
User::create(
NewUser {
username: String::from("admin"),
password: String::from(&pw),
username: String::from("admin"),
password: String::from(&pw),
first_name: String::from(""),
last_name: String::from(""),
email: String::from(""),
ssh_key: None,
},
&conn,
)
})
.and_then(|mut user| {
user.change_with(UserChange {
username: None,
password: Some(pw),
username: None,
password: Some(pw),
first_name: None,
last_name: None,
email: None,
ssh_key: None,
})?;
user.admin = true;
user.update(&conn)
Expand Down
4 changes: 4 additions & 0 deletions src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::ConcreteConnection;

use self::schema::clients;

use chrono::{NaiveDateTime, Utc};

const SECRET_LENGTH: usize = 64;

mod schema {
Expand All @@ -17,6 +19,7 @@ mod schema {
secret -> Text,
needs_grant -> Bool,
redirect_uri_list -> Text,
created_at -> Timestamp,
}
}
}
Expand All @@ -28,6 +31,7 @@ pub struct Client {
pub secret: String,
pub needs_grant: bool,
pub redirect_uri_list: String,
pub created_at: NaiveDateTime,
}

#[derive(FromForm, Deserialize, Debug, Clone)]
Expand Down
Loading