Skip to content

Commit

Permalink
separate newclient page, validation on name size
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbloch committed Mar 17, 2021
1 parent 1817736 commit 260f7ff
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 24 deletions.
9 changes: 9 additions & 0 deletions src/controllers/clients_controller.rs
Expand Up @@ -26,6 +26,15 @@ pub fn list_clients(
})
}

#[get("/clients/new")]
pub fn create_client_page(
session: AdminSession,
) -> Result<impl Responder<'static>> {
Ok(template! { "clients/new_client.html";
current_user: User = session.admin,
})
}

#[post("/clients", data = "<client>")]
pub fn create_client(
client: Api<NewClient>,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions src/models/client.rs
Expand Up @@ -8,6 +8,8 @@ use crate::ConcreteConnection;
use self::schema::clients;

use chrono::NaiveDateTime;
use validator::{Validate, ValidationError};


const SECRET_LENGTH: usize = 64;

Expand All @@ -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,
Expand Down Expand Up @@ -67,6 +70,7 @@ impl Client {
client: NewClient,
conn: &ConcreteConnection,
) -> Result<Client> {
client.validate()?;
let client = NewClientWithSecret {
name: client.name,
needs_grant: client.needs_grant,
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions static/style.css
Expand Up @@ -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%;
}
41 changes: 20 additions & 21 deletions templates/clients/index.html
@@ -1,12 +1,23 @@
{% extends "base_logged_in.html" %}
{% block content %}
<h1>Clients ({{ clients.len() }})</h1>

<div class="row">
<div class="column">
<h1>Clients ({{ clients.len() }})</h1>
</div>
<div class="column">
<div class="center-end-children">
<a class="button" href="/clients/new">New client</a>
</div>
</div>
</div>

<table>
<tr>
<th>Name</th>
<th>secret</th>
<th>needs_grant</th>
<th>redirect_uri_list</th>
<th>Secret</th>
<th>Needs grant</th>
<th>Redirect uri list</th>
</tr>
{% for client in clients %}
<tr>
Expand All @@ -16,23 +27,11 @@ <h1>Clients ({{ clients.len() }})</h1>
<td>{{ client.redirect_uri_list }}</td>
</tr>
{% endfor %}
{% if clients.len() == 0 %}
<tr class="centered">
<td colspan="4">No clients configured</td>
</tr>
{% endif %}
</table>

{% if current_user.admin %}
<form action="/clients" method="post">
<fieldset>
<label for="name">Name</label>
<input type="text" placeholder="application name" id="name" name="name">
<label for="needs_grant">Needs_grant</label>
<input type="checkbox" id="needs_grant" name="needs_grant">
<label for="redirect_uri_list">Redirect Uri's</label>
<!-- <input type="textarea" id="redirect_uri_list" name="redirect_uri_list">-->
<textarea id="redirect_uri_list" name="redirect_uri_list" rows="4" cols="50">
test
</textarea>
<button type="submit">Create</button>
</fieldset>
</form>
{% endif %}

{% endblock content %}
23 changes: 23 additions & 0 deletions templates/clients/new_client.html
@@ -0,0 +1,23 @@
{% extends "base_logged_in.html" %}
{% block content %}

<h1>Create new client</h1>

<p>Insert explanation about what everything means or what a client is?</p>

<form action="/clients" method="post">
<fieldset>
<label for="name">Name</label>
<input type="text" placeholder="application name" id="name" name="name" required="required" minlength="1" maxlength="80">
<label for="needs_grant">Needs_grant</label>
<input type="checkbox" id="needs_grant" name="needs_grant">
<label for="redirect_uri_list">Redirect Uri's</label>
<!-- <input type="textarea" id="redirect_uri_list" name="redirect_uri_list">-->
<textarea id="redirect_uri_list" name="redirect_uri_list" rows="4" cols="50">
test
</textarea>
<button type="submit">Create</button>
</fieldset>
</form>

{% endblock content %}

0 comments on commit 260f7ff

Please sign in to comment.