Spaces:
No application file
No application file
File size: 17,486 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
<?php
namespace Mautic\CoreBundle\Security\Permissions;
use Mautic\UserBundle\Form\Type\PermissionListType;
use Symfony\Component\Form\FormBuilderInterface;
abstract class AbstractPermissions
{
/**
* @var array
*/
protected $permissions = [];
public function __construct(
protected array $params
) {
}
/**
* This method is called before the permissions object is used.
* Define permissions with `addExtendedPermissions` here instead of constructor.
*/
public function definePermissions(): void
{
// Override this method in the final class
}
/**
* Returns bundle's permissions array.
*
* @return array
*/
public function getPermissions()
{
return $this->permissions;
}
/**
* Checks to see if the requested permission is supported by the bundle.
*
* @param string $name
* @param string $level
*
* @return bool
*/
public function isSupported($name, $level = '')
{
[$name, $level] = $this->getSynonym($name, $level);
if (empty($level)) {
// verify permission name only
return isset($this->permissions[$name]);
}
// verify permission name and level as well
return isset($this->permissions[$name][$level]);
}
/**
* Allows permission classes to be disabled if criteria is not met (such as bundle is disabled).
*/
public function isEnabled(): bool
{
return true;
}
/**
* Returns the value assigned to a specific permission.
*
* @param string $name
* @param string $perm
*
* @return int
*/
public function getValue($name, $perm)
{
return ($this->isSupported($name, $perm)) ? $this->permissions[$name][$perm] : 0;
}
/**
* Builds the bundle's specific form elements for its permissions.
*/
public function buildForm(FormBuilderInterface &$builder, array $options, array $data): void
{
}
/**
* Returns the name of the permission set (should be the bundle identifier).
*
* @return string|void
*/
abstract public function getName();
/**
* Takes an array from PermissionRepository::getPermissionsByRole() and converts the bitwise integers to an array
* of permission names that can be used in forms, for example.
*
* @return mixed
*/
public function convertBitsToPermissionNames(array $permissions)
{
static $permissionLevels = [];
$bundle = $this->getName();
if (!in_array($bundle, $permissionLevels)) {
$permissionLevels[$bundle] = [];
if (isset($permissions[$bundle])) {
if ($this->isEnabled()) {
foreach ($permissions[$bundle] as $details) {
$permName = $details['name'];
$permBitwise = $details['bitwise'];
// ensure the permission still exists
if ($this->isSupported($permName)) {
$levels = $this->permissions[$permName];
// ensure that at least keys exist
$permissionLevels[$bundle][$permName] = [];
// $permissionLevels[$bundle][$permName]["$bundle:$permName"] = $permId;
foreach ($levels as $levelName => $levelBit) {
// compare bit against levels to see if it is a match
if ($levelBit & $permBitwise) {
// bitwise compares so add the level
$permissionLevels[$bundle][$permName][] = $levelName;
continue;
}
}
}
}
}
}
}
return $permissionLevels[$bundle];
}
/**
* Allows the bundle permission class to utilize synonyms for permissions.
*
* @param string $name
* @param string $level
*
* @return array
*/
protected function getSynonym($name, $level)
{
if (in_array($level, ['viewown', 'viewother'])) {
if (isset($this->permissions[$name]['view'])) {
$level = 'view';
}
} elseif ('view' == $level) {
if (isset($this->permissions[$name]['viewown'])) {
$level = 'viewown';
}
} elseif (in_array($level, ['editown', 'editother'])) {
if (isset($this->permissions[$name]['edit'])) {
$level = 'edit';
}
} elseif ('edit' == $level) {
if (isset($this->permissions[$name]['editown'])) {
$level = 'editown';
}
} elseif (in_array($level, ['deleteown', 'deleteother'])) {
if (isset($this->permissions[$name]['delete'])) {
$level = 'delete';
}
} elseif ('delete' == $level) {
if (isset($this->permissions[$name]['deleteown'])) {
$level = 'deleteown';
}
} elseif (in_array($level, ['publishown', 'publishother'])) {
if (isset($this->permissions[$name]['publish'])) {
$level = 'publish';
}
} elseif ('publish' == $level) {
if (isset($this->permissions[$name]['publishown'])) {
$level = 'publishown';
}
}
return [$name, $level];
}
/**
* Determines if the user has access to the specified permission.
*
* @param array $userPermissions
* @param string $name
* @param string $level
*
* @return bool
*/
public function isGranted($userPermissions, $name, $level)
{
[$name, $level] = $this->getSynonym($name, $level);
if (!isset($userPermissions[$name])) {
// the user doesn't have implicit access
return false;
} elseif (isset($this->permissions[$name]['full']) && $this->permissions[$name]['full'] & $userPermissions[$name]) {
return true;
} else {
// otherwise test for specific level
$result = ($this->permissions[$name][$level] & $userPermissions[$name]);
return ($result) ? true : false;
}
}
/**
* @param bool $isSecondRound
*
* @return bool Return true if a second round is required after all other bundles have analyzed it's permissions
*/
public function analyzePermissions(array &$permissions, $allPermissions, $isSecondRound = false): bool
{
$hasViewAccess = false;
foreach ($permissions as $level => &$perms) {
foreach ($perms as $perm) {
$required = [];
switch ($perm) {
case 'editother':
case 'edit':
$required = ['viewother', 'viewown'];
break;
case 'deleteother':
case 'delete':
$required = ['editother', 'viewother', 'viewown'];
break;
case 'publishother':
case 'publish':
$required = ['viewother', 'viewown'];
break;
case 'viewother':
case 'editown':
case 'deleteown':
case 'publishown':
case 'create':
$required = ['viewown'];
break;
}
foreach ($required as $r) {
[$ignore, $r] = $this->getSynonym($level, $r);
if ($this->isSupported($level, $r) && !in_array($r, $perms)) {
$perms[] = $r;
}
}
}
$hasViewAccess = (!$hasViewAccess && (in_array('view', $perms) || in_array('viewown', $perms)));
}
// check categories for view permissions and add it if the user has view access to the other permissions
if (isset($this->permissions['categories']) && $hasViewAccess && (!isset($permissions['categories']) || !in_array('view', $permissions['categories']))) {
$permissions['categories'][] = 'view';
}
return false;
}
/**
* Generates an array of granted and total permissions.
*
* @return array
*/
public function getPermissionRatio(array $data)
{
$totalAvailable = $totalGranted = 0;
foreach ($this->permissions as $level => $perms) {
$perms = array_keys($perms);
$totalAvailable += count($perms);
if (in_array('full', $perms)) {
if (1 === count($perms)) {
// full is the only permission so count as 1
if (!empty($data[$level]) && !in_array('full', $data[$level])) {
++$totalGranted;
}
} else {
// remove full from total count
--$totalAvailable;
if (!empty($data[$level]) && in_array('full', $data[$level])) {
// user has full access so sum perms minus full
$totalGranted += count($perms) - 1;
// move on to the next level
continue;
}
}
}
if (isset($data[$level])) {
$totalGranted += count($data[$level]);
}
}
return [$totalGranted, $totalAvailable];
}
/**
* Gives the bundle an opportunity to change how JavaScript calculates permissions granted.
*/
public function parseForJavascript(array &$perms): void
{
}
/**
* @param array<int|string> $permissions
*/
protected function addCustomPermission(string $level, array $permissions): void
{
$this->permissions[$level] = $permissions;
}
/**
* Adds a custom permission to the form builder, i.e. config only bundles.
*
* @param array<string> $choices
* @param array<string> $data
*/
protected function addCustomFormFields(string $bundle, string $level, FormBuilderInterface &$builder, string $label, array $choices, array $data): void
{
$builder->add("$bundle:$level", PermissionListType::class, [
'choices' => $choices,
'label' => $label,
'data' => (!empty($data[$level]) ? $data[$level] : []),
'bundle' => $bundle,
'level' => $level,
]);
}
/**
* Adds the standard permission set of view, edit, create, delete, publish and full.
*
* @param array $permissionNames
* @param bool $includePublish
*/
protected function addStandardPermissions($permissionNames, $includePublish = true)
{
if (!is_array($permissionNames)) {
$permissionNames = [$permissionNames];
}
foreach ($permissionNames as $p) {
$this->permissions[$p] = [
'view' => 4,
'edit' => 16,
'create' => 32,
'delete' => 128,
'full' => 1024,
];
if ($includePublish) {
$this->permissions[$p]['publish'] = 512;
}
}
}
/**
* Adds the standard permission set of view, edit, create, delete, publish and full to the form builder.
*
* @param string $bundle
* @param string $level
* @param FormBuilderInterface $builder
* @param array $data
* @param bool $includePublish
*/
protected function addStandardFormFields($bundle, $level, &$builder, $data, $includePublish = true)
{
$choices = [
'mautic.core.permissions.view' => 'view',
'mautic.core.permissions.edit' => 'edit',
'mautic.core.permissions.create' => 'create',
'mautic.core.permissions.delete' => 'delete',
];
if ($includePublish) {
$choices['mautic.core.permissions.publish'] = 'publish';
}
$choices['mautic.core.permissions.full'] = 'full';
$label = ('categories' == $level) ? 'mautic.category.permissions.categories' : "mautic.$bundle.permissions.$level";
$builder->add(
"$bundle:$level",
PermissionListType::class,
[
'choices' => $choices,
'label' => $label,
'bundle' => $bundle,
'level' => $level,
'data' => (!empty($data[$level]) ? $data[$level] : []),
]
);
}
/**
* @param string $bundle
* @param string $level
*
* @return string
*/
protected function getLabel($bundle, $level)
{
return ('categories' === $level) ? 'mautic.category.permissions.categories' : "mautic.{$bundle}.permissions.{$level}";
}
/**
* Add a single full permission.
*
* @param array $permissionNames
*/
protected function addManagePermission($permissionNames)
{
if (!is_array($permissionNames)) {
$permissionNames = [$permissionNames];
}
foreach ($permissionNames as $p) {
$this->permissions[$p] = [
'manage' => 1024,
];
}
}
/**
* Adds a single full permission to the form builder, i.e. config only bundles.
*
* @param string $bundle
* @param string $level
* @param FormBuilderInterface $builder
* @param array $data
*/
protected function addManageFormFields($bundle, $level, &$builder, $data)
{
$choices = [
'mautic.core.permissions.manage' => 'manage',
];
$builder->add(
"$bundle:$level",
PermissionListType::class,
[
'choices' => $choices,
'label' => "mautic.$bundle.permissions.$level",
'data' => (!empty($data[$level]) ? $data[$level] : []),
'bundle' => $bundle,
'level' => $level,
]
);
}
/**
* Adds the standard permission set of viewown, viewother, editown, editother, create, deleteown, deleteother,
* publishown, publishother and full.
*
* @param array|string $permissionNames
* @param bool $includePublish
*/
protected function addExtendedPermissions($permissionNames, $includePublish = true)
{
if (!is_array($permissionNames)) {
$permissionNames = [$permissionNames];
}
foreach ($permissionNames as $p) {
$this->permissions[$p] = [
'viewown' => 2,
'viewother' => 4,
'editown' => 8,
'editother' => 16,
'create' => 32,
'deleteown' => 64,
'deleteother' => 128,
'full' => 1024,
];
if ($includePublish) {
$this->permissions[$p]['publishown'] = 256;
$this->permissions[$p]['publishother'] = 512;
}
}
}
/**
* Adds the standard permission set of viewown, viewother, editown, editother, create, deleteown, deleteother,
* publishown, publishother and full to the form builder.
*
* @param string $bundle
* @param string $level
* @param FormBuilderInterface $builder
* @param array $data
* @param bool $includePublish
*/
protected function addExtendedFormFields($bundle, $level, &$builder, $data, $includePublish = true)
{
$choices = [
'mautic.core.permissions.viewown' => 'viewown',
'mautic.core.permissions.viewother' => 'viewother',
'mautic.core.permissions.editown' => 'editown',
'mautic.core.permissions.editother' => 'editother',
'mautic.core.permissions.create' => 'create',
'mautic.core.permissions.deleteown' => 'deleteown',
'mautic.core.permissions.deleteother' => 'deleteother',
'mautic.core.permissions.full' => 'full',
];
if ($includePublish) {
$choices['mautic.core.permissions.publishown'] = 'publishown';
$choices['mautic.core.permissions.publishother'] = 'publishother';
}
$builder->add(
"$bundle:$level",
PermissionListType::class,
[
'choices' => $choices,
'choices_as_values' => true,
'label' => $this->getLabel($bundle, $level),
'data' => (!empty($data[$level]) ? $data[$level] : []),
'bundle' => $bundle,
'level' => $level,
]
);
}
}
|