Skip to content

Commit

Permalink
Updated Permission Library
Browse files Browse the repository at this point in the history
  • Loading branch information
ozanuykun committed Apr 25, 2018
1 parent 920a6cb commit 451f74d
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 135 deletions.
40 changes: 27 additions & 13 deletions Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,68 @@
* @author Ozan UYKUN [ozan@znframework.com]
*/

use ZN\Response;

class Method extends PermissionExtends
{
/**
* Post
*
* @param mixed $roleId = 6
* @param mixed $roleId = NULL
* @param array $table = NULL
* @param mixed $callback = NULL
*
* @return bool
*/
public static function post($roleId = 6) : Bool
public static function post($roleId = NULL, Array $table = NULL, $callback = NULL) : Bool
{
return self::use($roleId, __FUNCTION__);
return self::use($roleId, __FUNCTION__, $table, $callback);
}

/**
* Get
*
* @param mixed $roleId = 6
* @param mixed $roleId = NULL
* @param array $table = NULL
* @param mixed $callback = NULL
*
* @return bool
*/
public static function get($roleId = 6) : Bool
public static function get($roleId = NULL, Array $table = NULL, $callback = NULL) : Bool
{
return self::use($roleId, __FUNCTION__);
return self::use($roleId, __FUNCTION__, $table, $callback);
}

/**
* Request
*
* @param mixed $roleId = 6
*
* @param mixed $roleId = NULL
* @param array $table = NULL
* @param mixed $callback = NULL
* @return bool
*/
public static function request($roleId = 6) : Bool
public static function request($roleId = NULL, Array $table = NULL, $callback = NULL) : Bool
{
return self::use($roleId, __FUNCTION__);
return self::use($roleId, __FUNCTION__, $table, $callback);
}

/**
* Method
*
* @param mixed $roleId = 6
* @param string $method = 'post'
* @param mixed $roleId = NULL
* @param string $method = 'post'
* @param array $table = NULL
* @param mixed $callback = NULL
*
* @return bool
*/
public static function use($roleId = 6, $method = 'post') : Bool
public static function use($roleId = NULL, $method = 'post', Array $table = NULL, $callback = NULL) : Bool
{
if( $roleId !== NULL && $table !== NULL )
{
return self::predefinedPermissionConfiguration($roleId, $table, $callback, 'method', [$roleId, $method, NULL, 'method']);
}

return self::common(self::$roleId ?? $roleId, $method, NULL, 'method');
}
}
13 changes: 11 additions & 2 deletions Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@
* @author Ozan UYKUN [ozan@znframework.com]
*/

use ZN\Response;

class Page extends PermissionExtends
{
/**
* Page
*
* @param mixed $roleId = 6
* @param mixed $roleId = NULL
* @param array $table = NULL
* @param mixed $callback = NULL
*
* @return mixed
*/
public static function use($roleId = 6)
public static function use($roleId = NULL, Array $table = NULL, $callback = NULL)
{
if( $roleId !== NULL && $table !== NULL )
{
return self::predefinedPermissionConfiguration($roleId, $table, $callback, 'page', [$roleId, NULL, NULL, 'page']);
}

return self::common(self::$roleId ?? $roleId, NULL, NULL, 'page');
}
}
135 changes: 135 additions & 0 deletions PermissionExtends.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

use ZN\Base;
use ZN\Config;
use ZN\Response;
use ZN\Singleton;
use ZN\Request\Method;
use ZN\Protection\Json;

class PermissionExtends
{
Expand Down Expand Up @@ -55,6 +58,84 @@ public static function roleId($roleId)
self::$roleId = $roleId;
}

/**
* Get permission rules
*
* @param string $type = 'page'
* @param int|string $ruleId = NULL
*
* @return array|string
*/
public static function getPermRules(String $type = 'page', $roleId = NULL)
{
return self::getNopermRules($type, $roleId, 'perm');
}

/**
* Get no permission rules
*
* @param string $type = 'page'
* @param int|string $ruleId = NULL
*
* @return array|string
*/
public static function getNopermRules(String $type = 'page', $roleId = NULL, $ptype = 'noperm')
{
$rules = self::getConfigByType($type);

$roleId = $roleId ?? self::$roleId;

if( is_array($return = ($rules[$roleId] ?? NULL)) )
{
return $return[$ptype];
}

return $return;
}

/**
* Set permission rules
*
* @param array $config
* @param int|string $ruleId = NULL
*
* @return array|string
*/
protected static function setPermRules(Array $config, $roleId = NULL, $ptype = 'perm')
{
$roleId = $roleId ?? self::$roleId;
$configs = array_keys(self::getConfigByType(NULL));
$newRules = [];

foreach( $configs as $con )
{
if( $subconfig = ($config[$con] ?? NULL) )
{
if( is_string($subconfig) )
{
self::getJsonDataToDatabaseAfterConvertArray($subconfig, $roleId);
}

$newRules[$con] = [$roleId => [$ptype => $subconfig]];
}
}

Config::set('Authorization', $newRules);
}

/**
* Set no permission rules
*
* @param array $config
* @param int|string $ruleId = NULL
*
* @return array|string
*/
protected static function setNopermRules(Array $config, $roleId = NULL)
{
self::setPermRules($config, $roleId, 'noperm');
}

/**
* Permission Common
*
Expand Down Expand Up @@ -158,4 +239,58 @@ protected static function getConfigByType($type)
{
return Config::default('ZN\Authorization\AuthorizationDefaultConfiguration')::get('Authorization', $type);
}

/**
* Protected predefined Permission Configuration
*/
protected static function predefinedPermissionConfiguration($roleId, $table, $callback, $ctype, $code)
{
self::selectSetPermissionType($roleId, $table, $ctype);

if( ! self::common(...$code) )
{
if( is_callable($callback) )
{
return $callback();
}
else
{
Response::redirect($callback);
}
}
}

/**
* Protected select set permission type
*/
protected static function selectSetPermissionType($roleId, $table, $ctype)
{
self::roleId($roleId);

$type = key($table);
$rules = current($table);
$func = $type === 'perm' ? 'setPermRules' : 'setNopermRules';

self::$func([$ctype => $rules]);
}

/**
* Protected get json data to databse after convert array
*/
protected static function getJsonDataToDatabaseAfterConvertArray(&$subconfig, $roleId)
{
if( preg_match('/(\w+)\[(\w+)\]\:(\w+)/', $subconfig, $match) )
{
$json = Singleton::class('ZN\Database\DB')
->where($match[2], $roleId)
->select($match[3])
->get($match[1])
->value();

if( Json::check($json) )
{
$subconfig = json_decode($json);
}
}
}
}
17 changes: 12 additions & 5 deletions Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,20 @@ public static function end()
*/
public static function use($roleId = 0, $process = NULL, $object = NULL)
{
if( self::$roleId !== NULL )
if( is_array($process) )
{
$object = $process;
$process = $roleId;
$roleId = self::$roleId;
return self::selectSetPermissionType($roleId, $process, 'process');
}

else
{
if( self::$roleId !== NULL )
{
$object = $process;
$process = $roleId;
$roleId = self::$roleId;
}
}

return self::common($roleId, $process, $object, 'process');
}
}
Loading

0 comments on commit 451f74d

Please sign in to comment.