-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::collections::HashMap; | ||
|
||
/// Struct representing a user. | ||
pub struct User { | ||
username: String, | ||
password: String, | ||
email: String, | ||
is_admin: bool, | ||
} | ||
|
||
impl User { | ||
/// Creates a new user. | ||
pub fn new(username: &str, password: &str, email: &str, is_admin: bool) -> User { | ||
User { | ||
username: username.to_string(), | ||
password: password.to_string(), | ||
email: email.to_string(), | ||
is_admin, | ||
} | ||
} | ||
|
||
/// Validates the user's credentials. | ||
pub fn login(&self, username: &str, password: &str) -> bool { | ||
self.username == username && self.password == password | ||
} | ||
|
||
/// Checks if the user is an admin. | ||
pub fn is_admin(&self) -> bool { | ||
self.is_admin | ||
} | ||
|
||
/// Gets the user's email address. | ||
pub fn get_email(&self) -> &str { | ||
&self.email | ||
} | ||
|
||
/// Changes the user's password. | ||
pub fn change_password(&mut self, new_password: &str) { | ||
self.password = new_password.to_string(); | ||
} | ||
|
||
/// Resets the user's password to a random value. | ||
pub fn reset_password(&mut self) { | ||
// Generate a random password (not secure, for demonstration purposes only) | ||
let new_password = "new_password"; | ||
self.password = new_password.to_string(); | ||
} | ||
|
||
/// Checks if the user's password is expired. | ||
pub fn is_password_expired(&self) -> bool { | ||
// For demonstration purposes, assume password expires after 90 days | ||
// and the user has not changed their password for 90 days | ||
true | ||
} | ||
|
||
/// Adds a new user to the system. | ||
pub fn add_user(username: &str, password: &str, email: &str, is_admin: bool) -> User { | ||
User::new(username, password, email, is_admin) | ||
} | ||
|
||
/// Deletes a user from the system. | ||
pub fn delete_user(user_map: &mut HashMap<String, User>, username: &str) { | ||
user_map.remove(username); | ||
} | ||
|
||
/// Retrieves a user from the system by username. | ||
pub fn get_user(user_map: &HashMap<String, User>, username: &str) -> Option<&User> { | ||
user_map.get(username) | ||
} | ||
|
||
/// Updates a user's information. | ||
pub fn update_user(user_map: &mut HashMap<String, User>, username: &str, new_email: &str, is_admin: bool) { | ||
if let Some(user) = user_map.get_mut(username) { | ||
user.email = new_email.to_string(); | ||
user.is_admin = is_admin; | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut user_map = HashMap::new(); | ||
let user = User::add_user("admin", "password", "admin@example.com", true); | ||
user_map.insert(user.username.clone(), user); | ||
|
||
// Retrieve user by username | ||
if let Some(user) = User::get_user(&user_map, "admin") { | ||
println!("User found: {}", user.username); | ||
} else { | ||
println!("User not found"); | ||
} | ||
|
||
// Update user information | ||
User::update_user(&mut user_map, "admin", "new_admin@example.com", false); | ||
|
||
// Delete user | ||
User::delete_user(&mut user_map, "admin"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/// Struct representing a session. | ||
pub struct Session { | ||
logged_in: bool, | ||
} | ||
|
||
impl Session { | ||
/// Creates a new session. | ||
pub fn new() -> Session { | ||
Session { logged_in: false } | ||
} | ||
|
||
/// Logs the user in. | ||
pub fn login(&mut self) { | ||
self.logged_in = true; | ||
} | ||
|
||
/// Logs the user out. | ||
pub fn logout(&mut self) { | ||
self.logged_in = false; | ||
} | ||
|
||
/// Checks if the user is logged in. | ||
pub fn is_logged_in(&self) -> bool { | ||
self.logged_in | ||
} | ||
} | ||
|
||
/// Struct representing a session. | ||
pub struct Session { | ||
logged_in: bool, | ||
user_id: Option<u64>, | ||
} | ||
|
||
impl Session { | ||
/// Creates a new session. | ||
pub fn new() -> Session { | ||
Session { | ||
logged_in: false, | ||
user_id: None, | ||
} | ||
} | ||
|
||
/// Logs the user in with a specific user ID. | ||
pub fn login_with_id(&mut self, user_id: u64) { | ||
self.logged_in = true; | ||
self.user_id = Some(user_id); | ||
} | ||
|
||
/// Logs the user out. | ||
pub fn logout(&mut self) { | ||
self.logged_in = false; | ||
self.user_id = None; | ||
} | ||
|
||
/// Checks if the user is logged in. | ||
pub fn is_logged_in(&self) -> bool { | ||
self.logged_in | ||
} | ||
|
||
/// Gets the ID of the logged-in user. | ||
pub fn get_user_id(&self) -> Option<u64> { | ||
self.user_id | ||
} | ||
|
||
/// Sets the ID of the logged-in user. | ||
pub fn set_user_id(&mut self, user_id: u64) { | ||
self.user_id = Some(user_id); | ||
} | ||
|
||
/// Logs the user in with a username and password. | ||
pub fn login_with_username_password(&mut self, username: &str, password: &str) -> Result<(), String> { | ||
// Perform authentication logic here (dummy logic for demonstration) | ||
if username == "admin" && password == "password" { | ||
self.logged_in = true; | ||
self.user_id = Some(1); // Assuming user ID 1 for admin | ||
Ok(()) | ||
} else { | ||
Err("Invalid username or password".to_string()) | ||
} | ||
} | ||
|
||
/// Logs the user in with a fingerprint. | ||
pub fn login_with_fingerprint(&mut self, fingerprint: &str) { | ||
// Perform fingerprint authentication logic here | ||
self.logged_in = true; | ||
self.user_id = Some(2); // Assuming user ID 2 for fingerprint login | ||
} | ||
|
||
/// Logs the user in with a security token. | ||
pub fn login_with_token(&mut self, token: &str) { | ||
// Perform security token authentication logic here | ||
self.logged_in = true; | ||
self.user_id = Some(3); // Assuming user ID 3 for token login | ||
} | ||
|
||
/// Logs the user in with a smart card. | ||
pub fn login_with_smart_card(&mut self, card_id: &str) { | ||
// Perform smart card authentication logic here | ||
self.logged_in = true; | ||
self.user_id = Some(4); // Assuming user ID 4 for smart card login | ||
} | ||
|
||
// Add more login methods as needed... | ||
|
||
/// Checks if the user is an admin. | ||
pub fn is_admin(&self) -> bool { | ||
// Dummy implementation, assuming user ID 1 is an admin | ||
self.user_id == Some(1) | ||
} | ||
|
||
// Add more functionality... | ||
} | ||
|
||
fn main() { | ||
let mut session = Session::new(); | ||
println!("Is logged in: {}", session.is_logged_in()); | ||
|
||
session.login_with_username_password("admin", "password").unwrap(); | ||
println!("Is logged in: {}", session.is_logged_in()); | ||
println!("Is admin: {}", session.is_admin()); | ||
|
||
session.logout(); | ||
println!("Is logged in: {}", session.is_logged_in()); | ||
} | ||
|
||
fn main() { | ||
let mut session = Session::new(); | ||
println!("Is logged in: {}", session.is_logged_in()); | ||
|
||
session.login(); | ||
println!("Is logged in: {}", session.is_logged_in()); | ||
|
||
session.logout(); | ||
println!("Is logged in: {}", session.is_logged_in()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/// Struct representing a user. | ||
pub struct User { | ||
username: String, | ||
email: String, | ||
password: String, | ||
} | ||
|
||
impl User { | ||
/// Creates a new user. | ||
pub fn new(username: &str, email: &str, password: &str) -> User { | ||
User { | ||
username: username.to_string(), | ||
email: email.to_string(), | ||
password: password.to_string(), | ||
} | ||
} | ||
|
||
/// Gets the username of the user. | ||
pub fn get_username(&self) -> &str { | ||
&self.username | ||
} | ||
|
||
/// Gets the email of the user. | ||
pub fn get_email(&self) -> &str { | ||
&self.email | ||
} | ||
|
||
/// Sets the email of the user. | ||
pub fn set_email(&mut self, email: &str) { | ||
self.email = email.to_string(); | ||
} | ||
|
||
/// Checks if the password meets the required criteria. | ||
pub fn is_password_valid(&self) -> bool { | ||
// Add password validation logic here | ||
self.password.len() >= 8 | ||
} | ||
|
||
/// Registers the user. | ||
pub fn register(&self) -> Result<(), String> { | ||
// Add registration logic here | ||
if self.is_password_valid() { | ||
Ok(()) | ||
} else { | ||
Err("Password is too short".to_string()) | ||
} | ||
} | ||
} | ||
|
||
/// Struct representing a user. | ||
pub struct User { | ||
username: String, | ||
email: String, | ||
password: String, | ||
} | ||
|
||
impl User { | ||
/// Creates a new user. | ||
pub fn new(username: &str, email: &str, password: &str) -> User { | ||
User { | ||
username: username.to_string(), | ||
email: email.to_string(), | ||
password: password.to_string(), | ||
} | ||
} | ||
|
||
/// Gets the username of the user. | ||
pub fn get_username(&self) -> &str { | ||
&self.username | ||
} | ||
|
||
/// Gets the email of the user. | ||
pub fn get_email(&self) -> &str { | ||
&self.email | ||
} | ||
|
||
/// Sets the email of the user. | ||
pub fn set_email(&mut self, email: &str) { | ||
self.email = email.to_string(); | ||
} | ||
|
||
/// Checks if the password meets the required criteria. | ||
pub fn is_password_valid(&self) -> bool { | ||
// Add password validation logic here | ||
self.password.len() >= 8 | ||
} | ||
|
||
/// Registers the user. | ||
pub fn register(&self) -> Result<(), String> { | ||
// Add registration logic here | ||
if self.is_password_valid() { | ||
Ok(()) | ||
} else { | ||
Err("Password is too short".to_string()) | ||
} | ||
} | ||
|
||
/// Updates the username of the user. | ||
pub fn update_username(&mut self, new_username: &str) { | ||
self.username = new_username.to_string(); | ||
} | ||
|
||
/// Checks if the user's email is valid. | ||
pub fn is_email_valid(&self) -> bool { | ||
// Add email validation logic here | ||
self.email.contains("@") | ||
} | ||
|
||
/// Updates the user's password. | ||
pub fn update_password(&mut self, new_password: &str) { | ||
self.password = new_password.to_string(); | ||
} | ||
|
||
/// Sends a confirmation email to the user. | ||
pub fn send_confirmation_email(&self) { | ||
// Add email sending logic here | ||
println!("Confirmation email sent to {}", self.email); | ||
} | ||
|
||
/// Sets the user's email to a new value and sends a confirmation email. | ||
pub fn set_email_with_confirmation(&mut self, new_email: &str) { | ||
self.email = new_email.to_string(); | ||
self.send_confirmation_email(); | ||
} | ||
|
||
/// Retrieves a user from the database by username. | ||
pub fn find_user_by_username(username: &str) -> Option<User> { | ||
// Add database query logic here | ||
Some(User::new(username, "user@example.com", "password123")) | ||
} | ||
|
||
/// Deletes the user's account. | ||
pub fn delete_account(&self) { | ||
// Add account deletion logic here | ||
println!("User account deleted"); | ||
} | ||
} | ||
|
||
fn main() { | ||
let user = User::new("user123", "user@example.com", "password123"); | ||
|
||
match user.register() { | ||
Ok(_) => println!("User registered successfully"), | ||
Err(err) => println!("Error registering user: {}", err), | ||
} | ||
|
||
user.send_confirmation_email(); | ||
} | ||
|
||
fn main() { | ||
let user = User::new("user123", "user@example.com", "password123"); | ||
|
||
match user.register() { | ||
Ok(_) => println!("User registered successfully"), | ||
Err(err) => println!("Error registering user: {}", err), | ||
} | ||
} |
Oops, something went wrong.