Pslink/src/queries.rs

474 lines
16 KiB
Rust
Raw Normal View History

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
use std::path::Path;
2021-02-14 22:28:34 +01:00
use actix_identity::Identity;
use actix_web::web;
use diesel::{prelude::*, sqlite::SqliteConnection};
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
};
/// Create a connection to the database
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
pub(super) fn establish_connection(database_url: &Path) -> Result<SqliteConnection, ServerError> {
match SqliteConnection::establish(&database_url.display().to_string()) {
2021-02-14 22:28:34 +01:00
Ok(c) => Ok(c),
Err(e) => {
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
eprintln!(
"Error connecting to database: {}, {}",
database_url.display(),
e
);
2021-02-14 22:28:34 +01:00
Err(ServerError::User(
"Error connecting to Database".to_string(),
))
}
}
}
/// 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.
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
const fn admin_or_self(&self, id: i32) -> 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.
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
pub(crate) fn authenticate(
id: &Identity,
server_config: &ServerConfig,
) -> Result<Role, ServerError> {
2021-02-14 22:28:34 +01:00
if let Some(username) = id.identity() {
use super::schema::users::dsl;
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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let user = dsl::users
.filter(dsl::username.eq(&username))
.first::<User>(&connection)?;
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.
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
pub(crate) fn list_all_allowed(
id: &Identity,
server_config: &ServerConfig,
) -> Result<List<FullLink>, ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::clicks;
use super::schema::links;
use super::schema::users;
// query to select all users could be const but typespecification is too complex. A filter can be added in the match below.
let query = links::dsl::links
.inner_join(users::dsl::users)
.left_join(clicks::dsl::clicks)
.group_by(links::id)
.select((
(
links::id,
links::title,
links::target,
links::code,
links::author,
links::created_at,
),
(
users::id,
users::username,
users::email,
users::password,
users::role,
),
(diesel::dsl::sql::<diesel::sql_types::Integer>(
"COUNT(clicks.id)",
),),
));
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 authenticate(id, server_config)? {
2021-02-14 22:28:34 +01:00
Role::Admin { user } | Role::Regular { user } => {
// show all links
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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let all_links: Vec<FullLink> = query
.load(&connection)?
.into_iter()
.map(|l: (Link, User, Count)| FullLink {
link: l.0,
user: l.1,
clicks: l.2,
})
.collect();
Ok(List {
user,
list: all_links,
})
}
Role::Disabled | Role::NotAuthenticated => Err(ServerError::User("Not allowed".to_owned())),
}
}
/// Only admins can list all 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
pub(crate) fn list_users(
id: &Identity,
server_config: &ServerConfig,
) -> Result<List<User>, ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::users::dsl::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
match authenticate(id, server_config)? {
2021-02-14 22:28:34 +01:00
Role::Admin { 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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let all_users: Vec<User> = users.load(&connection)?;
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
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
pub(crate) fn get_user(
id: &Identity,
user_id: &str,
server_config: &ServerConfig,
) -> Result<Item<User>, ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::users;
if let Ok(uid) = user_id.parse::<i32>() {
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);
let auth = authenticate(id, server_config)?;
slog_info!(server_config.log, "{:?}", &auth);
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(uid) {
match auth {
Role::Admin { user } | Role::Regular { 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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let viewed_user = users::dsl::users
.filter(users::dsl::id.eq(&uid))
.first::<User>(&connection)?;
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)
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
pub(crate) fn get_user_by_name(
username: &str,
server_config: &ServerConfig,
) -> Result<User, ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let user = users::dsl::users
.filter(users::dsl::username.eq(username))
.first::<User>(&connection)?;
Ok(user)
}
pub(crate) 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);
let auth = authenticate(id, server_config)?;
2021-02-14 22:28:34 +01:00
match auth {
Role::Admin { user } => {
use super::schema::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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
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
)?;
diesel::insert_into(users::table)
.values(&new_user)
.execute(&connection)?;
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
let new_user = get_user_by_name(&data.username, server_config)?;
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-02-14 22:28:34 +01:00
pub(crate) 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> {
if let Ok(uid) = user_id.parse::<i32>() {
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
let auth = authenticate(id, server_config)?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(uid) {
match auth {
Role::Admin { .. } | Role::Regular { .. } => {
use super::schema::users::dsl::{email, id, password, username, 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
slog_info!(server_config.log, "Updating userinfo: ");
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
// Update username and email - if they have not been changed their values will be replaced by the old ones.
diesel::update(users.filter(id.eq(&uid)))
.set((
username.eq(data.username.clone()),
email.eq(data.email.clone()),
))
.execute(&connection)?;
// Update the password only if the user entered something.
if data.password.len() > 3 {
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
let hash = NewUser::hash_password(&data.password, server_config)?;
2021-02-14 22:28:34 +01:00
diesel::update(users.filter(id.eq(&uid)))
.set((password.eq(hash),))
.execute(&connection)?;
}
let changed_user = users.filter(id.eq(&uid)).first::<User>(&connection)?;
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()))
}
}
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
pub(crate) fn toggle_admin(
id: &Identity,
user_id: &str,
server_config: &ServerConfig,
) -> Result<Item<User>, ServerError> {
2021-02-14 22:28:34 +01:00
if let Ok(uid) = user_id.parse::<i32>() {
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
let auth = authenticate(id, server_config)?;
2021-02-14 22:28:34 +01:00
match auth {
Role::Admin { .. } => {
use super::schema::users::dsl::{id, role, 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
slog_info!(server_config.log, "Changing administrator priviledges: ");
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let unchanged_user = users.filter(id.eq(&uid)).first::<User>(&connection)?;
let new_role = 2 - (unchanged_user.role + 1) % 2;
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-02-14 22:28:34 +01:00
"Assigning new role: {} - old was {}",
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
new_role,
unchanged_user.role
2021-02-14 22:28:34 +01:00
);
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
// Update the role eg. admin vs. normal vs. disabled
2021-02-14 22:28:34 +01:00
diesel::update(users.filter(id.eq(&uid)))
.set((role.eq(new_role),))
.execute(&connection)?;
let changed_user = users.filter(id.eq(&uid)).first::<User>(&connection)?;
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.
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
pub(crate) fn get_link(
id: &Identity,
link_code: &str,
server_config: &ServerConfig,
) -> Result<Item<Link>, ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::links::dsl::{code, links};
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 authenticate(id, server_config)? {
2021-02-14 22:28:34 +01:00
Role::Admin { user } | Role::Regular { 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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let link: Link = links
.filter(code.eq(&link_code))
.first::<Link>(&connection)?;
Ok(Item { user, item: link })
}
Role::Disabled | Role::NotAuthenticated => Err(ServerError::User("Not Allowed".to_owned())),
}
}
/// Get link **without authentication**
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
pub(crate) fn get_link_simple(
link_code: &str,
server_config: &ServerConfig,
) -> Result<Link, ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::links::dsl::{code, links};
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 link for {:?}", link_code);
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let link: Link = links
.filter(code.eq(&link_code))
.first::<Link>(&connection)?;
Ok(link)
}
/// Click on a 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
pub(crate) fn click_link(link_id: i32, server_config: &ServerConfig) -> Result<(), ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::clicks;
let new_click = NewClick::new(link_id);
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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
diesel::insert_into(clicks::table)
.values(&new_click)
.execute(&connection)?;
Ok(())
}
/// Click on a 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
pub(crate) fn delete_link(
id: &Identity,
link_code: &str,
server_config: &ServerConfig,
) -> Result<(), ServerError> {
2021-02-14 22:28:34 +01:00
use super::schema::links::dsl::{code, links};
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
let connection = establish_connection(&server_config.db)?;
let auth = authenticate(id, server_config)?;
let link = get_link_simple(link_code, server_config)?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(link.author) {
diesel::delete(links.filter(code.eq(&link_code))).execute(&connection)?;
Ok(())
} else {
Err(ServerError::User("Permission denied!".to_owned()))
}
}
/// Update a link if the user is admin or it is its own link.
pub(crate) 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,
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: &web::Form<LinkForm>,
server_config: &ServerConfig,
2021-02-14 22:28:34 +01:00
) -> Result<Item<Link>, ServerError> {
use super::schema::links::dsl::{code, links, target, title};
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
);
let auth = authenticate(id, server_config)?;
match auth {
2021-02-14 22:28:34 +01:00
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
let query = get_link(id, link_code, server_config)?;
2021-02-14 22:28:34 +01:00
if auth.admin_or_self(query.item.author) {
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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
diesel::update(links.filter(code.eq(&query.item.code)))
.set((
code.eq(&data.code),
target.eq(&data.target),
title.eq(&data.title),
))
.execute(&connection)?;
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
get_link(id, &data.code, server_config)
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())),
}
}
pub(crate) 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> {
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
let auth = authenticate(id, server_config)?;
2021-02-14 22:28:34 +01:00
match auth {
Role::Admin { user } | Role::Regular { user } => {
use super::schema::links;
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
let connection = establish_connection(&server_config.db)?;
2021-02-14 22:28:34 +01:00
let new_link = NewLink::from_link_form(data.into_inner(), user.id);
diesel::insert_into(links::table)
.values(&new_link)
.execute(&connection)?;
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
let new_link = get_link_simple(&new_link.code, server_config)?;
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()))
}
}
}