Skip to content

Commit adfeb98

Browse files
committed
is() now supports and/or operators, updated can/is methods
1 parent d0bc6d6 commit adfeb98

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

src/Kodeine/Acl/Helper/Helper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ protected function parseOperator($str)
5252

5353
/**
5454
* Converts strings having comma
55-
* or pipe to an array
55+
* or pipe to an array in
56+
* lowercase
5657
*
5758
* @param $str
5859
* @return array
@@ -61,9 +62,10 @@ protected function hasDelimiterToArray($str)
6162
{
6263
if ( is_string($str) && preg_match('/[,|]/is', $str) ) {
6364
$str = preg_split('/ ?[,|] ?/', $str);
65+
$str = array_filter($str, 'strtolower');
6466
}
6567

66-
return $str;
68+
return strtolower($str);
6769
}
6870

6971
/**

src/Kodeine/Acl/Middleware/HasPermission.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public function handle($request, Closure $next)
3636
{
3737
$this->request = $request;
3838

39-
if ( ( ! $this->getAction('is') or $this->hasRole())
40-
and ( ! $this->getAction('can') and ! $this->getAction('protect_alias')
41-
or $this->hasPermission())
39+
if ( ( ! $this->getAction('is') or $this->hasRole() ) and
40+
( ! $this->getAction('can') or $this->hasPermission() ) and
41+
( ! $this->getAction('protect_alias') or $this->protectMethods() )
4242
) {
4343
return $next($request);
4444
}
@@ -79,11 +79,6 @@ protected function hasPermission()
7979
$request = $this->request;
8080
$do = $this->getAction('can');
8181

82-
// if method protection is needed.
83-
if ( ! $do && $this->getAction('protect_alias') ) {
84-
return $this->protectMethods();
85-
}
86-
8782
return ! $this->forbiddenRoute() && $request->user()->can($do);
8883
}
8984

src/Kodeine/Acl/Traits/HasRole.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,28 @@ public function getRoles()
4040
* @param string $slug
4141
* @return bool
4242
*/
43-
public function is($slug)
43+
public function is($slug, $operator = null)
4444
{
45-
$slug = strtolower($slug);
45+
$operator = is_null(null) ? $this->parseOperator($slug) : $operator;
4646

47-
return in_array($slug, $this->getRoles());
47+
$roles = $this->getRoles();
48+
$slug = $this->hasDelimiterToArray($slug);
49+
50+
// array of slugs
51+
if ( is_array($slug) ) {
52+
53+
if ( ! in_array($operator, ['and', 'or']) ) {
54+
$e = 'Invalid operator, available operators are "and", "or".';
55+
throw new \InvalidArgumentException($e);
56+
}
57+
58+
$call = 'isWith' . ucwords($operator);
59+
60+
return $this->$call($slug, $roles);
61+
}
62+
63+
// single slug
64+
return in_array($slug, $roles);
4865
}
4966

5067
/**
@@ -121,6 +138,38 @@ public function revokeAllRoles()
121138
|
122139
*/
123140

141+
/**
142+
* @param $slug
143+
* @param $roles
144+
* @return bool
145+
*/
146+
protected function isWithAnd($slug, $roles)
147+
{
148+
foreach ($slug as $check) {
149+
if ( ! in_array($check, $roles) ) {
150+
return false;
151+
}
152+
}
153+
154+
return true;
155+
}
156+
157+
/**
158+
* @param $slug
159+
* @param $roles
160+
* @return bool
161+
*/
162+
protected function isWithOr($slug, $roles)
163+
{
164+
foreach ($slug as $check) {
165+
if ( in_array($check, $roles) ) {
166+
return true;
167+
}
168+
}
169+
170+
return false;
171+
}
172+
124173
/**
125174
* Parses role id from object, array
126175
* or a string.
@@ -168,15 +217,16 @@ protected function parseRoleId($role)
168217
public function __call($method, $arguments = [])
169218
{
170219
// Handle isRoleSlug() methods
171-
if ( starts_with($method, 'is') and $method !== 'is' ) {
220+
if ( starts_with($method, 'is') and $method !== 'is' and ! starts_with($method, 'isWith') ) {
172221
$role = substr($method, 2);
173222

174223
return $this->is($role);
175224
}
176225

177226
// Handle canDoSomething() methods
178-
if ( starts_with($method, 'can') and $method !== 'can' ) {
227+
if ( starts_with($method, 'can') and $method !== 'can' and ! starts_with($method, 'canWith') ) {
179228
$permission = substr($method, 3);
229+
$permission = snake_case($permission, '.');
180230

181231
return $this->can($permission);
182232
}

0 commit comments

Comments
 (0)