https://github.com/ksoichiro/ability
Simple authorization library for Java.
https://github.com/ksoichiro/ability
java
Last synced: about 1 month ago
JSON representation
Simple authorization library for Java.
- Host: GitHub
- URL: https://github.com/ksoichiro/ability
- Owner: ksoichiro
- License: apache-2.0
- Created: 2015-05-10T12:32:51.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-24T14:17:47.000Z (about 11 years ago)
- Last Synced: 2025-03-04T19:44:23.536Z (over 1 year ago)
- Topics: java
- Language: Java
- Size: 207 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ability
[](https://travis-ci.org/ksoichiro/ability)
[](https://github.com/ksoichiro/ability/releases/latest)
Simple authorization library for Java.
## Usage
### Add dependency
```groovy
repositories {
mavenCentral()
}
dependencies {
compile "com.github.ksoichiro:ability:0.0.1"
}
```
Or snapshot:
```groovy
repositories {
maven {
url uri('https://oss.sonatype.org/content/repositories/snapshots/')
}
}
dependencies {
compile "com.github.ksoichiro:ability:0.0.1-SNAPSHOT"
}
```
### Create rules
Model class implements `Rule` interface and implements `allowed` method.
```java
public class Book implements Rule {
enum Actions {
READ,
EDIT,
}
private boolean published;
private User author;
@Override
public Set allowed(Object object, Object subject) {
Set rules = new HashSet();
if (subject == null || !(subject instanceof Book) || object == null || !(object instanceof User)) {
return rules;
}
User user = (User) object;
Book book = (Book) subject;
if (book.published) {
rules.add(Actions.READ.name());
}
if (book.author != null && book.author.getId() == user.getId()) {
rules.add(Actions.EDIT.name());
}
return rules;
}
// Getters and setters are omitted
}
```
### Add rules
User class has `Ability` instance and add some authorization methods.
```java
public class User {
private Ability ability;
public User() {
ability = new Ability();
ability.addRule(Book.class);
}
public boolean can(String action, Object subject) {
return ability.allowed(this, action, subject);
}
public boolean canReadBook(Book subject) {
return ability.allowed(this, Book.Actions.READ.name(), subject);
}
// Other unimportant codes are omitted
}
```
### Use
```java
User user1 = new User(1);
User user2 = new User(2);
Book book1 = new Book();
book1.setAuthor(user1);
book1.setPublished(true);
Book book2 = new Book();
assertTrue(user1.canReadBook(book1));
assertTrue(user1.can(Book.Actions.EDIT.name(), book1));
assertFalse(user2.can(Book.Actions.EDIT.name(), book1));
// Or you can directly use it
Ability ability = new Ability();
ability.addRule(Book.class);
assertTrue(ability.allowed(user1, Book.Actions.READ.name(), book1));
```
## Thanks
* Inspired by [Six](https://github.com/randx/six), which is used in GitLab
## License
Copyright 2015 Soichiro Kashima
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.