Implements a policy adapter for casbin with MongoDB support.
Kind: global class
- MongooseAdapter
- new MongooseAdapter(uri, [options])
- instance
- .setFiltered([isFiltered])
- .loadPolicyLine(line, model)
- .loadPolicy(model) ⇒
Promise.<void> - .loadFilteredPolicy(model, [filter])
- .savePolicyLine(ptype, rule) ⇒
Object - .savePolicy(model) ⇒
Promise.<Boolean> - .addPolicy(sec, ptype, rule) ⇒
Promise.<void> - .removePolicy(sec, ptype, rule) ⇒
Promise.<void> - .removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues) ⇒
Promise.<void>
- 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 |
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 |
|---|---|---|---|
| [isFiltered] | Boolean |
true |
Flag that represents the current state of adapter (filtered or not) |
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 |
Persists one policy rule into MongoDB. This method is used by casbin 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. 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 removing 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 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 |
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' });