$subconfig]; } $newRules[$con] = [$roleId => $add]; } } Config::set('Auth', $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 * * @param mixed $roleId = 6 * @param mixed $process = NULL * @param mixed $object = NULL * @param mixed $function = NULL * @param mixed $realpath = NULL * * @return mixed */ protected static function common($roleId = 6, $process = NULL, $object = NULL, $function = NULL, $realpath = NULL) { self::$permission = self::getConfigByType($function); if( isset(self::$permission[$roleId]) ) { $rules = self::$permission[$roleId]; } else { return false; } if( $function === 'method' ) { $currentUrl = NULL; } else { $currentUrl = $process ?? $realpath ?? Base::currentPath(); } $object = $object ?? true; switch( $rules ?? NULL ) { case 'all': return $object; case 'any': return false; } $pages = current($rules); $type = key($rules); foreach( $pages as $page ) { $page = trim((string) $page); $rule = strpos((string) $page[0], '!') === 0 ? substr((string) $page, 1) : $page; // @codeCoverageIgnoreStart if( $type === 'perm' ) { if( self::control($currentUrl, $rule, $process, $function) ) { return $object; } else { self::$result = false; } } // @codeCoverageIgnoreEnd else { if( self::control($currentUrl, $rule, $process, $function) ) { return false; } else { self::$result = $object; } } } return self::$result; } /** * Control Permission * * @param string $currentUrl * @param string $page * @param string $process * @param string $function * * @return string */ protected static function control($currentUrl, $page, $process, $function) { if( $function === 'method' ) { return Method::$process($page); } return strpos(strtolower((string) $currentUrl), strtolower((string) $page)) > -1; } /** * Protected get config by type */ protected static function getConfigByType($type) { $config = Config::default('ZN\Authorization\AuthorizationDefaultConfiguration')::get('Auth'); if( $type === NULL ) { return $config; } return $config[$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); // @codeCoverageIgnore } } return false; // @codeCoverageIgnore } /** * 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('/(?