-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
adds sso_identifier to /admin/users #6491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
b30819a
3fa90cc
745f7db
cc6866f
42a7b66
5ba2860
245d263
1629217
07f39d8
cd7ac37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,6 +304,10 @@ async fn get_user_or_404(user_id: &UserId, conn: &DbConn) -> ApiResult<User> { | |
| } | ||
| } | ||
|
|
||
| async fn get_sso_user(user_id: &UserId, conn: &DbConn) -> Option<SsoUser> { | ||
| SsoUser::find_by_uuid(user_id, conn).await.and_then(|user_and_sso| user_and_sso.1) | ||
| } | ||
|
|
||
| #[post("/invite", format = "application/json", data = "<data>")] | ||
| async fn invite_user(data: Json<InviteData>, _token: AdminToken, conn: DbConn) -> JsonResult { | ||
| let data: InviteData = data.into_inner(); | ||
|
|
@@ -347,68 +351,59 @@ fn logout(cookies: &CookieJar<'_>) -> Redirect { | |
| Redirect::to(admin_path()) | ||
| } | ||
|
|
||
| #[get("/users")] | ||
| async fn get_users_json(_token: AdminToken, conn: DbConn) -> Json<Value> { | ||
| let users = User::get_all(&conn).await; | ||
| async fn get_users_property(users: Vec<(User, Option<SsoUser>)>, conn: &DbConn) -> Vec<Value> { | ||
|
||
| let mut users_json = Vec::with_capacity(users.len()); | ||
| for (u, _) in users { | ||
| let mut usr = u.to_json(&conn).await; | ||
| for (u, sso_u) in users { | ||
| let mut usr = u.to_json(conn).await; | ||
| usr["cipherCount"] = json!(Cipher::count_owned_by_user(&u.uuid, conn).await); | ||
| usr["attachmentCount"] = json!(Attachment::count_by_user(&u.uuid, conn).await); | ||
| usr["attachmentSize"] = json!(get_display_size(Attachment::size_by_user(&u.uuid, conn).await)); | ||
| usr["userEnabled"] = json!(u.enabled); | ||
| usr["createdAt"] = json!(format_naive_datetime_local(&u.created_at, DT_FMT)); | ||
| usr["lastActive"] = match u.last_active(&conn).await { | ||
| usr["lastActive"] = match u.last_active(conn).await { | ||
| Some(dt) => json!(format_naive_datetime_local(&dt, DT_FMT)), | ||
| None => json!(None::<String>), | ||
| None => json!("Never"), | ||
|
||
| }; | ||
|
|
||
| usr["ssoIdentifier"] = json!(sso_u.map(|u| u.identifier.to_string()).unwrap_or(String::new())); | ||
|
|
||
| users_json.push(usr); | ||
| } | ||
| users_json | ||
| } | ||
|
|
||
| #[get("/users")] | ||
| async fn get_users_json(_token: AdminToken, conn: DbConn) -> Json<Value> { | ||
| let users = User::get_all(&conn).await; | ||
| let users_json = get_users_property(users, &conn).await; | ||
| Json(Value::Array(users_json)) | ||
| } | ||
|
|
||
| #[get("/users/overview")] | ||
| async fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult<Html<String>> { | ||
| let users = User::get_all(&conn).await; | ||
| let mut users_json = Vec::with_capacity(users.len()); | ||
| for (u, sso_u) in users { | ||
| let mut usr = u.to_json(&conn).await; | ||
| usr["cipher_count"] = json!(Cipher::count_owned_by_user(&u.uuid, &conn).await); | ||
| usr["attachment_count"] = json!(Attachment::count_by_user(&u.uuid, &conn).await); | ||
| usr["attachment_size"] = json!(get_display_size(Attachment::size_by_user(&u.uuid, &conn).await)); | ||
| usr["user_enabled"] = json!(u.enabled); | ||
| usr["created_at"] = json!(format_naive_datetime_local(&u.created_at, DT_FMT)); | ||
| usr["last_active"] = match u.last_active(&conn).await { | ||
| Some(dt) => json!(format_naive_datetime_local(&dt, DT_FMT)), | ||
| None => json!("Never"), | ||
| }; | ||
|
|
||
| usr["sso_identifier"] = json!(sso_u.map(|u| u.identifier.to_string()).unwrap_or(String::new())); | ||
|
|
||
| users_json.push(usr); | ||
| } | ||
|
|
||
| let users_json = get_users_property(users, &conn).await; | ||
| let text = AdminTemplateData::new("admin/users", json!(users_json)).render()?; | ||
| Ok(Html(text)) | ||
| } | ||
|
|
||
| #[get("/users/by-mail/<mail>")] | ||
| async fn get_user_by_mail_json(mail: &str, _token: AdminToken, conn: DbConn) -> JsonResult { | ||
| if let Some(u) = User::find_by_mail(mail, &conn).await { | ||
| let mut usr = u.to_json(&conn).await; | ||
| usr["userEnabled"] = json!(u.enabled); | ||
| usr["createdAt"] = json!(format_naive_datetime_local(&u.created_at, DT_FMT)); | ||
| Ok(Json(usr)) | ||
| if let Some((u, sso)) = SsoUser::find_by_mail(mail, &conn).await { | ||
| let user_json = get_users_property(vec![(u, sso)], &conn).await[0].clone(); | ||
| Ok(Json(user_json)) | ||
| } else { | ||
| err_code!("User doesn't exist", Status::NotFound.code); | ||
| } | ||
| } | ||
|
|
||
| #[get("/users/<user_id>")] | ||
| async fn get_user_json(user_id: UserId, _token: AdminToken, conn: DbConn) -> JsonResult { | ||
| let u = get_user_or_404(&user_id, &conn).await?; | ||
| let mut usr = u.to_json(&conn).await; | ||
| usr["userEnabled"] = json!(u.enabled); | ||
| usr["createdAt"] = json!(format_naive_datetime_local(&u.created_at, DT_FMT)); | ||
| Ok(Json(usr)) | ||
| let user = get_user_or_404(&user_id, &conn).await?; | ||
| let sso_user = get_sso_user(&user_id, &conn).await; | ||
| let user_json = get_users_property(vec![(user, sso_user)], &conn).await[0].clone(); | ||
|
|
||
| Ok(Json(user_json)) | ||
| } | ||
|
|
||
| #[post("/users/<user_id>/delete", format = "application/json")] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -527,6 +527,17 @@ impl SsoUser { | |
| }} | ||
| } | ||
|
|
||
| pub async fn find_by_uuid(uuid: &UserId, conn: &DbConn) -> Option<(User, Option<Self>)> { | ||
| db_run! { conn: { | ||
| users::table | ||
| .left_join(sso_users::table) | ||
| .select(<(User, Option<Self>)>::as_select()) | ||
| .filter(users::uuid.eq(uuid)) | ||
| .first::<(User, Option<Self>)>(conn) | ||
| .ok() | ||
| }} | ||
| } | ||
|
|
||
|
||
| pub async fn delete(user_uuid: &UserId, conn: &DbConn) -> EmptyResult { | ||
| db_run! { conn: { | ||
| diesel::delete(sso_users::table.filter(sso_users::user_uuid.eq(user_uuid))) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function would become obsolete if
SsoUser::find_by_uuidwere to return anOption<SsoUser>.