Implements a policy adapter for casbin with MongoDB support.
Kind: global class
- MongooseAdapter
- new MongooseAdapter(uri, [options])
- instance
- ._open() ⇒
Promise.<void> - .setFiltered([enable])
- .isFiltered() ⇒
boolean - .setSynced([synced])
- .setAutoAbort([abort])
- .setAutoCommit([commit])
- .getSession() ⇒
Promise.<Session> - .setSession()
- .getTransaction() ⇒
Promise.<Session> - .commitTransaction() ⇒
Promise.<void> - .abortTransaction() ⇒
Promise.<void> - .loadPolicyLine(line, model)
- .loadPolicy(model) ⇒
Promise.<void> - .loadFilteredPolicy(model, [filter])
- .savePolicyLine(ptype, rule) ⇒
Object - .savePolicy(model) ⇒
Promise.<Boolean> - .addPolicy(sec, ptype, rule) ⇒
Promise.<void> - .addPolicies(sec, ptype, rule) ⇒
Promise.<void> - .removePolicy(sec, ptype, rule) ⇒
Promise.<void> - .removePolicies(sec, ptype, rules) ⇒
Promise.<void> - .removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues) ⇒
Promise.<void>
- ._open() ⇒
- static
Creates a new instance of mongoose adapter for casbin. It does not wait for successfull connection to MongoDB. So, if you want to have a possibility to wait until connection successful, use newAdapter instead.
| Param | Type | Default | Description |
|---|---|---|---|
| uri | String |
Mongo URI where casbin rules must be persisted | |
| [options] | Object |
{} |
Additional options to pass on to mongoose client |
Example
const adapter = new MongooseAdapter('MONGO_URI');
const adapter = new MongooseAdapter('MONGO_URI', { mongoose_options: 'here' })Opens a connection to mongoDB
Kind: instance method of MongooseAdapter
Switch adapter to (non)filtered state. Casbin uses this flag to determine if it should load the whole policy from DB or not.
Kind: instance method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| [enable] | Boolean |
true |
Flag that represents the current state of adapter (filtered or not) |
isFiltered determines whether the filtered model is enabled for the adapter.
Kind: instance method of MongooseAdapter
SyncedAdapter: Switch adapter to (non)synced state. This enables mongoDB transactions when loading and saving policies to DB.
Kind: instance method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| [synced] | Boolean |
true |
Flag that represents the current state of adapter (filtered or not) |
SyncedAdapter: Automatically abort on Error. When enabled, functions will automatically abort on error
Kind: instance method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| [abort] | Boolean |
true |
Flag that represents if automatic abort should be enabled or not |
SyncedAdapter: Automatically commit after each addition. When enabled, functions will automatically commit after function has finished
Kind: instance method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| [commit] | Boolean |
true |
Flag that represents if automatic commit should be enabled or not |
SyncedAdapter: Gets active session or starts a new one. Sessions are used to handle transactions.
Kind: instance method of MongooseAdapter
SyncedAdapter: Sets current session to specific one. Do not use this unless you know what you are doing.
Kind: instance method of MongooseAdapter
SyncedAdapter: Gets active transaction or starts a new one. Transaction must be closed before changes are done to the database. See: commitTransaction, abortTransaction
Kind: instance method of MongooseAdapter
Returns: Promise.<Session> - Returns a session with active transaction
SyncedAdapter: Commits active transaction. Documents are not saved before this function is used. Transaction closes after the use of this function.
Kind: instance method of MongooseAdapter
SyncedAdapter: Aborts active transaction. All Document changes within this transaction are reverted. Transaction closes after the use of this function.
Kind: instance method of MongooseAdapter
Loads one policy rule into casbin model. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| line | Object |
Record with one policy rule from MongoDB |
| model | Object |
Casbin model to which policy rule must be loaded |
Implements the process of loading policy from database into enforcer. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| model | Model |
Model instance from enforcer |
Loads partial policy based on filter criteria. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| model | Model |
Enforcer model |
| [filter] | Object |
MongoDB filter to query |
Generates one policy rule ready to be saved into MongoDB. This method is used by casbin to generate Mongoose Model Object for single policy and should not be called by user.
Kind: instance method of MongooseAdapter
Returns: Object - Returns a created CasbinRule record for MongoDB
| Param | Type | Description |
|---|---|---|
| ptype | String |
Policy type to save into MongoDB |
| rule | Array.<String> |
An array which consists of policy rule elements to store |
Implements the process of saving policy from enforcer into database. If you are using replica sets with mongo, this function will use mongo transaction, so every line in the policy needs tosucceed for this to take effect. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| model | Model |
Model instance from enforcer |
Implements the process of adding policy rule. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| sec | String |
Section of the policy |
| ptype | String |
Type of the policy (e.g. "p" or "g") |
| rule | Array.<String> |
Policy rule to add into enforcer |
Implements the process of adding a list of policy rules. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| sec | String |
Section of the policy |
| ptype | String |
Type of the policy (e.g. "p" or "g") |
| rule | Array.<String> |
Policy rule to add into enforcer |
Implements the process of removing a list of policy rules. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| sec | String |
Section of the policy |
| ptype | String |
Type of the policy (e.g. "p" or "g") |
| rule | Array.<String> |
Policy rule to remove from enforcer |
Implements the process of removing a policyList rules. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| sec | String |
Section of the policy |
| ptype | String |
Type of the policy (e.g. "p" or "g") |
| rules | Array.<String> |
Policy rule to remove from enforcer |
Implements the process of removing policy rules. This method is used by casbin and should not be called by user.
Kind: instance method of MongooseAdapter
| Param | Type | Description |
|---|---|---|
| sec | String |
Section of the policy |
| ptype | String |
Type of the policy (e.g. "p" or "g") |
| fieldIndex | Number |
Index of the field to start filtering from |
| ...fieldValues | String |
Policy rule to match when removing (starting from fieldIndex) |
Creates a new instance of mongoose adapter for casbin. Instead of constructor, it does wait for successfull connection to MongoDB. Preferable way to construct an adapter instance, is to use this static method.
Kind: static method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| uri | String |
Mongo URI where casbin rules must be persisted | |
| [options] | Object |
{} |
Additional options to pass on to mongoose client |
| [adapterOptions] | Object |
{} |
Additional options to pass on to adapter |
Example
const adapter = await MongooseAdapter.newAdapter('MONGO_URI');
const adapter = await MongooseAdapter.newAdapter('MONGO_URI', { mongoose_options: 'here' });Creates a new instance of mongoose adapter for casbin. It does the same as newAdapter, but it also sets a flag that this adapter is in filtered state. That way, casbin will not call loadPolicy() automatically.
Kind: static method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| uri | String |
Mongo URI where casbin rules must be persisted | |
| [options] | Object |
{} |
Additional options to pass on to mongoose client |
Example
const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI');
const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI', { mongoose_options: 'here' });Creates a new instance of mongoose adapter for casbin. It does the same as newAdapter, but it checks wether database is a replica set. If it is, it enables transactions for the adapter. Transactions are never commited automatically. You have to use commitTransaction to add pending changes.
Kind: static method of MongooseAdapter
| Param | Type | Default | Description |
|---|---|---|---|
| uri | String |
Mongo URI where casbin rules must be persisted | |
| [options] | Object |
{} |
Additional options to pass on to mongoose client |
| autoAbort | Boolean |
true |
Whether to abort transactions on Error automatically |
Example
const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI');
const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI', { mongoose_options: 'here' });