Skip to content

Commit

Permalink
fix: some command issue, and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Dec 25, 2018
1 parent a76f9b7 commit d1b0733
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
4 changes: 4 additions & 0 deletions helpers/permit.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ function allows($user, $permission, $params = [])
if (!function_exists('json_to_array')) {
function json_to_array($json)
{
if (is_array($json)) {
return $json;
}

$json_out = json_decode($json, true);
if (is_string($json_out) || is_null($json_out)) {
return [];
Expand Down
19 changes: 11 additions & 8 deletions src/Commands/FetchPermissionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,21 @@ public function handle()
*/
public function getUserPermissions()
{
$headers = ['Module', 'Ability', 'Permission'];
$headers = ['Ability', 'Permission'];

$user = $this->user->find($this->argument('needle'));
$data = [];
if ($user) {
$permissions = json_to_array($user->permissions);
$permissions = $user->abilities;

if (!is_array($permissions)) {
$permissions = [];
}

foreach ($permissions as $module=>$permission) {
$this->warn("\n" . strtoupper($module));
$data = [];
foreach ($permission as $ability=>$perm) {
$vals = [$module, $ability];
$vals = [$ability];
if (is_bool($perm)) {
if ($perm) {
$vals[] = 'true';
Expand All @@ -96,9 +97,9 @@ public function getUserPermissions()
}
$data[] = $vals;
}
$this->table($headers, $data);
}

$this->table($headers, $data);
} else {
$this->error("No user found!");
}
Expand All @@ -109,12 +110,11 @@ public function getUserPermissions()
*/
public function getRolePermissions()
{
$headers = ['Module', 'Ability', 'Permission'];
$headers = ['Ability', 'Permission'];

$role_name = $this->argument('needle');

$role = $this->permission->findBy('role_name', $role_name);
$data = [];
if ($role) {
$permissions = json_to_array($role->permission);

Expand All @@ -123,6 +123,9 @@ public function getRolePermissions()
}

foreach ($permissions as $module=>$permission) {
$this->warn("\n" . strtoupper($module));
$data = [];

foreach ($permission as $ability=>$perm) {
$vals = [$module, $ability];
if (is_bool($perm)) {
Expand All @@ -137,9 +140,9 @@ public function getRolePermissions()
}
$data[] = $vals;
}
$this->table($headers, $data);
}

$this->table($headers, $data);
} else {
$this->error("No role found!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Nahid\Permit\Permissions\PermissionRepository;
use Nahid\Permit\Users\UserRepository;

class AddPermissionCommand extends Command
class SetPermissionCommand extends Command
{

/**
Expand All @@ -29,7 +29,7 @@ class AddPermissionCommand extends Command
*
* @var string
*/
protected $signature = 'permit:add {type : two types 1. user 2. role} {needle : desire permission entity} {permission : permission name}';
protected $signature = 'permit:set {type : two types 1. user 2. role} {needle : desire permission entity} {permission : permission name}';

/**
* The console command description.
Expand Down Expand Up @@ -68,11 +68,11 @@ public function handle()
}

if ($type == 'user') {
return $this->addUserPermission();
return $this->setUserPermission();
}

if ($type == 'role') {
return $this->addRolePermission();
return $this->setRolePermission();
}

$this->error('Bad parameters');
Expand All @@ -97,30 +97,44 @@ protected function fetchPolicy($ability)
*
* @return bool
*/
public function addUserPermission()
public function setUserPermission()
{
$expected_values = ['true'=>true, 'false'=>false];
$user_id = $this->argument('needle');
$prm = $this->argument('permission');
$explode = explode('=', $prm);
$ability = true;

if (count($explode) == 2) {
$prm = $explode[0];
if (isset($expected_values[$explode[1]])) {
$ability = $expected_values[$explode[1]];
} else {
$ability = $this->fetchPolicy($explode[1]);
}

}

$prms = explode('.', $prm);
$abilities = config('permit.abilities');

$module = $prms[0];
$mod_perms = [];
if (isset($abilities[$module])) {
$mod_perms = $abilities[$module];
if (!isset($abilities[$module])) {
$this->error('No modules are defined in config');
return false;
}

$mod_perms = $abilities[$module];
$user = $this->user->find($user_id);

if ($user) {
$permission = json_to_array($user->permissions);
if (in_array($prms[1], $mod_perms)) {
$permission[$module][$prms[1]] = true;
} elseif (array_key_exists($prms[1], $mod_perms)) {
$policy = $this->fetchPolicy($mod_perms[$prms[1]]);
$permission[$module][$prms[1]] = $policy;
if (!in_array($prms[1], $mod_perms) && !array_key_exists($prms[1], $mod_perms)) {
$this->error('Please set this permission in config/permit.php first!');
return false;
}

$permission[$module][$prms[1]] = $ability;
$this->user->update($user->id, ['permissions' => json_encode($permission)]);
$this->info('Successfully added permission to user');
return true;
Expand All @@ -135,30 +149,44 @@ public function addUserPermission()
*
* @return bool
*/
public function addRolePermission()
public function setRolePermission()
{
$expected_values = ['true'=>true, 'false'=>false];
$role_name = $this->argument('needle');
$prm = $this->argument('permission');
$prms = explode('.', $prm);
$abilities = config('permit.abilities');
$explode = explode('=', $prm);
$ability = true;

if (count($explode) == 2) {
$prm = $explode[0];
if (isset($expected_values[$explode[1]])) {
$ability = $expected_values[$explode[1]];
} else {
$ability = $this->fetchPolicy($explode[1]);
}

}

$prms = explode('.', $prm);

$module = $prms[0];
$mod_perms = [];
if (isset($abilities[$module])) {
$mod_perms = $abilities[$module];
if (!isset($abilities[$module])) {
$this->error('No modules are defined in config');
return false;
}

$mod_perms = $abilities[$module];
$role = $this->permission->findBy('role_name', $role_name);

if ($role) {
$permission = json_to_array($role->permission);
if (in_array($prms[1], $mod_perms)) {
$permission[$module][$prms[1]] = true;
} elseif (array_key_exists($prms[1], $mod_perms)) {
$policy = $this->fetchPolicy($mod_perms[$prms[1]]);
$permission[$module][$prms[1]] = $policy;
if (!in_array($prms[1], $mod_perms) && !array_key_exists($prms[1], $mod_perms)) {
$this->error('Please set this permission in config/permit.php first!');
return false;
}

$permission[$module][$prms[1]] = $ability;
$this->permission->update($role->id, ['permission' => json_encode($permission)]);
$this->info('Successfully added permission to role');
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public function roleCan($user, $permission, $params = [])
public function allows($user, $permission, $params = [])
{
if ($user instanceof $this->userModelNamespace) {
if ($user->{$this->roleColumn} == $this->superUser) {
return true;
}

$this->abilities = $user->abilities;

if (count($this->abilities) > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/PermitServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function register()
$this->commands([
\Nahid\Permit\Commands\CreateRoleCommand::class,
\Nahid\Permit\Commands\PermissionSyncCommand::class,
\Nahid\Permit\Commands\AddPermissionCommand::class,
\Nahid\Permit\Commands\SetPermissionCommand::class,
\Nahid\Permit\Commands\FetchPermissionsCommand::class,
\Nahid\Permit\Commands\RemovePermissionCommand::class,
]);

$this->app['router']->aliasMiddleware('permit', \Nahid\Permit\Middleware\PermitMiddleware::class);
$this->app->routeMiddleware(['permit' => \Nahid\Permit\Middleware\PermitMiddleware::class]);
}
/**
* Setup the config.
Expand Down

0 comments on commit d1b0733

Please sign in to comment.