Skip to content

Commit

Permalink
Ready to use
Browse files Browse the repository at this point in the history
  • Loading branch information
farhoudi committed Nov 6, 2020
1 parent f3a9afd commit dba0583
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
26 changes: 3 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
/vendor/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
.env
Homestead.yaml
Homestead.json
/.vagrant
.phpunit.result.cache
/.idea
/vendor
composer.lock
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"license": "MIT",
"require": {
"php": ">=5.5.9",
"illuminate/support": "5.2.*"
"php": ">=5.6.0",
"laravel/framework": ">=5.3.0"
},
"autoload": {
"psr-4": {
Expand Down
40 changes: 40 additions & 0 deletions src/Middleware/HasPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Farhoudi\Rbac\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class HasPermission
{

protected $auth;

/**
* Creates a new instance of the middleware.
*
* @param Guard $auth
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param $permissions
* @return mixed
*/
public function handle($request, Closure $next, $permissions)
{
if ($this->auth->guest() || !$request->user()->hasPermission(explode('|', $permissions))) {
return redirect()->route('login');
}

return $next($request);
}

}
40 changes: 40 additions & 0 deletions src/Middleware/HasRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Farhoudi\Rbac\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class HasRole
{

protected $auth;

/**
* Creates a new instance of the middleware.
*
* @param Guard $auth
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param $roles
* @return mixed
*/
public function handle($request, Closure $next, $roles)
{
if ($this->auth->guest() || !$request->user()->hasRole(explode('|', $roles))) {
return redirect()->route('login');
}

return $next($request);
}

}
24 changes: 14 additions & 10 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
/**
* Trait Rbac
*/
trait Rbac {
trait Rbac
{

private $rbacIsLoaded = false;

Expand All @@ -20,7 +21,8 @@ trait Rbac {
* @param string|array $role
* @return bool
*/
public function hasRole($role) {
public function hasRole($role)
{
if (!$this->rbacIsLoaded) {
$this->loadPermissions();
$this->rbacIsLoaded = true;
Expand Down Expand Up @@ -48,7 +50,8 @@ public function hasRole($role) {
* @param string|array $permission
* @return bool
*/
public function hasPermission($permission) {
public function hasPermission($permission)
{
if (!$this->rbacIsLoaded) {
$this->loadPermissions();
$this->rbacIsLoaded = true;
Expand Down Expand Up @@ -77,7 +80,8 @@ public function hasPermission($permission) {
*
* @param string|Role $role
*/
public function assignRole($role) {
public function assignRole($role)
{
if (is_array($role)) {
foreach ($role as $item) {
$this->assignRole($item);
Expand All @@ -97,12 +101,11 @@ public function assignRole($role) {
}
}

if ($this->rbacIsLoaded) {
$this->loadPermissions();
}
$this->loadPermissions();
}

public function loadPermissions() {
public function loadPermissions()
{
$this->roles = $this->roles()->with(['permissions'])->get();
$this->permissions = new Collection();
foreach ($this->roles as $key => $role) {
Expand All @@ -116,8 +119,9 @@ public function loadPermissions() {
return $this->permissions;
}

public function roles() {
public function roles()
{
return $this->belongsToMany(Role::class, 'rbac_role_user', 'user_id', 'role_id');
}

}
}

0 comments on commit dba0583

Please sign in to comment.