This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 70db944
Showing
13 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/examples export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.lock | ||
package.xml | ||
/vendor | ||
.idea | ||
.php_cs.cache | ||
docs/_build | ||
/.vagrant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http://github.com/saritasa/php-roles-simple/contributors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1.0.0 | ||
----- | ||
|
||
- Initial version: | ||
HasRole trait |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]" | ||
} | ||
], | ||
"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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="test/bootstrap.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Saritasa Roles Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"providers": [ | ||
], | ||
"aliases": [ | ||
{ | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Saritasa\Roles\Enums; | ||
|
||
use Saritasa\Enum; | ||
|
||
/** | ||
* Human gender - Male or Female | ||
*/ | ||
class Roles extends Enum | ||
{ | ||
const None = 0; | ||
const User = 1; | ||
const Admin = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Saritasa; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use ReflectionClass; | ||
use Saritasa\Exceptions\NotImplementedException; | ||
|
||
/** | ||
* Trait to add to your User model | ||
*/ | ||
trait HasRole | ||
{ | ||
public function role(): BelongsTo | ||
{ | ||
return $this->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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Saritasa\Roles; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
interface IHasRole | ||
{ | ||
public function role(): BelongsTo; | ||
|
||
public function getRole(): Role; | ||
|
||
public function hasRole($role): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Saritasa\Roles\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* User role | ||
* | ||
* @property int $id Identifier | ||
*/ | ||
class Role extends Model | ||
{ | ||
protected $table = 'roles'; | ||
public $timestamps = false; | ||
|
||
protected $visible = [ | ||
'id', | ||
'name' | ||
]; | ||
} |