Skip to content

Commit

Permalink
add groups for current user
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Sep 28, 2020
1 parent 8f6ba05 commit ef9ea34
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 32 deletions.
73 changes: 42 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions src/api/info.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
packs: Vec<String>,
diff: Vec<String>,
}

#[guard(Authenticated)]
async fn groups(
pool: web::Data<Pool>,
scope_and_user: ScopeAndUser,
groups: Groups,
) -> Result<HttpResponse, ApiError> {
let mut packs = operations::groups::groups_for_current_user(&pool, &scope_and_user)?;
packs.sort();
let mut token: Vec<String> = groups
.groups
.into_iter()
.filter_map(|g| g.strip_prefix("mozilliansorg_").map(String::from))
.collect();
token.sort();

let diff = packs
.iter()
.cloned()
.collect::<HashSet<_>>()
.difference(&token.iter().cloned().collect::<HashSet<_>>())
.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)))
}
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/db/operations/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>, 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())
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ async fn main() -> std::io::Result<()> {
.service(api::admins::admins_app::<CisClient>())
.service(api::requests::requests_app())
.service(api::sudo::sudo_app::<CisClient>())
.service(api::forms::forms_app::<CisClient>()),
.service(api::forms::forms_app::<CisClient>())
.service(api::info::info_app(provider.clone())),
)
})
.bind("0.0.0.0:8085")?
Expand Down

0 comments on commit ef9ea34

Please sign in to comment.