Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean code #255

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Kodeine/Acl/Helper/FieldException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Kodeine\Acl\Helper;

use Exception;

class FieldException extends Exception
{
protected $_field;
public function __construct($message="", $codeStatus=0,$status, Exception $previous=NULL, $field = NULL)
{
$this->_field = $field;
parent::__construct($message, $codeStatus,$status , $previous);
}
public function getField()
{
return $this->_field;
}
}
86 changes: 56 additions & 30 deletions src/Kodeine/Acl/Middleware/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kodeine\Acl\Middleware;

use Closure;
use Kodeine\Acl\Helper\FieldException;

class HasPermission
{
Expand Down Expand Up @@ -40,28 +41,53 @@ public function handle($request, Closure $next)
{
$this->request = $request;

// override crud resources via config
$this->crudConfigOverride();
$this->crudConfigOverrideViaConfig();


// if route has access
if (( ! $this->getAction('is') or $this->hasRole()) and
( ! $this->getAction('can') or $this->hasPermission()) and
( ! $this->getAction('protect_alias') or $this->protectMethods())
) {
return $next($request);
try {
$this->authoriztionChacker($request , $next);

} catch (\Throwable $th) {
return $this->makeResponseWithException($th->message,$th->status,$th->codeStatus);
}


if ( $request->isJson() || $request->wantsJson() ) {
return response()->json([
'error' => [
'status_code' => 401,
'code' => 'INSUFFICIENT_PERMISSIONS',
'description' => 'You are not authorized to access this resource.'
],
], 401);
}
return $this->IfRouteHasAccess($request,$next);

return abort(401, 'You are not authorized to access this resource.');

}

public function makeResponseWithException(string $description ,string $status, int $statusCode)
{
return response()->json([
'error' => [
'status_code' => $statusCode,
'code' => $status,
'description' => $description
],
], $statusCode);
}

public function authoriztionChacker($request)
{

if ($request->isJson() || $request->wantsJson()) {
throw new FieldException("You are not authorized to access this resource.",'INSUFFICIENT_PERMISSIONS', 401 );

}
}


public function IfRouteHasAccess($request,Closure $next)
{
// if route has access
if ((!$this->getAction('is') or $this->hasRole()) and
(!$this->getAction('can') or $this->hasPermission()) and
(!$this->getAction('protect_alias') or $this->protectMethods())
) {
return $next($request);
}
}

/**
Expand All @@ -74,7 +100,7 @@ protected function hasRole()
$request = $this->request;
$role = $this->getAction('is');

return ! $this->forbiddenRoute() && $request->user()->hasRole($role);
return !$this->forbiddenRoute() && $request->user()->hasRole($role);
}

/**
Expand All @@ -87,7 +113,7 @@ protected function hasPermission()
$request = $this->request;
$do = $this->getAction('can');

return ! $this->forbiddenRoute() && $request->user()->hasPermission($do);
return !$this->forbiddenRoute() && $request->user()->hasPermission($do);
}

/**
Expand Down Expand Up @@ -115,8 +141,7 @@ protected function protectMethods()
// and does not have methods like
// UserController@index but only
// UserController we use crud restful.
$methods = is_array($methods) ? $methods :
(in_array($caller, $resources) ?
$methods = is_array($methods) ? $methods : (in_array($caller, $resources) ?
$this->crud['resource'] : $this->crud['restful']);

// determine crud method we're trying to protect
Expand All @@ -133,10 +158,11 @@ protected function protectMethods()
return $e . '.' . $this->parseAlias();
}, array_keys($crud)));

return ! $this->forbiddenRoute() && $request->user()->hasPermission($permission);
return !$this->forbiddenRoute() && $request->user()->hasPermission($permission);
}

private function filterMethods($methods, $callback) {

private function filterMethods($methods, $callback)
{
$filtered = [];

foreach ($methods as $key => $value) {
Expand Down Expand Up @@ -181,7 +207,7 @@ protected function getAction($key)
*/
protected function parseAlias()
{
if ( $alias = $this->getAction('protect_alias') ) {
if ($alias = $this->getAction('protect_alias')) {
return $alias;
}

Expand All @@ -205,9 +231,9 @@ protected function parseMethod()
$action = $this->request->route()->getActionName();

// parse index, store, create etc
if ( preg_match('/@([^\s].+)$/is', $action, $m) ) {
if (preg_match('/@([^\s].+)$/is', $action, $m)) {
$controller = $m[1];
if ( $controller != 'Closure' ) {
if ($controller != 'Closure') {
return $controller;
}
}
Expand All @@ -218,15 +244,15 @@ protected function parseMethod()
/**
* Override crud property via config.
*/
protected function crudConfigOverride()
protected function crudConfigOverrideViaConfig()
{
// Override crud restful from config.
if ( ($restful = config('acl.crud.restful')) != null ) {
if (($restful = config('acl.crud.restful')) != null) {
$this->crud['restful'] = $restful;
}

// Override crud resource from config.
if ( ($resource = config('acl.crud.resource')) != null ) {
if (($resource = config('acl.crud.resource')) != null) {
$this->crud['resource'] = $resource;
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Models/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function itHasRelationships()
$this->assertInstanceOf(Permission::class, $permission);
}

/** @test */
/** @test */
public function itCanCreate()
{
$attributes = [
Expand Down Expand Up @@ -109,6 +109,9 @@ public function itCanUpdate()
$this->assertDatabaseHas('roles', $role->toArray());
}




/** @test */
public function itCanDelete()
{
Expand Down