Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit c73aae2

Browse files
committed
improved MaskBuilder
1 parent f33c6a8 commit c73aae2

File tree

5 files changed

+174
-74
lines changed

5 files changed

+174
-74
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Acl\Permission;
13+
14+
/**
15+
* This abstract class implements nearly all the MaskBuilderInterface methods
16+
*/
17+
abstract class AbstractMaskBuilder implements MaskBuilderInterface
18+
{
19+
/**
20+
* @var int
21+
*/
22+
protected $mask;
23+
24+
/**
25+
* Constructor.
26+
*
27+
* @param int $mask optional; defaults to 0
28+
*/
29+
public function __construct($mask = 0)
30+
{
31+
$this->set($mask);
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function set($mask)
38+
{
39+
if (!is_int($mask)) {
40+
throw new \InvalidArgumentException('$mask must be an integer.');
41+
}
42+
43+
$this->mask = $mask;
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function get()
52+
{
53+
return $this->mask;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function add($mask)
60+
{
61+
$this->mask |= $this->resolveMask($mask);
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function remove($mask)
70+
{
71+
$this->mask &= ~$this->resolveMask($mask);
72+
73+
return $this;
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function reset()
80+
{
81+
$this->mask = 0;
82+
83+
return $this;
84+
}
85+
}

Acl/Permission/BasicPermissionMap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ public function contains($permission)
105105
{
106106
return isset($this->map[$permission]);
107107
}
108+
109+
/**
110+
* Returns a new instance of the MaskBuilder used in the permissionMap
111+
*
112+
* @return MaskBuilderInterface
113+
*/
114+
public function getMaskBuilder()
115+
{
116+
return new MaskBuilder();
117+
}
108118
}

Acl/Permission/MaskBuilder.php

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
* @author Johannes M. Schmitt <[email protected]>
4444
*/
45-
class MaskBuilder
45+
class MaskBuilder extends AbstractMaskBuilder
4646
{
4747
const MASK_VIEW = 1; // 1 << 0
4848
const MASK_CREATE = 2; // 1 << 1
@@ -67,50 +67,6 @@ class MaskBuilder
6767
const OFF = '.';
6868
const ON = '*';
6969

70-
private $mask;
71-
72-
/**
73-
* Constructor.
74-
*
75-
* @param int $mask optional; defaults to 0
76-
*
77-
* @throws \InvalidArgumentException
78-
*/
79-
public function __construct($mask = 0)
80-
{
81-
if (!is_int($mask)) {
82-
throw new \InvalidArgumentException('$mask must be an integer.');
83-
}
84-
85-
$this->mask = $mask;
86-
}
87-
88-
/**
89-
* Adds a mask to the permission.
90-
*
91-
* @param mixed $mask
92-
*
93-
* @return MaskBuilder
94-
*
95-
* @throws \InvalidArgumentException
96-
*/
97-
public function add($mask)
98-
{
99-
$this->mask |= $this->getMask($mask);
100-
101-
return $this;
102-
}
103-
104-
/**
105-
* Returns the mask of this permission.
106-
*
107-
* @return int
108-
*/
109-
public function get()
110-
{
111-
return $this->mask;
112-
}
113-
11470
/**
11571
* Returns a human-readable representation of the permission.
11672
*
@@ -135,34 +91,6 @@ public function getPattern()
13591
return $pattern;
13692
}
13793

138-
/**
139-
* Removes a mask from the permission.
140-
*
141-
* @param mixed $mask
142-
*
143-
* @return MaskBuilder
144-
*
145-
* @throws \InvalidArgumentException
146-
*/
147-
public function remove($mask)
148-
{
149-
$this->mask &= ~$this->getMask($mask);
150-
151-
return $this;
152-
}
153-
154-
/**
155-
* Resets the PermissionBuilder.
156-
*
157-
* @return MaskBuilder
158-
*/
159-
public function reset()
160-
{
161-
$this->mask = 0;
162-
163-
return $this;
164-
}
165-
16694
/**
16795
* Returns the code for the passed mask.
16896
*
@@ -204,7 +132,7 @@ public static function getCode($mask)
204132
*
205133
* @throws \InvalidArgumentException
206134
*/
207-
private function getMask($code)
135+
public function resolveMask($code)
208136
{
209137
if (is_string($code)) {
210138
if (!defined($name = sprintf('static::MASK_%s', strtoupper($code)))) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Acl\Permission;
13+
14+
/**
15+
* This is the interface that must be implemented by mask builders.
16+
*/
17+
interface MaskBuilderInterface
18+
{
19+
/**
20+
* Set the mask of this permission
21+
*
22+
* @param int $mask
23+
*
24+
* @return MaskBuilderInterface
25+
* @throws \InvalidArgumentException if $mask is not an integer
26+
*/
27+
public function set($mask);
28+
29+
/**
30+
* Returns the mask of this permission.
31+
*
32+
* @return int
33+
*/
34+
public function get();
35+
36+
/**
37+
* Adds a mask to the permission.
38+
*
39+
* @param mixed $mask
40+
*
41+
* @return MaskBuilderInterface
42+
*
43+
* @throws \InvalidArgumentException
44+
*/
45+
public function add($mask);
46+
47+
/**
48+
* Removes a mask from the permission.
49+
*
50+
* @param mixed $mask
51+
*
52+
* @return MaskBuilderInterface
53+
*
54+
* @throws \InvalidArgumentException
55+
*/
56+
public function remove($mask);
57+
58+
/**
59+
* Resets the PermissionBuilder.
60+
*
61+
* @return MaskBuilderInterface
62+
*/
63+
public function reset();
64+
65+
/**
66+
* Returns the mask for the passed code
67+
*
68+
* @param mixed $code
69+
*
70+
* @return int
71+
*
72+
* @throws \InvalidArgumentException
73+
*/
74+
public function resolveMask($code);
75+
}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* added LogoutUrlGenerator
88
* added the triggering of the `Symfony\Component\Security\Http\SecurityEvents::INTERACTIVE_LOGIN` in `Symfony\Component\Security\Http\Firewall\SimplePreAuthenticationListener`
9+
* The MaskBuilder logic has been abstracted in the `Symfony\Component\Security\Acl\Permission\AbstractMaskBuilder`
10+
and described in the `Symfony\Component\Security\Acl\Permission\MaskBuilderInterface`
911

1012
2.6.0
1113
-----

0 commit comments

Comments
 (0)