Sumba

Sumba class definition.

Constructor

new Sumba()

Constructor

Methods

(async) adminMenu(locals, req) → {array}

Build admin menu based on locals and request.

  • If multi-site is enabled, it will include the cross-site menu with options to manage all sites and site settings.
  • If caching is enabled, it will include the cache storage menu in the appropriate section.
  • Session management menu is also included in the appropriate section.
Parameters:
NameTypeDescription
localsobject

The locals object.

reqobject

The request object.

Returns:
  • The admin menu items.
Type: 
array

(async) applyActionHook(optsopt)

Apply action hook to the provided model.

Parameters:
NameTypeAttributesDefaultDescription
optsobject<optional>
{}

The options object.

Properties
NameTypeAttributesDefaultDescription
modelobject

The model object.

teamIdsarray<optional>
[]

The team IDs.

optionsobject<optional>
{}

Additional options.

(async) applyQueryHook(model, id, options) → {object}

Apply query hook to the provided model.

Parameters:
NameTypeDescription
modelobject

The model object.

idstring | number

The ID of the model.

optionsobject

Additional options.

Returns:

The result of the model hook.

Type: 
object

(async) checkIconset(req, reply) → {Promise.<void>}

Check and set the iconset for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

replyObject

The reply object.

Returns:
  • A promise that resolves when the iconset is checked and set.
Type: 
Promise.<void>

(async) checkRoute(req) → {Promise.<void>}

Check the route for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

Returns:
  • A promise that resolves when the route is checked.
Type: 
Promise.<void>

checkRouteGuard(guards, paths) → {string|undefined}

Check route guard

Parameters:
NameTypeDescription
guardsArray

The array of guard objects.

pathsArray

The array of paths to check.

Returns:
  • The matched path or undefined if no match is found.
Type: 
string | undefined

(async) checkTeam(req, reply) → {Promise.<void>}

Check and set the team for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

replyObject

The reply object.

Returns:
  • A promise that resolves when the team is checked and set.
Type: 
Promise.<void>

(async) checkTheme(req, reply) → {Promise.<void>}

Check and set the theme for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

replyObject

The reply object.

Returns:
  • A promise that resolves when the theme is checked and set.
Type: 
Promise.<void>

(async) checkUser(req, reply, source) → {Promise.<(boolean|void)>}

Check and set the user for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

replyObject

The reply object.

sourceObject

The source object.

Returns:
  • A promise that resolves to a boolean indicating the result of the check or void.
Type: 
Promise.<(boolean|void)>

(async) checkXSite(req, reply) → {Promise.<void>}

Check and set the Cross-site settings for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

replyObject

The reply object.

Returns:
  • A promise that resolves when the XSite is checked and set.
Type: 
Promise.<void>

(async) clearCacheSite(id, result)

Clear the cache for a specific site.

Parameters:
NameTypeDescription
idstring | number

The ID of the site.

resultobject

The result object containing site data.

(async) clearCacheUser(id, result)

Clear the cache for a specific user.

Parameters:
NameTypeDescription
idstring | number

The ID of the user.

resultobject

The result object containing user data.

(async) createJwtFromUserRecord(rec) → {Promise.<object>}

Create JWT from user record

Parameters:
NameTypeDescription
recobject

The user record object.

Returns:
  • The JWT token and its expiration date.
Type: 
Promise.<object>

(async) createNewSite(alias, hostnameopt, verboseopt)

Create a new site with the specified alias and optional hostname, using fixtures for initial data.

Parameters:
NameTypeAttributesDefaultDescription
aliasstring

The alias for the new site.

hostnamestring<optional>

The hostname for the new site.

verboseboolean<optional>
false

Whether to log verbose output.

(async) createRefs(site, data, options, verbose)

Create references for the specified site using the provided data, options, and verbosity settings.

Parameters:
NameTypeDescription
siteobject

The site object for which to create references.

dataobject

The data object containing fixtures for the site.

optionsobject

Options to pass to the createRecord method.

verboseboolean

Whether to log verbose output.

filterModelFromSetting(opts) → {object}

Filter model data based on settings.

Parameters:
NameTypeDescription
optsobject

The options object.

Properties
NameTypeAttributesDefaultDescription
modelobject

The model object.

fieldstring

The field to filter.

optionsobject<optional>
{}

Additional options.

Returns:

The filtered results and operation values.

Type: 
object

generatePassword(req) → {string}

Generate a random password based on the configuration settings

Parameters:
NameTypeDescription
reqobject

The request object.

Returns:
  • The generated password.
Type: 
string

(async) getActionGuards(rereadopt) → {Promise.<Array>}

Get action guards, optionally reloading them from the database.

Parameters:
NameTypeAttributesDescription
rereadboolean<optional>

Whether to reload the guards from the database.

Returns:
  • A promise that resolves to an array of action guards.
Type: 
Promise.<Array>

(async) getAllFixtures(aliasopt) → {Promise.<object>}

Get all fixtures for the specified alias, replacing any occurrences of '{alias}' in the fixture data with the provided alias.

