/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// import { BatchAdapter, FilteredAdapter, Model, UpdatableAdapter } from 'casbin'; import { ClientSession, Connection, ConnectOptions, FilterQuery, Model as MongooseModel } from 'mongoose'; import { IModel } from './model'; export interface MongooseAdapterOptions { filtered?: boolean; synced?: boolean; autoAbort?: boolean; autoCommit?: boolean; timestamps?: boolean; collectionName?: string; } export interface policyLine { ptype?: string; v0?: string; v1?: string; v2?: string; v3?: string; v4?: string; v5?: string; } export interface sessionOption { session?: ClientSession; } /** * Implements a policy adapter for casbin with MongoDB support. * * @class */ export declare class MongooseAdapter implements BatchAdapter, FilteredAdapter, UpdatableAdapter { connection?: Connection; private filtered; private isSynced; private uri; private options?; private autoAbort; private autoCommit; private session; private casbinRule; /** * 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. * * @constructor * @param {String} uri Mongo URI where casbin rules must be persisted * @param {Object} [options={}] Additional options to pass on to mongoose client * @param {Object} [adapterOptions={}] adapterOptions additional adapter options * @example * const adapter = new MongooseAdapter('MONGO_URI'); * const adapter = new MongooseAdapter('MONGO_URI', { mongoose_options: 'here' }) */ constructor(uri: string, options?: ConnectOptions, adapterOptions?: MongooseAdapterOptions); /** * 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. * * @static * @param {String} uri Mongo URI where casbin rules must be persisted * @param {Object} [options={}] Additional options to pass on to mongoose client * @param {Object} [adapterOptions={}] 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' }); */ static newAdapter(uri: string, options?: ConnectOptions, adapterOptions?: MongooseAdapterOptions): Promise; /** * 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. * * @static * @param {String} uri Mongo URI where casbin rules must be persisted * @param {Object} [options] 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' }); */ static newFilteredAdapter(uri: string, options?: ConnectOptions): Promise; /** * 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. * * @static * @param {String} uri Mongo URI where casbin rules must be persisted * @param {Object} [options={}] Additional options to pass on to mongoose client * @param {Boolean} autoAbort Whether to abort transactions on Error automatically * @param autoCommit * @example * const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI'); * const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI', { mongoose_options: 'here' }); */ static newSyncedAdapter(uri: string, options?: ConnectOptions, autoAbort?: boolean, autoCommit?: boolean): Promise; /** * Switch adapter to (non)filtered state. * Casbin uses this flag to determine if it should load the whole policy from DB or not. * * @param {Boolean} [enable=true] Flag that represents the current state of adapter (filtered or not) */ setFiltered(enable?: boolean): void; /** * isFiltered determines whether the filtered model is enabled for the adapter. * @returns {boolean} */ isFiltered(): boolean; /** * SyncedAdapter: Switch adapter to (non)synced state. * This enables mongoDB transactions when loading and saving policies to DB. * * @param {Boolean} [synced=true] Flag that represents the current state of adapter (filtered or not) */ setSynced(synced?: boolean): void; /** * SyncedAdapter: Automatically abort on Error. * When enabled, functions will automatically abort on error * * @param {Boolean} [abort=true] Flag that represents if automatic abort should be enabled or not */ setAutoAbort(abort?: boolean): void; /** * SyncedAdapter: Automatically commit after each addition. * When enabled, functions will automatically commit after function has finished * * @param {Boolean} [commit=true] Flag that represents if automatic commit should be enabled or not */ setAutoCommit(commit?: boolean): void; /** * SyncedAdapter: Gets active session or starts a new one. Sessions are used to handle transactions. */ getSession(): Promise; /** * SyncedAdapter: Sets current session to specific one. Do not use this unless you know what you are doing. */ setSession(session: ClientSession): Promise; /** * SyncedAdapter: Gets active transaction or starts a new one. Transaction must be closed before changes are done * to the database. See: commitTransaction, abortTransaction * @returns {Promise} Returns a session with active transaction */ getTransaction(): Promise; /** * SyncedAdapter: Commits active transaction. Documents are not saved before this function is used. * Transaction closes after the use of this function. * @returns {Promise} */ commitTransaction(): Promise; /** * SyncedAdapter: Aborts active transaction. All Document changes within this transaction are reverted. * Transaction closes after the use of this function. * @returns {Promise} */ abortTransaction(): Promise; /** * Loads one policy rule into casbin model. * This method is used by casbin and should not be called by user. * * @param {Object} line Record with one policy rule from MongoDB * @param {Object} model Casbin model to which policy rule must be loaded */ loadPolicyLine(line: policyLine, model: Model): void; /** * Implements the process of loading policy from database into enforcer. * This method is used by casbin and should not be called by user. * * @param {Model} model Model instance from enforcer * @returns {Promise} */ loadPolicy(model: Model): Promise; /** * Loads partial policy based on filter criteria. * This method is used by casbin and should not be called by user. * * @param {Model} model Enforcer model * @param {Object} [filter] MongoDB filter to query */ loadFilteredPolicy(model: Model, filter: FilterQuery | null): Promise; /** * 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. * * @param {String} pType Policy type to save into MongoDB * @param {Array} rule An array which consists of policy rule elements to store * @returns {IModel} Returns a created CasbinRule record for MongoDB */ savePolicyLine(pType: string, rule: string[]): IModel; /** * 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. * * @param {Model} model Model instance from enforcer * @returns {Promise} */ savePolicy(model: Model): Promise; /** * Implements the process of adding policy rule. * This method is used by casbin and should not be called by user. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array} rule Policy rule to add into enforcer * @returns {Promise} */ addPolicy(sec: string, pType: string, rule: string[]): Promise; /** * Implements the process of adding a list of policy rules. * This method is used by casbin and should not be called by user. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array} rules Policy rule to add into enforcer * @returns {Promise} */ addPolicies(sec: string, pType: string, rules: Array): Promise; /** * Implements the process of updating policy rule. * This method is used by casbin and should not be called by user. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array} oldRule Policy rule to remove from enforcer * @param {Array} newRule Policy rule to add into enforcer * @returns {Promise} */ updatePolicy(sec: string, pType: string, oldRule: string[], newRule: string[]): Promise; /** * Implements the process of removing a list of policy rules. * This method is used by casbin and should not be called by user. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array} rule Policy rule to remove from enforcer * @returns {Promise} */ removePolicy(sec: string, pType: string, rule: string[]): Promise; /** * Implements the process of removing a policyList rules. * This method is used by casbin and should not be called by user. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array} rules Policy rule to remove from enforcer * @returns {Promise} */ removePolicies(sec: string, pType: string, rules: Array): Promise; /** * Implements the process of removing policy rules. * This method is used by casbin and should not be called by user. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Number} fieldIndex Index of the field to start filtering from * @param {...String} fieldValues Policy rule to match when removing (starting from fieldIndex) * @returns {Promise} */ removeFilteredPolicy(sec: string, pType: string, fieldIndex: number, ...fieldValues: string[]): Promise; close(): Promise; /** * Just for testing. */ getCasbinRule(): MongooseModel & IModel & { _id: import("mongoose").Types.ObjectId; }, any>; }