Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ArslanKathia/57743288bb322b2437046fc538d87427 to your computer and use it in GitHub Desktop.
Save ArslanKathia/57743288bb322b2437046fc538d87427 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=
//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];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment