2018-09-13 16:04:00 +02:00
|
|
|
use rocket::State;
|
2018-10-10 20:40:39 +02:00
|
|
|
use rocket::request::Form;
|
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::CONFIG;
|
|
|
|
use crate::db::DbConn;
|
|
|
|
use crate::db::models::*;
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::api::{PasswordData, JsonResult, EmptyResult, NumberOrString, JsonUpcase, WebSocketUsers, UpdateType};
|
2018-12-15 03:56:00 +01:00
|
|
|
use crate::auth::{Headers, AdminHeaders, OwnerHeaders, encode_jwt, decode_invite_jwt, InviteJWTClaims, JWT_ISSUER};
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-08-14 12:06:42 +02:00
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
use rocket::Route;
|
|
|
|
|
|
|
|
pub fn routes() -> Vec<Route> {
|
|
|
|
routes![
|
|
|
|
get_organization,
|
|
|
|
create_organization,
|
|
|
|
delete_organization,
|
|
|
|
post_delete_organization,
|
|
|
|
leave_organization,
|
|
|
|
get_user_collections,
|
|
|
|
get_org_collections,
|
|
|
|
get_org_collection_detail,
|
|
|
|
get_collection_users,
|
|
|
|
put_organization,
|
|
|
|
post_organization,
|
|
|
|
post_organization_collections,
|
|
|
|
delete_organization_collection_user,
|
|
|
|
post_organization_collection_delete_user,
|
|
|
|
post_organization_collection_update,
|
|
|
|
put_organization_collection_update,
|
|
|
|
delete_organization_collection,
|
|
|
|
post_organization_collection_delete,
|
|
|
|
get_org_details,
|
|
|
|
get_org_users,
|
|
|
|
send_invite,
|
|
|
|
confirm_invite,
|
2018-12-15 03:56:00 +01:00
|
|
|
accept_invite,
|
2018-10-10 20:40:39 +02:00
|
|
|
get_user,
|
|
|
|
edit_user,
|
|
|
|
put_organization_user,
|
|
|
|
delete_user,
|
|
|
|
post_delete_user,
|
|
|
|
post_reinvite_user,
|
|
|
|
post_org_import,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct OrgData {
|
2018-06-01 00:18:50 +02:00
|
|
|
BillingEmail: String,
|
|
|
|
CollectionName: String,
|
|
|
|
Key: String,
|
|
|
|
Name: String,
|
|
|
|
#[serde(rename = "PlanType")]
|
2018-07-21 17:27:00 +02:00
|
|
|
_PlanType: NumberOrString, // Ignored, always use the same plan
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct OrganizationUpdateData {
|
2018-06-01 00:18:50 +02:00
|
|
|
BillingEmail: String,
|
|
|
|
Name: String,
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct NewCollectionData {
|
2018-06-01 00:18:50 +02:00
|
|
|
Name: String,
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 22:30:19 +01:00
|
|
|
#[post("/organizations", data = "<data>")]
|
2018-06-01 00:18:50 +02:00
|
|
|
fn create_organization(headers: Headers, data: JsonUpcase<OrgData>, conn: DbConn) -> JsonResult {
|
|
|
|
let data: OrgData = data.into_inner().data;
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let mut org = Organization::new(data.Name, data.BillingEmail);
|
2018-04-24 22:01:55 +02:00
|
|
|
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(
|
2018-06-01 00:18:50 +02:00
|
|
|
org.uuid.clone(), data.CollectionName);
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
user_org.key = data.Key;
|
2018-04-24 22:01:55 +02:00
|
|
|
user_org.access_all = true;
|
|
|
|
user_org.type_ = UserOrgType::Owner as i32;
|
|
|
|
user_org.status = UserOrgStatus::Confirmed as i32;
|
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
if org.save(&conn).is_err() {
|
|
|
|
err!("Failed creating organization")
|
|
|
|
}
|
|
|
|
if user_org.save(&conn).is_err() {
|
|
|
|
err!("Failed to add user to organization")
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:50:31 +02:00
|
|
|
if collection.save(&conn).is_err() {
|
|
|
|
err!("Failed creating Collection");
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
Ok(Json(org.to_json()))
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 17:45:30 +02:00
|
|
|
#[delete("/organizations/<org_id>", data = "<data>")]
|
2018-06-01 00:18:50 +02:00
|
|
|
fn delete_organization(org_id: String, data: JsonUpcase<PasswordData>, headers: OwnerHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
let data: PasswordData = data.into_inner().data;
|
|
|
|
let password_hash = data.MasterPasswordHash;
|
2018-04-25 00:34:40 +02:00
|
|
|
|
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-08-13 17:45:30 +02:00
|
|
|
#[post("/organizations/<org_id>/delete", data = "<data>")]
|
|
|
|
fn post_delete_organization(org_id: String, data: JsonUpcase<PasswordData>, headers: OwnerHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
delete_organization(org_id, data, headers, conn)
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:30:03 +02:00
|
|
|
#[post("/organizations/<org_id>/leave")]
|
|
|
|
fn leave_organization(org_id: String, headers: Headers, conn: DbConn) -> EmptyResult {
|
|
|
|
match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) {
|
|
|
|
None => err!("User not part of organization"),
|
|
|
|
Some(user_org) => {
|
2018-11-12 18:13:25 +01:00
|
|
|
if user_org.type_ == UserOrgType::Owner {
|
2018-07-11 16:30:03 +02:00
|
|
|
let num_owners = UserOrganization::find_by_org_and_type(
|
|
|
|
&org_id, UserOrgType::Owner as i32, &conn)
|
|
|
|
.len();
|
|
|
|
|
|
|
|
if num_owners <= 1 {
|
|
|
|
err!("The last owner can't leave")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match user_org.delete(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed leaving the organization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[get("/organizations/<org_id>")]
|
2018-05-30 22:30:45 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:25:52 +02:00
|
|
|
#[put("/organizations/<org_id>", data = "<data>")]
|
|
|
|
fn put_organization(org_id: String, headers: OwnerHeaders, data: JsonUpcase<OrganizationUpdateData>, conn: DbConn) -> JsonResult {
|
|
|
|
post_organization(org_id, headers, data, conn)
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[post("/organizations/<org_id>", data = "<data>")]
|
2018-06-01 00:18:50 +02:00
|
|
|
fn post_organization(org_id: String, _headers: OwnerHeaders, data: JsonUpcase<OrganizationUpdateData>, conn: DbConn) -> JsonResult {
|
|
|
|
let data: OrganizationUpdateData = data.into_inner().data;
|
2018-04-20 18:35:11 +02:00
|
|
|
|
|
|
|
let mut org = match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(organization) => organization,
|
|
|
|
None => err!("Can't find organization details")
|
|
|
|
};
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
org.name = data.Name;
|
|
|
|
org.billing_email = data.BillingEmail;
|
2018-04-20 18:35:11 +02:00
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
match org.save(&conn) {
|
|
|
|
Ok(()) => Ok(Json(org.to_json())),
|
|
|
|
Err(_) => err!("Failed to modify organization")
|
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
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()
|
2018-09-13 15:16:24 +02:00
|
|
|
.map(Collection::to_json)
|
|
|
|
.collect::<Value>(),
|
2018-10-01 18:02:58 +02:00
|
|
|
"Object": "list",
|
|
|
|
"ContinuationToken": null,
|
2018-02-17 22:30:19 +01:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/organizations/<org_id>/collections")]
|
2018-05-30 22:30:45 +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()
|
2018-09-13 15:16:24 +02:00
|
|
|
.map(Collection::to_json)
|
|
|
|
.collect::<Value>(),
|
2018-10-01 18:02:58 +02:00
|
|
|
"Object": "list",
|
|
|
|
"ContinuationToken": null,
|
2018-02-17 22:30:19 +01:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[post("/organizations/<org_id>/collections", data = "<data>")]
|
2018-06-01 00:18:50 +02:00
|
|
|
fn post_organization_collections(org_id: String, _headers: AdminHeaders, data: JsonUpcase<NewCollectionData>, conn: DbConn) -> JsonResult {
|
|
|
|
let data: NewCollectionData = data.into_inner().data;
|
2018-04-20 18:35:11 +02:00
|
|
|
|
|
|
|
let org = match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(organization) => organization,
|
|
|
|
None => err!("Can't find organization details")
|
|
|
|
};
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let mut collection = Collection::new(org.uuid.clone(), data.Name);
|
2018-04-20 18:35:11 +02:00
|
|
|
|
2018-10-01 18:50:31 +02:00
|
|
|
if collection.save(&conn).is_err() {
|
|
|
|
err!("Failed saving Collection");
|
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
Ok(Json(collection.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:45:30 +02:00
|
|
|
#[put("/organizations/<org_id>/collections/<col_id>", data = "<data>")]
|
|
|
|
fn put_organization_collection_update(org_id: String, col_id: String, headers: AdminHeaders, data: JsonUpcase<NewCollectionData>, conn: DbConn) -> JsonResult {
|
|
|
|
post_organization_collection_update(org_id, col_id, headers, data, conn)
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:35:11 +02:00
|
|
|
#[post("/organizations/<org_id>/collections/<col_id>", data = "<data>")]
|
2018-06-01 00:18:50 +02:00
|
|
|
fn post_organization_collection_update(org_id: String, col_id: String, _headers: AdminHeaders, data: JsonUpcase<NewCollectionData>, conn: DbConn) -> JsonResult {
|
|
|
|
let data: NewCollectionData = data.into_inner().data;
|
2018-04-20 18:35:11 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
};
|
|
|
|
|
2018-05-30 22:30:45 +02:00
|
|
|
if collection.org_uuid != org.uuid {
|
|
|
|
err!("Collection is not owned by organization");
|
|
|
|
}
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
collection.name = data.Name.clone();
|
2018-10-01 18:50:31 +02:00
|
|
|
if collection.save(&conn).is_err() {
|
|
|
|
err!("Failed updating Collection");
|
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
|
|
|
|
Ok(Json(collection.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:45:30 +02:00
|
|
|
|
|
|
|
#[delete("/organizations/<org_id>/collections/<col_id>/user/<org_user_id>")]
|
|
|
|
fn delete_organization_collection_user(org_id: String, col_id: String, org_user_id: String, _headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-05-30 17:01:56 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-04 14:37:44 +02:00
|
|
|
match UserOrganization::find_by_uuid_and_org(&org_user_id, &org_id, &conn) {
|
2018-05-30 17:01:56 +02:00
|
|
|
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-08-13 17:45:30 +02:00
|
|
|
#[post("/organizations/<org_id>/collections/<col_id>/delete-user/<org_user_id>")]
|
|
|
|
fn post_organization_collection_delete_user(org_id: String, col_id: String, org_user_id: String, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
delete_organization_collection_user(org_id, col_id, org_user_id, headers, conn)
|
2018-05-17 00:05:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 17:45:30 +02:00
|
|
|
#[delete("/organizations/<org_id>/collections/<col_id>")]
|
|
|
|
fn delete_organization_collection(org_id: String, col_id: String, _headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-05-30 18:12:18 +02:00
|
|
|
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-08-13 17:45:30 +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>")]
|
|
|
|
fn post_organization_collection_delete(org_id: String, col_id: String, headers: AdminHeaders, _data: JsonUpcase<DeleteCollectionData>, conn: DbConn) -> EmptyResult {
|
|
|
|
delete_organization_collection(org_id, col_id, headers, conn)
|
|
|
|
}
|
|
|
|
|
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"),
|
2018-05-30 22:30:45 +02:00
|
|
|
Some(collection) => {
|
|
|
|
if collection.org_uuid != org_id {
|
|
|
|
err!("Collection is not owned by organization")
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Json(collection.to_json()))
|
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
#[get("/organizations/<org_id>/collections/<coll_id>/users")]
|
2018-05-30 22:30:45 +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()
|
2018-09-13 21:55:23 +02:00
|
|
|
.to_json_collection_user_details(col_user.read_only, &conn)
|
2018-05-29 17:01:38 +02:00
|
|
|
}).collect();
|
2018-04-25 00:34:40 +02:00
|
|
|
|
|
|
|
Ok(Json(json!({
|
2018-05-29 17:01:38 +02:00
|
|
|
"Data": user_list,
|
2018-10-01 18:02:58 +02:00
|
|
|
"Object": "list",
|
|
|
|
"ContinuationToken": null,
|
2018-04-25 00:34:40 +02:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct OrgIdData {
|
2018-10-10 20:40:39 +02:00
|
|
|
#[form(field = "organizationId")]
|
|
|
|
organization_id: String
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
#[get("/ciphers/organization-details?<data..>")]
|
|
|
|
fn get_org_details(data: Form<OrgIdData>, headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
let ciphers = Cipher::find_by_org(&data.organization_id, &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-10-01 18:02:58 +02:00
|
|
|
"ContinuationToken": null,
|
2018-04-24 22:01:55 +02:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/organizations/<org_id>/users")]
|
2018-10-12 16:20:10 +02:00
|
|
|
fn get_org_users(org_id: String, _headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
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,
|
2018-10-01 18:02:58 +02:00
|
|
|
"Object": "list",
|
|
|
|
"ContinuationToken": null,
|
2018-04-24 22:01:55 +02:00
|
|
|
})))
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-08-14 12:06:42 +02:00
|
|
|
fn deserialize_collections<'de, D>(deserializer: D) -> Result<Vec<CollectionData>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
// Deserialize null to empty Vec
|
|
|
|
Deserialize::deserialize(deserializer).or(Ok(vec![]))
|
|
|
|
}
|
|
|
|
|
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-06-13 14:25:50 +02:00
|
|
|
Id: String,
|
|
|
|
ReadOnly: bool,
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct InviteData {
|
2018-06-01 00:18:50 +02:00
|
|
|
Emails: Vec<String>,
|
|
|
|
Type: NumberOrString,
|
2018-08-14 12:06:42 +02:00
|
|
|
#[serde(deserialize_with = "deserialize_collections")]
|
2018-06-01 00:18:50 +02:00
|
|
|
Collections: Vec<CollectionData>,
|
|
|
|
AccessAll: Option<bool>,
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/organizations/<org_id>/users/invite", data = "<data>")]
|
2018-06-01 00:18:50 +02:00
|
|
|
fn send_invite(org_id: String, data: JsonUpcase<InviteData>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
let data: InviteData = data.into_inner().data;
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-06-11 15:44:37 +02:00
|
|
|
let new_type = match UserOrgType::from_str(&data.Type.into_string()) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(new_type) => new_type as i32,
|
|
|
|
None => err!("Invalid type")
|
|
|
|
};
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if new_type != UserOrgType::User &&
|
|
|
|
headers.org_user_type != UserOrgType::Owner {
|
|
|
|
err!("Only Owners can invite Managers, Admins or Owners")
|
2018-04-24 23:04:10 +02:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-09-10 15:51:40 +02:00
|
|
|
for email in data.Emails.iter() {
|
|
|
|
let mut user_org_status = UserOrgStatus::Accepted as i32;
|
|
|
|
let user = match User::find_by_mail(&email, &conn) {
|
|
|
|
None => if CONFIG.invitations_allowed { // Invite user if that's enabled
|
|
|
|
let mut invitation = Invitation::new(email.clone());
|
|
|
|
match invitation.save(&conn) {
|
|
|
|
Ok(()) => {
|
2018-09-11 15:25:12 +02:00
|
|
|
let mut user = User::new(email.clone());
|
2018-10-14 19:32:43 +02:00
|
|
|
if user.save(&conn).is_err() {
|
|
|
|
err!("Failed to create placeholder for invited user")
|
|
|
|
} else {
|
2018-09-10 15:51:40 +02:00
|
|
|
user_org_status = UserOrgStatus::Invited as i32;
|
|
|
|
user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => err!(format!("Failed to invite: {}", email))
|
2018-04-24 23:04:10 +02:00
|
|
|
}
|
2018-09-10 15:51:40 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
err!(format!("User email does not exist: {}", email))
|
|
|
|
},
|
|
|
|
Some(user) => if UserOrganization::find_by_user_and_org(&user.uuid, &org_id, &conn).is_some() {
|
|
|
|
err!(format!("User already in organization: {}", email))
|
|
|
|
} else {
|
|
|
|
user
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-09-10 15:51:40 +02:00
|
|
|
};
|
|
|
|
|
2018-10-12 16:20:10 +02:00
|
|
|
// Don't create UserOrganization in virtual organization
|
|
|
|
if org_id != Organization::VIRTUAL_ID {
|
|
|
|
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;
|
|
|
|
new_user.type_ = new_type;
|
|
|
|
new_user.status = user_org_status;
|
|
|
|
|
|
|
|
// If no accessAll, add the collections received
|
|
|
|
if !access_all {
|
|
|
|
for col in &data.Collections {
|
|
|
|
match Collection::find_by_uuid_and_org(&col.Id, &org_id, &conn) {
|
|
|
|
None => err!("Collection not found in Organization"),
|
|
|
|
Some(collection) => {
|
|
|
|
if CollectionUser::save(&user.uuid, &collection.uuid, col.ReadOnly, &conn).is_err() {
|
|
|
|
err!("Failed saving collection access for user")
|
|
|
|
}
|
2018-05-28 18:26:02 +02:00
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 15:51:40 +02:00
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
if new_user.save(&conn).is_err() {
|
|
|
|
err!("Failed to add user to organization")
|
|
|
|
}
|
2018-10-12 16:20:10 +02:00
|
|
|
}
|
2018-12-15 03:56:00 +01:00
|
|
|
|
|
|
|
if CONFIG.email_invitations {
|
|
|
|
use crate::mail;
|
|
|
|
use chrono::{Duration, Utc};
|
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
let claims = InviteJWTClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
|
|
|
iss: JWT_ISSUER.to_string(),
|
|
|
|
sub: user.uuid.to_string(),
|
|
|
|
email: email.clone(),
|
|
|
|
};
|
|
|
|
let org_name = match Organization::find_by_uuid(&org_id, &conn) {
|
|
|
|
Some(org) => org.name,
|
|
|
|
None => err!("Error looking up organization")
|
|
|
|
};
|
|
|
|
let invite_token = encode_jwt(&claims);
|
|
|
|
let org_user_id = Organization::VIRTUAL_ID;
|
|
|
|
|
|
|
|
if let Some(ref mail_config) = CONFIG.mail {
|
|
|
|
if let Err(e) = mail::send_invite(&email, &org_id, &org_user_id, &invite_token, &org_name, mail_config) {
|
|
|
|
err!(format!("There has been a problem sending the email: {}", e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Figure out how to make this redirect to the registration page
|
|
|
|
#[get("/organizations/<org_id>/users/<org_user_id>/accept?<token>")]
|
|
|
|
fn accept_invite(org_id: String, org_user_id: String, token: String, conn: DbConn) -> EmptyResult {
|
|
|
|
let invite_claims: InviteJWTClaims = match decode_invite_jwt(&token) {
|
|
|
|
Ok(claims) => claims,
|
|
|
|
Err(msg) => err!("Invalid claim: {:#?}", msg),
|
|
|
|
};
|
|
|
|
|
|
|
|
match User::find_by_mail(&invite_claims.email, &conn) {
|
|
|
|
Some(user) => {
|
|
|
|
if Invitation::take(&invite_claims.email, &conn) {
|
|
|
|
for mut user_org in UserOrganization::find_invited_by_user(&user.uuid, &conn).iter_mut() {
|
|
|
|
user_org.status = UserOrgStatus::Accepted as i32;
|
|
|
|
if user_org.save(&conn).is_err() {
|
|
|
|
err!("Failed to accept user to organization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//rocket::response::Redirect::to(format!("/#/register?email={}", invite_claims.email))
|
|
|
|
} else {
|
|
|
|
err!("Invitation for user not found")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
err!("Invited user not found")
|
|
|
|
},
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
#[post("/organizations/<org_id>/users/<org_user_id>/confirm", data = "<data>")]
|
|
|
|
fn confirm_invite(org_id: String, org_user_id: String, data: JsonUpcase<Value>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data = data.into_inner().data;
|
|
|
|
|
2018-09-04 14:37:44 +02:00
|
|
|
let mut user_to_confirm = match UserOrganization::find_by_uuid_and_org(&org_user_id, &org_id, &conn) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(user) => user,
|
2018-09-04 14:37:44 +02:00
|
|
|
None => err!("The specified user isn't a member of the organization")
|
2018-04-25 00:34:40 +02:00
|
|
|
};
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if user_to_confirm.type_ != UserOrgType::User &&
|
|
|
|
headers.org_user_type != UserOrgType::Owner {
|
|
|
|
err!("Only Owners can confirm Managers, Admins or Owners")
|
2018-04-24 23:04:10 +02:00
|
|
|
}
|
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;
|
2018-06-13 14:25:50 +02:00
|
|
|
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-10-14 16:04:23 +02:00
|
|
|
match user_to_confirm.save(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed to add user to organization")
|
|
|
|
}
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
#[get("/organizations/<org_id>/users/<org_user_id>")]
|
|
|
|
fn get_user(org_id: String, org_user_id: String, _headers: AdminHeaders, conn: DbConn) -> JsonResult {
|
2018-09-04 14:37:44 +02:00
|
|
|
let user = match UserOrganization::find_by_uuid_and_org(&org_user_id, &org_id, &conn) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(user) => user,
|
2018-09-04 14:37:44 +02:00
|
|
|
None => err!("The specified user isn't a member of the organization")
|
2018-04-25 00:34:40 +02:00
|
|
|
};
|
|
|
|
|
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 {
|
2018-06-01 00:18:50 +02:00
|
|
|
Type: NumberOrString,
|
2018-08-14 12:06:42 +02:00
|
|
|
#[serde(deserialize_with = "deserialize_collections")]
|
2018-06-01 00:18:50 +02:00
|
|
|
Collections: Vec<CollectionData>,
|
|
|
|
AccessAll: bool,
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
#[put("/organizations/<org_id>/users/<org_user_id>", data = "<data>", rank = 1)]
|
|
|
|
fn put_organization_user(org_id: String, org_user_id: String, data: JsonUpcase<EditUserData>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
edit_user(org_id, org_user_id, data, headers, conn)
|
2018-08-13 17:45:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
#[post("/organizations/<org_id>/users/<org_user_id>", data = "<data>", rank = 1)]
|
|
|
|
fn edit_user(org_id: String, org_user_id: String, data: JsonUpcase<EditUserData>, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data: EditUserData = data.into_inner().data;
|
2018-04-25 00:34:40 +02:00
|
|
|
|
2018-06-11 15:44:37 +02:00
|
|
|
let new_type = match UserOrgType::from_str(&data.Type.into_string()) {
|
2018-11-12 18:13:25 +01:00
|
|
|
Some(new_type) => new_type,
|
2018-04-25 00:34:40 +02:00
|
|
|
None => err!("Invalid type")
|
|
|
|
};
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
let mut user_to_edit = match UserOrganization::find_by_uuid_and_org(&org_user_id, &org_id, &conn) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(user) => user,
|
|
|
|
None => err!("The specified user isn't member of the organization")
|
|
|
|
};
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if new_type != user_to_edit.type_ && (
|
|
|
|
user_to_edit.type_ >= UserOrgType::Admin ||
|
|
|
|
new_type >= UserOrgType::Admin
|
2018-09-04 12:24:53 +02:00
|
|
|
) &&
|
2018-11-12 18:13:25 +01:00
|
|
|
headers.org_user_type != UserOrgType::Owner {
|
2018-09-04 12:24:53 +02:00
|
|
|
err!("Only Owners can grant and remove Admin or Owner privileges")
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if user_to_edit.type_ == UserOrgType::Owner &&
|
|
|
|
headers.org_user_type != UserOrgType::Owner {
|
2018-09-04 12:24:53 +02:00
|
|
|
err!("Only Owners can edit Owner users")
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if user_to_edit.type_ == UserOrgType::Owner &&
|
|
|
|
new_type != UserOrgType::Owner {
|
2018-04-25 00:34:40 +02:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
user_to_edit.access_all = data.AccessAll;
|
2018-11-12 18:13:25 +01:00
|
|
|
user_to_edit.type_ = new_type as i32;
|
2018-04-25 00:34:40 +02:00
|
|
|
|
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) {
|
2018-06-11 15:44:37 +02:00
|
|
|
if c.delete(&conn).is_err() {
|
|
|
|
err!("Failed deleting old collection assignment")
|
2018-05-29 17:01:38 +02:00
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no accessAll, add the collections received
|
2018-06-01 00:18:50 +02:00
|
|
|
if !data.AccessAll {
|
2018-06-11 15:44:37 +02:00
|
|
|
for col in &data.Collections {
|
2018-06-13 14:25:50 +02:00
|
|
|
match Collection::find_by_uuid_and_org(&col.Id, &org_id, &conn) {
|
2018-05-28 18:26:02 +02:00
|
|
|
None => err!("Collection not found in Organization"),
|
|
|
|
Some(collection) => {
|
2018-06-13 14:25:50 +02:00
|
|
|
if CollectionUser::save(&user_to_edit.user_uuid, &collection.uuid, col.ReadOnly, &conn).is_err() {
|
2018-06-11 15:44:37 +02:00
|
|
|
err!("Failed saving collection access for user")
|
2018-05-28 18:26:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
match user_to_edit.save(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed to save user data")
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
#[delete("/organizations/<org_id>/users/<org_user_id>")]
|
|
|
|
fn delete_user(org_id: String, org_user_id: String, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
2018-10-12 16:20:10 +02:00
|
|
|
// We're deleting user in virtual Organization. Delete User, not UserOrganization
|
|
|
|
if org_id == Organization::VIRTUAL_ID {
|
|
|
|
match User::find_by_uuid(&org_user_id, &conn) {
|
|
|
|
Some(user_to_delete) => {
|
|
|
|
if user_to_delete.uuid == headers.user.uuid {
|
|
|
|
err!("Delete your account in the account settings")
|
|
|
|
} else {
|
|
|
|
match user_to_delete.delete(&conn) {
|
|
|
|
Ok(()) => return Ok(()),
|
|
|
|
Err(_) => err!("Failed to delete user - likely because it's the only owner of organization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => err!("User not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:37:44 +02:00
|
|
|
let user_to_delete = match UserOrganization::find_by_uuid_and_org(&org_user_id, &org_id, &conn) {
|
2018-04-25 00:34:40 +02:00
|
|
|
Some(user) => user,
|
|
|
|
None => err!("User to delete isn't member of the organization")
|
|
|
|
};
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if user_to_delete.type_ != UserOrgType::User &&
|
|
|
|
headers.org_user_type != UserOrgType::Owner {
|
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-11-12 18:13:25 +01:00
|
|
|
if user_to_delete.type_ == UserOrgType::Owner {
|
2018-04-25 00:34:40 +02:00
|
|
|
// 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-08-13 17:45:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
#[post("/organizations/<org_id>/users/<org_user_id>/delete")]
|
|
|
|
fn post_delete_user(org_id: String, org_user_id: String, headers: AdminHeaders, conn: DbConn) -> EmptyResult {
|
|
|
|
delete_user(org_id, org_user_id, headers, conn)
|
2018-09-13 15:16:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:38:02 +02:00
|
|
|
#[post("/organizations/<_org_id>/users/<_org_user_id>/reinvite")]
|
|
|
|
fn post_reinvite_user(_org_id: String, _org_user_id: String, _headers: AdminHeaders, _conn: DbConn) -> EmptyResult {
|
|
|
|
err!("This functionality is not implemented. The user needs to manually register before they can be accepted into the organization.")
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:16:24 +02:00
|
|
|
use super::ciphers::CipherData;
|
|
|
|
use super::ciphers::update_cipher_from_data;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct ImportData {
|
|
|
|
Ciphers: Vec<CipherData>,
|
|
|
|
Collections: Vec<NewCollectionData>,
|
|
|
|
CollectionRelationships: Vec<RelationsData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct RelationsData {
|
|
|
|
// Cipher index
|
|
|
|
Key: usize,
|
|
|
|
// Collection index
|
|
|
|
Value: usize,
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
#[post("/ciphers/import-organization?<query..>", data = "<data>")]
|
|
|
|
fn post_org_import(query: Form<OrgIdData>, data: JsonUpcase<ImportData>, headers: Headers, conn: DbConn, ws: State<WebSocketUsers>) -> EmptyResult {
|
2018-09-13 15:16:24 +02:00
|
|
|
let data: ImportData = data.into_inner().data;
|
2018-10-10 20:40:39 +02:00
|
|
|
let org_id = query.into_inner().organization_id;
|
2018-09-13 15:16:24 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
};
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
if org_user.type_ < UserOrgType::Admin {
|
2018-09-13 15:16:24 +02:00
|
|
|
err!("Only admins or owners can import into an organization")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and create the collections
|
|
|
|
let collections: Vec<_> = data.Collections.into_iter().map(|coll| {
|
|
|
|
let mut collection = Collection::new(org_id.clone(), coll.Name);
|
2018-10-01 18:50:31 +02:00
|
|
|
if collection.save(&conn).is_err() {
|
|
|
|
err!("Failed to create Collection");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(collection)
|
2018-09-13 15:16:24 +02:00
|
|
|
}).collect();
|
|
|
|
|
|
|
|
// Read the relations between collections and ciphers
|
|
|
|
let mut relations = Vec::new();
|
|
|
|
for relation in data.CollectionRelationships {
|
|
|
|
relations.push((relation.Key, relation.Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and create the ciphers
|
|
|
|
let ciphers: Vec<_> = data.Ciphers.into_iter().map(|cipher_data| {
|
|
|
|
let mut cipher = Cipher::new(cipher_data.Type, cipher_data.Name.clone());
|
2018-09-13 16:04:00 +02:00
|
|
|
update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn, &ws, UpdateType::SyncCipherCreate).ok();
|
2018-09-13 15:16:24 +02:00
|
|
|
cipher
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
// Assign the collections
|
|
|
|
for (cipher_index, coll_index) in relations {
|
|
|
|
let cipher_id = &ciphers[cipher_index].uuid;
|
2018-10-01 18:50:31 +02:00
|
|
|
let coll = &collections[coll_index];
|
|
|
|
let coll_id = match coll {
|
|
|
|
Ok(coll) => coll.uuid.as_str(),
|
|
|
|
Err(_) => err!("Failed to assign to collection")
|
|
|
|
};
|
|
|
|
|
2018-10-06 23:56:05 +02:00
|
|
|
match CollectionCipher::save(cipher_id, coll_id, &conn) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(_) => err!("Failed to add cipher to collection")
|
|
|
|
};
|
2018-09-13 15:16:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut user = headers.user;
|
|
|
|
match user.update_revision(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed to update the revision, please log out and log back in to finish import.")
|
|
|
|
}
|
2018-10-10 20:40:39 +02:00
|
|
|
}
|