GithubHelp home page GithubHelp logo

devvoh / rights Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 7 KB

Rights is a simple bitmask-based rights system, using binary strings to represent rights.

License: MIT License

Makefile 6.14% PHP 93.86%
parable rights acl policy php7 php71 php72 php73

rights's Introduction

Rights

License

Rights is a simple bitmask-based rights system, using binary strings to represent rights. This allows for easy storage and fast handling.

Install

Php 7.1+ and composer are required.

$ composer require parable-php/rights

Usage

To create a rights instance with CRUD rights:

$rights = new Rights();

$rights->add('create', 'read', 'update', 'delete');

The binary string for 4 rights is 0000 (all disabled) -> 1111 (all enabled).

$rights->has('0001', 'create'); // true, since last position is 1
$rights->has('0001', 'read'); // false, since before-last is 0

Rights are represented in the binary string in reverse order. You can add as many rights as you want.

You can also combine two rights strings together, keeping the high values. This can be used to combine a user's individual rights and the rights set for that user's user group.

$combined = $rights->combine('1000', '0001'); // $combined = 1001

API

  • add(string ...$names): void - add a right by name
  • getAll(): int[] - get all rights ([string => int])
  • getNames(): string[] - get all rights' names
  • get(string $name): ?int - get specific right value by name
  • can(string $provided, string $name): bool - check if $provided string can take action $name
  • combine(string ...$rights): string - combine two strings, keeping the highest
  • getRightsFromNames(string ...$names): string - create binary string from rights passed
  • getNamesFromRights(string $rights): string[] - get enabled rights from binary string

Contributing

Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.

License

Rights is licensed under the MIT license.

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.