ability

Simple authorization library for Java

License

License

GroupId

GroupId

com.github.ksoichiro
ArtifactId

ArtifactId

ability
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

ability
Simple authorization library for Java
Project URL

Project URL

https://github.com/ksoichiro/ability
Source Code Management

Source Code Management

https://github.com/ksoichiro/ability

Download ability

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ksoichiro/ability/ -->
<dependency>
    <groupId>com.github.ksoichiro</groupId>
    <artifactId>ability</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ksoichiro/ability/
implementation 'com.github.ksoichiro:ability:0.0.1'
// https://jarcasting.com/artifacts/com.github.ksoichiro/ability/
implementation ("com.github.ksoichiro:ability:0.0.1")
'com.github.ksoichiro:ability:jar:0.0.1'
<dependency org="com.github.ksoichiro" name="ability" rev="0.0.1">
  <artifact name="ability" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ksoichiro', module='ability', version='0.0.1')
)
libraryDependencies += "com.github.ksoichiro" % "ability" % "0.0.1"
[com.github.ksoichiro/ability "0.0.1"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

ability

Build Status Maven Central

Simple authorization library for Java.

Usage

Add dependency

repositories {
    mavenCentral()
}
dependencies {
    compile "com.github.ksoichiro:ability:0.0.1"
}

Or snapshot:

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.

public class Book implements Rule {
    enum Actions {
        READ,
        EDIT,
    }

    private boolean published;
    private User author;

    @Override
    public Set<String> allowed(Object object, Object subject) {
        Set<String> rules = new HashSet<String>();
        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.

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

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, 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.

Versions

Version
0.0.1