Parameters:
NameTypeAttributesDescription
aliasstring<optional>

The alias for the fixtures.

Returns:
  • A promise that resolves to the fixtures data.
Type: 
Promise.<object>

(async) getAnonymousGuards(rereadopt) → {Promise.<Array>}

Get anonymous guards, optionally reloading them from the database.

Parameters:
NameTypeAttributesDescription
rereadboolean<optional>

Whether to reload the guards from the database.

Returns:
  • A promise that resolves to an array of anonymous guards.
Type: 
Promise.<Array>

(async) getApiKeyFromUserId(id) → {Promise.<string>}

Get API key from user ID by hashing the user's salt

Parameters:
NameTypeDescription
idstring | number

The user ID.

Returns:
  • A promise that resolves to the API key.
Type: 
Promise.<string>

(async) getAttribGuards(rereadopt) → {Promise.<Array>}

Get attribute guards, optionally reloading them from the database.

Parameters:
NameTypeAttributesDescription
rereadboolean<optional>

Whether to reload the guards from the database.

Returns:
  • A promise that resolves to an array of attribute guards.
Type: 
Promise.<Array>

(async) getCountriesValues() → {Promise.<Array.<{value: (string|number), text: string}>>}

Get a list of countries with their values and names

Returns:
  • A promise that resolves to an array of country values and names.
Type: 
Promise.<Array.<{value: (string|number), text: string}>>

getModelNames(asValueopt) → {Array}

Get all loaded model names, optionally returning them as value-text pairs.

Parameters:
NameTypeAttributesDescription
asValueboolean<optional>

Whether to return the model names as value-text pairs.

Returns:
  • An array of model names or value-text pairs.
Type: 
Array

(async) getQueryGuards(rereadopt) → {Promise.<Array>}

Get query guards, optionally reloading them from the database.

Parameters:
NameTypeAttributesDescription
rereadboolean<optional>

Whether to reload the guards from the database.

Returns:
  • A promise that resolves to an array of query guards.
Type: 
Promise.<Array>

(async) getSecureGuards(rereadopt) → {Promise.<Array>}

Get secure guards, optionally reloading them from the database.

Parameters:
NameTypeAttributesDescription
rereadboolean<optional>

Whether to reload the guards from the database.

Returns:
  • A promise that resolves to an array of secure guards.
Type: 
Promise.<Array>

(async) getSite(input, byIdopt) → {object}

Get site information based on the provided input, which can be either a hostname or an ID. If multi-site is enabled, it will fetch the site based on the hostname or ID; otherwise, it will return the default site.

Parameters:
NameTypeAttributesDefaultDescription
inputstring | number

The input value, which can be either a hostname or an ID.

byIdboolean<optional>
false

Whether to fetch the site by ID.

Returns:

The site information.

Type: 
object

getTeamIdsAndAliases(req) → {object}

Get the team IDs and aliases for the user in the request.

Parameters:
NameTypeDescription
reqobject

The request object.

Returns:

An object containing the team IDs and aliases.

Type: 
object

(async) getUserById(id, req) → {object|null}

Get a user by their ID.

Parameters:
NameTypeDescription
idstring | number

The ID of the user to retrieve.

reqobject

The request object.

Returns:

The user object if found, otherwise null.

Type: 
object | null

(async) getUserByToken(token, req) → {object|null}

Get a user by their token.

Parameters:
NameTypeDescription
tokenstring

The token of the user to retrieve.

reqobject

The request object.

Returns:

The user object if found, otherwise null.

Type: 
object | null

(async) hash(item) → {Promise.<string>}

Hash the given item using the specified algorithm from the configuration.

Parameters:
NameTypeDescription
item*

The item to be hashed.

Returns:
  • A promise that resolves to the hashed value.
Type: 
Promise.<string>

(async) init()

Plugin initialization method. This method is called by the Bajo framework during plugin initialization.

(async) isActionAllowed(action, model, req) → {Promise.<boolean>}

Check if the specified action is allowed for the given model and request. It retrieves the action guards, filters them based on the site ID, model name, and team IDs, and checks if the action is included in the allowed values. If any guard denies the action, it returns false; otherwise, it returns true.

Parameters:
NameTypeDescription
actionstring

The action to check. Available actions are: 'READ', 'CREATE', 'UPDATE', 'REMOVE' and 'EXPORT'.

modelobject

The model object.

reqobject

The request object.

Returns:
  • Returns true if the action is allowed, otherwise false.
Type: 
Promise.<boolean>

(async) mergeTeam(user, req)

Merge team information into the user object.

Parameters:
NameTypeDescription
userobject

The user object to merge team information into.

reqobject

The request object.

parseNsSettings(ns, setting, items)

Parse namespace settings for a request.

Parameters:
NameTypeDescription
nsstring

The namespace.

settingObject

The setting object.

itemsArray

The items to parse.

(async) passwordRule(req) → {object}

Generate a password validation rule based on the configuration settings.

Parameters:
NameTypeDescription
reqobject

