pwix:accounts-manager

v2.3.0Published last week

pwix:accounts-manager

What is it ?

A try to mutualize and factorize the most common parts of a simple accounts management system. It relies on pwix:accounts-core and extends it to provide:

  • a default schema to the collection, extending the standard Meteor.users one with properties:

    • whether the user is allowed to login to the client-side application, and the date and time of hist/her last connection

    • user and administrator notes

    • roles if opted-in for

  • Blaze components:

    • to list the accounts

    • to edit an account

    • to select one or several accounts.

In the same way that pwix:accounts-core, pwix:accounts-manager is able to manage several named accounts collections.

Each managed collection is made timestampable, and following fields are added (and maintained) to the fieldset definition:

1    createdAt: Date
2    createdBy: String
3    updatedAt: Date
4    updatedBy: String

Most of the configuration is done through the provided class which must be instanciated in common application code, apart from the schema which must be set, extended, updated via the provided method after class instanciation.

Provides

AccountsManager

The exported AccountsManager global object provides following items:

Classes

Account

This is a very thin extend of AccountsCore.Account class, and it actually eventually fully replace it. This means that invoking AccountsCore.Account and AccountsManager.Account provides you exactly the same extended class.

Methods
  • amAccount( args<Object> ): <amAccount>

    The constructor takes an object as single argument, with following keys:

    • editCloseAfterNew

      Whether the 'new account' dialog should be closed after having successfully created a new account, defaulting to true.

      When false, the 'new account' dialog is cleared after successful creation, and can be reused to define other new accounts.

    • editTabsFn

      A function which, when set, will receive a list of tab objects to be displayed on edition panel, and must return a list of tab objects.

      Prototype of the function is async fn( tabs<Array> ): tabs<Array>, where each array item is an object with following keys:

      • name: an optional name for the tab, defaulting to the template name

      • navLabel: a localized string to display on the navigation bar

      • paneTemplate: the name of the template to be displayed

      • paneData: the data context to be provided to the template, as an object or a function, defaulting to the default data context:

        • item: a ReactiveVar which holds the account object to edit (may be empty, but not null)
        • isNew: true|false
        • checker: a ReactiveVar which holds the parent Forms.Checker
        • amInstance: a ReactiveVar which holds the amAccount instance

      Default it to have:

      • an identity tab to let the user enter email addresses and usernames
      • a roles tab if the application has roles
      • a tab for admin notes
      • a tab for user notes.
    • listActiveCheckboxes

      Whether the checkboxes rendered in the tabular display are active, i.e. accept a click to switch their state, defaulting to false.

      Rationale: even if it would be very more easy to directly click on the tabular display to toggle a checkbox, some administrators may find this way too much easy, if not error prone, and prefer to have to pass through a distinct page/modal/display unit to securize a bit this update. The chosen default privileges the security over the esayness.

    • listFeedNow

      Whether the class should subscribe to the all publication to feed its internal list as soon as it is instanciated, defaulting to true.

      Note that this will run a Meteor.subscribe() function from inside a Tracker.autorun() computation code and is so subject to the usual limitations and caveats of Meteor computations.

  • fieldSet(): <Field.Set>

    This method returns the current Field.Set attached to the instance. This lets the caller reset, extend or update the collection fields set. It is the responsability of the caller to extend the previous collection schema to the new one after he/she has updated the fields set as this is not automatic.

    The default fields set is defined at instanciation time, and the corresponding schema attached to the collection at that same time. It is defined as:

