Skip to content

Commit

Permalink
Revert "fix code injection error"
Browse files Browse the repository at this point in the history
This reverts commit 89ade3c.
  • Loading branch information
makladuxbert committed Sep 9, 2017
1 parent 89ade3c commit e993f11
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 175 deletions.
4 changes: 1 addition & 3 deletions src/Commands/CreatePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Illuminate\Console\Command;
use Maklad\Permission\Contracts\PermissionInterface as Permission;
use Maklad\Permission\Helpers;

/**
* Class CreatePermission
Expand All @@ -21,8 +20,7 @@ class CreatePermission extends Command

public function handle()
{
$helpers = new Helpers();
$permissionClass = $helpers->app(Permission::class);
$permissionClass = \app(Permission::class);

$permission = $permissionClass::create([
'name' => $this->argument('name'),
Expand Down
4 changes: 1 addition & 3 deletions src/Commands/CreateRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Illuminate\Console\Command;
use Maklad\Permission\Contracts\RoleInterface as Role;
use Maklad\Permission\Helpers;

/**
* Class CreateRole
Expand All @@ -21,8 +20,7 @@ class CreateRole extends Command

public function handle()
{
$helpers = new Helpers();
$roleClass = $helpers->app(Role::class);
$roleClass = \app(Role::class);

$role = $roleClass::create([
'name' => $this->argument('name'),
Expand Down
7 changes: 2 additions & 5 deletions src/Exceptions/MakladException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Maklad\Permission\Helpers;
use Throwable;

/**
Expand All @@ -19,7 +18,6 @@
*/
class MakladException extends InvalidArgumentException
{
protected $helpers;
/**
* MakladException constructor.
*
Expand All @@ -29,11 +27,10 @@ class MakladException extends InvalidArgumentException
*/
public function __construct($message = '', $code = 0, Throwable $previous = null)
{
$this->helpers = new Helpers();
parent::__construct($message, $code, $previous);

if ($this->helpers->config('permission.log_registration_exception')) {
$logger = $this->helpers->app('log');
if (\config('permission.log_registration_exception')) {
$logger = \app('log');
$logger->alert($message);
}
}
Expand Down
55 changes: 4 additions & 51 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Maklad\Permission;

use Illuminate\Container\Container;
use Illuminate\Support\Collection;

/**
Expand All @@ -19,10 +18,10 @@ class Helpers
*/
public function getModelForGuard(string $guard)
{
$guards = new Collection($this->config('auth.guards'));
return $guards->map(function ($guard) {
return $this->config("auth.providers.{$guard['provider']}.model");
})->get($guard);
return \collect(\config('auth.guards'))
->map(function ($guard) {
return \config("auth.providers.{$guard['provider']}.model");
})->get($guard);
}

/**
Expand Down Expand Up @@ -80,50 +79,4 @@ public function getRoleDoesNotExistMessage(string $name, string $guardName): str
{
return "There is no role named `{$name}` for guard `{$guardName}`.";
}

/**
* @param null|string|array $key
* @param null $default
*
* @return mixed|static
*/
public function config($key = null, $default = null)
{
if (null === $key) {
return $this->app('config');
}

if (\is_array($key)) {
return $this->app('config')->set($key);
}

return $this->app('config')->get($key, $default);
}

/**
* @param null|string|array $abstract
* @param array $parameters
*
* @return mixed|static
*/
public function app($abstract = null, array $parameters = [])
{
if (null === $abstract) {
return Container::getInstance();
}

return empty($parameters)
? Container::getInstance()->make($abstract)
: Container::getInstance()->makeWith($abstract, $parameters);
}

/**
* @param $code
* @param string $message
* @param array $headers
*/
public function abort($code, $message = '', array $headers = [])
{
$this->app()->abort($code, $message, $headers);
}
}
6 changes: 2 additions & 4 deletions src/Middlewares/PermissionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Closure;
use Illuminate\Support\Facades\Auth;
use Maklad\Permission\Helpers;

/**
* Class PermissionMiddleware
Expand All @@ -22,16 +21,15 @@ class PermissionMiddleware
*/
public function handle($request, Closure $next, $permission)
{
$helpers = new Helpers();
if (Auth::guest()) {
$helpers->abort(403);
\abort(403);
}

$permissions = \is_array($permission) ? $permission : \explode('|', $permission);


if (! Auth::user()->hasAnyPermission($permissions)) {
$helpers->abort(403);
\abort(403);
}

return $next($request);
Expand Down
6 changes: 2 additions & 4 deletions src/Middlewares/RoleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Closure;
use Illuminate\Support\Facades\Auth;
use Maklad\Permission\Helpers;

/**
* Class RoleMiddleware
Expand All @@ -22,15 +21,14 @@ class RoleMiddleware
*/
public function handle($request, Closure $next, $role)
{
$helpers = new Helpers();
if (Auth::guest()) {
$helpers->abort(403);
\abort(403);
}

$roles = \is_array($role) ? $role : \explode('|', $role);

if (! Auth::user()->hasAnyRole($roles)) {
$helpers->abort(403);
\abort(403);
}

return $next($request);
Expand Down
22 changes: 11 additions & 11 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class Permission extends Model implements PermissionInterface
*/
public function __construct(array $attributes = [])
{
$this->helpers = new Helpers();
$attributes['guard_name'] = $attributes['guard_name'] ?? $this->helpers->config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

parent::__construct($attributes);

$this->setTable($this->helpers->config('permission.table_names.permissions'));
$this->helpers = new Helpers();

$this->setTable(\config('permission.table_names.permissions'));
}

/**
Expand All @@ -48,15 +49,15 @@ public function __construct(array $attributes = [])
*/
public static function create(array $attributes = [])
{
$helpers = new Helpers();
$attributes['guard_name'] = $attributes['guard_name'] ?? $helpers->config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

if (static::getPermissions()->where('name', $attributes['name'])->where(
'guard_name',
$attributes['guard_name']
)->first()) {
$name = $attributes['name'];
$guardName = $attributes['guard_name'];
$helpers = new Helpers();
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName));
}

Expand All @@ -70,8 +71,8 @@ public static function create(array $attributes = [])
public function roles(): BelongsToMany
{
return $this->belongsToMany(
$this->helpers->config('permission.models.role'),
$this->helpers->config('permission.table_names.role_has_permissions')
\config('permission.models.role'),
\config('permission.table_names.role_has_permissions')
);
}

Expand All @@ -95,12 +96,12 @@ public function users(): BelongsToMany
*/
public static function findByName(string $name, $guardName = null): PermissionInterface
{
$helpers = new Helpers();
$guardName = $guardName ?? $helpers->config('auth.defaults.guard');
$guardName = $guardName ?? \config('auth.defaults.guard');

$permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();

if (! $permission) {
$helpers = new Helpers();
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName));
}

Expand All @@ -113,7 +114,6 @@ public static function findByName(string $name, $guardName = null): PermissionIn
*/
protected static function getPermissions(): Collection
{
$helpers = new Helpers();
return $helpers->app(PermissionRegistrar::class)->getPermissions();
return \app(PermissionRegistrar::class)->getPermissions();
}
}
26 changes: 13 additions & 13 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class Role extends Model implements RoleInterface
*/
public function __construct(array $attributes = [])
{
$this->helpers = new Helpers();
$attributes['guard_name'] = $attributes['guard_name'] ?? $this->helpers->config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

parent::__construct($attributes);

$this->setTable($this->helpers->config('permission.table_names.roles'));
$this->helpers = new Helpers();

$this->setTable(\config('permission.table_names.roles'));
}

