Skip to content

Commit

Permalink
feat: add the code (#3)
Browse files Browse the repository at this point in the history
* feat: add the code

* fix: add Apache header and delete Chinese char
  • Loading branch information
tx2002 authored Oct 7, 2024
1 parent 870c624 commit 95a8dd0
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 1 deletion.
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on: [ push, pull_request ]

jobs:
test-and-coverage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'zulu'
cache: 'maven'
server-id: ossrh
server-username: OSSRH_JIRA_USERNAME
server-password: OSSRH_JIRA_PASSWORD
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
gpg-passphrase: GPG_PASSPHRASE

- name: Build with Maven
run: mvn clean test jacoco:report

- name: Upload To Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 20

- name: Semantic Release
run: |
npm install -g @conveyal/maven-semantic-release semantic-release
semantic-release --prepare @conveyal/maven-semantic-release --publish @semantic-release/github,@conveyal/maven-semantic-release --verify-conditions @semantic-release/github,@conveyal/maven-semantic-release --verify-release @conveyal/maven-semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GPG_KEY_NAME: ${{ secrets.GPG_KEY_NAME }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
OSSRH_JIRA_USERNAME: ${{ secrets.OSSRH_JIRA_USERNAME }}
OSSRH_JIRA_PASSWORD: ${{ secrets.OSSRH_JIRA_PASSWORD }}
80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
# string-adapter
# string-adapter
[![codebeat badge](https://codebeat.co/badges/998c8e12-ffdd-4196-b2a2-8979d7f1ee8a)](https://codebeat.co/projects/github.aaakk.us.kg-jcasbin-string-adapter-master)
[![build](https://github.com/jcasbin/string-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/jcasbin/string-adapter/actions)
[![codecov](https://codecov.io/github/jcasbin/string-adapter/branch/master/graph/badge.svg?token=4YRFEQY7VK)](https://codecov.io/github/jcasbin/string-adapter)
[![javadoc](https://javadoc.io/badge2/org.casbin/string-adapter/javadoc.svg)](https://javadoc.io/doc/org.casbin/string-adapter)
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/string-adapter.svg)](https://mvnrepository.com/artifact/org.casbin/string-adapter/latest)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

String Adapter is the string adapter for jCasbin, which provides interfaces for loading policies from a string and saving policies to it.

## Installation
```xml
<dependency>
<groupId>org.casbin</groupId>
<artifactId>string-adapter</artifactId>
<version>1.0.0</version>
</dependency>
```


## Example
```java
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.model.Model;

public class Example {
public static void main(String[] args) {

// Define the Casbin model
String modelText = "[request_definition]\n" +
"r = sub, obj, act\n\n" +
"[policy_definition]\n" +
"p = sub, obj, act\n\n" +
"[role_definition]\n" +
"g = _, _\n" +
"g2 = _, _\n\n" +
"[policy_effect]\n" +
"e = some(where (p.eft == allow))\n\n" +
"[matchers]\n" +
"m = g(r.sub, p.sub) && g2(r.obj, p.obj) && r.act == p.act";

// Load model
Model m = new Model();
m.loadModelFromText(modelText);

// Define policy
String line = "p, alice, data1, read\n" +
"p, bob, data2, write\n" +
"p, data_group_admin, data_group, write\n\n" +
"g, alice, data_group_admin\n" +
"g2, data1, data_group\n" +
"g2, data2, data_group";

// Create a StringAdapter
StringAdapter sa = new StringAdapter(line);

// Create Enforcer, and load model and policy
Enforcer e = new Enforcer(m, sa);

e.loadPolicy();
// check permissions
if (e.enforce("alice", "data1", "read")) {
System.out.println("permitted");
} else {
System.out.println("rejected");
}

e.savePolicy();
}
}
```

## Getting Help

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

## License

This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.
22 changes: 22 additions & 0 deletions maven-settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<settings>
<servers>
<server>
<id>ossrh</id>
<username>${OSSRH_JIRA_USERNAME}</username>
<password>${OSSRH_JIRA_PASSWORD}</password>
</server>
</servers>
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.keyname>${GPG_KEY_NAME}</gpg.keyname>
<gpg.passphrase>${GPG_PASSPHRASE}</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
162 changes: 162 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.casbin</groupId>
<artifactId>string-adapter</artifactId>
<version>1.0.0</version>

<name>String Adapter for JCasbin</name>
<description>Load policy from a string or save policy to it</description>
<url>https://github.com/jcasbin/string-adapter</url>
<inceptionYear>2024</inceptionYear>

<issueManagement>
<system>Github</system>
<url>https://github.com/jcasbin/string-adapter/issues</url>
</issueManagement>

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url>https://github.com/jcasbin/string-adapter</url>
<connection>[email protected]:jcasbin/string-adapter.git</connection>
<developerConnection>https://github.com/hsluoyz</developerConnection>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<!-- Automatically close and deploy from OSSRH -->
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>ossrh</publishingServerId>
<tokenAuth>true</tokenAuth>
<autoPublish>true</autoPublish>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://central.sonatype.com</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.casbin</groupId>
<artifactId>jcasbin</artifactId>
<version>1.31.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 95a8dd0

Please sign in to comment.