Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
populov committed May 11, 2017
0 parents commit 70db944
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/examples export-ignore
7 changes: 7 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://github.com/saritasa/php-roles-simple/contributors
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1.0.0
-----

- Initial version:
HasRole trait
19 changes: 19 additions & 0 deletions LICENSE
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.
58 changes: 58 additions & 0 deletions README.md
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)
36 changes: 36 additions & 0 deletions composer.json
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"
}
}
}
24 changes: 24 additions & 0 deletions phpunit.xml
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>
8 changes: 8 additions & 0 deletions provides.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"providers": [
],
"aliases": [
{
}
]
}
15 changes: 15 additions & 0 deletions src/Enums/Roles.php
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;
}
35 changes: 35 additions & 0 deletions src/HasRole.php
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");
}
}
}
14 changes: 14 additions & 0 deletions src/IHasRole.php
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;
}
21 changes: 21 additions & 0 deletions src/Models/Role.php
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'
];
}

0 comments on commit 70db944

Please sign in to comment.