Skip to content

Commit

Permalink
Wildcard fix: matches empty string (reverts 33b9990 and cancels backw…
Browse files Browse the repository at this point in the history
…ards incompatibility)
  • Loading branch information
xfra35 committed Feb 18, 2016
1 parent 21c01b9 commit 7b62d1c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ Wildcards can be used at various places:
* equivalent to `$f3->allow('/','')`
* equivalent to `$f3->allow('/')`

**IMPORTANT**: following the framework convention and unlike usual filesystem wildcards,
the plugin wildcards do not match empty strings. So `/admin*` doesn't match `/admin` (at least 1 character is required).
**NB**: wildcards match empty strings, so `/admin*` match `/admin`.

Routes tokens are also supported, so `$f3->allow('/blog/@id/@slug')` is recognized.

Expand Down
14 changes: 12 additions & 2 deletions lib/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,19 @@ function granted($route,$subject='') {
$specific=isset($this->rules[$subject][$verb])?$this->rules[$subject][$verb]:array();
$global=isset($this->rules['*'][$verb])?$this->rules['*'][$verb]:array();
$rules=$specific+$global;//subject-specific rules have precedence over global rules
krsort($rules);//specific paths are processed first
//specific paths are processed first:
$paths=array();
foreach ($keys=array_keys($rules) as $key) {
$path=str_replace('@','*@',$key);
if (substr($path,-1)!='*')
$path.='+';
$paths[]=$path;
}
$vals=array_values($rules);
array_multisort($paths,SORT_DESC,$keys,$vals);
$rules=array_combine($keys,$vals);
foreach($rules as $path=>$rule)
if (preg_match('/^'.preg_replace('/@\w*/','[^\/]+',str_replace('\*','.+',preg_quote($path,'/'))).'$/',$uri))
if (preg_match('/^'.preg_replace('/@\w*/','[^\/]+',str_replace('\*','.*',preg_quote($path,'/'))).'$/',$uri))
return $rule;
return $this->policy==self::ALLOW;
}
Expand Down
11 changes: 7 additions & 4 deletions tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ function run($f3) {
$access->deny('/admin*');
$access->allow('/admin*','admin');
$test->expect(
!$access->granted('/admin/foo') && !$access->granted('/admin/foo/bar') &&
$access->granted('/admin/foo','admin') && $access->granted('/admin/foo/bar','admin'),
!$access->granted('/admin') && !$access->granted('/admin/foo/bar') &&
$access->granted('/admin','admin') && $access->granted('/admin/foo/bar','admin'),
'Wildcard suffix'
);
$access->deny('/*/edit');
Expand All @@ -72,9 +72,12 @@ function run($f3) {
!$access->granted('/blog/entry/edit') && $access->granted('/blog/entry/edit','admin'),
'Wildcard prefix'
);
$access->deny('/blog*','admin');
$access->allow('/admin');
$access->allow('/admin/special/path');
$test->expect(
!$access->granted('/blog/entry/edit','admin'),
$access->granted('/admin') && !$access->granted('/admin/foo/bar') &&
$access->granted('/admin','admin') && $access->granted('/admin/foo/bar','admin') &&
$access->granted('/admin/special/path') && $access->granted('/admin/special/path','admin'),
'Wildcard precedence order'
);
//Tokens
Expand Down

0 comments on commit 7b62d1c

Please sign in to comment.