2018-02-23 00:38:54 +01:00
|
|
|
#![allow(unused_imports)]
|
|
|
|
|
2018-02-17 22:30:19 +01:00
|
|
|
use rocket_contrib::{Json, Value};
|
|
|
|
|
|
|
|
use db::DbConn;
|
|
|
|
use db::models::*;
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
use api::{PasswordData, JsonResult, EmptyResult, NumberOrString};
|
2018-05-30 14:28:31 +02:00
|
|
|
use auth::{Headers, AdminHeaders, OwnerHeaders};
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct OrgData {
|
|
|
|
billingEmail: String,
|
|
|
|
collectionName: String,
|
|
|
|
key: String,
|
|
|
|
name: String,
|
|
|
|
planType: String,
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct OrganizationUpdateData {
|
|
|
|
billingEmail: String,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct NewCollectionData {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
2018-02-17 22:30:19 +01:00
|
|
|
#[post("/organizations", data = "<data>")]
|
2018-04-24 22:01:55 +02:00
|
|
|
fn create_organization(headers: Headers, data: Json<OrgData>, conn: DbConn) -> JsonResult {
|
|
|
|
let data: OrgData = data.into_inner();
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
let mut org = Organization::new(data.name, data.billingEmail);
|
|
|
|
let mut user_org = UserOrganization::new(
|
2018-04-20 18:35:11 +02:00
|
|
|
headers.user.uuid.clone(), org.uuid.clone());
|
|
|
|
let mut collection = Collection::new(
|
|
|
|
org.uuid.clone(), data.collectionName);
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
user_org.key = data.key;
|
|
|
|
user_org.access_all = true;
|
|
|
|
user_org.type_ = UserOrgType::Owner as i32;
|
|
|
|
user_org.status = UserOrgStatus::Confirmed as i32;
|
|
|
|
|
|
|
|
org.save(&conn);
|
|
|
|
user_org.save(&conn);
|
2018-04-20 18:35:11 +02:00
|
|
|
collection.save(&conn);
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
Ok(Json(org.to_json()))
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
#[post("/organizations/<org_id>/delete", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn delete_organization(org_id: String, data: Json<PasswordData>, headers: OwnerHeaders, conn: DbConn) -> EmptyResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
let data: PasswordData = data.into_inner();
|
|
|
|
let password_hash = data.masterPasswordHash;
|
|
|
|
|
2018-05-18 17:52:51 +02:00
|
|
|
if !headers.user.check_valid_password(&password_hash) {
|
|
|
|
err!("Invalid password")
|
|
|
|
}
|
|
|
|
|
|
|
|
match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
None => err!("Organization not found"),
|
|
|
|
Some(org) => match org.delete(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed deleting the organization")
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[get("/organizations/<org_id>")]
|
2018-05-30 14:28:31 +02:00
|
|
|
fn get_organization(org_id: String, headers: OwnerHeaders, conn: DbConn) -> JsonResult {
|
2018-04-20 18:35:11 +02:00
|
|
|
match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(organization) => Ok(Json(organization.to_json())),
|
|
|
|
None => err!("Can't find organization details")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn post_organization(org_id: String, headers: OwnerHeaders, data: Json<OrganizationUpdateData>, conn: DbConn) -> JsonResult {
|
2018-04-20 18:35:11 +02:00
|
|
|
let data: OrganizationUpdateData = data.into_inner();
|
|
|
|
|
|
|
|
let mut org = match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(organization) => organization,
|
|
|
|
None => err!("Can't find organization details")
|
|
|
|
};
|
|
|
|
|
|
|
|
org.name = data.name;
|
|
|
|
org.billing_email = data.billingEmail;
|
|
|
|
org.save(&conn);
|
|
|
|
|
|
|
|
Ok(Json(org.to_json()))
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
|
|
|
|
// GET /api/collections?writeOnly=false
|
|
|
|
#[get("/collections")]
|
|
|
|
fn get_user_collections(headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
|
|
|
|
Ok(Json(json!({
|
2018-04-20 18:35:11 +02:00
|
|
|
"Data":
|
|
|
|
Collection::find_by_user_uuid(&headers.user.uuid, &conn)
|
|
|
|
.iter()
|
|
|
|
.map(|collection| {
|
|
|
|
collection.to_json()
|
|
|
|
}).collect::<Value>(),
|
2018-02-17 22:30:19 +01:00
|
|
|
"Object": "list"
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/organizations/<org_id>/collections")]
|
2018-05-30 14:28:31 +02:00
|
|
|
fn get_org_collections(org_id: String, headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
2018-02-17 22:30:19 +01:00
|
|
|
Ok(Json(json!({
|
2018-04-20 18:35:11 +02:00
|
|
|
"Data":
|
2018-05-11 20:08:02 +02:00
|
|
|
Collection::find_by_organization(&org_id, &conn)
|
2018-04-20 18:35:11 +02:00
|
|
|
.iter()
|
|
|
|
.map(|collection| {
|
|
|
|
collection.to_json()
|
|
|
|
}).collect::<Value>(),
|
2018-02-17 22:30:19 +01:00
|
|
|
"Object": "list"
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[post("/organizations/<org_id>/collections", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn post_organization_collections(org_id: String, headers: AdminHeaders, data: Json<NewCollectionData>, conn: DbConn) -> JsonResult {
|
2018-04-20 18:35:11 +02:00
|
|
|
let data: NewCollectionData = data.into_inner();
|
|
|
|
|
|
|
|
let org = match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(organization) => organization,
|
|
|
|
None => err!("Can't find organization details")
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut collection = Collection::new(org.uuid.clone(), data.name);
|
|
|
|
|
|
|
|
collection.save(&conn);
|
2018-05-04 19:25:50 +02:00
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
Ok(Json(collection.to_json()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/collections/<col_id>", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn post_organization_collection_update(org_id: String, col_id: String, headers: AdminHeaders, data: Json<NewCollectionData>, conn: DbConn) -> JsonResult {
|
2018-04-20 18:35:11 +02:00
|
|
|
let data: NewCollectionData = data.into_inner();
|
|
|
|
|
|
|
|
let org = match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(organization) => organization,
|
|
|
|
None => err!("Can't find organization details")
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut collection = match Collection::find_by_uuid(&col_id, &conn) {
|
|
|
|
Some(collection) => collection,
|
|
|
|
None => err!("Collection not found")
|
|
|
|
};
|
|
|
|
|
|
|
|
collection.name = data.name.clone();
|
|
|
|
collection.save(&conn);
|
|
|
|
|
|
|
|
Ok(Json(collection.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:01:38 +02:00
|
|
|
#[post("/organizations/<org_id>/collections/<col_id>/delete-user/<org_user_id>")]
|
2018-05-30 17:01:56 +02:00
|
|
|
fn post_organization_collection_delete_user(org_id: String, col_id: String, org_user_id: String, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
let collection = match Collection::find_by_uuid(&col_id, &conn) {
|
|
|
|
None => err!("Collection not found"),
|
|
|
|
Some(collection) => if collection.org_uuid == org_id {
|
|
|
|
collection
|
|
|
|
} else {
|
|
|
|
err!("Collection and Organization id do not match")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match UserOrganization::find_by_uuid(&org_user_id, &conn) {
|
|
|
|
None => err!("User not found in organization"),
|
|
|
|
Some(user_org) => {
|
|
|
|
match CollectionUser::find_by_collection_and_user(&collection.uuid, &user_org.user_uuid, &conn) {
|
|
|
|
None => err!("User not assigned to collection"),
|
|
|
|
Some(col_user) => {
|
|
|
|
match col_user.delete(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed removing user from collection")
|
2018-05-29 17:01:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 00:05:50 +02:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct DeleteCollectionData {
|
|
|
|
id: String,
|
|
|
|
orgId: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/collections/<col_id>/delete", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn post_organization_collection_delete(org_id: String, col_id: String, headers: AdminHeaders, data: Json<DeleteCollectionData>, conn: DbConn) -> EmptyResult {
|
|
|
|
match Collection::find_by_uuid(&col_id, &conn) {
|
|
|
|
None => err!("Collection not found"),
|
|
|
|
Some(collection) => if collection.org_uuid == org_id {
|
|
|
|
match collection.delete(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed deleting collection")
|
2018-05-17 00:05:50 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-30 18:12:18 +02:00
|
|
|
err!("Collection and Organization id do not match")
|
2018-05-17 00:05:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[get("/organizations/<org_id>/collections/<coll_id>/details")]
|
2018-05-30 14:28:31 +02:00
|
|
|
fn get_org_collection_detail(org_id: String, coll_id: String, headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
2018-04-20 18:35:11 +02:00
|
|
|
match Collection::find_by_uuid_and_user(&coll_id, &headers.user.uuid, &conn) {
|
|
|
|
None => err!("Collection not found"),
|
|
|
|
Some(collection) => Ok(Json(collection.to_json()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
#[get("/organizations/<org_id>/collections/<coll_id>/users")]
|
2018-05-30 14:28:31 +02:00
|
|
|
fn get_collection_users(org_id: String, coll_id: String, headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
// Get org and collection, check that collection is from org
|
2018-05-29 17:01:38 +02:00
|
|
|
let collection = match Collection::find_by_uuid_and_org(&coll_id, &org_id, &conn) {
|
|
|
|
None => err!("Collection not found in Organization"),
|
|
|
|
Some(collection) => collection
|
|
|
|
};
|
2018-04-25 00:34:40 +02:00
|
|
|
|
|
|
|
// Get the users from collection
|
2018-05-29 17:01:38 +02:00
|
|
|
let user_list: Vec<Value> = CollectionUser::find_by_collection(&collection.uuid, &conn)
|
|
|
|
.iter().map(|col_user| {
|
|
|
|
UserOrganization::find_by_user_and_org(&col_user.user_uuid, &org_id, &conn)
|
|
|
|
.unwrap()
|
|
|
|
.to_json_collection_user_details(&col_user.read_only, &conn)
|
|
|
|
}).collect();
|
2018-04-25 00:34:40 +02:00
|
|
|
|
|
|
|
Ok(Json(json!({
|
2018-05-29 17:01:38 +02:00
|
|
|
"Data": user_list,
|
2018-04-25 00:34:40 +02:00
|
|
|
"Object": "list"
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct OrgIdData {
|
|
|
|
organizationId: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/ciphers/organization-details?<data>")]
|
|
|
|
fn get_org_details(data: OrgIdData, headers: Headers, conn: DbConn) -> JsonResult {
|
2018-04-27 13:49:34 +02:00
|
|
|
let ciphers = Cipher::find_by_org(&data.organizationId, &conn);
|
2018-04-30 11:52:15 +02:00
|
|
|
let ciphers_json: Vec<Value> = ciphers.iter().map(|c| c.to_json(&headers.host, &headers.user.uuid, &conn)).collect();
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
Ok(Json(json!({
|
2018-04-27 13:49:34 +02:00
|
|
|
"Data": ciphers_json,
|
|
|
|
"Object": "list",
|
2018-04-24 22:01:55 +02:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/organizations/<org_id>/users")]
|
2018-05-30 14:28:31 +02:00
|
|
|
fn get_org_users(org_id: String, headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) {
|
|
|
|
Some(_) => (),
|
2018-04-24 23:04:10 +02:00
|
|
|
None => err!("User isn't member of organization")
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
let users = UserOrganization::find_by_org(&org_id, &conn);
|
2018-04-25 00:34:40 +02:00
|
|
|
let users_json: Vec<Value> = users.iter().map(|c| c.to_json_user_details(&conn)).collect();
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
Ok(Json(json!({
|
|
|
|
"Data": users_json,
|
|
|
|
"Object": "list"
|
|
|
|
})))
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
2018-04-25 00:34:40 +02:00
|
|
|
struct CollectionData {
|
2018-04-24 22:01:55 +02:00
|
|
|
id: String,
|
|
|
|
readOnly: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct InviteData {
|
|
|
|
emails: Vec<String>,
|
|
|
|
#[serde(rename = "type")]
|
2018-05-04 19:25:50 +02:00
|
|
|
type_: NumberOrString,
|
2018-04-25 00:34:40 +02:00
|
|
|
collections: Vec<CollectionData>,
|
2018-05-28 18:26:02 +02:00
|
|
|
accessAll: Option<bool>,
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/users/invite", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn send_invite(org_id: String, data: Json<InviteData>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-04-24 22:01:55 +02:00
|
|
|
let data: InviteData = data.into_inner();
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
let new_type = match UserOrgType::from_str(&data.type_.to_string()) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(new_type) => new_type as i32,
|
|
|
|
None => err!("Invalid type")
|
|
|
|
};
|
|
|
|
|
|
|
|
if new_type != UserOrgType::User as i32 &&
|
2018-05-30 18:12:18 +02:00
|
|
|
headers.org_user_type != UserOrgType::Owner as i32 {
|
2018-04-24 23:04:10 +02:00
|
|
|
err!("Only Owners can invite Admins or Owners")
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
for user_opt in data.emails.iter().map(|email| User::find_by_mail(email, &conn)) {
|
|
|
|
match user_opt {
|
|
|
|
None => err!("User email does not exist"),
|
|
|
|
Some(user) => {
|
2018-04-24 23:04:10 +02:00
|
|
|
match UserOrganization::find_by_user_and_org(&user.uuid, &org_id, &conn) {
|
|
|
|
Some(_) => err!("User already in organization"),
|
|
|
|
None => ()
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-05-28 18:26:02 +02:00
|
|
|
let mut new_user = UserOrganization::new(user.uuid.clone(), org_id.clone());
|
|
|
|
let access_all = data.accessAll.unwrap_or(false);
|
|
|
|
new_user.access_all = access_all;
|
2018-05-04 19:25:50 +02:00
|
|
|
new_user.type_ = new_type;
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
// If no accessAll, add the collections received
|
2018-05-28 18:26:02 +02:00
|
|
|
if !access_all {
|
|
|
|
for col in data.collections.iter() {
|
|
|
|
match Collection::find_by_uuid_and_org(&col.id, &org_id, &conn) {
|
|
|
|
None => err!("Collection not found in Organization"),
|
|
|
|
Some(collection) => {
|
|
|
|
match CollectionUser::save(&user.uuid, &collection.uuid, col.readOnly, &conn) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(_) => err!("Failed saving collection access for user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
new_user.save(&conn);
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/users/<user_id>/confirm", data = "<data>")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn confirm_invite(org_id: String, user_id: String, data: Json<Value>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
let mut user_to_confirm = match UserOrganization::find_by_uuid(&user_id, &conn) {
|
|
|
|
Some(user) => user,
|
|
|
|
None => err!("User to confirm isn't member of the organization")
|
|
|
|
};
|
|
|
|
|
|
|
|
if user_to_confirm.type_ != UserOrgType::User as i32 &&
|
2018-05-30 18:12:18 +02:00
|
|
|
headers.org_user_type != UserOrgType::Owner as i32 {
|
2018-04-24 23:04:10 +02:00
|
|
|
err!("Only Owners can confirm Admins or Owners")
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
if user_to_confirm.status != UserOrgStatus::Accepted as i32 {
|
2018-04-24 22:01:55 +02:00
|
|
|
err!("User in invalid state")
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
user_to_confirm.status = UserOrgStatus::Confirmed as i32;
|
|
|
|
user_to_confirm.key = match data["key"].as_str() {
|
2018-04-24 22:01:55 +02:00
|
|
|
Some(key) => key.to_string(),
|
|
|
|
None => err!("Invalid key provided")
|
|
|
|
};
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
user_to_confirm.save(&conn);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/organizations/<org_id>/users/<user_id>")]
|
2018-05-30 14:28:31 +02:00
|
|
|
fn get_user(org_id: String, user_id: String, headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
let user = match UserOrganization::find_by_uuid(&user_id, &conn) {
|
|
|
|
Some(user) => user,
|
|
|
|
None => err!("The specified user isn't member of the organization")
|
|
|
|
};
|
|
|
|
|
2018-05-11 20:08:02 +02:00
|
|
|
Ok(Json(user.to_json_details(&conn)))
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct EditUserData {
|
|
|
|
#[serde(rename = "type")]
|
2018-05-04 19:25:50 +02:00
|
|
|
type_: NumberOrString,
|
2018-04-25 00:34:40 +02:00
|
|
|
collections: Vec<CollectionData>,
|
|
|
|
accessAll: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/users/<user_id>", data = "<data>", rank = 1)]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn edit_user(org_id: String, user_id: String, data: Json<EditUserData>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
let data: EditUserData = data.into_inner();
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
let new_type = match UserOrgType::from_str(&data.type_.to_string()) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(new_type) => new_type as i32,
|
|
|
|
None => err!("Invalid type")
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut user_to_edit = match UserOrganization::find_by_uuid(&user_id, &conn) {
|
|
|
|
Some(user) => user,
|
|
|
|
None => err!("The specified user isn't member of the organization")
|
|
|
|
};
|
|
|
|
|
|
|
|
if new_type != UserOrgType::User as i32 &&
|
2018-05-30 18:12:18 +02:00
|
|
|
headers.org_user_type != UserOrgType::Owner as i32 {
|
2018-04-25 00:34:40 +02:00
|
|
|
err!("Only Owners can grant Admin or Owner type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_to_edit.type_ != UserOrgType::User as i32 &&
|
2018-05-30 18:12:18 +02:00
|
|
|
headers.org_user_type != UserOrgType::Owner as i32 {
|
2018-04-25 00:34:40 +02:00
|
|
|
err!("Only Owners can edit Admin or Owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_to_edit.type_ == UserOrgType::Owner as i32 &&
|
|
|
|
new_type != UserOrgType::Owner as i32 {
|
|
|
|
|
|
|
|
// Removing owner permmission, check that there are at least another owner
|
|
|
|
let num_owners = UserOrganization::find_by_org_and_type(
|
|
|
|
&org_id, UserOrgType::Owner as i32, &conn)
|
|
|
|
.len();
|
|
|
|
|
|
|
|
if num_owners <= 1 {
|
|
|
|
err!("Can't delete the last owner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
user_to_edit.access_all = data.accessAll;
|
|
|
|
user_to_edit.type_ = new_type;
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
// Delete all the odd collections
|
2018-05-29 17:01:38 +02:00
|
|
|
for c in CollectionUser::find_by_organization_and_user_uuid(&org_id, &user_to_edit.user_uuid, &conn) {
|
|
|
|
match c.delete(&conn) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(_) => err!("Failed deleting old collection assignment")
|
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no accessAll, add the collections received
|
|
|
|
if !data.accessAll {
|
2018-05-28 18:26:02 +02:00
|
|
|
for col in data.collections.iter() {
|
|
|
|
match Collection::find_by_uuid_and_org(&col.id, &org_id, &conn) {
|
|
|
|
None => err!("Collection not found in Organization"),
|
|
|
|
Some(collection) => {
|
|
|
|
match CollectionUser::save(&user_to_edit.user_uuid, &collection.uuid, col.readOnly, &conn) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(_) => err!("Failed saving collection access for user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
user_to_edit.save(&conn);
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/users/<user_id>/delete")]
|
2018-05-30 18:12:18 +02:00
|
|
|
fn delete_user(org_id: String, user_id: String, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-04-25 00:34:40 +02:00
|
|
|
let user_to_delete = match UserOrganization::find_by_uuid(&user_id, &conn) {
|
|
|
|
Some(user) => user,
|
|
|
|
None => err!("User to delete isn't member of the organization")
|
|
|
|
};
|
|
|
|
|
|
|
|
if user_to_delete.type_ != UserOrgType::User as i32 &&
|
2018-05-30 18:12:18 +02:00
|
|
|
headers.org_user_type != UserOrgType::Owner as i32 {
|
2018-04-24 23:04:10 +02:00
|
|
|
err!("Only Owners can delete Admins or Owners")
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
if user_to_delete.type_ == UserOrgType::Owner as i32 {
|
|
|
|
// Removing owner, check that there are at least another owner
|
|
|
|
let num_owners = UserOrganization::find_by_org_and_type(
|
|
|
|
&org_id, UserOrgType::Owner as i32, &conn)
|
|
|
|
.len();
|
|
|
|
|
|
|
|
if num_owners <= 1 {
|
|
|
|
err!("Can't delete the last owner")
|
|
|
|
}
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-05-18 17:52:51 +02:00
|
|
|
match user_to_delete.delete(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed deleting user from organization")
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|