mz_frontegg_mock/middleware/
role_update.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use axum::extract::Request;
11use axum::{extract::State, middleware::Next, response::IntoResponse};
12
13use crate::server::Context;
14use std::sync::Arc;
15
16pub async fn role_update_middleware(
17    State(context): State<Arc<Context>>,
18    request: Request,
19    next: Next,
20) -> impl IntoResponse {
21    {
22        let mut role_updates_rx = context.role_updates_rx.lock().unwrap();
23        while let Ok((email, roles)) = role_updates_rx.try_recv() {
24            let mut users = context.users.lock().unwrap();
25            users.get_mut(&email).unwrap().roles = roles;
26        }
27    }
28    next.run(request).await
29}