User

User

new User()

Source:
See:

A user of the BioPocket platform.

Database columns

  • id (bigint) - Internal ID (used for joins).
  • api_id (uuid) - External ID (used in the API).
  • email (string) - E-mail address.
  • password_hash (string) - Bcrypt hash of the user's password.
  • active (boolean) - Indicates whether the user can use the platform or has been deactivated by an administrator.
  • roles (string[]) - Roles of the user (used for authorization).
  • created_at (datetime) - Time at which the user was created.
  • updated_at (datetime) - Time at which the user was last modified (equal to the creation date if never modified).

Virtual properties

  • password (string) - Setting this property generates a new bcrypt hash and updates the password_hash column.

Extends

Methods

generateJwt(properties) → {string}

Source:

Returns a JWT that can be used to authenticate as this user.

Parameters:
Name Type Description
properties object

JWT properties, passed to generateToken in the utils/jwt module.

Returns:

A JWT.

Type
string

hasPassword(password) → {boolean}

Source:

Indicates whether this user has the specified password or not.

WARNING: this method is slow and blocking, as it computes a bcrypt hash synchronously. Do not overuse it.

Parameters:
Name Type Description
password string

The password to check.

Returns:

True if the user's password is the same as the specified one.

Type
boolean

hasRole(role) → {boolean}

Source:

Indicates whether this user has the specified role.

WARNING: this methods always returns true if the user has the role, even if the user is inactive. It is not sufficient to determine whether the user is currently authorized to perform the role.

Parameters:
Name Type Description
role string

The role to check.

Returns:

True if the specified role is among the user's assigned roles.

Type
boolean

isActive() → {boolean}

Source:

Indicates whether this user is active. Users may be deactivated by administrators.

Returns:

True if this user is active.

Type
boolean

parseFrom(source, properties, optionsopt) → {Model}

Source:
Inherited From:

Parses data from the specified source into this record's columns.

// The following code:
record.parseFrom(data, [ 'name', 'siteUrl' ]);

// Is equivalent to:
if (data.hasOwnProperty('name')) {
  record.set('name', data.name);
}
if (data.hasOwnProperty('siteUrl')) {
  record.set('site_url', data.siteUrl);
}

// The following code:
record.parseFrom(data, [ 'street', 'zipCode' ], { columnPrefix: 'address_', sourcePrefix: 'address.' });

// Is equivalent to:
if (data.hasOwnProperty('address') && data.address.hasOwnProperty('street')) {
  record.set('address_street', data.address.street);
}
if (data.hasOwnProperty('address') && data.address.hasOwnProperty('zipCode')) {
  record.set('address_zip_code', data.address.zipCode);
}
Parameters:
Name Type Attributes Description
source object

Source object (typically the parsed JSON request body).

properties Array.<string>

Camel-cased properties of the source object to parse. The column names will correspond to the underscored names of the properties (e.g. zipCode => zip_code).

options object <optional>

Deserialization options.

Properties
Name Type Attributes Description
columnPrefix string <optional>

Prefix to prepend to column names. For example, if the prefix is address_ and one of the properties to parse is zipCode, the column in which the value is stored will be address_zip_code.

sourcePrefix string <optional>

Prefix to prepend to property names before extracting them from the source object. For example, if the prefix is address. and one of the properties to parse is zipCode, the zipCode property of the source's object address sub-object will be extracted.

Returns:

This record.

Type
Model

serializeTo(target, properties, optionsopt) → {object}

Source:
Inherited From:

Serializes columns of this record into the specified target object.

// The following code:
record.serializeTo(target, [ 'name', 'site_url' ]);

// Is equivalent to:
if (record.has('name')) {
  target.name = record.get('name');
}
if (record.has('site_url')) {
  target.siteUrl = record.get('site_url');
}

// The following code:
record.serializeTo(target, [ 'street', 'zip_code' ], { columnPrefix: 'address_', targetPrefix: 'address.' });

// Is equivalent to:
if (!target.hasOwnProperty('address')) {
  target.address = {};
}
if (record.has('address_street')) {
  target.address.street = record.get('address_street');
}
if (record.has('address_zip_code')) {
  target.address.zipCode = record.get('address_zip_code');
}
Parameters:
Name Type Attributes Description
target object

Target object to attach serialized properties to.

properties Array.<string>

Underscored column names of the record to serialize. The target property names will correspond to the camelized names of the columns (e.g. zip_code => zipCode).

options object <optional>

Serialization options.

Properties
Name Type Attributes Description
columnPrefix string <optional>

Prefix to prepend to the column names to serialize. For example, if the prefix is address and one of the columns to serialize is zip_code, the column from which the value is extracted will be address_zip_code.

targetPrefix string <optional>

Prefix to prepend to the property names of the target object. For example, if the target prefix is address. and one of the columns to serialize is zip_code, the zipCode property of the target object's address sub-object will be set to the value of the column.

Returns:

The target object.

Type
object