Skip to content

Commit 1a6af6b

Browse files
committed
push for this OD month
1 parent e7cf143 commit 1a6af6b

File tree

12 files changed

+31
-225
lines changed

12 files changed

+31
-225
lines changed

contracts/course/course_access/src/functions/config.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,12 @@ use crate::schema::{KEY_COURSE_REG_ADDR, KEY_USER_MGMT_ADDR};
1010
const INIT_EVENT: Symbol = symbol_short!("initialz");
1111
const UPDATE_ADDRESS_EVENT: Symbol = symbol_short!("updAddr");
1212

13-
/// Storage key for initialization flag
13+
1414
const KEY_INIT: &str = "init";
1515

16-
/// Storage key for contract owner address
1716
const KEY_OWNER: &str = "owner";
1817

19-
/// One-time constructor that sets owner and external contract addresses.
20-
///
21-
/// This function initializes the contract with the necessary configuration
22-
/// and can only be called once during the contract's lifetime.
23-
///
24-
/// # Arguments
25-
///
26-
/// * `env` - The Soroban environment
27-
/// * `caller` - The address of the contract deployer who becomes the owner
28-
/// * `user_mgmt_addr` - Address of the user management contract
29-
/// * `course_registry_addr` - Address of the course registry contract
30-
///
31-
/// # Panics
32-
///
33-
/// Panics if the contract has already been initialized.
18+
3419
pub fn initialize(
3520
env: Env,
3621
caller: Address,
@@ -59,22 +44,7 @@ pub fn initialize(
5944
.publish((INIT_EVENT,), (caller, user_mgmt_addr, course_registry_addr));
6045
}
6146

62-
/// Update external contract addresses.
63-
///
64-
/// This function allows the contract owner to update the addresses of external
65-
/// contracts that this contract depends on for authentication and authorization.
66-
/// Only the stored owner can perform this operation.
67-
///
68-
/// # Arguments
69-
///
70-
/// * `env` - The Soroban environment
71-
/// * `caller` - The address of the user attempting to update the configuration
72-
/// * `user_mgmt_addr` - New address of the user management contract
73-
/// * `course_registry_addr` - New address of the course registry contract
74-
///
75-
/// # Panics
76-
///
77-
/// Panics if the contract is not initialized or if the caller is not the owner.
47+
7848
pub fn set_contract_addrs(
7949
env: Env,
8050
caller: Address,
@@ -93,7 +63,7 @@ pub fn set_contract_addrs(
9363
panic!("not initialized");
9464
}
9565

96-
// Only owner may update
66+
9767
let owner: Address = env
9868
.storage()
9969
.instance()

contracts/course/course_access/src/functions/contract_versioning.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ pub enum VersioningError {
2222
MigrationFailed = 6,
2323
}
2424

25-
/// Storage keys for versioning data
25+
2626
const VERSION_HISTORY_KEY: &str = "version_history";
2727
const MIGRATION_STATUS_KEY: &str = "migration_status";
2828

29-
/// Get the version history of the contract
29+
3030
pub fn get_version_history(env: &Env) -> Vec<String> {
3131
let key = String::from_str(env, VERSION_HISTORY_KEY);
3232
env.storage()
@@ -35,7 +35,6 @@ pub fn get_version_history(env: &Env) -> Vec<String> {
3535
.unwrap_or_else(|| vec![env])
3636
}
3737

38-
/// Store a new version in the history
3938
fn store_version_in_history(env: &Env, version: String) {
4039
let mut history: Vec<String> = get_version_history(env);
4140
history.push_back(version.clone());
@@ -44,7 +43,7 @@ fn store_version_in_history(env: &Env, version: String) {
4443
env.storage().instance().set(&key, &history);
4544
}
4645

47-
/// Check if a version exists in the history
46+
4847
fn version_exists_in_history(env: &Env, version: &String) -> bool {
4948
let history: Vec<String> = get_version_history(env);
5049
for v in history.iter() {
@@ -55,7 +54,7 @@ fn version_exists_in_history(env: &Env, version: &String) -> bool {
5554
false
5655
}
5756

58-
/// Get migration status information
57+
5958
pub fn get_migration_status(env: &Env) -> String {
6059
let key: String = String::from_str(env, MIGRATION_STATUS_KEY);
6160
env.storage()
@@ -64,22 +63,20 @@ pub fn get_migration_status(env: &Env) -> String {
6463
.unwrap_or_else(|| String::from_str(env, "No migrations pending"))
6564
}
6665

67-
/// Set migration status
66+
6867
fn set_migration_status(env: &Env, status: String) {
6968
let key = String::from_str(env, MIGRATION_STATUS_KEY);
7069
env.storage().instance().set(&key, &status);
7170
}
7271

73-
/// Check compatibility between two versions
72+
7473
pub fn is_version_compatible(_env: &Env, _from_version: String, _to_version: String) -> bool {
7574
// Simple compatibility check - for now, assume all versions are compatible
7675
// In a real implementation, you would parse semantic versions properly
7776
true
7877
}
7978

80-
/// Check if the caller is authorized to perform migration
81-
/// For course access, we'll allow any authenticated user for now
82-
/// In a production environment, this should check for admin privileges
79+
8380
fn is_authorized_for_migration(_env: &Env, _caller: Address) -> bool {
8481
// For now, we'll allow any authenticated user
8582
// In a real implementation, you would check against user management contract
@@ -93,32 +90,31 @@ fn is_authorized_for_migration(_env: &Env, _caller: Address) -> bool {
9390
true // Placeholder - allow all authenticated users
9491
}
9592

96-
/// Migrate access data between contract versions
9793
pub fn migrate_access_data(
9894
env: &Env,
9995
caller: Address,
10096
from_version: String,
10197
to_version: String,
10298
) -> bool {
103-
// Check if caller is authorized
99+
104100
if !is_authorized_for_migration(env, caller.clone()) {
105101
set_migration_status(env, String::from_str(env, "Migration failed: Unauthorized"));
106102
return false;
107103
}
108104

109-
// Validate versions exist in history
105+
110106
if !version_exists_in_history(env, &from_version) {
111107
set_migration_status(env, String::from_str(env, "Migration failed: Source version not found"));
112108
return false;
113109
}
114110

115-
// Check compatibility
111+
116112
if !is_version_compatible(env, from_version.clone(), to_version.clone()) {
117113
set_migration_status(env, String::from_str(env, "Migration failed: Versions not compatible"));
118114
return false;
119115
}
120116

121-
// Perform migration based on version differences
117+
122118
let migration_result: bool = perform_access_data_migration(env, &from_version, &to_version);
123119

124120
if migration_result {

contracts/course/course_access/src/functions/list_user_courses.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@ use soroban_sdk::{Address, Env, Vec};
55

66
use crate::schema::{DataKey, UserCourses};
77

8-
/// List all courses that a specific user has access to.
9-
///
10-
/// This function retrieves all courses that the specified user is enrolled in
11-
/// or has been granted access to. If the user has no courses, it returns
12-
/// an empty UserCourses struct.
13-
///
14-
/// # Arguments
15-
///
16-
/// * `env` - The Soroban environment
17-
/// * `user` - The address of the user to query courses for
18-
///
19-
/// # Returns
20-
///
21-
/// Returns a `UserCourses` struct containing the user's address and a list
22-
/// of course IDs they have access to. If no courses are found, returns
23-
/// an empty list.
8+
249
pub fn list_user_courses(env: Env, user: Address) -> UserCourses {
2510
let key: DataKey = DataKey::UserCourses(user.clone());
2611
let res: UserCourses = env.storage().persistent().get(&key).unwrap_or(UserCourses {

contracts/course/course_access/src/functions/revoke_all_access.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,7 @@ const COURSES_KEY: Symbol = symbol_short!("courses");
1515
/// Event symbol for revoke all access operations
1616
const REVOKE_ALL_EVENT: Symbol = symbol_short!("revokeAll");
1717

18-
/// Revoke access for all users from a specific course.
19-
///
20-
/// This function removes access for all users from the specified course and
21-
/// updates all related storage structures. Only admin users or course creators
22-
/// are authorized to perform this operation.
23-
///
24-
/// # Arguments
25-
///
26-
/// * `env` - The Soroban environment
27-
/// * `caller` - The address of the user requesting the operation
28-
/// * `course_id` - The unique identifier of the course to revoke all access from
29-
///
30-
/// # Returns
31-
///
32-
/// Returns the number of users whose access was revoked.
33-
///
34-
/// # Authorization
35-
///
36-
/// The caller must be either:
37-
/// - An admin user (verified via user management contract)
38-
/// - The creator of the course (verified via course registry contract)
39-
///
40-
/// # Events
41-
///
42-
/// Emits a `revokeall` event with the course ID and number of affected users.
43-
///
44-
/// # Panics
45-
///
46-
/// Panics with `Error::Unauthorized` if the caller is not authorized to perform this operation.
18+
4719
pub fn revoke_all_access(env: Env, caller: Address, course_id: String) -> u32 {
4820
caller.require_auth();
4921

contracts/course/course_access/src/functions/save_profile.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,7 @@ use crate::schema::{DataKey, UserProfile};
88

99
const SAVE_USER_PROFILE_EVENT: Symbol = symbol_short!("saveUsPrl");
1010

11-
/// Save or update a user's profile information on-chain.
12-
///
13-
/// This function stores user profile data in persistent storage, including
14-
/// personal and professional information. It validates required fields
15-
/// before saving the profile.
16-
///
17-
/// # Arguments
18-
///
19-
/// * `env` - The Soroban environment
20-
/// * `name` - The user's full name (required)
21-
/// * `email` - The user's email address (required)
22-
/// * `profession` - Optional profession or job title
23-
/// * `goals` - Optional learning goals or objectives
24-
/// * `country` - The user's country of residence (required)
25-
/// * `user` - The address of the user whose profile is being saved
26-
///
27-
/// # Panics
28-
///
29-
/// Panics with appropriate error if any required field is empty:
30-
/// - `Error::NameRequired` if name is empty
31-
/// - `Error::EmailRequired` if email is empty
32-
/// - `Error::CountryRequired` if country is empty
11+
3312
pub fn save_user_profile(
3413
env: Env,
3514
name: String,

contracts/course/course_access/src/functions/utils/storage_utils.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::functions::config::{TTL_BUMP, TTL_TTL};
99

1010
const TEMP_TTL: u32 = 900; // 15 minutes
1111

12-
/// Efficiently get or create UserCourses record
12+
1313
pub fn get_or_create_user_courses(
1414
env: &Env,
1515
user: &Address,
@@ -37,7 +37,7 @@ pub fn get_or_create_user_courses(
3737
user_courses
3838
}
3939

40-
/// Efficiently get or create CourseUsers record
40+
4141
pub fn get_or_create_course_users(
4242
env: &Env,
4343
course_id: &String,
@@ -58,14 +58,14 @@ pub fn get_or_create_course_users(
5858
users: Vec::new(env),
5959
});
6060

61-
// Cache result
61+
6262
env.storage().temporary().set(&temp_key, &course_users);
6363
env.storage().temporary().extend_ttl(&temp_key, 0, TEMP_TTL);
6464

6565
course_users
6666
}
6767

68-
/// Update both user courses and course users in a single atomic operation
68+
6969
pub fn update_access_mappings(
7070
env: &Env,
7171
course_id: &String,
@@ -105,7 +105,7 @@ pub fn update_access_mappings(
105105
env.storage().temporary().set(&temp_course_key, &course_users);
106106
}
107107

108-
/// Check if a user has access to a course with caching
108+
109109
pub fn has_course_access(
110110
env: &Env,
111111
course_id: &String,
@@ -134,7 +134,7 @@ pub fn has_course_access(
134134
has_access
135135
}
136136

137-
/// Clear access-related caches for a course
137+
138138
pub fn invalidate_course_access_cache(
139139
env: &Env,
140140
course_id: &String,
@@ -143,7 +143,6 @@ pub fn invalidate_course_access_cache(
143143
env.storage().temporary().remove(&temp_users_key);
144144
}
145145

146-
/// Clear access-related caches for a user
147146
pub fn invalidate_user_access_cache(
148147
env: &Env,
149148
user: &Address,

contracts/user_management/src/functions/delete_user.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,7 @@ use soroban_sdk::{symbol_short, Address, Env, Symbol};
99
// Event symbol for user deactivation
1010
const USER_DEACTIVATED_EVENT: Symbol = symbol_short!("usrDeact");
1111

12-
/// Delete (deactivate) a user account
13-
///
14-
/// This function performs a soft delete by marking the user as inactive instead of
15-
/// permanently removing their data. Only admins or the user themselves can trigger deletion.
16-
///
17-
/// # Arguments
18-
/// * `env` - Soroban environment
19-
/// * `caller` - Address performing the deletion (must be admin or the user themselves)
20-
/// * `user_id` - Address of the user to be deactivated
21-
///
22-
/// # Panics
23-
/// * If caller authentication fails
24-
/// * If user doesn't exist
25-
/// * If caller is neither admin nor the user themselves
26-
/// * If user is already inactive
27-
///
28-
/// # Events
29-
/// Emits a user deactivation event upon successful deletion
12+
3013
pub fn delete_user(env: Env, caller: Address, user_id: Address) {
3114
// Require authentication for the caller
3215
caller.require_auth();

contracts/user_management/src/functions/edit_user_profile.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,6 @@ fn check_edit_permission(env: &Env, caller: &Address, user_id: &Address) -> bool
3636
is_admin(env.clone(), caller.clone())
3737
}
3838

39-
/// Edit an existing user profile
40-
///
41-
/// Updates an existing user profile with new values for allowed fields.
42-
/// Only the user themselves or administrators can perform updates.
43-
/// Email and role fields cannot be updated through this function.
44-
///
45-
/// # Arguments
46-
/// * `env` - Soroban environment
47-
/// * `caller` - Address of the user performing the update
48-
/// * `user_id` - Address of the user whose profile is being updated
49-
/// * `updates` - ProfileUpdateParams containing fields to update
50-
///
51-
/// # Returns
52-
/// * `UserProfile` - The updated user profile
53-
///
54-
/// # Panics
55-
/// * If caller authentication fails
56-
/// * If user profile doesn't exist
57-
/// * If caller lacks permission to edit
58-
/// * If any field validation fails
59-
/// * If user is inactive
60-
///
61-
/// # Events
62-
/// Emits a user update event upon successful profile update
6339
pub fn edit_user_profile(
6440
env: Env,
6541
caller: Address,

contracts/user_management/src/functions/get_user_by_id.rs

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

10-
/// Get User by ID
11-
/// - Only the profile owner or an admin can access it.
12-
/// - Returns the full profile (assuming no sensitive data like passwords are stored in UserProfile).
10+
1311
pub fn get_user_by_id(env: Env, requester: Address, user_id: Address) -> UserProfile {
1412
// Require authentication for the requester
1513
requester.require_auth();

0 commit comments

Comments
 (0)