Pslink/src/queries.rs

423 lines
14 KiB
Rust
Raw Normal View History

2021-02-14 22:28:34 +01:00
use actix_identity::Identity;
use actix_web::web;
use serde::Serialize;
use super::models::{Count, Link, NewUser, User};
use crate::{
forms::LinkForm,
models::{NewClick, NewLink},
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
ServerConfig, ServerError,
2021-02-14 22:28:34 +01:00
};
/// The possible roles a user could have.
#[derive(Debug, Clone)]
pub enum Role {
NotAuthenticated,
Disabled,
Regular { user: User },
Admin { user: User },
}
impl Role {
/// Determin if the user is admin or the given user id is his own. This is used for things where users can edit or view their own entries, whereas admins can do so for all entries.
2021-03-21 08:31:47 +01:00
const fn admin_or_self(&self, id: i64) -> bool {
2021-02-14 22:28:34 +01:00
match self {
Self::Admin { .. } => true,
Self::Regular { user } => user.id == id,
Self::NotAuthenticated | Self::Disabled => false,
}
}
}
/// queries the user matching the given [`actix_identity::Identity`] and determins its authentication and permission level. Returns a [`Role`] containing the user if it is authenticated.
2021-03-21 08:31:47 +01:00
pub(crate) async fn authenticate(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
server_config: &ServerConfig,
) -> Result<Role, ServerError> {
2021-02-14 22:28:34 +01:00
if let Some(username) = id.identity() {
2021-03-21 08:31:47 +01:00
let user = User::get_user_by_name(&username, server_config).await?;
2021-02-14 22:28:34 +01:00
return Ok(match user.role {
0 => Role::Disabled,
1 => Role::Regular { user },
2 => Role::Admin { user },
_ => Role::NotAuthenticated,
});
}
Ok(Role::NotAuthenticated)
}
/// A generic list returntype containing the User and a Vec containing e.g. Links or Users
pub struct List<T> {
pub user: User,
pub list: Vec<T>,
}
/// A link together with its author and its click-count.
#[derive(Serialize)]
pub struct FullLink {
link: Link,
user: User,
clicks: Count,
}
/// Returns a List of `FullLink` meaning `Links` enriched by their author and statistics. This returns all links if the user is either Admin or Regular user.
2021-03-21 08:31:47 +01:00
pub(crate) async fn list_all_allowed(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
server_config: &ServerConfig,
) -> Result<List<FullLink>, ServerError> {
2021-03-21 08:31:47 +01:00
use crate::sqlx::Row;
match authenticate(id, server_config).await? {
2021-02-14 22:28:34 +01:00
Role::Admin { user } | Role::Regular { user } => {
2021-03-21 08:31:47 +01:00
let links = sqlx::query(
"select
links.id as lid,
links.title as ltitle,
links.target as ltarget,
links.code as lcode,
links.author as lauthor,
links.created_at as ldate,
users.id as usid,
users.username as usern,
users.email as uemail,
users.role as urole,
2021-03-30 13:18:13 +02:00
users.language as ulang,
2021-03-21 08:31:47 +01:00
count(clicks.id) as counter
from
links
join users on links.author = users.id
left join clicks on links.id = clicks.link
group by
links.id",
)
.fetch_all(&server_config.db_pool)
.await?
.into_iter()
.map(|v| FullLink {
link: Link {
id: v.get("lid"),
title: v.get("ltitle"),
target: v.get("ltarget"),
code: v.get("lcode"),
author: v.get("lauthor"),
created_at: v.get("ldate"),
},
user: User {
id: v.get("usid"),
username: v.get("usern"),
email: v.get("uemail"),
password: "invalid".to_owned(),
role: v.get("urole"),
2021-03-30 13:18:13 +02:00
language: v.get("ulang"),
2021-03-21 08:31:47 +01:00
},
clicks: Count {
number: v.get("counter"), /* count is never None */
},
});
2021-02-14 22:28:34 +01:00
// show all links
2021-03-21 08:31:47 +01:00
let all_links: Vec<FullLink> = links.collect();
2021-02-14 22:28:34 +01:00
Ok(List {
user,
list: all_links,
})
}
Role::Disabled | Role::NotAuthenticated => Err(ServerError::User("Not allowed".to_owned())),
}
}
/// Only admins can list all users
2021-03-21 08:31:47 +01:00
pub(crate) async fn list_users(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
server_config: &ServerConfig,
) -> Result<List<User>, ServerError> {
2021-03-21 08:31:47 +01:00
match authenticate(id, server_config).await? {
2021-02-14 22:28:34 +01:00
Role::Admin { user } => {
2021-03-21 08:31:47 +01:00
let all_users: Vec<User> = User::get_all_users(server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(List {
user,
list: all_users,
})
}
_ => Err(ServerError::User(
"Administrator permissions required".to_owned(),
)),
}
}
/// A generic returntype containing the User and a single item
pub struct Item<T> {
pub user: User,
pub item: T,
}
/// Get a user if permissions are accordingly
2021-03-21 08:31:47 +01:00
pub(crate) async fn get_user(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
user_id: &str,
server_config: &ServerConfig,
) -> Result<Item<User>, ServerError> {
2021-03-21 08:31:47 +01:00
if let Ok(uid) = user_id.parse::<i64>() {
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
slog_info!(server_config.log, "Getting user {}", uid);
2021-03-21 08:31:47 +01:00
let auth = authenticate(id, server_config).await?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(uid) {
match auth {
Role::Admin { user } | Role::Regular { user } => {
2021-03-21 08:31:47 +01:00
let viewed_user = User::get_user(uid as i64, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(Item {
user,
item: viewed_user,
})
}
Role::Disabled | Role::NotAuthenticated => {
unreachable!("should already be unreachable because of `admin_or_self`")
}
}
} else {
Err(ServerError::User("Permission Denied".to_owned()))
}
} else {
Err(ServerError::User("Permission Denied".to_owned()))
}
}
/// Get a user **without permission checks** (needed for login)
2021-03-21 08:31:47 +01:00
pub(crate) async fn get_user_by_name(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
username: &str,
server_config: &ServerConfig,
) -> Result<User, ServerError> {
2021-03-21 08:31:47 +01:00
let user = User::get_user_by_name(username, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(user)
}
2021-03-21 08:31:47 +01:00
pub(crate) async fn create_user(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
data: &web::Form<NewUser>,
server_config: &ServerConfig,
2021-02-14 22:28:34 +01:00
) -> Result<Item<User>, ServerError> {
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
slog_info!(server_config.log, "Creating a User: {:?}", &data);
2021-03-21 08:31:47 +01:00
let auth = authenticate(id, server_config).await?;
2021-02-14 22:28:34 +01:00
match auth {
Role::Admin { user } => {
let new_user = NewUser::new(
data.username.clone(),
data.email.clone(),
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
&data.password,
server_config,
2021-02-14 22:28:34 +01:00
)?;
2021-03-21 08:31:47 +01:00
new_user.insert_user(server_config).await?;
2021-02-14 22:28:34 +01:00
2021-03-21 08:31:47 +01:00
// querry the new user
let new_user = get_user_by_name(&data.username, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(Item {
user,
item: new_user,
})
}
Role::Regular { .. } | Role::Disabled | Role::NotAuthenticated => {
Err(ServerError::User("Permission denied!".to_owned()))
}
}
}
/// Take a [`actix_web::web::Form<NewUser>`] and update the corresponding entry in the database.
/// The password is only updated if a new password of at least 4 characters is provided.
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
/// The `user_id` is never changed.
2021-03-21 08:31:47 +01:00
pub(crate) async fn update_user(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
2021-02-14 22:28:34 +01:00
user_id: &str,
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
server_config: &ServerConfig,
data: &web::Form<NewUser>,
2021-02-14 22:28:34 +01:00
) -> Result<Item<User>, ServerError> {
2021-03-21 08:31:47 +01:00
if let Ok(uid) = user_id.parse::<i64>() {
let auth = authenticate(id, server_config).await?;
let unmodified_user = User::get_user(uid, server_config).await?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(uid) {
match auth {
Role::Admin { .. } | Role::Regular { .. } => {
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
slog_info!(server_config.log, "Updating userinfo: ");
2021-03-21 08:31:47 +01:00
let password = if data.password.len() > 3 {
NewUser::hash_password(&data.password, server_config)?
} else {
unmodified_user.password
};
let new_user = User {
id: uid,
username: data.username.clone(),
email: data.email.clone(),
password,
role: unmodified_user.role,
2021-03-30 13:18:13 +02:00
language: unmodified_user.language,
2021-03-21 08:31:47 +01:00
};
new_user.update_user(server_config).await?;
let changed_user = User::get_user(uid, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(Item {
user: changed_user.clone(),
item: changed_user,
})
}
Role::NotAuthenticated | Role::Disabled => {
unreachable!("Should be unreachable because of the `admin_or_self`")
}
}
} else {
Err(ServerError::User("Not a valid UID".to_owned()))
}
} else {
Err(ServerError::User("Permission denied".to_owned()))
}
}
2021-03-21 08:31:47 +01:00
pub(crate) async fn toggle_admin(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
user_id: &str,
server_config: &ServerConfig,
) -> Result<Item<User>, ServerError> {
2021-03-21 08:31:47 +01:00
if let Ok(uid) = user_id.parse::<i64>() {
let auth = authenticate(id, server_config).await?;
2021-02-14 22:28:34 +01:00
match auth {
Role::Admin { .. } => {
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
slog_info!(server_config.log, "Changing administrator priviledges: ");
2021-02-14 22:28:34 +01:00
2021-03-21 08:31:47 +01:00
let unchanged_user = User::get_user(uid, server_config).await?;
let old = unchanged_user.role;
unchanged_user.toggle_admin(server_config).await?;
slog_info!(server_config.log, "Toggling role: old was {}", old);
2021-02-14 22:28:34 +01:00
2021-03-21 08:31:47 +01:00
let changed_user = User::get_user(uid, server_config).await?;
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
slog_info!(
server_config.log,
2021-03-21 08:31:47 +01:00
"Toggled role: new is {}",
changed_user.role
2021-02-14 22:28:34 +01:00
);
Ok(Item {
user: changed_user.clone(),
item: changed_user,
})
}
Role::Regular { .. } | Role::NotAuthenticated | Role::Disabled => {
Err(ServerError::User("Permission denied".to_owned()))
}
}
} else {
Err(ServerError::User("Permission denied".to_owned()))
}
}
/// Get one link if permissions are accordingly.
2021-03-21 08:31:47 +01:00
pub(crate) async fn get_link(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
link_code: &str,
server_config: &ServerConfig,
) -> Result<Item<Link>, ServerError> {
2021-03-21 08:31:47 +01:00
match authenticate(id, server_config).await? {
2021-02-14 22:28:34 +01:00
Role::Admin { user } | Role::Regular { user } => {
2021-03-21 08:31:47 +01:00
let link = Link::get_link_by_code(link_code, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(Item { user, item: link })
}
Role::Disabled | Role::NotAuthenticated => Err(ServerError::User("Not Allowed".to_owned())),
}
}
/// Get link **without authentication**
2021-03-21 08:31:47 +01:00
pub(crate) async fn get_link_simple(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
link_code: &str,
server_config: &ServerConfig,
) -> Result<Link, ServerError> {
slog_info!(server_config.log, "Getting link for {:?}", link_code);
2021-03-21 08:31:47 +01:00
let link = Link::get_link_by_code(link_code, server_config).await?;
slog_info!(server_config.log, "Foun d link for {:?}", link);
2021-02-14 22:28:34 +01:00
Ok(link)
}
/// Click on a link
2021-03-21 08:31:47 +01:00
pub(crate) async fn click_link(
link_id: i64,
server_config: &ServerConfig,
) -> Result<(), ServerError> {
slog_info!(server_config.log, "Clicking on {:?}", link_id);
2021-02-14 22:28:34 +01:00
let new_click = NewClick::new(link_id);
2021-03-21 08:31:47 +01:00
new_click.insert_click(server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(())
}
/// Click on a link
2021-03-21 08:31:47 +01:00
pub(crate) async fn delete_link(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
link_code: &str,
server_config: &ServerConfig,
) -> Result<(), ServerError> {
2021-03-21 08:31:47 +01:00
let auth = authenticate(id, server_config).await?;
let link = get_link_simple(link_code, server_config).await?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(link.author) {
2021-03-21 08:31:47 +01:00
Link::delete_link_by_code(link_code, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(())
} else {
Err(ServerError::User("Permission denied!".to_owned()))
}
}
2021-03-21 08:31:47 +01:00
2021-02-14 22:28:34 +01:00
/// Update a link if the user is admin or it is its own link.
2021-03-21 08:31:47 +01:00
pub(crate) async fn update_link(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
2021-02-14 22:28:34 +01:00
link_code: &str,
2021-03-21 08:31:47 +01:00
data: web::Form<LinkForm>,
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
server_config: &ServerConfig,
2021-02-14 22:28:34 +01:00
) -> Result<Item<Link>, ServerError> {
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
slog_info!(
server_config.log,
"Changing link to: {:?} {:?}",
&data,
&link_code
);
2021-03-21 08:31:47 +01:00
let auth = authenticate(id, server_config).await?;
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
match auth {
2021-02-14 22:28:34 +01:00
Role::Admin { .. } | Role::Regular { .. } => {
2021-03-21 08:31:47 +01:00
let query = get_link(id, link_code, server_config).await?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(query.item.author) {
2021-03-21 08:31:47 +01:00
let mut link = query.item;
let LinkForm {
title,
target,
code,
} = data.into_inner();
link.code = code.clone();
link.target = target;
link.title = title;
link.update_link(server_config).await?;
get_link(id, &code, server_config).await
2021-02-14 22:28:34 +01:00
} else {
Err(ServerError::User("Not Allowed".to_owned()))
}
}
Role::Disabled | Role::NotAuthenticated => Err(ServerError::User("Not Allowed".to_owned())),
}
}
2021-03-21 08:31:47 +01:00
pub(crate) async fn create_link(
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
id: &Identity,
2021-02-14 22:28:34 +01:00
data: web::Form<LinkForm>,
Add command line interface Add a command line interface to the binary and remove parts that were hardcoded. new --help is: ``` pslink 0.1.0 Dietrich <dietrich@teilgedanken.de> A simple webservice that allows registered users to create short links including qr-codes. Anyone can visit the shortened links. This is an ideal setup for small busines or for publishing papers. USAGE: pslink [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --db <database> The path of the sqlite database [env: PSLINK_DATABASE=] [default: links.db] -i, --hostip <internal_ip> The host (ip) that will run the pslink service [env: PSLINK_IP=] [default: localhost] -p, --port <port> The port the pslink service will run on [env: PSLINK_PORT=] [default: 8080] -t, --protocol <protocol> The protocol that is used in the qr-codes (http results in slightly smaller codes in some cases) [env: PSLINK_PROTOCOL=] [default: http] [possible values: http, https] -u, --public-url <public_url> The host url or the page that will be part of the short urls. [env: PSLINK_PUBLIC_URL=] [default: localhost:8080] --secret <secret> The secret that is used to encrypt the password database keep this as inacessable as possible. As commandlineparameters are visible to all users it is not wise to use this as a commandline parameter but rather as an environment variable. [env: PSLINK_SECRET=] [default: ] SUBCOMMANDS: runserver Run the server create-admin Create an admin user. generate-env Generate an .env file template using default settings and exit migrate-database Apply any pending migrations and exit help Prints this message or the help of the given subcommand(s) ```
2021-03-07 19:14:34 +01:00
server_config: &ServerConfig,
2021-02-14 22:28:34 +01:00
) -> Result<Item<Link>, ServerError> {
2021-03-21 08:31:47 +01:00
let auth = authenticate(id, server_config).await?;
2021-02-14 22:28:34 +01:00
match auth {
Role::Admin { user } | Role::Regular { user } => {
2021-03-21 08:31:47 +01:00
let code = data.code.clone();
slog_info!(server_config.log, "Creating link for: {}", &code);
2021-02-14 22:28:34 +01:00
let new_link = NewLink::from_link_form(data.into_inner(), user.id);
2021-03-21 08:31:47 +01:00
slog_info!(server_config.log, "Creating link for: {:?}", &new_link);
2021-02-14 22:28:34 +01:00
2021-03-21 08:31:47 +01:00
new_link.insert(server_config).await?;
let new_link = get_link_simple(&code, server_config).await?;
2021-02-14 22:28:34 +01:00
Ok(Item {
user,
item: new_link,
})
}
Role::Disabled | Role::NotAuthenticated => {
Err(ServerError::User("Permission denied!".to_owned()))
}
}
}