From 70db944e8dbe3c77ecaa1446af11891b1b74c526 Mon Sep 17 00:00:00 2001 From: Sergey Populov Date: Thu, 11 May 2017 20:34:57 +0500 Subject: [PATCH] Initial version --- .gitattributes | 1 + .gitignore | 7 ++++++ AUTHORS | 1 + CHANGES | 5 ++++ LICENSE | 19 +++++++++++++++ README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++ composer.json | 36 ++++++++++++++++++++++++++++ phpunit.xml | 24 +++++++++++++++++++ provides.json | 8 +++++++ src/Enums/Roles.php | 15 ++++++++++++ src/HasRole.php | 35 +++++++++++++++++++++++++++ src/IHasRole.php | 14 +++++++++++ src/Models/Role.php | 21 ++++++++++++++++ 13 files changed, 244 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 AUTHORS create mode 100644 CHANGES create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 provides.json create mode 100644 src/Enums/Roles.php create mode 100644 src/HasRole.php create mode 100644 src/IHasRole.php create mode 100644 src/Models/Role.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e9d9d40 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/examples export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc6b485 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.lock +package.xml +/vendor +.idea +.php_cs.cache +docs/_build +/.vagrant \ No newline at end of file diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..d8e769e --- /dev/null +++ b/AUTHORS @@ -0,0 +1 @@ +http://github.com/saritasa/php-roles-simple/contributors diff --git a/CHANGES b/CHANGES new file mode 100644 index 0000000..911e7a7 --- /dev/null +++ b/CHANGES @@ -0,0 +1,5 @@ +1.0.0 +----- + +- Initial version: +HasRole trait \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b285bb0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2016 Saritasa + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fce1e30 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# Saritasa Roles for Laravel + +Simplified implementation of user roles for Laravel applications: +User has role_id field, and corresponding roles table exists + +## Usage + +Install the ```saritasa/php-roles-simple``` package: + +```bash +$ composer require saritasa/php-roles-simple +``` + +Add the RolesServiceProvider service provider ``config/app.php``: + +```php +'providers' => array( + // ... + Saritasa\Roles\RolesServiceProvider::class, +) +``` + +## Available classes + +### HasRoles +Provides hasRole method; + +**Example**: +```php +class User extends Model implements IHasRoles +{ + uses HasRoles +} +``` +then somewere in code: +```php +if ($user->hasRole(Roles::Admin)) { ... }} +$user->role->name; +``` + +## Contributing + +### Requirements +This package must: +* Do not depend on any framework or library +* Do not depend on other Saritasa packages +* Do not register any providers + +1. Create fork +2. Checkout fork +3. Develop locally as usual. **Code must follow [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/)** +4. Update README.md to describe new or changed functionality. Add changes description to CHANGE file. +5. When ready, create pull request + +## Resources + +* [Bug Tracker](http://github.com/saritasa/php-common/issues) +* [Code](http://github.com/saritasa/php-common) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4be83d8 --- /dev/null +++ b/composer.json @@ -0,0 +1,36 @@ +{ + "name": "saritasa/php-roles-simple", + "type": "library", + "description": "User has field role_id and corresponding lookup table", + "keywords": ["php", "saritasa"], + "license": "MIT", + "authors": [ + { + "name": "Sergey Populov", + "email": "sergey@gmail.com" + } + ], + "require": { + "php": ">=7.0", + "illuminate/database": "^5.4", + "saritasa/php-common": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7" + }, + "minimum-stability": "stable", + "autoload": { + "psr-4" : { + "Saritasa\\Roles\\": "src/" + } + }, + "config": { + "preferred-install": "dist", + "sort-packages": true + }, + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..92d3858 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,24 @@ + + + + + + ./tests/ + + + + + + ./src/ + + + diff --git a/provides.json b/provides.json new file mode 100644 index 0000000..0cd180b --- /dev/null +++ b/provides.json @@ -0,0 +1,8 @@ +{ + "providers": [ + ], + "aliases": [ + { + } + ] +} diff --git a/src/Enums/Roles.php b/src/Enums/Roles.php new file mode 100644 index 0000000..00bb69b --- /dev/null +++ b/src/Enums/Roles.php @@ -0,0 +1,15 @@ +belongsTo(Role::class); + } + + public function getRole(): Role + { + return (!$this->role) ? $this->role = $this->role()->get() : $this->role; + } + + public function hasRole($role): bool + { + if (is_int($role)) { + return $this->role_id == $role; + } else if (is_string($role)) { + return $this->role->name == $role; + } else { + throw new NotImplementedException("function hasRole() accepts either int (role_id) or string (role name). " + .gettype($role)." was given"); + } + } +} diff --git a/src/IHasRole.php b/src/IHasRole.php new file mode 100644 index 0000000..4b17db5 --- /dev/null +++ b/src/IHasRole.php @@ -0,0 +1,14 @@ +