123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace Symfony\Component\Security\Acl\Permission;
- class MaskBuilder extends AbstractMaskBuilder
- {
- const MASK_VIEW = 1;
- const MASK_CREATE = 2;
- const MASK_EDIT = 4;
- const MASK_DELETE = 8;
- const MASK_UNDELETE = 16;
- const MASK_OPERATOR = 32;
- const MASK_MASTER = 64;
- const MASK_OWNER = 128;
- const MASK_IDDQD = 1073741823;
- const CODE_VIEW = 'V';
- const CODE_CREATE = 'C';
- const CODE_EDIT = 'E';
- const CODE_DELETE = 'D';
- const CODE_UNDELETE = 'U';
- const CODE_OPERATOR = 'O';
- const CODE_MASTER = 'M';
- const CODE_OWNER = 'N';
- const ALL_OFF = '................................';
- const OFF = '.';
- const ON = '*';
-
- public function getPattern()
- {
- $pattern = self::ALL_OFF;
- $length = strlen($pattern);
- $bitmask = str_pad(decbin($this->mask), $length, '0', STR_PAD_LEFT);
- for ($i = $length - 1; $i >= 0; --$i) {
- if ('1' === $bitmask[$i]) {
- try {
- $pattern[$i] = self::getCode(1 << ($length - $i - 1));
- } catch (\Exception $e) {
- $pattern[$i] = self::ON;
- }
- }
- }
- return $pattern;
- }
-
- public static function getCode($mask)
- {
- if (!is_int($mask)) {
- throw new \InvalidArgumentException('$mask must be an integer.');
- }
- $reflection = new \ReflectionClass(get_called_class());
- foreach ($reflection->getConstants() as $name => $cMask) {
- if (0 !== strpos($name, 'MASK_') || $mask !== $cMask) {
- continue;
- }
- if (!defined($cName = 'static::CODE_'.substr($name, 5))) {
- throw new \RuntimeException('There was no code defined for this mask.');
- }
- return constant($cName);
- }
- throw new \InvalidArgumentException(sprintf('The mask "%d" is not supported.', $mask));
- }
-
- public function resolveMask($code)
- {
- if (is_string($code)) {
- if (!defined($name = sprintf('static::MASK_%s', strtoupper($code)))) {
- throw new \InvalidArgumentException(sprintf('The code "%s" is not supported', $code));
- }
- return constant($name);
- }
- if (!is_int($code)) {
- throw new \InvalidArgumentException('$code must be an integer.');
- }
- return $code;
- }
- }
|