UserMemmoryCollection

Create an user collection stored in memmory

Constructors

this
this(immutable(string[]) accessList, User[] list)

Members

Functions

byId
User byId(string id)

Get an user by id

byToken
User byToken(string token)

Get an user by an existing token

contains
bool contains(string identification)

Check if the collection has an user by email or username

createToken
Token createToken(string email, SysTime expire, string[] scopes, string type)

Create a token for an user

createUser
bool createUser(UserData data, string password)

Create a new user data from some user data

empower
void empower(string email, string access)

Empower an user with some scope access

opIndex
User opIndex(string identification)

Get an user by email or username

revoke
void revoke(string token)

Revoke a token

Inherited Members

From UserCollection

opBinaryRight
alias opBinaryRight = Collection!User.opBinaryRight
opIndex
alias opIndex = Collection!User.opIndex
createUser
bool createUser(UserData data, string password)

Create a new user data from some user data

createToken
Token createToken(string email, SysTime expire, string[] scopes, string type)

Create a token for an user

revoke
void revoke(string token)

Revoke a token

empower
void empower(string email, string access)

Empower an user with some scope access

byToken
User byToken(string token)

Get an user by an existing token

byId
User byId(string id)

Get an user by id

contains
bool contains(string email)

Check if the collection has an user by email

Examples

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);

Meta