mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-03 22:01:50 +00:00
refactor: Add function to check user profile visibility
This commit is contained in:
3 files changed
+76
-9
No files matched your search
@@ -29,10 +29,18 @@ pub(crate) async fn get_profile_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<get_profile::v3::Request>,
|
||||
) -> Result<get_profile::v3::Response> {
|
||||
if services.config.require_auth_for_profile_requests && body.identity.is_none() {
|
||||
return Err!(Request(Unauthorized(
|
||||
"This server requires authentication to access user profiles."
|
||||
)));
|
||||
let querying_user = body
|
||||
.identity
|
||||
.as_ref()
|
||||
.and_then(|identity| identity.sender_user())
|
||||
.into();
|
||||
|
||||
if !services
|
||||
.users
|
||||
.user_can_see_profile(&body.user_id, querying_user)
|
||||
.await
|
||||
{
|
||||
return Err!(Request(Unauthorized("You cannot access this user's profile.")));
|
||||
}
|
||||
|
||||
let Some(profile) = fetch_full_profile(&services, &body.user_id).await else {
|
||||
@@ -46,10 +54,18 @@ pub(crate) async fn get_profile_field_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<get_profile_field::v3::Request>,
|
||||
) -> Result<get_profile_field::v3::Response> {
|
||||
if services.config.require_auth_for_profile_requests && body.identity.is_none() {
|
||||
return Err!(Request(Unauthorized(
|
||||
"This server requires authentication to access user profiles."
|
||||
)));
|
||||
let querying_user = body
|
||||
.identity
|
||||
.as_ref()
|
||||
.and_then(|identity| identity.sender_user())
|
||||
.into();
|
||||
|
||||
if !services
|
||||
.users
|
||||
.user_can_see_profile(&body.user_id, querying_user)
|
||||
.await
|
||||
{
|
||||
return Err!(Request(Unauthorized("You cannot access this user's profile.")));
|
||||
}
|
||||
|
||||
let value = fetch_profile_field(&services, &body.user_id, body.field.clone()).await?;
|
||||
|
||||
@@ -12,7 +12,7 @@ pub use account::{AccessTokenStatus, AccountStatus};
|
||||
use conduwuit::{Err, Error, Result, err, utils};
|
||||
use database::Map;
|
||||
pub use device::{DeviceToken, PreviousTokenAction};
|
||||
pub use profile::ProfileFieldChange;
|
||||
pub use profile::{ProfileFieldChange, ProfileQuerier};
|
||||
use ruma::{UserId, api::error::ErrorKind, encryption::CrossSigningKey, serde::Raw};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
@@ -39,6 +39,30 @@ impl ProfileFieldChange {
|
||||
}
|
||||
}
|
||||
|
||||
/// The entity querying a user's profile.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub enum ProfileQuerier<'id> {
|
||||
/// The user owning the profile. This variant exists to improve the
|
||||
/// ergonomics of code that looks up a user's own profile, so the user's
|
||||
/// ID doesn't need to be passed twice.
|
||||
Owner,
|
||||
/// Another user, possibly but not necessarily the user owning the profile.
|
||||
Other(&'id UserId),
|
||||
/// An anonymous entity.
|
||||
#[default]
|
||||
Anonymous,
|
||||
}
|
||||
|
||||
impl<'id> From<Option<&'id UserId>> for ProfileQuerier<'id> {
|
||||
fn from(value: Option<&'id UserId>) -> Self {
|
||||
if let Some(user_id) = value {
|
||||
Self::Other(user_id)
|
||||
} else {
|
||||
Self::Anonymous
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Service {
|
||||
pub async fn set_profile_field(
|
||||
&self,
|
||||
@@ -360,4 +384,31 @@ impl super::Service {
|
||||
.ready_for_each(|(key, _)| self.set_profile_key(user_id, &key, None))
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Check if a user can access another user's profile.
|
||||
// This function doesn't do much of anything right now, but future MSCs that
|
||||
// add profile access controls should be implemented here.
|
||||
pub fn user_can_see_profile(
|
||||
&self,
|
||||
target_user: &UserId,
|
||||
querying_user: ProfileQuerier<'_>,
|
||||
) -> impl Future<Output = bool> {
|
||||
std::future::ready(match querying_user {
|
||||
| ProfileQuerier::Anonymous => {
|
||||
// For unauthenticated requests we check the relevant config
|
||||
// option.
|
||||
!self.services.config.require_auth_for_profile_requests
|
||||
},
|
||||
| ProfileQuerier::Other(querying_user) if querying_user != target_user => {
|
||||
// Currently users can't control their own profile visibility,
|
||||
// so we always return true for authenticated
|
||||
// requests from other users.
|
||||
true
|
||||
},
|
||||
| ProfileQuerier::Owner | ProfileQuerier::Other(_) => {
|
||||
// Users can always query their own profile.
|
||||
true
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user