Skip to content

Commit

Permalink
Merge pull request #21 from xTwo56/feat_delete
Browse files Browse the repository at this point in the history
[feature]: Delete and restrict user
  • Loading branch information
MishraSomesh001 authored Apr 16, 2024
2 parents 42881a0 + 045b43d commit 5516857
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions contract/Database.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ contract Database{
mapping( string => Details) private aadharToUser;
mapping( address => Details) private list;
uint256 private count=0;
mapping (string => bool) public added;
mapping( string => bool ) public added;
mapping( address => bool ) public restrictedUser;
mapping( string => address ) public aadharToAddress;

address public admin;
bool alreadyset=false;

Expand All @@ -25,8 +28,9 @@ contract Database{
}



modifier personPresent{
require(keccak256(abi.encodePacked(list[msg.sender].aadharId)) != keccak256(abi.encodePacked("")), "Person doesn't exist");
require(keccak256(abi.encodePacked(list[msg.sender].aadharId)) != keccak256(abi.encodePacked("")), "User doesn't exist");
_;
}
modifier Added (string memory aadhar)
Expand All @@ -35,6 +39,7 @@ contract Database{
_;
}


modifier onlyOnce()
{
require(!alreadyset,"Admin is already set");
Expand All @@ -47,6 +52,11 @@ contract Database{
_;
}

modifier isNotRestricted(){
require(!restrictedUser[msg.sender], "User is restricted");
_;
}

function set_Admin( address _admin) public onlyOnce()
{
admin=_admin;
Expand All @@ -58,23 +68,24 @@ contract Database{
admin=newadmin;
}

function getAdress()external view returns(address){
function getAddress()external view returns(address){
return msg.sender;
}


function addPerson(string memory aadharId,string memory name, string memory DOB, string memory phoneNo, string memory rollNo, string memory batchNo) public Added (aadharId)

function addPerson(string memory aadharId,string memory name, string memory DOB, string memory phoneNo, string memory rollNo, string memory batchNo) public Added (aadharId) isNotRestricted
{
require(bytes(list[msg.sender].aadharId).length == 0, "User already exists");
Details memory person = Details({aadharId: aadharId,name: name,DOB: DOB,phoneNo: phoneNo, rollNo: rollNo, batchNo: batchNo});
list[msg.sender]=person;
added[aadharId] = true;
aadharToAddress[aadharId] = msg.sender;
aadharToUser[aadharId] = person;
added[aadharId]=true;
count++;
}

function updateDetails(string memory aadharId,string memory name, string memory DOB, string memory phoneNo, string memory rollNo, string memory batchNo)public personPresent{
function updateDetails(string memory aadharId,string memory name, string memory DOB, string memory phoneNo, string memory rollNo, string memory batchNo)public personPresent isNotRestricted {
Details storage person = list[msg.sender];
person.aadharId = aadharId;
person.name = name;
Expand All @@ -85,18 +96,33 @@ contract Database{
added[aadharId]=true;

}
function checkDetails()public personPresent view returns(string[6] memory){
function checkDetails()public personPresent view personPresent returns(string[6] memory){
Details memory person = list[msg.sender];
string[6] memory details = [person.aadharId, person.name, person.DOB, person.phoneNo, person.rollNo, person.batchNo];
return details;
}

function findByAadhaarId(string memory _aadharId)public view onlyAdmin returns(string[6] memory){
require((bytes(aadharToUser[_aadharId].aadharId).length > 0), "Not found");
require((bytes(aadharToUser[_aadharId].aadharId).length > 0), "User not found");
Details memory person = aadharToUser[_aadharId];
string[6] memory details = [person.aadharId, person.name, person.DOB, person.phoneNo, person.rollNo, person.batchNo];
return details;
}

}
function deleteUser(string memory _aadharId)public onlyAdmin {
require((bytes(aadharToUser[_aadharId].aadharId).length > 0), "User not found");
Details memory person = aadharToUser[_aadharId];
person.aadharId = "";
list[aadharToAddress[_aadharId]] = person;
aadharToUser[_aadharId] = person;

added[_aadharId] = false;
count--;
restrictedUser[aadharToAddress[_aadharId]] = true;
}

function restrictUser(string memory _aadharId)public onlyAdmin {
restrictedUser[aadharToAddress[_aadharId]] = true;
}

}

0 comments on commit 5516857

Please sign in to comment.