1    [
2        // if have email addresses
3        {
4            name: 'emails',
5            type: Array,
6            optional: true,
7            dt_visible: false
8        },
9        {
10            name: 'emails.$',
11            type: Object,
12            optional: true,
13            tabular: false
14        },
15        {
16            name: 'emails.$._id',
17            type: String,
18            dt_data: false,
19            dt_visible: false
20        },
21        {
22            name: 'emails.$.address',
23            type: String,
24            regEx: SimpleSchema.RegEx.Email,
25            dt_data: 'emails.0.address',
26            dt_title: pwixI18n.label( I18N, 'list.email_address_th' ),
27            dt_template: Meteor.isClient && Template.dt_email_addresses,
28            form_check: amChecks.email_address,
29            form_type: acInstance.emailAtLeastOne() ? Forms.FieldType.C.MANDATORY : Forms.FieldType.C.OPTIONAL
30        },
31        {
32            name: 'emails.$.verified',
33            type: Boolean,
34            defaultValue: false,
35            dt_data: 'emails.0.verified',
36            dt_title: pwixI18n.label( I18N, 'list.email_verified_th' ),
37            dt_template: Meteor.isClient && Template.dt_email_verified,
38            dt_className: 'dt-center',
39            dt_orderDataType: 'dom-checkbox',
40            form_check: amChecks.email_verified
41        },
42        {
43            name: 'emails.$.preferred',
44            type: Boolean,
45            optional: true,
46            tabular: false,
47            form_check: amChecks.email_preferred,
48            form_type: Forms.FieldType.C.OPTIONAL
49        },
50        {
51            name: 'emails.$.label',
52            type: String,
53            optional: true,
54            dt_data: 'any',
55            dt_title: pwixI18n.label( I18N, 'list.email_label_th' ),
56            form_check: amChecks.email_label,
57            form_type: Forms.FieldType.C.OPTIONAL
58        },
59        // if have username(s)
60        {
61            name: 'username',
62            type: String,
63            optional: true,
64            dt_title: pwixI18n.label( I18N, 'list.username_th' ),
65            form_check: amChecks.username,
66            form_type: acInstance.usernameAtLeastOne() ? Forms.FieldType.C.MANDATORY : Forms.FieldType.C.OPTIONAL
67        },
68        {
69            name: 'usernames',
70            type: Array,
71            optional: true,
72            tabular: false
73        },
74        {
75            name: 'usernames.$',
76            type: Object,
77            optional: true,
78            tabular: false
79        },
80        {
81            name: 'usernames.$.username',
82            type: String,
83            optional: true,
84            tabular: false
85        },
86        {
87            name: 'usernames.$.preferred',
88            type: Boolean,
89            optional: true,
90            tabular: false
91        },
92        {
93            name: 'usernames.$.label',
94            type: String,
95            optional: true,
96            tabular: false
97        },
98        // for Meteor.users compatibility
99        {
100            name: 'profile',
101            type: Object,
102            optional: true,
103            blackbox: true,
104            tabular: false
105        },
106        {
107            name:  'services',
108            type: Object,
109            optional: true,
110            blackbox: true,
111            tabular: false
112        },
113        // if have roles, define a column to be used in the tabular display
114        {
115            name: 'roles',
116            schema: false,
117            dt_title: pwixI18n.label( I18N, 'list.roles_th' ),
118            dt_type: 'string',
119            form: false
120        },
121        // AccountsManager specifics
122        {
123            name: 'loginAllowed',
124            type: Boolean,
125            defaultValue: true,
126            dt_title: pwixI18n.label( I18N, 'list.login_allowed_th' ),
127            dt_className: 'dt-center',
128            dt_template: 'dt_checkbox',
129            dt_templateContext( rowData ){
130                return {
131                    item: rowData,
132                    readonly: true,
133                    enabled: true
134                }
135            },
136            dt_orderDataType: 'dom-checkbox',
137            form_check: amChecks.loginAllowed,
138            form_status: false
139        },
140        {
141            name: 'loginLastConnection',
142            type: Date,
143            optional: true,
144            dt_title: pwixI18n.label( I18N, 'list.last_connection_th' ),
145            dt_render( data, type, rowData, meta ){
146                return type === 'display' && rowData.loginLastConnection ? strftime( AccountsManager.configure().datetime, rowData.loginLastConnection ) : '';
147            },
148            dt_className: 'dt-center',
149            dt_type: 'date',
150            form_status: false,
151            form_check: false
152        },
153        Notes.fieldDef({
154            name: 'adminNotes',
155            dt_title: pwixI18n.label( I18N, 'list.admin_notes_th' ),
156            form_title: pwixI18n.label( I18N, 'tabs.admin_notes_title' )
157        }),
158        Notes.fieldDef({
159            name: 'userNotes',
160            dt_title: pwixI18n.label( I18N, 'list.user_notes_th' ),
161            form_title: pwixI18n.label( I18N, 'tabs.user_notes_title' )
162        }),
163        Timestampable.fieldDef()
164    ]
  • setupTabular( name<String>, args<Object> )

    This method initialize a named tabular display. args is an object with following keys:

    • fieldDef: an optional list of fields definitions to replace the default tabular fields set (which is itself the default fields set for the collection)

    • pub: an optional publication name, defaulting to 'pwix.AccountsManager.p.tabularLast'

Options

The class extends the AccountsCore.Options class to reactively manage amAccount instanciation arguments.

In other words, all instanciation arguments are available through <my_amAccount_instance>.opts().<my_argument>(), e.g. amInstance.opts().editTabsFn().

Helpers

AccountsManager.Checks
AccountsManager.Fielddef
AccountsManager.Tabular
AccountsManager.Transforms

Functions

AccountsManager.configure( o<Object> )

See below

AccountsManager.i18n.namespace()

Returns the i18n namespace used by the package. Used to add translations at runtime.

Available both on the client and the server.

AccountsCore.runAccountsSelection( selected<ReactiveVar>, opts<Object> )

Runs a modal dialog to let the user choose zero to many user accounts.

Parameters are:

  • selected: a ReactiveVar which contains the array of initially selected accounts identifiers (_id)

    This same ReactiveVar will contain the selection result when the dialog will be validated.

  • opts: an optional options object with following keys:

    • disabled: whether the selection component should be disabled, defaulting to false
    • selectOptions: additional configuration options for multiple-select selection component
    • instance: the name of the accounts instance, defaulting to 'users'
    • select_ph: the select component placeholder, defaulting to (localized) 'Select the desired accounts'
    • dialog_title: the dialog title, defaulting to (localized) 'Select one or more user accounts'
    • $target: a jQuery object which will receive the 'ah-accounts-select' event at the validation of the dialog

The modal triggers an 'ah-accounts-select' event at validation time, with data as:

  • items: an array of selected accounts documents
  • selected: an array of selected accounts identifiers.

