juicycleff / casbin-mongodb-adapter Goto Github PK
View Code? Open in Web Editor NEWPure MongoDB adapter for Casbin
License: MIT License
Pure MongoDB adapter for Casbin
License: MIT License
First off, thanks to all contributors for creating and maintaining this adapter!
I am getting the this.adapter.isFiltered
bug, using version v1.1.11, which is the most recent version on npm. Could you update the npm package to include the recent changes to the codebase?
In my code, I try to delete a policies via casbin's removePolicies([["GROUP-1", "DOMAIN-A", "POLICY-A1", "ACT-BIND"], ...])
, but this document not be deleted from database actually. I found the adapter calling the MongoDb deleteOne() function with current timestamp as below, but that document with different timestamp in my database, and that causes deleting failed.
{ createdAt: '2023-11-15T10:02:18.402Z', updatedAt: '2023-11-15T10:02:18.402Z', ptype: 'p', v0: 'GROUP-1, v1: 'DOMAIN-A', v2: 'POLICY_A1', v3: 'ACT-BIND' }
Please remove the timestamp from the deletion condition.
In my code, I update mongo DB documents with transaction, which could guaranty my updating would not leak or conflict. However, my transaction seems not able to apply to the casbint documents updating. Is that possible to let me to pass my client session to the adapter?
see mongodb/node-mongodb-native@5a37a92
and then will throw error: Casbin mongo adapter not connected
if (this.mongoClient.isConnected === undefined) {
throw new Error('Casbin mongo adapter not connected');
}
I was attempting to use the RBAC management api and noticed that the delete was not persisting (autosave I guess doesn't work for deleteUser
). So I attempted to use savePolicy
and got the following error:
TypeError: this.adapter.isFiltered is not a function
46 | const enforcer = await getInstance();
47 | const result = await enforcer.deleteUser(user.id);
> 48 | await enforcer.savePolicy();
| ^
49 | return result;
50 | },
51 |
at Enforcer.isFiltered (node_modules/casbin/lib/coreEnforcer.js:175:33)
at Enforcer.<anonymous> (node_modules/casbin/lib/coreEnforcer.js:185:22)
at node_modules/casbin/lib/coreEnforcer.js:21:71
at __awaiter (node_modules/casbin/lib/coreEnforcer.js:17:12)
at Enforcer.savePolicy (node_modules/casbin/lib/coreEnforcer.js:184:16)
at Object.removeUser (api/services/AuthorizationService.js:48:20)
at Object.<anonymous> (tests/services/authorization.test.js:79:3)
Diving deeper I noticed that node-casbin expects isFiltered
to be a function see https://github.com/casbin/node-casbin/blob/master/src/coreEnforcer.ts#L184.
public isFiltered(): boolean {
if ('isFiltered' in this.adapter) {
return this.adapter.isFiltered();
}
return false;
}
Current state of isFiltered
in the adapter.
Workaround for those interested:
adapter = await MongoAdapter.newAdapter({
// config for mongo here
});
// workaround
delete adapter.isFiltered;
// casbin initialization
enforcer = await casbin.newEnforcer(model, adapter);
I'm not currently using filtered yet... so I'm not sure how useful this will be to anyone.
The removeFilteredPolicy creates a new object using the CasbinRule class,
casbin-mongodb-adapter/src/lib/adapter.ts
Line 222 in 793909c
which create a instance with the properties createdAt
and updatedAt
providing a object which will use to in the deleteMany in the following format
{
createdAt: '2021-06-18T20:47:52.585Z',
updatedAt: '2021-06-18T20:47:52.585Z',
ptype: 'p',
v0: 'user',
v1: 'pietro.verdile',
v2: 1,
v3: 'library',
v4: 'systemTags',
v5: 'eq;lib_60ccdfa5bc9e5400376a03ac'
}
This object never will match any document in MongoDB due the properties createdAt and updatedAt.
[MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
const { MongoAdapter } = require('casbin-mongodb-adapter');
let uri = 'mongodb://192.168.99.100:27017,192.168.99.101:27017,192.168.99.102:27017/casbin';
const connect = (options) => {
const adapter = await MongoAdapter.newAdapter({
uri: uri ,
option: {
db: options.dbParameters(),
server: options.serverParameters(),
replset: options.replsetParameters(options.repl)
}
})
return adapter;
}
RUN return error :
(node:11963) UnhandledPromiseRejectionWarning: Error: The "url" argument must be of type string. Received undefined at MongoAdapter.open (/Users/iadm/Project/microservice/authorization/node_modules/casbin-mongodb-adapter/build/main/lib/adapter.js:162:19) at processTicksAndRejections (internal/process/task_queues.js:97:5) at async Function.newAdapter (/Users/iadm/Project/microservice/authorization/node_modules/casbin-mongodb-adapter/build/main/lib/adapter.js:35:9) at async EventEmitter.<anonymous> (/Users/iadm/Project/microservice/authorization/src/config/db/mongo.js:15:21) (Use
node --trace-warnings ...to show where the warning was created)
Thanks for your contribution to Casbin community :)
My suggestion is that this repo can add badges to achieve similar quality like others: https://github.com/node-casbin/sequelize-adapter
Hello,
I am using following casbin and driver version
"casbin": "5.7.1"
"casbin-mongodb-adapter": "1.2.0"
I am trying to use batch method - addNamedPolicies('p', permissions);
I am getting following error while using this method
Error: cannot to save policy, the adapter does not implement the BatchAdapter
Can you please help me fix all the batch processing methods like addNamedPolicies(), RemoveNamedGroupingPolicies() issue in the adapter.
I am working on a project where I'd like to use this package. However, when trying to install with npm, the (intended) error message pops up: casbin-mongo-adapter must be installed with Yarn
.
Is there any reason behind this rather than a a preference for Yarn? I work in an all-npm environment (including our CI servers, all other team members' development environments, ...), so changing to Yarn for just this one package is not an option.
If there is no other reason rather than a preference for Yarn, would you agree to removing the preinstall hook?
Update: I find this especially confusing as the README states to install using npm
.
Hello,
I am using following version of casbin and adapter
"casbin": "5.7.1",
"casbin-mongodb-adapter": "1.2.2"
After adding batch processing in latest adapter version "1.2.2", I am able to use addNamedGroupingPolicies method.
But, while trying the removeNamedGroupingPolicies method I am getting following error
Reference Method - https://casbin.org/docs/en/management-api#removenamedgroupingpolicies
Example:- enforcer.removeNamedGroupingPolicies("g2", [[ "[email protected]", "Group1", "Floor1"]]);
error: MongoError: BSON field 'delete.deletes.q' is the wrong type 'array', expected type 'object'
error": {
"ok": 0,
"code": 14,
"codeName": "TypeMismatch",
"name": "MongoError"
}
Can you please help me resolve this issue.
Thank you.
I am working on a project that deals with a larger amount of permissions. There is a REST service to manage permissions/roles. Each time a permission or role has been changed, I called await enforcer.savePolicy();
.
Now I realized that savePolicy
effectively drops the entire casbin mongo collection and re-creates it with the new policy data. When calling it in concurrent scenarios (e.g. I have two concurrent requests to add a permission), that results in the following situation:
savePolicy
is called and drops the collection,savePolicy
is called and drops the collection,In other scenarios (I cannot quite reproduce when exactly), step 4 throws an error (ns not found
) as it apparently tries to perform operations on the now-dropped collection.
I solved this in my application now by waiting for all write operations to finish, and only then call savePolicy
.
Is this something that could be implemented in the adapter itself? Alternatively, would this behavior be something worth mentioning in the documentation?
Update: The problem only occured once we moved to a replica set rather than a single Mongo instance.
@juicycleff policies are not being deleted because CasbinRule comes with createdAt and updatedAt attributes, can you please either construct an empty object or delete
line.createdAt`
I will open a PR for this
package.json
{
"dependencies": {
"mongodb": "^4.3.1"
}
}
Error: Error: Casbin mongo adapter not connected
This property this.mongoClient.isConnected
is undefined
and is used to validate connection previous query.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.