Skip to content

Commit 871e7ed

Browse files
authored
Merge pull request #222 from Pfed-prog/main
closes #217 and #216
2 parents ad84e0c + 2b9e72d commit 871e7ed

File tree

2 files changed

+113
-17
lines changed

2 files changed

+113
-17
lines changed

contracts/user_management/src/functions/get_user_by_id.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@ use crate::error::{handle_error, Error};
77
use crate::schema::{DataKey, UserProfile};
88
use core::iter::Iterator;
99

10-
10+
/// Retrieves a user profile by user ID.
11+
///
12+
/// # Arguments
13+
///
14+
/// * `env` - Soroban environment.
15+
/// * `requester` - The address of the requester (must be authenticated).
16+
/// * `user_id` - The address of the user whose profile is to be retrieved.
17+
///
18+
/// # Returns
19+
///
20+
/// * `UserProfile` - The user profile associated with the provided user ID.
21+
///
22+
/// # Panics
23+
///
24+
/// * Panics if the requester is not authorized or if the profile does not exist.
1125
pub fn get_user_by_id(env: Env, requester: Address, user_id: Address) -> UserProfile {
1226
// Require authentication for the requester
1327
requester.require_auth();
@@ -28,6 +42,16 @@ pub fn get_user_by_id(env: Env, requester: Address, user_id: Address) -> UserPro
2842
profile
2943
}
3044

