|
@@ -2,6 +2,7 @@ use rocket::request::Form;
|
|
use rocket::Route;
|
|
use rocket::Route;
|
|
use rocket_contrib::json::Json;
|
|
use rocket_contrib::json::Json;
|
|
use serde_json::Value;
|
|
use serde_json::Value;
|
|
|
|
+use num_traits::FromPrimitive;
|
|
|
|
|
|
use crate::api::{
|
|
use crate::api::{
|
|
EmptyResult, JsonResult, JsonUpcase, JsonUpcaseVec, Notify, NumberOrString, PasswordData, UpdateType,
|
|
EmptyResult, JsonResult, JsonUpcase, JsonUpcaseVec, Notify, NumberOrString, PasswordData, UpdateType,
|
|
@@ -45,6 +46,9 @@ pub fn routes() -> Vec<Route> {
|
|
delete_user,
|
|
delete_user,
|
|
post_delete_user,
|
|
post_delete_user,
|
|
post_org_import,
|
|
post_org_import,
|
|
|
|
+ list_policies,
|
|
|
|
+ get_policy,
|
|
|
|
+ put_policy,
|
|
]
|
|
]
|
|
}
|
|
}
|
|
|
|
|
|
@@ -830,22 +834,13 @@ struct RelationsData {
|
|
fn post_org_import(
|
|
fn post_org_import(
|
|
query: Form<OrgIdData>,
|
|
query: Form<OrgIdData>,
|
|
data: JsonUpcase<ImportData>,
|
|
data: JsonUpcase<ImportData>,
|
|
- headers: Headers,
|
|
|
|
|
|
+ headers: AdminHeaders,
|
|
conn: DbConn,
|
|
conn: DbConn,
|
|
nt: Notify,
|
|
nt: Notify,
|
|
) -> EmptyResult {
|
|
) -> EmptyResult {
|
|
let data: ImportData = data.into_inner().data;
|
|
let data: ImportData = data.into_inner().data;
|
|
let org_id = query.into_inner().organization_id;
|
|
let org_id = query.into_inner().organization_id;
|
|
|
|
|
|
- let org_user = match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) {
|
|
|
|
- Some(user) => user,
|
|
|
|
- None => err!("User is not part of the organization"),
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- if org_user.atype < UserOrgType::Admin {
|
|
|
|
- err!("Only admins or owners can import into an organization")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// Read and create the collections
|
|
// Read and create the collections
|
|
let collections: Vec<_> = data
|
|
let collections: Vec<_> = data
|
|
.Collections
|
|
.Collections
|
|
@@ -866,6 +861,8 @@ fn post_org_import(
|
|
relations.push((relation.Key, relation.Value));
|
|
relations.push((relation.Key, relation.Value));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ let headers: Headers = headers.into();
|
|
|
|
+
|
|
// Read and create the ciphers
|
|
// Read and create the ciphers
|
|
let ciphers: Vec<_> = data
|
|
let ciphers: Vec<_> = data
|
|
.Ciphers
|
|
.Ciphers
|
|
@@ -901,3 +898,59 @@ fn post_org_import(
|
|
let mut user = headers.user;
|
|
let mut user = headers.user;
|
|
user.update_revision(&conn)
|
|
user.update_revision(&conn)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+#[get("/organizations/<org_id>/policies")]
|
|
|
|
+fn list_policies(org_id: String, _headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
|
|
|
+ let policies = OrgPolicy::find_by_org(&org_id, &conn);
|
|
|
|
+ let policies_json: Vec<Value> = policies.iter().map(OrgPolicy::to_json).collect();
|
|
|
|
+
|
|
|
|
+ Ok(Json(json!({
|
|
|
|
+ "Data": policies_json,
|
|
|
|
+ "Object": "list",
|
|
|
|
+ "ContinuationToken": null
|
|
|
|
+ })))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[get("/organizations/<org_id>/policies/<pol_type>")]
|
|
|
|
+fn get_policy(org_id: String, pol_type: i32, _headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
|
|
|
+ let pol_type_enum = match OrgPolicyType::from_i32(pol_type) {
|
|
|
|
+ Some(pt) => pt,
|
|
|
|
+ None => err!("Invalid policy type"),
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ let policy = match OrgPolicy::find_by_org_and_type(&org_id, pol_type, &conn) {
|
|
|
|
+ Some(p) => p,
|
|
|
|
+ None => OrgPolicy::new(org_id, pol_type_enum, "{}".to_string()),
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ Ok(Json(policy.to_json()))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[derive(Deserialize)]
|
|
|
|
+struct PolicyData {
|
|
|
|
+ enabled: bool,
|
|
|
|
+ #[serde(rename = "type")]
|
|
|
|
+ _type: i32,
|
|
|
|
+ data: Value,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[put("/organizations/<org_id>/policies/<pol_type>", data = "<data>")]
|
|
|
|
+fn put_policy(org_id: String, pol_type: i32, data: Json<PolicyData>, _headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
|
|
|
+ let data: PolicyData = data.into_inner();
|
|
|
|
+
|
|
|
|
+ let pol_type_enum = match OrgPolicyType::from_i32(pol_type) {
|
|
|
|
+ Some(pt) => pt,
|
|
|
|
+ None => err!("Invalid policy type"),
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ let mut policy = match OrgPolicy::find_by_org_and_type(&org_id, pol_type, &conn) {
|
|
|
|
+ Some(p) => p,
|
|
|
|
+ None => OrgPolicy::new(org_id, pol_type_enum, "{}".to_string()),
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ policy.enabled = data.enabled;
|
|
|
|
+ policy.data = serde_json::to_string(&data.data)?;
|
|
|
|
+ policy.save(&conn)?;
|
|
|
|
+
|
|
|
|
+ Ok(Json(policy.to_json()))
|
|
|
|
+}
|