Skip to content

Commit bf7d4f2

Browse files
authored
feat: add the code (#2)
* feat: add the code * feat: add the test code
1 parent 77c2c50 commit bf7d4f2

File tree

10 files changed

+877
-1
lines changed

10 files changed

+877
-1
lines changed

.github/workflows/ci.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: CI
2+
3+
on: [ push, pull_request ]
4+
5+
jobs:
6+
test-and-coverage:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v3
11+
- name: Set up JDK 1.8
12+
uses: actions/setup-java@v3
13+
with:
14+
java-version: '8'
15+
distribution: 'zulu'
16+
cache: 'maven'
17+
server-id: ossrh
18+
server-username: OSSRH_JIRA_USERNAME
19+
server-password: OSSRH_JIRA_PASSWORD
20+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
21+
gpg-passphrase: GPG_PASSPHRASE
22+
23+
- name: Build with Maven
24+
run: mvn clean test jacoco:report
25+
26+
- name: Upload To Codecov
27+
uses: codecov/codecov-action@v1
28+
with:
29+
token: ${{ secrets.CODECOV_TOKEN }}
30+
31+
- name: Set up Node.js
32+
uses: actions/setup-node@v2
33+
with:
34+
node-version: 20
35+
36+
- name: Semantic Release
37+
run: |
38+
npm install -g @conveyal/maven-semantic-release semantic-release
39+
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
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
GPG_KEY_NAME: ${{ secrets.GPG_KEY_NAME }}
43+
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
44+
OSSRH_JIRA_USERNAME: ${{ secrets.OSSRH_JIRA_USERNAME }}
45+
OSSRH_JIRA_PASSWORD: ${{ secrets.OSSRH_JIRA_PASSWORD }}

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1-
# session-role-manager
1+
# session-role-manager
2+
[![codebeat badge](https://codebeat.co/badges/998c8e12-ffdd-4196-b2a2-8979d7f1ee8a)](https://codebeat.co/projects/github-com-jcasbin-session-role-manager-master)
3+
[![build](https://github.com/jcasbin/session-role-manager/actions/workflows/ci.yml/badge.svg)](https://github.com/jcasbin/session-role-manager/actions)
4+
[![codecov](https://codecov.io/github/jcasbin/session-role-manager/branch/master/graph/badge.svg?token=4YRFEQY7VK)](https://codecov.io/github/jcasbin/session-role-manager)
5+
[![javadoc](https://javadoc.io/badge2/org.casbin/session-role-manager/javadoc.svg)](https://javadoc.io/doc/org.casbin/session-role-manager)
6+
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/session-role-manager.svg)](https://mvnrepository.com/artifact/org.casbin/session-role-manager/latest)
7+
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)
8+
9+
Session Role Manager is the [Session-based](https://en.wikipedia.org/wiki/Session_(computer_science)) role manager for [jCasbin](https://github.com/casbin/jcasbin). With this library, jCasbin can load session-based role hierarchy (user-role mapping) from jCasbin policy or save role hierarchy to it. The session is only active in the specified time range.
10+
11+
## Installation
12+
```xml
13+
<dependency>
14+
<groupId>org.casbin</groupId>
15+
<artifactId>session-role-manager</artifactId>
16+
<version>1.0.0</version>
17+
</dependency>
18+
```
19+
20+
## Example
21+
```java
22+
import org.casbin.jcasbin.main.Enforcer;
23+
import org.casbin.jcasbin.persist.file_adapter.FileAdapter;
24+
import static org.junit.jupiter.api.Assertions.assertFalse;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
public class Example {
28+
public static void main(String[] args) {
29+
// Create a new Enforcer using the model path. The default role manager is used initially.
30+
Enforcer e = new Enforcer("examples/rbac_model_with_sessions.conf");
31+
32+
// Manually set an adapter for the policy.
33+
FileAdapter a = new FileAdapter("examples/rbac_policy_with_sessions.csv");
34+
e.setAdapter(a);
35+
36+
// Use our custom role manager.
37+
SessionRoleManager rm = new SessionRoleManager(10);
38+
e.setRoleManager(rm);
39+
40+
// If our role manager relies on Casbin policy (e.g., reading "g" policy rules),
41+
// we need to set the role manager before loading the policy.
42+
e.loadPolicy();
43+
44+
// Current role inheritance tree (Time ranges shown in parentheses):
45+
// delta echo foxtrott
46+
// \ / \ /
47+
// (0-20) \ (5-15) / \ (10-20) / (10-12)
48+
// \ / \ /
49+
// bravo charlie
50+
// \ /
51+
// (0-10) \ / (5-15)
52+
// \ /
53+
// alpha
54+
55+
// Test permissions for different time points
56+
assertTrue(e.enforce("alpha", "data1", "read", "00"));
57+
assertTrue(e.enforce("alpha", "data1", "read", "05"));
58+
assertTrue(e.enforce("alpha", "data1", "read", "10"));
59+
assertFalse(e.enforce("alpha", "data1", "read", "15"));
60+
assertFalse(e.enforce("alpha", "data1", "read", "20"));
61+
62+
assertFalse(e.enforce("alpha", "data2", "read", "00"));
63+
assertTrue(e.enforce("alpha", "data2", "read", "05"));
64+
assertTrue(e.enforce("alpha", "data2", "read", "10"));
65+
assertTrue(e.enforce("alpha", "data2", "read", "15"));
66+
assertFalse(e.enforce("alpha", "data2", "read", "20"));
67+
68+
assertFalse(e.enforce("alpha", "data3", "read", "00"));
69+
assertFalse(e.enforce("alpha", "data3", "read", "05"));
70+
assertTrue(e.enforce("alpha", "data3", "read", "10"));
71+
assertFalse(e.enforce("alpha", "data3", "read", "15"));
72+
assertFalse(e.enforce("alpha", "data3", "read", "20"));
73+
}
74+
}
75+
76+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Request definition
2+
[request_definition]
3+
r = sub, obj, act, time
4+
5+
# Policy definition
6+
[policy_definition]
7+
p = sub, obj, act
8+
9+
# Policy effect
10+
[policy_effect]
11+
e = some(where (p_eft == allow)) && !some(where (p_eft == deny))
12+
13+
# Role definition
14+
[role_definition]
15+
g = _, _, _, _
16+
17+
# Matchers
18+
[matchers]
19+
m = g(r.sub, p.sub, r.time) && r.obj == p.obj && r.act == p.act
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
p, delta, data1, read
2+
p, echo, data2, read
3+
p, foxtrott, data3, read
4+
5+
g, alpha, bravo, 00, 10
6+
g, alpha, charlie, 05, 15
7+
g, bravo, delta, 00, 20
8+
g, bravo, echo, 05, 15
9+
g, charlie, echo, 10, 20
10+
g, charlie, foxtrott, 10, 12

maven-settings.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<settings>
2+
<servers>
3+
<server>
4+
<id>ossrh</id>
5+
<username>${OSSRH_JIRA_USERNAME}</username>
6+
<password>${OSSRH_JIRA_PASSWORD}</password>
7+
</server>
8+
</servers>
9+
<profiles>
10+
<profile>
11+
<id>ossrh</id>
12+
<activation>
13+
<activeByDefault>true</activeByDefault>
14+
</activation>
15+
<properties>
16+
<gpg.executable>gpg</gpg.executable>
17+
<gpg.keyname>${GPG_KEY_NAME}</gpg.keyname>
18+
<gpg.passphrase>${GPG_PASSPHRASE}</gpg.passphrase>
19+
</properties>
20+
</profile>
21+
</profiles>
22+
</settings>

pom.xml

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.casbin</groupId>
8+
<artifactId>session-role-manager</artifactId>
9+
<version>1.0.0</version>
10+
11+
<name>Session based role manager for jCasbin</name>
12+
<description>Load session-based role hierarchy from jCasbin policy or save role hierarchy to it</description>
13+
<url>https://github.com/jcasbin/session-role-manager</url>
14+
<inceptionYear>2024</inceptionYear>
15+
16+
<issueManagement>
17+
<system>Github</system>
18+
<url>https://github.com/jcasbin/session-role-manager/issues</url>
19+
</issueManagement>
20+
21+
<parent>
22+
<groupId>org.sonatype.oss</groupId>
23+
<artifactId>oss-parent</artifactId>
24+
<version>7</version>
25+
</parent>
26+
27+
<licenses>
28+
<license>
29+
<name>The Apache Software License, Version 2.0</name>
30+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
31+
<distribution>repo</distribution>
32+
</license>
33+
</licenses>
34+
35+
<scm>
36+
<url>https://github.com/jcasbin/session-role-manager</url>
37+
<connection>git@github.com:jcasbin/session-role-manager.git</connection>
38+
<developerConnection>https://github.com/hsluoyz</developerConnection>
39+
</scm>
40+
41+
<developers>
42+
<developer>
43+
<name>Xu Tan</name>
44+
<email>1428427011@qq.com</email>
45+
<url>https://github.com/tx2002</url>
46+
</developer>
47+
</developers>
48+
49+
<properties>
50+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
51+
</properties>
52+
53+
<profiles>
54+
<profile>
55+
<id>default</id>
56+
<activation>
57+
<activeByDefault>true</activeByDefault>
58+
</activation>
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<!-- Automatically close and deploy from OSSRH -->
63+
<groupId>org.sonatype.central</groupId>
64+
<artifactId>central-publishing-maven-plugin</artifactId>
65+
<version>0.5.0</version>
66+
<extensions>true</extensions>
67+
<configuration>
68+
<publishingServerId>ossrh</publishingServerId>
69+
<tokenAuth>true</tokenAuth>
70+
<autoPublish>true</autoPublish>
71+
</configuration>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-source-plugin</artifactId>
76+
<version>3.2.1</version>
77+
<executions>
78+
<execution>
79+
<phase>package</phase>
80+
<goals>
81+
<goal>jar-no-fork</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-javadoc-plugin</artifactId>
89+
<version>3.2.0</version>
90+
<executions>
91+
<execution>
92+
<phase>package</phase>
93+
<goals>
94+
<goal>jar</goal>
95+
</goals>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-gpg-plugin</artifactId>
102+
<version>1.5</version>
103+
<executions>
104+
<execution>
105+
<id>sign-artifacts</id>
106+
<phase>verify</phase>
107+
<goals>
108+
<goal>sign</goal>
109+
</goals>
110+
</execution>
111+
</executions>
112+
<configuration>
113+
<gpgArguments>
114+
<arg>--pinentry-mode</arg>
115+
<arg>loopback</arg>
116+
</gpgArguments>
117+
</configuration>
118+
</plugin>
119+
<plugin>
120+
<groupId>org.jacoco</groupId>
121+
<artifactId>jacoco-maven-plugin</artifactId>
122+
<version>0.7.6.201602180812</version>
123+
<executions>
124+
<execution>
125+
<id>prepare-agent</id>
126+
<goals>
127+
<goal>prepare-agent</goal>
128+
</goals>
129+
</execution>
130+
</executions>
131+
</plugin>
132+
<plugin>
133+
<groupId>org.apache.maven.plugins</groupId>
134+
<artifactId>maven-compiler-plugin</artifactId>
135+
<configuration>
136+
<source>8</source>
137+
<target>8</target>
138+
</configuration>
139+
</plugin>
140+
</plugins>
141+
</build>
142+
<distributionManagement>
143+
<snapshotRepository>
144+
<id>ossrh</id>
145+
<url>https://central.sonatype.com</url>
146+
</snapshotRepository>
147+
</distributionManagement>
148+
</profile>
149+
</profiles>
150+
151+
<dependencies>
152+
<dependency>
153+
<groupId>junit</groupId>
154+
<artifactId>junit</artifactId>
155+
<version>4.13.1</version>
156+
<scope>test</scope>
157+
</dependency>
158+
<dependency>
159+
<groupId>org.casbin</groupId>
160+
<artifactId>jcasbin</artifactId>
161+
<version>1.31.2</version>
162+
</dependency>
163+
<dependency>
164+
<groupId>commons-collections</groupId>
165+
<artifactId>commons-collections</artifactId>
166+
<version>3.2.2</version>
167+
</dependency>
168+
</dependencies>
169+
170+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.casbin.rolemanager;
16+
17+
public class Session {
18+
private SessionRole role;
19+
private String startTime;
20+
private String endTime;
21+
22+
public Session(SessionRole role, String startTime, String endTime) {
23+
this.role = role;
24+
this.startTime = startTime;
25+
this.endTime = endTime;
26+
}
27+
28+
public SessionRole getRole() {
29+
return role;
30+
}
31+
32+
public String getStartTime() {
33+
return startTime;
34+
}
35+
36+
public String getEndTime() {
37+
return endTime;
38+
}
39+
}

0 commit comments

Comments
 (0)