This function is available on client-side only.

async AccountsManager.updateUser( userDoc<Object>, options<Object> ): <Boolean>

Events

On server side, AccountsManager.s.eventEmitter is an event emitter, and emits:

  • create, when a new account has been created, on any collection, with an object as argument containing:

    • amInstance: the instance name
    • item: the created user document
    • userId: the responsible user identifier.
  • update, after an item has been updated, on any collection, with an object as argument containing:

    • amInstance: the instance name
    • item: the updated user document
    • userId: the responsible user identifier.
  • delete, after an item has been deleted, on any collection, with an object as argument containing:

    • amInstance: the instance name
    • id: the identifier of the deleted user
    • userId: the responsible user identifier.

Blaze components

AccountEditPanel

A tabbed editing component to be run inside of a page or of a modal. Default tabs are named and ordered as:

  • account_ident_tab
  • maybe account_roles_tab if the pwix:roles package is used by the application
  • account_admin_notes_tab
  • account_user_notes_tab

This list of tabs can be reviewed, replaced or updated by the application via the editTabsFn instanciation parameter.

When run from AccountsList, it is run in a modal to edit the current item.

It expects following data context:

  • name: the amAccount instance name
  • item: the account's object to be edited, or null
  • tabs: an optional array of tabs provided by the application
  • tabsBefore: an optional array of tabs provided by the application
  • tabsUpdates: an optional updates object

AccountNewButton

A PlusButton component customized to create a new account.

It takes itself care of checking the permissions of the user, and, depending of its runtime parameters, either is disabled, or doesn't display at all if the user is not allowed.

It takes the very same data context than below AccountsList.

AccountsList

The component list the defined accounts as a pwix:tabular table, with standard 'Informations', 'Edit' and 'Delete' buttons.

It takes itself care of checking the permissions of the user, and, depending of its runtime parameters, either disabled, or doesn't display at all, the relevant buttons if the user is not allowed.

Known data context is:

  • name

    The (mandatory) tabular name to display. Must have been previously defined in common code through amAccount.setupTabular() method.

  • tabs

    An optional array of tabs to be displayed before the 'roles' tab (if any).

  • tabsBefore

    An optional array of tabs to be displayed before the 'ident_tab' tab.

  • tabsUpdates

    An optional object where keys are the name of the targeted tab, and the value an object which describes the update.

    E.g.

1    tabsUpdates: {
2        account_ident_tab: {
3            navLabel: 'Email & password'
4        },
5        account_admin_notes_tab: {
6            shown: false
7        }
  • editTitle

    An optional function to be called with the to-be-edited item as an argument, expected to return the dialog title.

Permissions management

This package extends AccountsCore.isAllowed() function with following permissions:

  • pwix.accounts_manager.data.adminNotes: whether the current user is able to see/edit admin notes:
    • instance: the amAccount instance
    • id: the account identifier

Note that reading an account from a server task is not subject to permissions as this is nonetheless required for authentication needs. The above permissions talk about external requests or user-requested tasks.

Configuration

This package relies on pwix:accounts-conf package for most of its configuration. Please see the relevant documentation.

This package's behavior can be configured through a call to the AccountsManager.configure() method, with just a single javascript object argument, which itself should only contains the options you want override.

Known configuration options are:

  • datetime

    The strftime format string used to display date and time timestamps.

    Defaults to %Y-%m-%d %H:%M:%S.

  • verbosity

    The verbosity level as:

    • AccountsManager.C.Verbose.NONE

    or an OR-ed value of integer constants:

    • AccountsManager.C.Verbose.CONFIGURE

      Trace configuration operations

    • AccountsManager.C.Verbose.FUNCTIONS

      Trace functions calls

    • AccountsManager.C.Verbose.READY

      Track the readyness status of the package.

    Defaults to AccountsManager.C.Verbose.CONFIGURE.

A function can be provided by the application for each of these parameters. The function will be called without argument and must return a suitable value.

Please note that AccountsManager.configure() method should be called in the same terms both in client and server sides.

Remind too that Meteor packages are instanciated at application level. They are so only configurable once, or, in other words, only one instance has to be or can be configured. Addtionnal calls to AccountsManager.configure() will just override the previous one. You have been warned: only the application should configure a package.

AccountsManager.configure() is a reactive data source.

NPM peer dependencies

Starting with v 1.0.0, and in accordance with advices from the Meteor Guide, we no more hardcode NPM dependencies in the Npm.depends clause of the package.js.

Instead we check npm versions of installed packages at runtime, on server startup, in development environment.

Dependencies as of v 2.3.0:

1    'lodash': '^4.17.0',
2    'multiple-select-vanilla': '^5.1.0',
3    'strftime': '^0.10.2'

Each of these dependencies should be installed at application level:

    meteor npm install <package> --save

Translations

New and updated translations are willingly accepted, and more than welcome. Just be kind enough to submit a PR on the Github repository.

Cookies and comparable technologies

None at the moment.

Issues & help

In case of support or error, please report your issue request to our Issues tracker.


P. Wieser

  • Last updated on 2026, Apr. 13rd