Add registration.default_role configuration option

This commit is contained in:
silverpill 2023-03-03 18:14:00 +00:00
parent 0995186bc8
commit 6335e216a9
6 changed files with 48 additions and 8 deletions

View file

@ -9,6 +9,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added ### Added
- Allow to add notes to generated invite codes. - Allow to add notes to generated invite codes.
- Added `registration.default_role` configuration option.
### Deprecated
- Deprecated `default_role_read_only_user` configuration option (replaced by `registration.default_role`).
## [1.15.0] - 2023-02-27 ## [1.15.0] - 2023-02-27

View file

@ -25,6 +25,8 @@ instance_description: my instance
registration: registration:
# Possible values: open, invite # Possible values: open, invite
type: invite type: invite
# Possible values: user, read_only_user
default_role: user
# EIP-4361 login message # EIP-4361 login message
#login_message: 'Do not sign this message on other sites!' #login_message: 'Do not sign this message on other sites!'

View file

@ -15,7 +15,7 @@ pub use blockchain::{
pub use config::{Config, Instance}; pub use config::{Config, Instance};
pub use environment::Environment; pub use environment::Environment;
pub use loader::parse_config; pub use loader::parse_config;
pub use registration::RegistrationType; pub use registration::{DefaultRole, RegistrationType};
pub const MITRA_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const MITRA_VERSION: &str = env!("CARGO_PKG_VERSION");

View file

@ -15,7 +15,7 @@ use mitra_utils::{
use super::config::Config; use super::config::Config;
use super::environment::Environment; use super::environment::Environment;
use super::registration::RegistrationType; use super::registration::{DefaultRole, RegistrationType};
struct EnvConfig { struct EnvConfig {
config_path: String, config_path: String,
@ -124,6 +124,14 @@ pub fn parse_config() -> (Config, Vec<&'static str>) {
config.registration.registration_type = RegistrationType::Invite; config.registration.registration_type = RegistrationType::Invite;
}; };
}; };
if let Some(read_only_user) = config.registration.default_role_read_only_user {
warnings.push("'default_role_read_only_user' setting is deprecated, use 'registration.default_role' instead");
if read_only_user {
config.registration.default_role = DefaultRole::ReadOnlyUser;
} else {
config.registration.default_role = DefaultRole::NormalUser;
};
};
if let Some(post_character_limit) = config.post_character_limit { if let Some(post_character_limit) = config.post_character_limit {
warnings.push("'post_character_limit' setting is deprecated, use 'limits.posts.character_limit' instead"); warnings.push("'post_character_limit' setting is deprecated, use 'limits.posts.character_limit' instead");
config.limits.posts.character_limit = post_character_limit; config.limits.posts.character_limit = post_character_limit;

View file

@ -28,11 +28,37 @@ impl<'de> Deserialize<'de> for RegistrationType {
} }
} }
#[derive(Clone)]
pub enum DefaultRole {
NormalUser,
ReadOnlyUser,
}
impl Default for DefaultRole {
fn default() -> Self { Self::NormalUser }
}
impl<'de> Deserialize<'de> for DefaultRole {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let role_str = String::deserialize(deserializer)?;
let role = match role_str.as_str() {
"user" => Self::NormalUser,
"read_only_user" => Self::ReadOnlyUser,
_ => return Err(DeserializerError::custom("unknown role name")),
};
Ok(role)
}
}
#[derive(Clone, Default, Deserialize)] #[derive(Clone, Default, Deserialize)]
pub struct RegistrationConfig { pub struct RegistrationConfig {
#[serde(rename = "type")] #[serde(rename = "type")]
pub registration_type: RegistrationType, pub registration_type: RegistrationType,
pub(super) default_role_read_only_user: Option<bool>, // deprecated
#[serde(default)] #[serde(default)]
pub default_role_read_only_user: bool, // default is false pub default_role: DefaultRole,
} }

View file

@ -11,7 +11,7 @@ use actix_web::{
use actix_web_httpauth::extractors::bearer::BearerAuth; use actix_web_httpauth::extractors::bearer::BearerAuth;
use uuid::Uuid; use uuid::Uuid;
use mitra_config::{Config, RegistrationType}; use mitra_config::{Config, DefaultRole, RegistrationType};
use mitra_utils::{ use mitra_utils::{
caip2::ChainId, caip2::ChainId,
canonicalization::canonicalize_object, canonicalization::canonicalize_object,
@ -184,10 +184,9 @@ pub async fn create_account(
let AccountCreateData { username, invite_code, .. } = let AccountCreateData { username, invite_code, .. } =
account_data.into_inner(); account_data.into_inner();
let role = if config.registration.default_role_read_only_user { let role = match config.registration.default_role {
Role::ReadOnlyUser DefaultRole::NormalUser => Role::NormalUser,
} else { DefaultRole::ReadOnlyUser => Role::ReadOnlyUser,
Role::NormalUser
}; };
let user_data = UserCreateData { let user_data = UserCreateData {
username, username,