Created
August 18, 2017 22:26
-
-
Save youfoundron/fb150e424d4d5ecadda85ffa4ddd8d6b to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.15+commit.bbb8e64f.js&optimize=undefined&gist=fb150e424d4d5ecadda85ffa4ddd8d6b
This file contains hidden or 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
pragma solidity ^0.4.11; | |
contract UserManager { | |
address public owner; | |
uint public numUsers = 0; | |
mapping(address => address) private linkedUsers; | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function UserManager () { | |
owner = msg.sender; | |
} | |
function noUsers () constant private returns(bool) { | |
return linkedUsers[owner] == 0; | |
} | |
function userExists (address searchUser) constant public returns(bool) { | |
if (searchUser != 0) { | |
bool foundUser = false; | |
address currUser = owner; | |
while (!foundUser && currUser != 0) { | |
currUser = linkedUsers[currUser]; | |
foundUser = currUser == searchUser; | |
} | |
return foundUser; | |
} else { | |
return false; | |
} | |
} | |
function getUsers () constant public returns(address[]) { | |
// it is not possible to resize memory arrays | |
address[] memory users = new address[](numUsers); | |
address currUser = linkedUsers[owner]; | |
for (uint i = 0; i < numUsers; i++) { | |
users[i] = currUser; | |
currUser = linkedUsers[currUser]; | |
} | |
return users; | |
} | |
function _addUser (address newUser) private { | |
address lastUser = linkedUsers[owner]; | |
while (linkedUsers[lastUser] != newUser) { | |
if (linkedUsers[lastUser] == 0) { | |
linkedUsers[lastUser] = newUser; | |
} else { | |
lastUser = linkedUsers[lastUser]; | |
} | |
} | |
} | |
function addUser (address newUser) onlyOwner { | |
require(newUser != owner); | |
require(!userExists(newUser)); | |
if (noUsers()) { | |
linkedUsers[owner] = newUser; | |
} else { | |
_addUser(newUser); | |
} | |
numUsers += 1; | |
} | |
function _removeUser (address oldUser) private { | |
bool userRemoved; | |
address lastUser = owner; | |
while (!userRemoved) { | |
if (linkedUsers[lastUser] == oldUser) { | |
linkedUsers[lastUser] = linkedUsers[oldUser]; | |
userRemoved = true; | |
} else { | |
lastUser = linkedUsers[lastUser]; | |
} | |
} | |
} | |
function removeUser (address oldUser) onlyOwner { | |
require(userExists(oldUser)); | |
_removeUser(oldUser); | |
numUsers -= 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment