GithubHelp home page GithubHelp logo

php-oso's Introduction

Oso PHP

Test codecov StyleCI

This library integrates the authorization framework Oso with PHP, since it currently lacks an official integration.

Installation:

composer require j0sh0nat0r/oso (NOTE: The package is not yet available so installation from git is required)

Quick example:

use J0sh0nat0r\Oso\Oso;
use J0sh0nat0r\Oso\QueryOpts;
use J0sh0nat0r\Oso\Variable;

class Repository {
    public function __construct(public string $name, public bool $isPublic) { }
}

class Role {
    public function __construct(public string $name, public Repository $repository) { }
}

class User {
    public function __construct(public array $roles) { }
}

$reposDb = [
    'gmail' => new Repository('gmail', false),
    'react' => new Repository('react', true),
    'oso' => new Repository('oso', false)
];

$usersDb = [
    'larry' => new User([new Role('admin', $reposDb['gmail'])]),
    'anne' => new User([new Role('maintainer', $reposDb['react'])]),
    'graham' => new User([new Role('contributor', $reposDb['oso'])]),
];

$oso = new Oso();

$oso->registerClass(User::class);
$oso->registerClass(Repository::class);

$oso->loadStr(<<<POLAR
actor User {}

resource Repository {
  permissions = ["read", "push", "delete"];
  roles = ["contributor", "maintainer", "admin"];

  "read" if "contributor";
  "push" if "maintainer";
  "delete" if "admin";

  "maintainer" if "admin";
  "contributor" if "maintainer";
}

# This rule tells Oso how to fetch roles for a repository
has_role(actor: User, role_name: String, repository: Repository) if
  role in actor.roles and
  role_name = role.name and
  repository = role.repository;

has_permission(_actor: User, "read", repository: Repository) if
  repository.isPublic;

allow(actor, action, resource) if
  has_permission(actor, action, resource);
POLAR
);

// Check a single permission
echo 'Larry can push to gmail: ' . var_export($oso->isAllowed($usersDb['larry'], 'push', $reposDb['gmail']), true) . PHP_EOL;

// Using queries as iterators
// This code loosely mirrors the implementation of `authorizedActions`
foreach ($reposDb as $repo) {
    echo "$repo->name:", PHP_EOL;

    foreach ($usersDb as $name => $user) {
        // Here we ask Oso to return the allowed action to us using a variable
        $query = $oso->queryRule('allow', QueryOpts::default(), $user, new Variable('action'), $repo);

        $allowedActions = array_column(iterator_to_array($query), 'action');

        if (!empty($allowedActions)) {
            echo "\t$name: ", implode(', ', $allowedActions), PHP_EOL;
        }
    }
}

php-oso's People

Contributors

j0sh0nat0r avatar stylecibot avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

d70rr3s

php-oso's Issues

How to contribute

Hi, I was looking to start experiment using Oso in several PHP projects and follow around until found your project. Are you planning maintain this as an usable library (until Oso fork/provides one themselves)? If so, I would love to contribute. Any thoughts or plan on how working further on it? Pardon me, if I mix up words or expressions are not quite right, my English is a little rusty.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.