Skip to content

Instantly share code, notes, and snippets.

@youfoundron
Created August 19, 2017 16:48
Show Gist options
  • Save youfoundron/3a7d92ae197b19c82b023d90ab6bbb56 to your computer and use it in GitHub Desktop.
Save youfoundron/3a7d92ae197b19c82b023d90ab6bbb56 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=3a7d92ae197b19c82b023d90ab6bbb56
pragma solidity ^0.4.11;
contract UserManager {
address public owner;
address[] public users;
mapping(address => uint) userIndexes;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function numUsers () constant public returns(uint) {
return users.length;
}
function getUsers () constant public returns(address[]) {
return users;
}
function hasUser (address searchUser) constant public returns(bool) {
if (users.length > 0) {
uint searchIndex = userIndexes[searchUser];
address foundUser = users[searchIndex];
return searchUser == foundUser;
} else {
return false;
}
}
function UserManager () {
owner = msg.sender;
}
function addUser (address newUser) onlyOwner {
require(!hasUser(newUser));
userIndexes[newUser] = users.push(newUser) - 1;
}
function removeUser (address oldUser) onlyOwner {
require(hasUser(oldUser));
uint oldUserIndex = userIndexes[oldUser];
address lastUser = users[users.length - 1]; // copy the last address in the user array
users[oldUserIndex] = lastUser; // replace the old address with the last address
delete users[oldUserIndex]; // delete the last address from the array
userIndexes[oldUser] = 0; // map old address to index 0
users.length--; // decrement the length of the user array
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment