This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

BBB_Owned.sol 4.7 KB
Newer Older
Robert Muth committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
pragma solidity >=0.5.0 <0.6.0;

/* MIT Licence:

Copyright (c) 2018-2019 TU Berlin, DSI https://www.dsi.tu-berlin.de

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

contract BBB_Owned {
    address payable internal dsi;
    address payable internal urd;

    address internal crawler;

    address[] authorities;
    uint256 authoritiesSize = 0;

    bool public suspended = false;

    constructor(address payable _dsi, address payable _urd) internal { 
        dsi = _dsi;
        urd = _urd;
    }

    function changeOwner(address payable _dsi, address payable _urd) public ownerOnly {
        dsi = _dsi;
        urd = _urd;
    }

    function isOwner(address payable _sender) view internal returns (bool) {
        return _sender == dsi || _sender == urd;
    }

    modifier ownerOnly() { 
        require(isOwner(msg.sender), "Owner only"); 
        _;
    }

    function destroy() public ownerOnly {
        assert(msg.sender == dsi || msg.sender == urd); // Just to be save 
        selfdestruct(msg.sender);
    }

    // Suspension
    modifier altering() { 
        require(!suspended); 
        _;
    }

    event Suspension (
        bool indexed suspended
    );
    
    function suspend() public authorityOnly {
        suspended = true;
        emit Suspension(true);
    }

    function unsuspend() public ownerOnly {
        suspended = false;
        emit Suspension(false);
    }

    // Crawler Modifier
    function setCrawler(address _crawler) public ownerOnly altering { crawler = _crawler; }
    function isCrawler(address _sender) internal view returns (bool) { return crawler != address(0) && _sender == crawler; }

    // Authorities
    function addAuthority(address _authority) public ownerOnly altering {
        assert(authoritiesSize < 2**256 - 1);

        // Check for uniques
        for(uint i = 0; i < authoritiesSize; i++) {
            if(authorities[i] == _authority)
                revert();
        }   

        authorities.push(_authority);
        authoritiesSize++;
    }

    function removeAllAuthorities() public ownerOnly altering {
        for(uint i = authoritiesSize - 1; i >= 0; i--) {
            delete authorities[i];
        }

        authoritiesSize = 0;
        authorities.length = 0;
    }

    function removeAuthority(address _authority) public altering {
        // Check for owner or the athority itself
        require(isOwner(msg.sender) || (_authority == msg.sender && isAuthority(msg.sender)));
        require(authoritiesSize > 0);
        
        // Note: only removes 1 item -- even if the authority is contained multiple times

        uint foundIndex = 0;
        bool found = false;
        for(uint i = 0; i < authoritiesSize - 1; i++) {
            if(!found && authorities[i] == _authority) {
                foundIndex = i;
                found = true;
            }

            if(found) {
                authorities[i] = authorities[i + 1];
            }
        }

        if(!(found || authorities[authoritiesSize - 1] == _authority)) { 
            revert();
        } else {
            delete authorities[authorities.length - 1];
            authorities.length--;
            authoritiesSize--;
        }
    }

    function isAuthority(address _authority) public view returns (bool) {
        for(uint i = 0; i < authoritiesSize; i++) {
            if(authorities[i] == _authority)
                return true;
        }

        return false;
    }

    modifier crawlerOnly() {
        require(isCrawler(msg.sender) || isOwner(msg.sender)); 
        _;
    }

    modifier authorityOnly() {
        require(isAuthority(msg.sender) || isOwner(msg.sender)); 
        _;
    }

    modifier authorityOrCrawlerOnly() {
        require(isAuthority(msg.sender) || isCrawler(msg.sender) || isOwner(msg.sender)); 
        _;
    }
}