The request object.

Returns:
  • The Joi password validation rule.
Type: 
object

pathsToCheck(req) → {Array}

Gather all paths to check for a request.

Parameters:
NameTypeDescription
reqObject

The request object.

Returns:
  • An array of paths to check.
Type: 
Array

(async) populateRouteGuards(sitesopt)

Populate route guards at start

Parameters:
NameTypeAttributesDescription
sitesarray<optional>

List of sites to populate route guards for. If not provided, it will fetch all active sites.

(async) rebuildFilter(model, filteropt, optionsopt) → {Promise.<object>}

Rebuild the filter for the provided model.

Parameters:
NameTypeAttributesDefaultDescription
modelobject

The model object.

filterobject<optional>
{}

The filter object.

optionsobject<optional>
{}

Additional options.

Returns:

The rebuilt filter.

Type: 
Promise.<object>

(async) removeRefs(site, options, verbose)

Remove references for the specified site using the provided options and verbosity settings.

Parameters:
NameTypeDescription
siteobject

The site object for which to remove references.

optionsobject

Options to pass to the removeRecord method.

verboseboolean

Whether to log verbose output.

(async) removeSite(alias, verboseopt)

Remove a site with the specified alias, along with its references, using the provided verbosity settings.

Parameters:
NameTypeAttributesDefaultDescription
aliasstring

The alias of the site to remove.

verboseboolean<optional>
false

Whether to log verbose output.

(async) resetToken(textopt) → {Promise.<{salt: string, token: string}>}

Reset token by generating a new salt and hashing it twice to create a new token.

Parameters:
NameTypeAttributesDescription
textstring<optional>

Optional text to use as the salt. If not provided, a new salt will be generated.

Returns:
  • A promise that resolves to an object containing the salt and token.
Type: 
Promise.<{salt: string, token: string}>

(async) runAttribGuard(optsopt)

Apply attribute guards to the provided model.

Parameters:
NameTypeAttributesDefaultDescription
optsobject<optional>
{}

The options object.

Properties
NameTypeAttributesDefaultDescription
modelobject

The model object.

teamIdsarray<optional>
[]

The team IDs.

optionsobject<optional>
{}

Additional options.

(async) runQueryGuard(optsopt)

Apply query guards to the provided query.

Parameters:
NameTypeAttributesDefaultDescription
optsobject<optional>
{}

The options object.

Properties
NameTypeAttributesDefaultDescription
modelobject

The model object.

qobject<optional>
{}

The query object.

teamIdsarray<optional>
[]

The team IDs.

optionsobject<optional>
{}

Additional options.

(async) sendMail(tpl, paramsopt)

Method to send mail through Masohi Messaging System. It is a thin wrapper for masohi-mail send method.

If both masohiMail and waibu are not loaded, nothing is delivered.

Parameters:
NameTypeAttributesDefaultDescription
tplstring | Array

Mail's template to use. If a string is given, the same template will be used for html & plaintext versions. Otherwise, the first template will be used for html mail, and the second one is for it's plaintext version

paramsObject<optional>
{}

masohi-mail's params object.

(async) signin(options)

Sign in user and redirect to the specified URL

Parameters:
NameTypeDescription
optionsobject

The options object.

Properties
NameTypeDescription
reqobject

The request object.

replyobject

The reply object.

userobject

The user object.

(async) signout(options)

Sign out user and redirect to the specified URL

Parameters:
NameTypeDescription
optionsobject

The options object.

Properties
NameTypeAttributesDescription
reqobject

The request object.

replyobject

The reply object.

reasonstring<optional>

The reason for signing out.

(async) start()

Plugin start method. This method is called by the Bajo framework during plugin start.

(async) verifyApiKey(req, reply, source, payload) → {boolean}

Verify API key

Parameters:
NameTypeDescription
reqobject

The request object.

replyobject

The reply object.

sourcestring

The source of the authentication setting.

payloadobject

The payload object.

Returns:
  • True if the API key is valid, otherwise false.
Type: 
boolean

(async) verifyBasic(req, reply, source, payload) → {boolean|string}

Verify basic authentication

Parameters:
NameTypeDescription
reqobject

The request object.

replyobject

The reply object.

sourcestring

The source of the authentication setting.

payloadobject

The payload object.

Returns:
  • True if the basic authentication is valid, otherwise false or an error message.
Type: 
boolean | string

(async) verifyJwt(req, reply, source, payload) → {boolean}

Verify JWT authentication

Parameters:
NameTypeDescription
reqobject

The request object.

replyobject

The reply object.

sourcestring

The source of the authentication setting.

payloadobject

The payload object.

Returns:
  • True if the JWT authentication is valid, otherwise false.
Type: 
boolean

(async) verifySession(req, reply, source, payload) → {boolean}

Verify user session

Parameters:
NameTypeDescription
reqobject

The request object.

replyobject

The reply object.

sourcestring

The source of the authentication setting.

payloadobject

The payload object.

Returns:
  • True if the session is valid, otherwise false.
Type: 
boolean