Skip to content

Commit

Permalink
docs: improve the README like other adapter repos
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou committed Dec 28, 2024
1 parent 2ddda6b commit 97e0a79
Showing 1 changed file with 53 additions and 43 deletions.
96 changes: 53 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
jcasbin-mongodb-adapter
===
MongoDB policy storage, implemented as an adapter for [jcasbin](https://github.com/casbin/jcasbin).
[![codebeat badge](https://codebeat.co/badges/df265133-60a0-4ee7-b57d-25fd27273905)](https://codebeat.co/projects/github-com-jcasbin-mongo-adapter-master)
[![build](https://github.com/jcasbin/jcasbin-mongo-adapter/actions/workflows/maven-ci.yml/badge.svg)](https://github.com/jcasbin/jcasbin-mongo-adapter/actions/workflows/maven-ci.yml)
[![codecov](https://codecov.io/gh/casbin/jcasbin-mongo-adapter/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin/jcasbin-mongo-adapter)
[![javadoc](https://javadoc.io/badge2/org.casbin/jcasbin-mongo-adapter/javadoc.svg)](https://javadoc.io/doc/org.casbin/jcasbin-mongo-adapter)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.zzl221000/jcasbin-mongo-adapter.svg)](https://mvnrepository.com/artifact/com.github.zzl221000/jcasbin-mongo-adapter/latest)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

## Getting Started
jcasbin-mongodb-adapter is the [MongoDB](https://github.com/mongodb/mongo) adapter for [jCasbin](https://github.com/casbin/jcasbin). With this library, jCasbin can load policy from a MongoDB database or save policy to it.

### maven
add `jitpack` repository
```xml
## Installation

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```
add `jcasbin-mongo-adapter` dependency
```xml
For Maven:

```xml
<dependency>
<groupId>com.github.zzl221000</groupId>
<groupId>org.casbin</groupId>
<artifactId>jcasbin-mongo-adapter</artifactId>
<version>v1.0</version>
<version>LATEST</version>
</dependency>
```

Expand All @@ -34,42 +30,56 @@ optional: If your project already has a mongodb driver, please ignore it.
</dependency>
```

## Simple Example

```java
package org.casbin.adapter;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.persist.Adapter;
import org.jim.jcasbin.MongoAdapter;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.main.SyncedEnforcer;
import org.casbin.jcasbin.model.Model;
import com.ruiling.system.config.errors.CasbinModelConfigNotFoundException;

public class Demo {
import java.util.ArrayList;

public class Test {
public static void main(String[] args) {
Adapter adapter = new MongoAdapter(mongoClient, "casbin");
Model model = new Model();
try {
String modelContext = properties.getModelContext();
model.loadModelFromText(modelContext);
} catch (CasbinModelConfigNotFoundException e) {
// use default model configuration
// request definition
model.addDef("r", "r", "sub, obj, act");
// policy definition
model.addDef("p", "p", "sub, obj, act");
// role definition
model.addDef("g", "g", "_, _");
// policy effect
model.addDef("e", "e", "some(where (p.eft == allow))");
// matchers
model.addDef("m", "m", "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act");
// Use MongoDB connection string and database name to initialize a MongoAdapter.
String connectionString = "mongodb://localhost:27017/";
MongoClient mongoClient = MongoClients.create(connectionString);
MongoDatabase database = mongoClient.getDatabase("casbin");

// Check and create a collection
if (!database.listCollectionNames().into(new ArrayList<>()).contains("casbin_rule")) {
database.createCollection("casbin_rule");
}
Enforcerenforcer = new SyncedEnforcer(model, adapter);
// set auto save policy
enforcer.enableAutoSave(true);
}

Adapter adapter = new MongoAdapter(mongoClient, "casbin");
Enforcer enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");

// Check the permission.
enforcer.enforce("alice", "data1", "read");

// Modify the policy.
// enforcer.addPolicy(...);
// enforcer.removePolicy(...);

// Save the policy back to DB.
adapter.savePolicy(enforcer.getModel());

// Close the MongoDB client.
mongoClient.close();
}
}

```

## Getting Help

- [jCasbin](https://github.com/casbin/jcasbin)

## License

[Apache-2.0](./LICENSE)
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.

0 comments on commit 97e0a79

Please sign in to comment.