PluginProbe
User Access Manager / 2.2.24
User Access Manager v2.2.24
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.2.24, at src/UserGroup/DynamicUserGroup.php

139 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\UserGroup;
19
20 use Exception;
21 use UserAccessManager\Config\MainConfig;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\Object\ObjectHandler;
24 use UserAccessManager\Util\Util;
25 use UserAccessManager\Wrapper\Php;
26 use UserAccessManager\Wrapper\Wordpress;
27
28 /**
29 * Class DynamicUserGroup
30 *
31 * @package UserAccessManager\UserGroup
32 */
33 class DynamicUserGroup extends AbstractUserGroup
34 {
35 const USER_TYPE = 'user';
36 const ROLE_TYPE = 'role';
37 const NOT_LOGGED_IN_USER_ID = 0;
38
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
55 * @throws UserGroupTypeException
56 */
57 public function __construct(
58 Php $php,
59 Wordpress $wordpress,
60 Database $database,
61 MainConfig $config,
62 Util $util,
63 ObjectHandler $objectHandler,
64 AssignmentInformationFactory $assignmentInformationFactory,
65 string $type,
66 $id
67 ) {
68 $this->type = $type;
69
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) {
82 throw new UserGroupTypeException('Invalid dynamic group type.');
83 }
84 }
85
86 /**
87 * Returns the dynamic user group id.
88 * @return string
89 */
90 public function getId(): string
91 {
92 return $this->type . '|' . $this->id;
93 }
94
95 /**
96 * Returns the dynamic group name.
97 * @return string
98 */
99 public function getName(): string
100 {
101 if ($this->name === null) {
102 $this->name = '';
103
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 $userName = $userData !== false ? "$userData->display_name ($userData->user_login)" : '';
109 $this->name = TXT_UAM_USER . ": $userName";
110 } elseif ($this->type === self::ROLE_TYPE) {
111 $roles = $this->wordpress->getRoles()->roles;
112 $this->name = TXT_UAM_ROLE . ': ';
113 $this->name .= (isset($roles[$this->id]['name']) === true) ? $roles[$this->id]['name'] : $this->id;
114 }
115 }
116
117 return $this->name;
118 }
119
120 /**
121 * Checks if the user group is assigned to a user.
122 * @param string $objectType
123 * @param int|string $objectId
124 * @param null $fromDate
125 * @param null $toDate
126 * @return bool
127 * @throws UserGroupAssignmentException
128 * @throws Exception
129 */
130 public function addObject(string $objectType, $objectId, $fromDate = null, $toDate = null): bool
131 {
132 if ($this->objectHandler->getGeneralObjectType($objectType) === ObjectHandler::GENERAL_USER_OBJECT_TYPE) {
133 throw new UserGroupAssignmentException('Dynamic user groups can\'t be assigned to user.');
134 }
135
136 return parent::addObject($objectType, $objectId, $fromDate, $toDate);
137 }
138 }
139