PluginProbe
User Access Manager / 2.3.18
User Access Manager v2.3.18
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / UserGroup / DynamicUserGroup.php

DynamicUserGroup.php in User Access Manager 2.3.18, at src/UserGroup/DynamicUserGroup.php

80 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\UserGroup;
6
7 use Exception;
8 use UserAccessManager\Config\MainConfig;
9 use UserAccessManager\Database\Database;
10 use UserAccessManager\Object\ObjectHandler;
11 use UserAccessManager\Wrapper\Wordpress;
12
13 class DynamicUserGroup extends AbstractUserGroup
14 {
15 public const USER_TYPE = 'user';
16 public const ROLE_TYPE = 'role';
17 public const NOT_LOGGED_IN_USER_ID = 0;
18
19 /**
20 * @throws UserGroupTypeException
21 */
22 public function __construct(
23 Wordpress $wordpress,
24 Database $database,
25 MainConfig $config,
26 ObjectHandler $objectHandler,
27 AssignedObjectsLoader $assignedObjectsLoader,
28 protected ?string $type,
29 int|string $id
30 ) {
31 parent::__construct($wordpress, $database, $config, $objectHandler, $assignedObjectsLoader, $id);
32
33 if ($this->type !== self::USER_TYPE && $this->type !== self::ROLE_TYPE) {
34 throw new UserGroupTypeException('Invalid dynamic group type.');
35 }
36 }
37
38 public function getId(): string
39 {
40 return $this->type . '|' . $this->id;
41 }
42
43 public function getName(): string
44 {
45 return $this->name ??= ($this->type === self::ROLE_TYPE) ? $this->createRoleName() : $this->createUserName();
46 }
47
48 private function createRoleName(): string
49 {
50 $roles = $this->wordpress->getRoles()->roles;
51
52 return TXT_UAM_ROLE . ': ' . ($roles[$this->id]['name'] ?? $this->id);
53 }
54
55 private function createUserName(): string
56 {
57 if ((int) $this->id === self::NOT_LOGGED_IN_USER_ID) {
58 return TXT_UAM_ADD_DYNAMIC_NOT_LOGGED_IN_USERS;
59 }
60
61 $userData = $this->wordpress->getUserData($this->id);
62 $userName = ($userData !== false) ? "$userData->display_name ($userData->user_login)" : '';
63
64 return TXT_UAM_USER . ": $userName";
65 }
66
67 /**
68 * @throws UserGroupAssignmentException
69 * @throws Exception
70 */
71 public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool
72 {
73 if ($this->objectHandler->getGeneralObjectType($objectType) === ObjectHandler::GENERAL_USER_OBJECT_TYPE) {
74 throw new UserGroupAssignmentException('Dynamic user groups can\'t be assigned to user.');
75 }
76
77 return parent::addObject($objectType, $objectId, $fromDate, $toDate);
78 }
79 }
80