Get an user by id
Get an user by an existing token
Check if the collection has an user by email or username
Create a token for an user
Create a new user data from some user data
Empower an user with some scope access
Get an user by email or username
Revoke a token
Create a new user data from some user data
Create a token for an user
Revoke a token
Empower an user with some scope access
Get an user by an existing token
Get an user by id
Check if the collection has an user by email
Throw exceptions on selecting invalid users
auto collection = new UserMemmoryCollection([]); auto user = new User("user", "password"); collection.add(user); assert(collection["user"] == user, "It should return user by name"); assert(collection.contains("user"), "It should find user by name"); assert(!collection.contains("other user"), "It should not find user by name"); ({ collection["other user"]; }).should.throwAnyException;
User access
auto collection = new UserMemmoryCollection(["doStuff"]); auto user = new User("user", "password"); user.id = 1; auto otherUser = new User("otherUser", "password"); otherUser.id = 2; collection.add(user); collection.add(otherUser); collection.empower("user", "doStuff"); assert(user.can!"doStuff", "It should return true if the user can `doStuff`"); assert(!otherUser.can!"doStuff", "It should return false if the user can not `doStuff`");
Searching for a missing token
auto collection = new UserMemmoryCollection([]); auto user = new User("user", "password"); collection.add(user); auto token = user.createToken(Clock.currTime + 3600.seconds); collection.byToken(token.name).name.should.equal(user.name).because("It should find user by token"); ({ collection.byToken("token"); }).should.throwAnyException;
Token revoke
auto collection = new UserMemmoryCollection([]); auto user = new User("user", "password"); collection.add(user); auto token = user.createToken(Clock.currTime + 3600.seconds); assert(collection.byToken(token.name) == user, "It should find user by token"); collection.revoke(token.name); ({ collection.byToken(token.name); }).should.throwAnyException;
Get tokens by type
auto collection = new UserMemmoryCollection([]); auto user = new User("user", "password"); collection.add(user); auto token = user.createToken(Clock.currTime + 3600.seconds, [], "activation").name; auto tokens = collection["user"].getTokensByType("activation").map!(a => a.name).array; tokens.length.should.equal(1); tokens.should.contain(token);
Get user by id
auto collection = new UserMemmoryCollection([]); auto user = new User("user", "password"); user.id = 1; collection.add(user); auto result = collection.byId("1"); result.id.should.equal("1");
Remove user by id
bool wasRemoved; void onRemove(User user) { wasRemoved = user.id == "1"; } auto collection = new UserMemmoryCollection([]); collection.onRemove = &onRemove; auto user = new User("user", "password"); user.id = 1; collection.add(user); collection.remove("1"); collection.length.should.equal(0); wasRemoved.should.equal(true);
Create an user collection stored in memmory