Skip to content

Commit

Permalink
fixed username bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Polkaverse committed Aug 17, 2023
1 parent 45b9119 commit 5b16baf
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pallets/user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub use pallet::*;
use sp_runtime::RuntimeDebug;
pub use weights::WeightInfo;

pub mod types;
#[cfg(test)]
mod mock;
pub mod types;

#[cfg(test)]
mod tests;
Expand All @@ -33,6 +33,7 @@ pub mod pallet {
use crate::types::User;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use scale_info::prelude::string::String;

use super::*;

Expand Down Expand Up @@ -77,6 +78,8 @@ pub mod pallet {
pub enum Error<T> {
/// Username not available.
UsernameNotAvailable,
/// Username is not valid.
UsernameInvalid,
}

#[pallet::hooks]
Expand All @@ -101,8 +104,24 @@ pub mod pallet {
) -> DispatchResult {
let user = ensure_signed(origin.clone())?;

// Validating the username/avatar. They should not have the blank spaces
if name.is_some() {
let username: String =
String::from_utf8(name.clone().unwrap().to_vec()).expect("Invalid username");
ensure!(!username.contains(" "), Error::<T>::UsernameInvalid);
}
if avatar.is_some() {
let profile: String =
String::from_utf8(avatar.clone().unwrap().to_vec()).expect("Invalid username");
ensure!(!profile.contains(" "), Error::<T>::UsernameInvalid);
}

// Validating the duplicate username
for (_, userdata) in Users::<T>::iter() {
ensure!(userdata.name != name, Error::<T>::UsernameNotAvailable);
ensure!(
userdata.name != name || userdata.name == None,
Error::<T>::UsernameNotAvailable
);
}

// creating the user data structure as per given inputs
Expand Down

0 comments on commit 5b16baf

Please sign in to comment.