/**
Expand All @@ -50,13 +51,12 @@ public function __construct(array $attributes = [])
*/
public static function create(array $attributes = [])
{
$helpers = new Helpers();

$attributes['guard_name'] = $attributes['guard_name'] ?? $helpers->config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
$name = $attributes['name'];
$name = $attributes['name'];
$guardName = $attributes['guard_name'];
$helpers = new Helpers();
throw new RoleAlreadyExists($helpers->getRoleAlreadyExistsMessage($name, $guardName));
}

Expand All @@ -70,8 +70,8 @@ public static function create(array $attributes = [])
public function permissions(): BelongsToMany
{
return $this->belongsToMany(
$this->helpers->config('permission.models.permission'),
$this->helpers->config('permission.table_names.role_has_permissions')
\config('permission.models.permission'),
\config('permission.table_names.role_has_permissions')
);
}

Expand All @@ -94,12 +94,12 @@ public function users(): BelongsToMany
*/
public static function findByName(string $name, $guardName = null): RoleInterface
{
$helpers = new Helpers();
$guardName = $guardName ?? $helpers->config('auth.defaults.guard');
$guardName = $guardName ?? \config('auth.defaults.guard');

$role = static::where('name', $name)->where('guard_name', $guardName)->first();

if (! $role) {
$helpers = new Helpers();
throw new RoleDoesNotExist($helpers->getRoleDoesNotExistMessage($name, $guardName));
}

Expand All @@ -118,12 +118,12 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
public function hasPermissionTo($permission): bool
{
if (\is_string($permission)) {
$permission = $this->helpers->app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
$permission = \app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
}

if (! $this->getGuardNames()->contains($permission->guard_name)) {
$expected = $this->getGuardNames();
$given = $permission->guard_name;
$given = $permission->guard_name;

throw new GuardDoesNotMatch($this->helpers->getGuardDoesNotMatchMessage($expected, $given));
}
Expand Down
13 changes: 2 additions & 11 deletions src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ class PermissionRegistrar
/** @var string */
protected $cacheKey = 'maklad.permission.cache';

protected $helpers;

public function __construct(Gate $gate, Repository $cache)
{
$this->gate = $gate;
$this->cache = $cache;
$this->helpers = new Helpers();
}

public function registerPermissions(): bool
Expand All @@ -49,16 +46,10 @@ public function forgetCachedPermissions()
$this->cache->forget($this->cacheKey);
}

/**
* Get permissions
*
* @return Collection
*/
public function getPermissions(): Collection
{
$expirationTime = $this->helpers->config('permission.cache_expiration_time');
return $this->cache->remember($this->cacheKey, $expirationTime, function () {
return $this->helpers->app(Permission::class)->with('roles')->get();
return $this->cache->remember($this->cacheKey, \config('permission.cache_expiration_time'), function () {
return \app(Permission::class)->with('roles')->get();
});
}
}
Loading

0 comments on commit e993f11

Please sign in to comment.