-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE enterprisesettings; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE enterprisesettings ( | ||
id bigserial PRIMARY KEY, | ||
admin_device_management BOOLEAN NOT NULL DEFAULT false | ||
); | ||
|
||
INSERT INTO enterprisesettings (admin_device_management) values (false); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use model_derive::Model; | ||
use sqlx::PgExecutor; | ||
use struct_patch::Patch; | ||
|
||
use crate::enterprise::license::{get_cached_license, validate_license}; | ||
|
||
#[derive(Model, Deserialize, Serialize, Patch)] | ||
#[patch(attribute(derive(Serialize, Deserialize)))] | ||
pub struct EnterpriseSettings { | ||
#[serde(skip)] | ||
pub id: Option<i64>, | ||
// If true, only admins can manage devices | ||
pub admin_device_management: bool, | ||
} | ||
|
||
// We want to be conscious of what the defaults are here | ||
#[allow(clippy::derivable_impls)] | ||
impl Default for EnterpriseSettings { | ||
fn default() -> Self { | ||
Self { | ||
id: None, | ||
admin_device_management: false, | ||
} | ||
} | ||
} | ||
|
||
impl EnterpriseSettings { | ||
/// If license is valid returns current [`EnterpriseSettings`] object. | ||
/// Otherwise returns [`EnterpriseSettings::default()`]. | ||
pub async fn get<'e, E>(executor: E) -> Result<Self, sqlx::Error> | ||
where | ||
E: PgExecutor<'e>, | ||
{ | ||
// avoid holding the rwlock across await, makes the future !Send | ||
// and therefore unusable in axum handlers | ||
let is_valid = { | ||
let license = get_cached_license(); | ||
validate_license(license.as_ref()).is_ok() | ||
}; | ||
if is_valid { | ||
let settings = Self::find_by_id(executor, 1).await?; | ||
Ok(settings.expect("EnterpriseSettings not found")) | ||
} else { | ||
Ok(EnterpriseSettings::default()) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod enterprise_settings; | ||
pub mod openid_provider; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use axum::{extract::State, http::StatusCode, Json}; | ||
use serde_json::json; | ||
use struct_patch::Patch; | ||
|
||
use super::LicenseInfo; | ||
use crate::{ | ||
appstate::AppState, | ||
auth::{AdminRole, SessionInfo}, | ||
enterprise::db::models::enterprise_settings::{EnterpriseSettings, EnterpriseSettingsPatch}, | ||
handlers::{ApiResponse, ApiResult}, | ||
}; | ||
|
||
pub async fn get_enterprise_settings( | ||
session: SessionInfo, | ||
State(appstate): State<AppState>, | ||
) -> ApiResult { | ||
debug!( | ||
"User {} retrieving enterprise settings", | ||
session.user.username | ||
); | ||
let settings = EnterpriseSettings::get(&appstate.pool).await?; | ||
info!( | ||
"User {} retrieved enterprise settings", | ||
session.user.username | ||
); | ||
Ok(ApiResponse { | ||
json: json!(settings), | ||
status: StatusCode::OK, | ||
}) | ||
} | ||
|
||
pub async fn patch_enterprise_settings( | ||
_license: LicenseInfo, | ||
_admin: AdminRole, | ||
State(appstate): State<AppState>, | ||
session: SessionInfo, | ||
Json(data): Json<EnterpriseSettingsPatch>, | ||
) -> ApiResult { | ||
debug!( | ||
"Admin {} patching enterprise settings.", | ||
session.user.username, | ||
); | ||
let mut settings = EnterpriseSettings::get(&appstate.pool).await?; | ||
|
||
settings.apply(data); | ||
settings.save(&appstate.pool).await?; | ||
info!("Admin {} patched settings.", session.user.username); | ||
Ok(ApiResponse::default()) | ||
} |