From 260f7ff45e394cb2c02944763f18cc4ae65878ef Mon Sep 17 00:00:00 2001 From: mcbloch Date: Wed, 17 Mar 2021 01:34:20 +0100 Subject: [PATCH] separate newclient page, validation on name size --- src/controllers/clients_controller.rs | 9 ++++++ src/lib.rs | 1 + src/models/client.rs | 10 +++++-- static/style.css | 14 +++++++++ templates/clients/index.html | 41 +++++++++++++-------------- templates/clients/new_client.html | 23 +++++++++++++++ 6 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 templates/clients/new_client.html diff --git a/src/controllers/clients_controller.rs b/src/controllers/clients_controller.rs index ed380427..89791944 100644 --- a/src/controllers/clients_controller.rs +++ b/src/controllers/clients_controller.rs @@ -26,6 +26,15 @@ pub fn list_clients( }) } +#[get("/clients/new")] +pub fn create_client_page( + session: AdminSession, +) -> Result> { + Ok(template! { "clients/new_client.html"; + current_user: User = session.admin, + }) +} + #[post("/clients", data = "")] pub fn create_client( client: Api, diff --git a/src/lib.rs b/src/lib.rs index bd53995f..f70f1563 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,7 @@ fn assemble(rocket: Rocket) -> Rocket { routes![ favicon, clients_controller::create_client, + clients_controller::create_client_page, clients_controller::list_clients, oauth_controller::authorize, oauth_controller::grant_get, diff --git a/src/models/client.rs b/src/models/client.rs index fe7b5057..80c43ab5 100644 --- a/src/models/client.rs +++ b/src/models/client.rs @@ -8,6 +8,8 @@ use crate::ConcreteConnection; use self::schema::clients; use chrono::NaiveDateTime; +use validator::{Validate, ValidationError}; + const SECRET_LENGTH: usize = 64; @@ -34,8 +36,9 @@ pub struct Client { pub created_at: NaiveDateTime, } -#[derive(FromForm, Deserialize, Debug, Clone)] +#[derive(Validate, FromForm, Deserialize, Debug, Clone)] pub struct NewClient { + #[validate(length(min = 1, max = 80))] pub name: String, pub needs_grant: bool, pub redirect_uri_list: String, @@ -67,6 +70,7 @@ impl Client { client: NewClient, conn: &ConcreteConnection, ) -> Result { + client.validate()?; let client = NewClientWithSecret { name: client.name, needs_grant: client.needs_grant, @@ -75,11 +79,11 @@ impl Client { }; let client = conn .transaction(|| { - // Create a new user + // Create a new client diesel::insert_into(clients::table) .values(&client) .execute(conn)?; - // Fetch the last created user + // Fetch the last created client clients::table.order(clients::id.desc()).first(conn) }) .map_err(ZauthError::from); diff --git a/static/style.css b/static/style.css index 9228b557..aeaaf551 100644 --- a/static/style.css +++ b/static/style.css @@ -121,3 +121,17 @@ button { justify-content: center; /* Horizontal center */ align-items: center; /* Vertical center */ } + +.centered { + text-align: center; + vertical-align: middle; +} + +.center-end-children { + display: flex; + align-items: center; + justify-content: end; + + width: 100%; + height: 100%; +} \ No newline at end of file diff --git a/templates/clients/index.html b/templates/clients/index.html index fdb43d16..6ca0768f 100644 --- a/templates/clients/index.html +++ b/templates/clients/index.html @@ -1,12 +1,23 @@ {% extends "base_logged_in.html" %} {% block content %} -

Clients ({{ clients.len() }})

+ +
+
+

Clients ({{ clients.len() }})

+
+
+ +
+
+ - - - + + + {% for client in clients %} @@ -16,23 +27,11 @@

Clients ({{ clients.len() }})

{% endfor %} + {% if clients.len() == 0 %} + + + + {% endif %}
Namesecretneeds_grantredirect_uri_listSecretNeeds grantRedirect uri list
{{ client.redirect_uri_list }}
No clients configured
-{% if current_user.admin %} -
-
- - - - - - - - -
-
-{% endif %} - {% endblock content %} diff --git a/templates/clients/new_client.html b/templates/clients/new_client.html new file mode 100644 index 00000000..2517957d --- /dev/null +++ b/templates/clients/new_client.html @@ -0,0 +1,23 @@ +{% extends "base_logged_in.html" %} +{% block content %} + +

Create new client

+ +

Insert explanation about what everything means or what a client is?

+ +
+
+ + + + + + + + +
+
+ +{% endblock content %} \ No newline at end of file