Created
April 15, 2023 17:17
-
-
Save ArslanKathia/2504f8eb1cd15270627ea82cc374b78d to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
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
//SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract IdentityManagement{ | |
//address owner; | |
//structure to hold user data | |
struct User{ | |
string firstName; | |
string lastName; | |
string email; | |
uint256 phone; | |
address walletAddress; | |
bool isRegistered; | |
bool isAdmin; | |
} | |
mapping(address => User) private users; | |
event UserRegister(address userAddress); | |
event UserUpdate(address userAddress); | |
modifier isOnlyAdmin{ | |
require(users[msg.sender].isAdmin,"User not admin"); | |
_; | |
} | |
modifier onlyRegistered{ | |
require(users[msg.sender].isRegistered,"User not Regsitered"); | |
_; | |
} | |
constructor(){ | |
User memory adminUser = User({ | |
firstName: "Admin", | |
lastName: "User", | |
email: "[email protected]", | |
phone : 23432412312, | |
walletAddress : msg.sender, | |
isRegistered : true, | |
isAdmin :true | |
}); | |
users[msg.sender] = adminUser; | |
} | |
//funciton to register a new user | |
function registerUser(string memory _firstName,string memory _lastName,string memory _email,uint256 _phone,address _walletAddress) public { | |
//check user is registered or not | |
require(!users[msg.sender].isRegistered,"User is already registered"); | |
//add new User | |
User memory newUser = User({ | |
firstName : _firstName, | |
lastName : _lastName, | |
email : _email, | |
phone : _phone, | |
walletAddress: _walletAddress, | |
isRegistered : true, | |
isAdmin:false | |
}); | |
users[msg.sender] = newUser; | |
emit UserRegister(msg.sender); | |
} | |
function updateUserData(string memory _firstName, | |
string memory _lastName, | |
string memory _email, | |
uint256 _phone, | |
address _walletAddress | |
) | |
public onlyRegistered{ | |
users[msg.sender].firstName = _firstName; | |
users[msg.sender].lastName = _lastName; | |
users[msg.sender].email = _email; | |
users[msg.sender].phone = _phone; | |
users[msg.sender].walletAddress = _walletAddress; | |
emit UserUpdate(msg.sender); | |
} | |
function grantAdminAccess(address _user) public isOnlyAdmin{ | |
require(users[_user].isRegistered,"User is not registered"); | |
users[_user].isAdmin = true; | |
} | |
function revokeAdminAccess(address _user)public isOnlyAdmin{ | |
require(users[_user].isRegistered,"User is not registered"); | |
users[_user].isAdmin = false; | |
} | |
function deleteUser(address _user) public isOnlyAdmin{ | |
require(users[_user].isRegistered,"User isn't exist"); | |
delete users[_user]; | |
} | |
function getUserDetails(address _user) public isOnlyAdmin onlyRegistered view returns(User memory){ | |
require(users[_user].isRegistered,"User isn't Registered"); | |
return users[_user]; | |
} | |
function compareStrings(string memory a, string memory b) public pure returns(bool){ | |
return keccak256(bytes(a)) == keccak256(bytes(b)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment