Skip to content

Latest commit

 

History

History
347 lines (257 loc) · 15.6 KB

API.md

File metadata and controls

347 lines (257 loc) · 15.6 KB

MongooseAdapter

Implements a policy adapter for casbin with MongoDB support.

Kind: global class

new MongooseAdapter(uri, [options])

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' })

mongooseAdapter._open() ⇒ Promise.<void>

Opens a connection to mongoDB

Kind: instance method of MongooseAdapter

mongooseAdapter.setFiltered([enable])

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)

mongooseAdapter.isFiltered() ⇒ boolean

isFiltered determines whether the filtered model is enabled for the adapter.

Kind: instance method of MongooseAdapter

mongooseAdapter.setSynced([synced])

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)

mongooseAdapter.setAutoAbort([abort])

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

mongooseAdapter.setAutoCommit([commit])

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

mongooseAdapter.getSession() ⇒ Promise.<Session>

SyncedAdapter: Gets active session or starts a new one. Sessions are used to handle transactions.

Kind: instance method of MongooseAdapter

mongooseAdapter.setSession()

SyncedAdapter: Sets current session to specific one. Do not use this unless you know what you are doing.

Kind: instance method of MongooseAdapter

mongooseAdapter.getTransaction() ⇒ Promise.<Session>

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

mongooseAdapter.commitTransaction() ⇒ Promise.<void>

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

mongooseAdapter.abortTransaction() ⇒ Promise.<void>

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

mongooseAdapter.loadPolicyLine(line, model)

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

mongooseAdapter.loadPolicy(model) ⇒ Promise.<void>

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

mongooseAdapter.loadFilteredPolicy(model, [filter])

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

mongooseAdapter.savePolicyLine(ptype, rule) ⇒ Object

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

mongooseAdapter.savePolicy(model) ⇒ Promise.<Boolean>

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

mongooseAdapter.addPolicy(sec, ptype, rule) ⇒ Promise.<void>

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

mongooseAdapter.addPolicies(sec, ptype, rule) ⇒ Promise.<void>

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

mongooseAdapter.removePolicy(sec, ptype, rule) ⇒ Promise.<void>

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

mongooseAdapter.removePolicies(sec, ptype, rules) ⇒ Promise.<void>

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

mongooseAdapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues) ⇒ Promise.<void>

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)

MongooseAdapter.newAdapter(uri, [options], [adapterOptions])

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' });

MongooseAdapter.newFilteredAdapter(uri, [options])

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' });

MongooseAdapter.newSyncedAdapter(uri, [options], autoAbort)

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' });