github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/server/lib/user/contracts/UserManager.sol (about) 1 import "./User.sol"; 2 import "./UserRole.sol"; 3 import "../../common/ErrorCodes.sol"; 4 import "../../common/Util.sol"; 5 6 /** 7 * Interface for User data contracts 8 */ 9 contract UserManager is ErrorCodes, Util, UserRole { 10 User[] users; 11 /* 12 note on mapping to array index: 13 a non existing mapping will return 0, so 0 should not be a valid value in a map, 14 otherwise exists() will not work 15 */ 16 mapping (bytes32 => uint) usernameToIdMap; 17 18 /** 19 * Constructor 20 */ 21 function UserManager() { 22 users.length = 1; // see above note 23 } 24 25 function exists(string username) returns (bool) { 26 return usernameToIdMap[b32(username)] != 0; 27 } 28 29 function getUser(string username) returns (address) { 30 uint userId = usernameToIdMap[b32(username)]; 31 return users[userId]; 32 } 33 34 function createUser(address account, string username, bytes32 pwHash, UserRole role) returns (ErrorCodes) { 35 // name must be < 32 bytes 36 if (bytes(username).length > 32) return ErrorCodes.ERROR; 37 // fail if username exists 38 if (exists(username)) return ErrorCodes.EXISTS; 39 // add user 40 uint userId = users.length; 41 usernameToIdMap[b32(username)] = userId; 42 users.push(new User(account, username, pwHash, userId, role)); 43 return ErrorCodes.SUCCESS; 44 } 45 46 function login(string username, bytes32 pwHash) returns (bool) { 47 // fail if username doesnt exists 48 if (!exists(username)) return false; 49 // get the user 50 address a = getUser(username); 51 User user = User(a); 52 return user.authenticate(pwHash); 53 } 54 }