User

Class used to manage one user

Constructors

this
this()
this
this(UserData userData)
this
this(string email, string password)

Members

Aliases

ChangedEvent
alias ChangedEvent = void delegate(User)

Event type raised when the user data has changed

Functions

addScope
void addScope(string access)

Add a scope to the user

can
bool can()

Check if an user can access a scope

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

Create an user token

getScopes
string[] getScopes(string token)

Get the user scopes assigned to a particullar token

getScopes
string[] getScopes()

Get all user scopes

getTokensByType
auto getTokensByType(string type)

Get a range of tokens of a certain type

isValidPassword
bool isValidPassword(string password)

Validate a password

isValidToken
bool isValidToken(string token)

Validate a token

isValidToken
bool isValidToken(string token, string requiredScope)

Validate a token against a scope

removeScope
void removeScope(string access)

Remove a scope from user

revoke
void revoke(string token)

Revoke a token

setPassword
void setPassword(string password)

Change the user password

setPassword
void setPassword(string password, string salt)

Change the user password by providing a salting string

toJson
Json toJson()

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.

toPublicJson
Json toPublicJson()

Convert the object to a json that can be shared with the outside world

toString
string toString()

Convert the user object ot a Json pretty string

Properties

email
string email [@property getter]

Get the user email

email
string email [@property setter]

Set the user email

id
auto id [@property getter]

Get the user id

id
ulong id [@property setter]

Set the user id

isActive
bool isActive [@property getter]

Check if the user is active

isActive
bool isActive [@property setter]

Check the user active status

name
auto name [@property getter]

Get the user real name

name
string name [@property setter]

Set the user real name

username
auto username [@property getter]

Get the user alias name

username
string username [@property setter]

Set the user alias name

Static functions

fromJson
User fromJson(Json data)

Restore the user from a json value

Variables

onChange
ChangedEvent onChange;

Event raised when the user changed

Examples

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

Meta