From ef9ea34368c711c17c6696a99d5ad9f13c093570 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Mon, 28 Sep 2020 19:44:08 +0200 Subject: [PATCH] add groups for current user --- Cargo.lock | 73 +++++++++++++++++++++---------------- src/api/info.rs | 51 ++++++++++++++++++++++++++ src/api/mod.rs | 1 + src/db/operations/groups.rs | 11 ++++++ src/main.rs | 3 +- 5 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 src/api/info.rs diff --git a/Cargo.lock b/Cargo.lock index f00e7f5..1f06f1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,7 +79,7 @@ dependencies = [ "serde_urlencoded", "sha-1", "slab", - "time 0.2.20", + "time 0.2.22", ] [[package]] @@ -112,9 +112,9 @@ dependencies = [ [[package]] name = "actix-router" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d7a10ca4d94e8c8e7a87c5173aba1b97ba9a6563ca02b0e1cd23531093d3ec8" +checksum = "bbd1f7dbda1645bf7da33554db60891755f6c01c1b2169e2f4c492098d30c235" dependencies = [ "bytestring", "http", @@ -263,7 +263,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "socket2", - "time 0.2.20", + "time 0.2.22", "tinyvec", "url", ] @@ -529,14 +529,16 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "chrono" -version = "0.4.15" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" +checksum = "d021fddb7bd3e734370acfa4a83f34095571d8570c039f1420d77540f68d5772" dependencies = [ + "libc", "num-integer", "num-traits 0.2.12", "serde 1.0.116", "time 0.1.44", + "winapi 0.3.9", ] [[package]] @@ -636,7 +638,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1373a16a4937bc34efec7b391f9c1500c30b8478a701a4f44c9165cc0475a6e0" dependencies = [ "percent-encoding", - "time 0.2.20", + "time 0.2.22", "version_check", ] @@ -853,8 +855,8 @@ dependencies = [ [[package]] name = "dino_park_gate" -version = "0.8.0" -source = "git+https://github.com/mozilla-iam/dino-park-gate?branch=0.8#d29c50669fc5a754ef52e660319a2722b14ed180" +version = "0.8.1" +source = "git+https://github.com/mozilla-iam/dino-park-gate?branch=0.8#912ccd4198753b124283543781bb81157520f730" dependencies = [ "actix-service", "actix-web", @@ -1209,9 +1211,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +checksum = "4c30f6d0bc6b00693347368a67d41b58f2fb851215ff1da49e90fe2c5c667151" dependencies = [ "libc", ] @@ -1270,6 +1272,12 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + [[package]] name = "humantime" version = "1.3.0" @@ -1281,9 +1289,9 @@ dependencies = [ [[package]] name = "hyper" -version = "0.13.7" +version = "0.13.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e68a8dd9716185d9e64ea473ea6ef63529252e3e27623295a0378a19665d5eb" +checksum = "2f3afcfae8af5ad0576a31e768415edb627824129e8e5a29b8bfccb2f234e835" dependencies = [ "bytes", "futures-channel", @@ -1293,10 +1301,10 @@ dependencies = [ "http", "http-body", "httparse", + "httpdate", "itoa", "pin-project", "socket2", - "time 0.1.44", "tokio", "tower-service", "tracing", @@ -1339,9 +1347,12 @@ dependencies = [ [[package]] name = "instant" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" +checksum = "63312a18f7ea8760cdd0a7c5aac1a619752a246b833545e3e36d1f81f7cd9e66" +dependencies = [ + "cfg-if", +] [[package]] name = "iovec" @@ -1780,9 +1791,9 @@ checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" [[package]] name = "openssl-src" -version = "111.10.2+1.1.1g" +version = "111.11.0+1.1.1h" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a287fdb22e32b5b60624d4a5a7a02dbe82777f730ec0dbc42a0554326fef5a70" +checksum = "380fe324132bea01f45239fadfec9343adb044615f29930d039bec1ae7b9fa5b" dependencies = [ "cc", ] @@ -1844,18 +1855,18 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pin-project" -version = "0.4.23" +version = "0.4.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa" +checksum = "f48fad7cfbff853437be7cf54d7b993af21f53be7f0988cbfe4a51535aa77205" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "0.4.23" +version = "0.4.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f" +checksum = "24c6d293bdd3ca5a1697997854c6cf7855e43fb6a0ba1c47af57a5bcafd158ae" dependencies = [ "proc-macro2", "quote", @@ -1864,9 +1875,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715" +checksum = "71f349a4f0e70676ffb2dbafe16d0c992382d02f0a952e3ddf584fc289dac6b3" [[package]] name = "pin-utils" @@ -1909,9 +1920,9 @@ checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" [[package]] name = "proc-macro2" -version = "1.0.21" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c" +checksum = "51ef7cd2518ead700af67bf9d1a658d90b6037d77110fd9c0445429d0ba1c6c9" dependencies = [ "unicode-xid", ] @@ -2180,7 +2191,7 @@ dependencies = [ "rustc_version", "serde 1.0.116", "sha2", - "time 0.2.20", + "time 0.2.22", "tokio", ] @@ -2534,9 +2545,9 @@ checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd" [[package]] name = "syn" -version = "1.0.41" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b" +checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228" dependencies = [ "proc-macro2", "quote", @@ -2629,9 +2640,9 @@ dependencies = [ [[package]] name = "time" -version = "0.2.20" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d4953c513c9bf1b97e9cdd83f11d60c4b0a83462880a360d80d96953a953fee" +checksum = "55b7151c9065e80917fbf285d9a5d1432f60db41d170ccafc749a136b41a93af" dependencies = [ "const_fn", "libc", diff --git a/src/api/info.rs b/src/api/info.rs new file mode 100644 index 0000000..e0961a5 --- /dev/null +++ b/src/api/info.rs @@ -0,0 +1,51 @@ +use crate::api::error::ApiError; +use crate::db::operations; +use crate::db::Pool; +use actix_web::dev::HttpServiceFactory; +use actix_web::web; +use actix_web::HttpResponse; +use dino_park_gate::groups::Groups; +use dino_park_gate::groups::GroupsFromToken; +use dino_park_gate::provider::Provider; +use dino_park_gate::scope::ScopeAndUser; +use serde::Serialize; +use std::collections::HashSet; + +#[derive(Serialize)] +pub struct AllGroups { + token: Vec, + packs: Vec, + diff: Vec, +} + +#[guard(Authenticated)] +async fn groups( + pool: web::Data, + scope_and_user: ScopeAndUser, + groups: Groups, +) -> Result { + let mut packs = operations::groups::groups_for_current_user(&pool, &scope_and_user)?; + packs.sort(); + let mut token: Vec = groups + .groups + .into_iter() + .filter_map(|g| g.strip_prefix("mozilliansorg_").map(String::from)) + .collect(); + token.sort(); + + let diff = packs + .iter() + .cloned() + .collect::>() + .difference(&token.iter().cloned().collect::>()) + .cloned() + .collect(); + Ok(HttpResponse::Ok().json(AllGroups { token, packs, diff })) +} + +pub fn info_app(provider: Provider) -> impl HttpServiceFactory { + let middleware = GroupsFromToken::new(provider); + web::scope("/info") + .wrap(middleware) + .service(web::resource("/groups").route(web::get().to(groups))) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index d5a365d..9579a63 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -3,6 +3,7 @@ pub mod current; pub mod error; pub mod forms; pub mod groups; +pub mod info; pub mod internal; pub mod invitations; pub mod members; diff --git a/src/db/operations/groups.rs b/src/db/operations/groups.rs index 5546365..59d7f8f 100644 --- a/src/db/operations/groups.rs +++ b/src/db/operations/groups.rs @@ -219,3 +219,14 @@ pub fn reserve_group( internal::group::reserve_group(&connection, &user.user_uuid, group_name) } + +pub fn groups_for_current_user( + pool: &Pool, + scope_and_user: &ScopeAndUser, +) -> Result, Error> { + let connection = pool.get()?; + let user = internal::user::user_by_id(&connection, &scope_and_user.user_id)?; + + internal::group::groups_for_user(&connection, &user.user_uuid) + .map(|groups| groups.into_iter().map(|g| g.name).collect()) +} diff --git a/src/main.rs b/src/main.rs index 035ca86..cf7698a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,8 @@ async fn main() -> std::io::Result<()> { .service(api::admins::admins_app::()) .service(api::requests::requests_app()) .service(api::sudo::sudo_app::()) - .service(api::forms::forms_app::()), + .service(api::forms::forms_app::()) + .service(api::info::info_app(provider.clone())), ) }) .bind("0.0.0.0:8085")?