PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.3
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
← All changes | src/UserGroup/DynamicUserGroup.php +84 -26 2.3.182.2.3 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * DynamicUserGroup.php
4 + *
5 + * The DynamicUserGroup class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\UserGroup;
@@ -7,69 +20,114 @@
7 20 use Exception;
8 21 use UserAccessManager\Config\MainConfig;
9 22 use UserAccessManager\Database\Database;
10 23 use UserAccessManager\Object\ObjectHandler;
24 +use UserAccessManager\Util\Util;
25 +use UserAccessManager\Wrapper\Php;
11 26 use UserAccessManager\Wrapper\Wordpress;
12 27
28 +/**
29 + * Class DynamicUserGroup
30 + *
31 + * @package UserAccessManager\UserGroup
32 + */
13 33 class DynamicUserGroup extends AbstractUserGroup
14 34 {
15 - public const USER_TYPE = 'user';
16 - public const ROLE_TYPE = 'role';
17 - public const NOT_LOGGED_IN_USER_ID = 0;
35 + const USER_TYPE = 'user';
36 + const ROLE_TYPE = 'role';
37 + const NOT_LOGGED_IN_USER_ID = 0;
18 38
19 39 /**
40 + * @var string
41 + */
42 + protected $type = null;
43 +
44 + /**
45 + * DynamicUserGroup constructor.
46 + * @param Php $php
47 + * @param Wordpress $wordpress
48 + * @param Database $database
49 + * @param MainConfig $config
50 + * @param Util $util
51 + * @param ObjectHandler $objectHandler
52 + * @param AssignmentInformationFactory $assignmentInformationFactory
53 + * @param string $type
54 + * @param int|string $id
20 55 * @throws UserGroupTypeException
21 56 */
22 57 public function __construct(
58 + Php $php,
23 59 Wordpress $wordpress,
24 60 Database $database,
25 61 MainConfig $config,
62 + Util $util,
26 63 ObjectHandler $objectHandler,
27 - AssignedObjectsLoader $assignedObjectsLoader,
28 - protected ?string $type,
29 - int|string $id
64 + AssignmentInformationFactory $assignmentInformationFactory,
65 + string $type,
66 + $id
30 67 ) {
31 - parent::__construct($wordpress, $database, $config, $objectHandler, $assignedObjectsLoader, $id);
68 + $this->type = $type;
32 69
33 - if ($this->type !== self::USER_TYPE && $this->type !== self::ROLE_TYPE) {
70 + parent::__construct(
71 + $php,
72 + $wordpress,
73 + $database,
74 + $config,
75 + $util,
76 + $objectHandler,
77 + $assignmentInformationFactory,
78 + $id
79 + );
80 +
81 + if ($type !== self::USER_TYPE && $type !== self::ROLE_TYPE) {
34 82 throw new UserGroupTypeException('Invalid dynamic group type.');
35 83 }
36 84 }
37 85
86 + /**
87 + * Returns the dynamic user group id.
88 + * @return string
89 + */
38 90 public function getId(): string
39 91 {
40 92 return $this->type . '|' . $this->id;
41 93 }
42 94
95 + /**
96 + * Returns the dynamic group name.
97 + * @return string
98 + */
43 99 public function getName(): string
44 100 {
45 - return $this->name ??= ($this->type === self::ROLE_TYPE) ? $this->createRoleName() : $this->createUserName();
46 - }
101 + if ($this->name === null) {
102 + $this->name = '';
47 103
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;
104 + if ($this->type === self::USER_TYPE && (int) $this->id === self::NOT_LOGGED_IN_USER_ID) {
105 + $this->name = TXT_UAM_ADD_DYNAMIC_NOT_LOGGED_IN_USERS;
106 + } elseif ($this->type === self::USER_TYPE) {
107 + $userData = $this->wordpress->getUserData($this->id);
108 + $this->name = TXT_UAM_USER . ": {$userData->display_name} ($userData->user_login)";
109 + } elseif ($this->type === self::ROLE_TYPE) {
110 + $roles = $this->wordpress->getRoles()->roles;
111 + $this->name = TXT_UAM_ROLE . ': ';
112 + $this->name .= (isset($roles[$this->id]['name']) === true) ? $roles[$this->id]['name'] : $this->id;
113 + }
59 114 }
60 115
61 - $userData = $this->wordpress->getUserData($this->id);
62 - $userName = ($userData !== false) ? "$userData->display_name ($userData->user_login)" : '';
63 -
64 - return TXT_UAM_USER . ": $userName";
116 + return $this->name;
65 117 }
66 118
67 119 /**
120 + * Checks if the user group is assigned to a user.
121 + * @param string $objectType
122 + * @param int|string $objectId
123 + * @param null $fromDate
124 + * @param null $toDate
125 + * @return bool
68 126 * @throws UserGroupAssignmentException
69 127 * @throws Exception
70 128 */
71 - public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool
129 + public function addObject(string $objectType, $objectId, $fromDate = null, $toDate = null): bool
72 130 {
73 131 if ($this->objectHandler->getGeneralObjectType($objectType) === ObjectHandler::GENERAL_USER_OBJECT_TYPE) {
74 132 throw new UserGroupAssignmentException('Dynamic user groups can\'t be assigned to user.');
75 133 }