|
| 1 | +// Copyright 2023 The Matrix.org Foundation C.I.C. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use async_graphql::{Context, Description, Enum, InputObject, Object, ID}; |
| 16 | +use mas_storage::{ |
| 17 | + job::{DeactivateUserJob, JobRepositoryExt, ProvisionUserJob}, |
| 18 | + user::UserRepository, |
| 19 | +}; |
| 20 | +use tracing::info; |
| 21 | + |
| 22 | +use crate::{ |
| 23 | + model::{NodeType, User}, |
| 24 | + state::ContextExt, |
| 25 | +}; |
| 26 | + |
| 27 | +#[derive(Default)] |
| 28 | +pub struct UserMutations { |
| 29 | + _private: (), |
| 30 | +} |
| 31 | + |
| 32 | +/// The input for the `addUser` mutation. |
| 33 | +#[derive(InputObject)] |
| 34 | +struct AddUserInput { |
| 35 | + /// The username of the user to add. |
| 36 | + username: String, |
| 37 | +} |
| 38 | + |
| 39 | +/// The status of the `addUser` mutation. |
| 40 | +#[derive(Enum, Copy, Clone, Eq, PartialEq)] |
| 41 | +enum AddUserStatus { |
| 42 | + /// The user was added. |
| 43 | + Added, |
| 44 | + |
| 45 | + /// The user already exists. |
| 46 | + Exists, |
| 47 | + |
| 48 | + /// The username is invalid. |
| 49 | + Invalid, |
| 50 | +} |
| 51 | + |
| 52 | +/// The payload for the `addUser` mutation. |
| 53 | +#[derive(Description)] |
| 54 | +enum AddUserPayload { |
| 55 | + Added(mas_data_model::User), |
| 56 | + Exists(mas_data_model::User), |
| 57 | + Invalid, |
| 58 | +} |
| 59 | + |
| 60 | +#[Object(use_type_description)] |
| 61 | +impl AddUserPayload { |
| 62 | + /// Status of the operation |
| 63 | + async fn status(&self) -> AddUserStatus { |
| 64 | + match self { |
| 65 | + Self::Added(_) => AddUserStatus::Added, |
| 66 | + Self::Exists(_) => AddUserStatus::Exists, |
| 67 | + Self::Invalid => AddUserStatus::Invalid, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// The user that was added. |
| 72 | + async fn user(&self) -> Option<User> { |
| 73 | + match self { |
| 74 | + Self::Added(user) | Self::Exists(user) => Some(User(user.clone())), |
| 75 | + Self::Invalid => None, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// The input for the `lockUser` mutation. |
| 81 | +#[derive(InputObject)] |
| 82 | +struct LockUserInput { |
| 83 | + /// The ID of the user to lock. |
| 84 | + user_id: ID, |
| 85 | + |
| 86 | + /// Permanently lock the user. |
| 87 | + deactivate: Option<bool>, |
| 88 | +} |
| 89 | + |
| 90 | +/// The status of the `lockUser` mutation. |
| 91 | +#[derive(Enum, Copy, Clone, Eq, PartialEq)] |
| 92 | +enum LockUserStatus { |
| 93 | + /// The user was locked. |
| 94 | + Locked, |
| 95 | + |
| 96 | + /// The user was not found. |
| 97 | + NotFound, |
| 98 | +} |
| 99 | + |
| 100 | +/// The payload for the `lockUser` mutation. |
| 101 | +#[derive(Description)] |
| 102 | +enum LockUserPayload { |
| 103 | + /// The user was locked. |
| 104 | + Locked(mas_data_model::User), |
| 105 | + |
| 106 | + /// The user was not found. |
| 107 | + NotFound, |
| 108 | +} |
| 109 | + |
| 110 | +#[Object(use_type_description)] |
| 111 | +impl LockUserPayload { |
| 112 | + /// Status of the operation |
| 113 | + async fn status(&self) -> LockUserStatus { |
| 114 | + match self { |
| 115 | + Self::Locked(_) => LockUserStatus::Locked, |
| 116 | + Self::NotFound => LockUserStatus::NotFound, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// The user that was locked. |
| 121 | + async fn user(&self) -> Option<User> { |
| 122 | + match self { |
| 123 | + Self::Locked(user) => Some(User(user.clone())), |
| 124 | + Self::NotFound => None, |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn valid_username_character(c: char) -> bool { |
| 130 | + c.is_ascii_lowercase() |
| 131 | + || c.is_ascii_digit() |
| 132 | + || c == '=' |
| 133 | + || c == '_' |
| 134 | + || c == '-' |
| 135 | + || c == '.' |
| 136 | + || c == '/' |
| 137 | + || c == '+' |
| 138 | +} |
| 139 | + |
| 140 | +// XXX: this should probably be moved somewhere else |
| 141 | +fn username_valid(username: &str) -> bool { |
| 142 | + if username.is_empty() || username.len() > 255 { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + // Should not start with an underscore |
| 147 | + if username.get(0..1) == Some("_") { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + // Should only contain valid characters |
| 152 | + if !username.chars().all(valid_username_character) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + true |
| 157 | +} |
| 158 | + |
| 159 | +#[Object] |
| 160 | +impl UserMutations { |
| 161 | + /// Add a user. This is only available to administrators. |
| 162 | + async fn add_user( |
| 163 | + &self, |
| 164 | + ctx: &Context<'_>, |
| 165 | + input: AddUserInput, |
| 166 | + ) -> Result<AddUserPayload, async_graphql::Error> { |
| 167 | + let state = ctx.state(); |
| 168 | + let requester = ctx.requester(); |
| 169 | + let clock = state.clock(); |
| 170 | + let mut rng = state.rng(); |
| 171 | + |
| 172 | + if !requester.is_admin() { |
| 173 | + return Err(async_graphql::Error::new("Unauthorized")); |
| 174 | + } |
| 175 | + |
| 176 | + let mut repo = state.repository().await?; |
| 177 | + |
| 178 | + if let Some(user) = repo.user().find_by_username(&input.username).await? { |
| 179 | + return Ok(AddUserPayload::Exists(user)); |
| 180 | + } |
| 181 | + |
| 182 | + // Do some basic check on the username |
| 183 | + if !username_valid(&input.username) { |
| 184 | + return Ok(AddUserPayload::Invalid); |
| 185 | + } |
| 186 | + |
| 187 | + let user = repo.user().add(&mut rng, &clock, input.username).await?; |
| 188 | + |
| 189 | + repo.job() |
| 190 | + .schedule_job(ProvisionUserJob::new(&user)) |
| 191 | + .await?; |
| 192 | + |
| 193 | + repo.save().await?; |
| 194 | + |
| 195 | + Ok(AddUserPayload::Added(user)) |
| 196 | + } |
| 197 | + |
| 198 | + /// Lock a user. This is only available to administrators. |
| 199 | + async fn lock_user( |
| 200 | + &self, |
| 201 | + ctx: &Context<'_>, |
| 202 | + input: LockUserInput, |
| 203 | + ) -> Result<LockUserPayload, async_graphql::Error> { |
| 204 | + let state = ctx.state(); |
| 205 | + let requester = ctx.requester(); |
| 206 | + |
| 207 | + if !requester.is_admin() { |
| 208 | + return Err(async_graphql::Error::new("Unauthorized")); |
| 209 | + } |
| 210 | + |
| 211 | + let mut repo = state.repository().await?; |
| 212 | + |
| 213 | + let user_id = NodeType::User.extract_ulid(&input.user_id)?; |
| 214 | + let user = repo.user().lookup(user_id).await?; |
| 215 | + |
| 216 | + let Some(user) = user else { |
| 217 | + return Ok(LockUserPayload::NotFound); |
| 218 | + }; |
| 219 | + |
| 220 | + let deactivate = input.deactivate.unwrap_or(false); |
| 221 | + |
| 222 | + let user = repo.user().lock(&state.clock(), user).await?; |
| 223 | + |
| 224 | + if deactivate { |
| 225 | + info!("Scheduling deactivation of user {}", user.id); |
| 226 | + repo.job() |
| 227 | + .schedule_job(DeactivateUserJob::new(&user, deactivate)) |
| 228 | + .await?; |
| 229 | + } |
| 230 | + |
| 231 | + repo.save().await?; |
| 232 | + |
| 233 | + Ok(LockUserPayload::Locked(user)) |
| 234 | + } |
| 235 | +} |
0 commit comments