{"id":19298209,"url":"https://github.com/casbin/mongodb-adapter","last_synced_at":"2025-04-04T08:07:09.522Z","repository":{"id":23756031,"uuid":"99772519","full_name":"casbin/mongodb-adapter","owner":"casbin","description":"MongoDB adapter for Casbin","archived":false,"fork":false,"pushed_at":"2024-07-22T00:56:29.000Z","size":70,"stargazers_count":258,"open_issues_count":1,"forks_count":53,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-29T22:37:47.630Z","etag":null,"topics":["access-control","adapter","authorization","casbin","mongodb","storage-driver"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"casbin"}},"created_at":"2017-08-09T06:24:39.000Z","updated_at":"2024-10-23T14:48:13.000Z","dependencies_parsed_at":"2024-11-09T16:28:01.188Z","dependency_job_id":"4c4e5a20-a5b8-40de-8896-67eca65fbce0","html_url":"https://github.com/casbin/mongodb-adapter","commit_stats":{"total_commits":38,"total_committers":27,"mean_commits":"1.4074074074074074","dds":0.7894736842105263,"last_synced_commit":"3a26594fc7942cf1111319a30f1009741c03fa5d"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmongodb-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmongodb-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmongodb-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmongodb-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin","download_url":"https://codeload.github.com/casbin/mongodb-adapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245984583,"owners_count":20704797,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["access-control","adapter","authorization","casbin","mongodb","storage-driver"],"created_at":"2024-11-09T23:07:15.572Z","updated_at":"2025-03-28T07:05:12.348Z","avatar_url":"https://github.com/casbin.png","language":"Go","funding_links":["https://github.com/sponsors/casbin"],"categories":["Go"],"sub_categories":[],"readme":"MongoDB Adapter [![CI](https://github.com/casbin/mongodb-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin/mongodb-adapter/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/casbin/mongodb-adapter/badge.svg?branch=master)](https://coveralls.io/github/casbin/mongodb-adapter?branch=master) [![Godoc](https://godoc.org/github.com/casbin/mongodb-adapter?status.svg)](https://godoc.org/github.com/casbin/mongodb-adapter)\n====\n\nMongoDB Adapter is the [Mongo DB](https://www.mongodb.com) adapter for [Casbin](https://github.com/casbin/casbin). With this library, Casbin can load policy from MongoDB or save policy to it.\n\n## Installation\n\n    go get -u github.com/casbin/mongodb-adapter/v3\n\n## Simple Example\n\n```go\npackage main\n\nimport (\n\t\"github.com/casbin/casbin/v2\"\n\t\"github.com/casbin/mongodb-adapter/v3\"\n)\n\nfunc main() {\n\t// Initialize a MongoDB adapter and use it in a Casbin enforcer:\n\t// The adapter will use the database named \"casbin\".\n\t// If it doesn't exist, the adapter will create it automatically.\n\ta,err := mongodbadapter.NewAdapter(\"127.0.0.1:27017\") // Your MongoDB URL. \n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// Or you can use an existing DB \"abc\" like this:\n\t// The adapter will use the table named \"casbin_rule\".\n\t// If it doesn't exist, the adapter will create it automatically.\n\t// a := mongodbadapter.NewAdapter(\"127.0.0.1:27017/abc\")\n\n\te, err := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Load the policy from DB.\n\te.LoadPolicy()\n\t\n\t// Check the permission.\n\te.Enforce(\"alice\", \"data1\", \"read\")\n\t\n\t// Modify the policy.\n\t// e.AddPolicy(...)\n\t// e.RemovePolicy(...)\n\t\n\t// Save the policy back to DB.\n\te.SavePolicy()\n}\n```\n## Advanced Example\n\n```go\npackage main\n\nimport (\n\t\"github.com/casbin/casbin/v2\"\n\t\"github.com/casbin/mongodb-adapter/v3\"\n\tmongooptions \"go.mongodb.org/mongo-driver/mongo/options\"\n)\n\nfunc main() {\n\t// Initialize a MongoDB adapter with NewAdapterWithClientOption:\n\t// The adapter will use custom mongo client options.\n\t// custom database name.\n\t// default collection name 'casbin_rule'.\n\tmongoClientOption := mongooptions.Client().ApplyURI(\"mongodb://127.0.0.1:27017\")\n\tdatabaseName := \"casbin\"\n\ta,err := mongodbadapter.NewAdapterWithClientOption(mongoClientOption, databaseName)\n\t// Or you can use NewAdapterWithCollectionName for custom collection name.\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\te, err := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Load the policy from DB.\n\te.LoadPolicy()\n\t\n\t// Check the permission.\n\te.Enforce(\"alice\", \"data1\", \"read\")\n\t\n\t// Modify the policy.\n\t// e.AddPolicy(...)\n\t// e.RemovePolicy(...)\n\t\n\t// Save the policy back to DB.\n\te.SavePolicy()\n}\n```\n\n## Filtered Policies\n\n```go\nimport \"github.com/globalsign/mgo/bson\"\n\n// This adapter also implements the FilteredAdapter interface. This allows for\n// efficent, scalable enforcement of very large policies:\nfilter := \u0026bson.M{\"v0\": \"alice\"}\ne.LoadFilteredPolicy(filter)\n\n// The loaded policy is now a subset of the policy in storage, containing only\n// the policy lines that match the provided filter. This filter should be a\n// valid MongoDB selector using BSON. A filtered policy cannot be saved.\n```\n\n## Getting Help\n\n- [Casbin](https://github.com/casbin/casbin)\n\n## License\n\nThis project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fmongodb-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin%2Fmongodb-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fmongodb-adapter/lists"}