Ethereum Smart Contracts - Owner Modifier

@filip is this a proper way to use ‘onlyOwner’? I took my own spin on it but want to know if there are any security concerns.

pragma solidity 0.5.1;

contract DogContract {
    
    struct Dog {
        string name;
        uint age;
        
    }
    
    address owner;

    mapping(address => Dog) dogOwner;
    
    modifier onlyNewOwners(){
        require(dogOwner[msg.sender].age == 0, "Already owned!");
        _;
    }
    
    // _adddress is supplied within the function, then compared against msg.sender to verify the dog owner is the requester
    modifier onlyOwner(address _address){
        require(_address == msg.sender, "You are not the owner!");
        _;
    }
    
    // constructor sets the contract owner immediatly - later used for validation
    constructor() public {
        owner = msg.sender;
    }
    
    // use onlyOwner to make sure that only the dog owner can delete the dog. Feed _address from function paramater to mofifier paramater
    function deleteDog(address _address) public onlyOwner(_address) {
        delete dogOwner[_address];
    }
    
    function addDog(string memory _name, uint _age) public onlyNewOwners {
        dogOwner[msg.sender] = Dog(_name, _age);
    }
    
    function getDog(address _address) public view returns (string memory){
        return dogOwner[_address].name;
    }
}

Depends on what the purpose was with that modifier. Usually onlyOwner is used to control that only the owner of the contract (the creator) can run the function. Your modifier checks that only the dog owner can delete the dog. So they are two different modifiers.

1 Like

Ah ok, I see that now. Thank you!

1 Like