Skip to content

Commit

Permalink
feat: add LoadPolicyArray() to load policy from array (#102)
Browse files Browse the repository at this point in the history
* feat: add LoadPolicyArray() to load policy from array

* fix: use str_getcsv() to read the csv string to support the characters wrapped in quotation marks
  • Loading branch information
leeqvip committed Sep 8, 2021
1 parent f0052ee commit c414268
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Persist/AdapterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ public function loadPolicyLine(string $line, Model $model): void
return;
}

$tokens = explode(', ', $line);
$key = $tokens[0];
$tokens = array_map("trim", str_getcsv($line));

$this->loadPolicyArray($tokens, $model);
}

/**
* Loads a policy rule to model.
*
* @param array $rule
* @param Model $model
*/
public function loadPolicyArray(array $rule, Model $model): void
{
$key = $rule[0];
$sec = $key[0];

if (!isset($model[$sec][$key])) {
Expand All @@ -44,7 +56,8 @@ public function loadPolicyLine(string $line, Model $model): void
if (!($assertion instanceof Assertion)) {
return;
}
$rule = \array_slice($tokens, 1);

$rule = \array_slice($rule, 1);
$assertion->policy[] = $rule;
$assertion->policyMap[implode(Policy::DEFAULT_SEP, $rule)] = count($assertion->policy) - 1;

Expand Down

0 comments on commit c414268

Please sign in to comment.