45+
/// Checks whether the given address is an admin.
46+
///
47+
/// # Arguments
48+
///
49+
/// * `env` - Soroban environment.
50+
/// * `who` - The address to check for admin status.
51+
///
52+
/// # Returns
53+
///
54+
/// * `bool` - True if the address is an admin, otherwise false.
3155
fn is_admin(env: &Env, who: &Address) -> bool {
3256
// Use the secure admin check from admin_management module
3357
use crate::schema::AdminConfig;

contracts/user_management/src/functions/list_all_registered_users.rs

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ fn string_contains(haystack: &String, needle: &String) -> bool {
1616
/// Security constants
1717
const MAX_PAGE_SIZE_ABSOLUTE: u32 = 1000;
1818

19-
19+
/// Lists all registered users with pagination and filtering (admin-only).
20+
///
21+
/// # Arguments
22+
///
23+
/// * `env` - Soroban environment.
24+
/// * `caller` - Address of the caller (must be admin).
25+
/// * `page` - The page number for pagination.
26+
/// * `page_size` - The size of each page.
27+
/// * `filter` - Optional filter criteria for users.
28+
///
29+
/// # Returns
30+
///
31+
/// * `Vec<LightProfile>` - A vector of lightweight user profiles.
2032
pub fn list_all_users(
2133
env: Env,
2234
caller: Address,
@@ -81,7 +93,7 @@ pub fn list_all_users(
8193
}
8294
}
8395

84-
let total_filtered = filtered_profiles.len();
96+
let total_filtered: u32 = filtered_profiles.len();
8597
if total_filtered == 0 {
8698
return Vec::new(&env);
8799
}
@@ -120,7 +132,15 @@ pub fn list_all_users(
120132
result
121133
}
122134

123-
/// Checks whether the system is properly initialized
135+
/// Checks whether the system is properly initialized.
136+
///
137+
/// # Arguments
138+
///
139+
/// * `env` - Soroban environment.
140+
///
141+
/// # Returns
142+
///
143+
/// * `bool` - True if the system is initialized, otherwise false.
124144
fn is_system_initialized(env: &Env) -> bool {
125145
if let Some(config) = env
126146
.storage()
@@ -133,15 +153,32 @@ fn is_system_initialized(env: &Env) -> bool {
133153
}
134154
}
135155

136-
/// Gets the admin configuration with defaults
156+
/// Gets the admin configuration with defaults.
157+
///
158+
/// # Arguments
159+
///
160+
/// * `env` - Soroban environment.
161+
///
162+
/// # Returns
163+
///
164+
/// * `AdminConfig` - The admin configuration.
137165
fn get_admin_config(env: &Env) -> AdminConfig {
138166
env.storage()
139167
.persistent()
140168
.get::<DataKey, AdminConfig>(&DataKey::AdminConfig)
141169
.unwrap_or_else(|| handle_error(env, Error::SystemNotInitialized))
142170
}
143171

144-
/// Checks whether the caller is an admin with enhanced security
172+
/// Checks whether the caller is an admin with enhanced security.
173+
///
174+
/// # Arguments
175+
///
176+
/// * `env` - Soroban environment.
177+
/// * `who` - Address of the caller.
178+
///
179+
/// # Returns
180+
///
181+
/// * `bool` - True if the caller is an admin, otherwise false.
145182
fn is_admin(env: &Env, who: &Address) -> bool {
146183
// First check if system is initialized
147184
if !is_system_initialized(env) {
@@ -166,7 +203,16 @@ fn is_admin(env: &Env, who: &Address) -> bool {
166203
}
167204
}
168205

169-
/// Checks if a profile matches the given filter criteria
206+
/// Checks if a profile matches the given filter criteria.
207+
///
208+
/// # Arguments
209+
///
210+
/// * `profile` - The user profile to check.
211+
/// * `filter` - Optional filter criteria.
212+
///
213+
/// # Returns
214+
///
215+
/// * `bool` - True if the profile matches the filter, otherwise false.
170216
fn matches_filter(
171217
profile: &LightProfile,
172218
filter: &Option<UserFilter>,
@@ -213,7 +259,17 @@ fn matches_filter(
213259
true
214260
}
215261

216-
/// Validates and sanitizes input parameters
262+
/// Validates and sanitizes input parameters.
263+
///
264+
/// # Arguments
265+
///
266+
/// * `page_size` - The size of the page to validate.
267+
/// * `filter` - Optional filter criteria.
268+
/// * `config` - The admin configuration to use for validation.
269+
///
270+
/// # Returns
271+
///
272+
/// * `Result<(), &'static str>` - Ok if validation passes, Err with a message otherwise.
217273
fn validate_input(
218274
page_size: u32,
219275
filter: &Option<UserFilter>,
@@ -279,7 +335,7 @@ pub fn list_all_users_cursor(
279335
}
280336

281337
// Get admin configuration
282-
let config = get_admin_config(&env);
338+
let config: AdminConfig = get_admin_config(&env);
283339

284340
// Authorization: only admins can call
285341
if !is_admin(&env, &caller) {
@@ -308,17 +364,17 @@ pub fn list_all_users_cursor(
308364
}
309365

310366
// Find the starting index based on cursor
311-
let start_index = if let Some(cursor) = &pagination.cursor {
367+
let start_index: u32 = if let Some(cursor) = &pagination.cursor {
312368
find_address_index(&users_index, cursor).unwrap_or(0)
313369
} else {
314370
0
315371
};
316372

317373
// Collect filtered profiles starting from the cursor position
318-
let mut result_data = Vec::new(&env);
319-
let mut processed_count = 0;
374+
let mut result_data: Vec<LightProfile> = Vec::new(&env);
375+
let mut processed_count: u32 = 0;
320376
let mut next_cursor: Option<Address> = None;
321-
let mut total_matching = 0u32;
377+
let mut total_matching: u32 = 0u32;
322378

323379
for i in start_index..users_index.len() {
324380
if processed_count >= pagination.limit {
@@ -361,11 +417,11 @@ pub fn list_all_users_cursor(
361417
}
362418

363419
// Determine if there are more pages
364-
let has_more = if next_cursor.is_some() {
420+
let has_more: bool = if next_cursor.is_some() {
365421
true
366422
} else {
367423
// Check if there are more items after the current batch
368-
let mut found_more = false;
424+
let mut found_more: bool = false;
369425
for i in (start_index + processed_count)..users_index.len() {
370426
if let Some(addr) = users_index.get(i) {
371427
if let Some(profile) = env
@@ -399,7 +455,14 @@ pub fn list_all_users_cursor(
399455

400456
/// Finds the index of an address in the users index vector.
401457
///
402-
/// Returns the index if found, None otherwise.
458+
/// # Arguments
459+
///
460+
/// * `users_index` - A vector of user addresses.
461+
/// * `target` - The address to find.
462+
///
463+
/// # Returns
464+
///
465+
/// * `Option<u32>` - The index if found, None otherwise.
403466
fn find_address_index(users_index: &Vec<Address>, target: &Address) -> Option<u32> {
404467
for i in 0..users_index.len() {
405468
if let Some(addr) = users_index.get(i) {
@@ -412,12 +475,21 @@ fn find_address_index(users_index: &Vec<Address>, target: &Address) -> Option<u3
412475
}
413476

414477
/// Validates pagination parameters for cursor-based pagination.
478+
///
479+
/// # Arguments
480+
///
481+
/// * `pagination` - Pagination parameters to validate.
482+
/// * `config` - The admin configuration to use for validation.
483+
///
484+
/// # Returns
485+
///
486+
/// * `Result<(), &'static str>` - Ok if validation passes, Err with a message otherwise.
415487
fn validate_pagination_params(
416488
pagination: &PaginationParams,
417489
config: &AdminConfig,
418490
) -> Result<(), &'static str> {
419491
// Validate limit
420-
let max_allowed = config.max_page_size.min(MAX_PAGE_SIZE_ABSOLUTE);
492+
let max_allowed: u32 = config.max_page_size.min(MAX_PAGE_SIZE_ABSOLUTE);
421493
if pagination.limit == 0 {
422494
return Err("limit must be greater than 0");
423495
}

0 commit comments

Comments
 (0)