Event type raised when the user data has changed
Add a scope to the user
Check if an user can access a scope
Create an user token
Get the user scopes assigned to a particullar token
Get all user scopes
Get a range of tokens of a certain type
Validate a password
Validate a token
Validate a token against a scope
Remove a scope from user
Revoke a token
Change the user password
Change the user password by providing a salting string
Convert the object to a json. It's not safe to share this value with the outside world. Use it to store the user to db.
Convert the object to a json that can be shared with the outside world
Convert the user object ot a Json pretty string
Get the user email
Set the user email
Get the user id
Set the user id
Check if the user is active
Check the user active status
Get the user real name
Set the user real name
Get the user alias name
Set the user alias name
Restore the user from a json value
Event raised when the user changed
Password validation
auto user = new User("user", "password"); auto password = user.toJson["password"].to!string; auto salt = user.toJson["salt"].to!string; assert(password == sha1UUID(salt ~ ".password").to!string, "It should salt the password"); assert(user.isValidPassword("password"), "It should return true for a valid password"); assert(!user.isValidPassword("other passowrd"), "It should return false for an invalid password");
Converting a user to a public json
auto user = new User("user", "password"); auto json = user.toPublicJson; assert("id" in json, "It should contain the id"); assert("name" in json, "It should contain the name"); assert("username" in json, "It should contain the username"); assert("email" in json, "It should contain the email"); assert("password" !in json, "It should not contain the password"); assert("salt" !in json, "It should not contain the salt"); assert("scopes" in json, "It should contain the scope"); assert("tokens" !in json, "It should not contain the tokens");
User serialization
auto user = new User("user", "password"); auto json = user.toJson; assert("_id" in json, "It should contain the id"); assert("email" in json, "It should contain the email"); assert("password" in json, "It should contain the password"); assert("salt" in json, "It should contain the salt"); assert("scopes" in json, "It should contain the scope"); assert("tokens" in json, "It should contain the tokens");
User data deserialization
auto json = `{ "_id": "1", "name": "name", "username": "username", "email": "test@asd.asd", "password": "password", "salt": "salt", "isActive": true, "scopes": ["scopes"], "tokens": [ { "name": "token", "expire": "2100-01-01T00:00:00", "scopes": [], "type": "Bearer" }], }`.parseJsonString; auto user = User.fromJson(json); auto juser = user.toJson; assert(user.id == "1", "It should deserialize the id"); assert(user.name == "name", "It should deserialize the name"); assert(user.username == "username", "It should deserialize the username"); assert(user.email == "test@asd.asd", "It should deserialize the email"); assert(juser["password"] == "password", "It should deserialize the password"); assert(juser["salt"] == "salt", "It should deserialize the salt"); assert(juser["isActive"] == true, "It should deserialize the isActive field"); assert(juser["scopes"][0] == "scopes", "It should deserialize the scope"); assert(juser["tokens"][0]["name"] == "token", "It should deserialize the tokens");
Change event
auto user = new User(); auto changed = false; void userChanged(User u) { changed = true; } user.onChange = &userChanged; user.id = 1; assert(changed, "onChange should be called when the id is changed"); changed = false; user.email = "email"; assert(changed, "onChange should be called when the email is changed"); changed = false; user.setPassword("password"); assert(changed, "onChange should be called when the password is changed"); changed = false; user.setPassword("password", "salt"); assert(changed, "onChange should be called when the password is changed"); changed = false; user.createToken(Clock.currTime + 3600.seconds); assert(changed, "onChange should be called when a token is created");
Class used to manage one user