UNPKG

casbin-mongoose-adapter

Version:
293 lines (292 loc) 13.2 kB
/// <reference types="mongoose/types/aggregate" /> /// <reference types="mongoose/types/callback" /> /// <reference types="mongoose/types/collection" /> /// <reference types="mongoose/types/connection" /> /// <reference types="mongoose/types/cursor" /> /// <reference types="mongoose/types/document" /> /// <reference types="mongoose/types/error" /> /// <reference types="mongoose/types/expressions" /> /// <reference types="mongoose/types/helpers" /> /// <reference types="mongoose/types/middlewares" /> /// <reference types="mongoose/types/indexes" /> /// <reference types="mongoose/types/models" /> /// <reference types="mongoose/types/mongooseoptions" /> /// <reference types="mongoose/types/pipelinestage" /> /// <reference types="mongoose/types/populate" /> /// <reference types="mongoose/types/query" /> /// <reference types="mongoose/types/schemaoptions" /> /// <reference types="mongoose/types/schematypes" /> /// <reference types="mongoose/types/session" /> /// <reference types="mongoose/types/types" /> /// <reference types="mongoose/types/utility" /> /// <reference types="mongoose/types/validation" /> /// <reference types="mongoose/types/virtuals" /> /// <reference types="mongoose/types/inferschematype" /> 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; } 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<MongooseAdapter>; /** * 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<MongooseAdapter>; /** * 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<MongooseAdapter>; /** * 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<ClientSession>; /** * SyncedAdapter: Sets current session to specific one. Do not use this unless you know what you are doing. */ setSession(session: ClientSession): Promise<void>; /** * 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<ClientSession>} Returns a session with active transaction */ getTransaction(): Promise<ClientSession>; /** * SyncedAdapter: Commits active transaction. Documents are not saved before this function is used. * Transaction closes after the use of this function. * @returns {Promise<void>} */ commitTransaction(): Promise<void>; /** * SyncedAdapter: Aborts active transaction. All Document changes within this transaction are reverted. * Transaction closes after the use of this function. * @returns {Promise<void>} */ abortTransaction(): Promise<void>; /** * 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<void>} */ loadPolicy(model: Model): Promise<void>; /** * 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<IModel> | null): Promise<void>; /** * 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<String>} 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<Boolean>} */ savePolicy(model: Model): Promise<boolean>; /** * 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<String>} rule Policy rule to add into enforcer * @returns {Promise<void>} */ addPolicy(sec: string, pType: string, rule: string[]): 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. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array<String>} rules Policy rule to add into enforcer * @returns {Promise<void>} */ addPolicies(sec: string, pType: string, rules: Array<string[]>): Promise<void>; /** * 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<String>} oldRule Policy rule to remove from enforcer * @param {Array<String>} newRule Policy rule to add into enforcer * @returns {Promise<void>} */ updatePolicy(sec: string, pType: string, oldRule: string[], newRule: string[]): 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. * * @param {String} sec Section of the policy * @param {String} pType Type of the policy (e.g. "p" or "g") * @param {Array<String>} rule Policy rule to remove from enforcer * @returns {Promise<void>} */ removePolicy(sec: string, pType: string, rule: string[]): Promise<void>; /** * 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<String>} rules Policy rule to remove from enforcer * @returns {Promise<void>} */ removePolicies(sec: string, pType: string, rules: Array<string[]>): Promise<void>; /** * 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<void>} */ removeFilteredPolicy(sec: string, pType: string, fieldIndex: number, ...fieldValues: string[]): Promise<void>; close(): Promise<void>; /** * Just for testing. */ getCasbinRule(): MongooseModel<IModel, {}, {}, {}, import("mongoose").Document<unknown, {}, IModel> & IModel & { _id: import("mongoose").Types.ObjectId; }, any>; }