| 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 |
namespace UserAccessManager\UserGroup; |
| 16 |
|
| 17 |
use UserAccessManager\Config\MainConfig; |
| 18 |
use UserAccessManager\Database\Database; |
| 19 |
use UserAccessManager\Object\ObjectHandler; |
| 20 |
use UserAccessManager\Util\Util; |
| 21 |
use UserAccessManager\Wrapper\Php; |
| 22 |
use UserAccessManager\Wrapper\Wordpress; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class DynamicUserGroup |
| 26 |
* |
| 27 |
* @package UserAccessManager\UserGroup |
| 28 |
*/ |
| 29 |
class DynamicUserGroup extends AbstractUserGroup |
| 30 |
{ |
| 31 |
const USER_TYPE = 'user'; |
| 32 |
const ROLE_TYPE = 'role'; |
| 33 |
const NOT_LOGGED_IN_USER_ID = 0; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $type = null; |
| 39 |
|
| 40 |
/** |
| 41 |
* DynamicUserGroup constructor. |
| 42 |
* |
| 43 |
* @param Php $php |
| 44 |
* @param Wordpress $wordpress |
| 45 |
* @param Database $database |
| 46 |
* @param MainConfig $config |
| 47 |
* @param Util $util |
| 48 |
* @param ObjectHandler $objectHandler |
| 49 |
* @param AssignmentInformationFactory $assignmentInformationFactory |
| 50 |
* @param string $type |
| 51 |
* @param string $id |
| 52 |
* |
| 53 |
* @throws UserGroupTypeException |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
Php $php, |
| 57 |
Wordpress $wordpress, |
| 58 |
Database $database, |
| 59 |
MainConfig $config, |
| 60 |
Util $util, |
| 61 |
ObjectHandler $objectHandler, |
| 62 |
AssignmentInformationFactory $assignmentInformationFactory, |
| 63 |
$type, |
| 64 |
$id |
| 65 |
) { |
| 66 |
$this->type = $type; |
| 67 |
|
| 68 |
parent::__construct( |
| 69 |
$php, |
| 70 |
$wordpress, |
| 71 |
$database, |
| 72 |
$config, |
| 73 |
$util, |
| 74 |
$objectHandler, |
| 75 |
$assignmentInformationFactory, |
| 76 |
$id |
| 77 |
); |
| 78 |
|
| 79 |
if ($type !== self::USER_TYPE && $type !== self::ROLE_TYPE) { |
| 80 |
throw new UserGroupTypeException('Invalid dynamic group type.'); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the dynamic user group id. |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function getId() |
| 90 |
{ |
| 91 |
return $this->type.'|'.$this->id; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the dynamic group name. |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function getName() |
| 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 |
$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 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return $this->name; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Checks if the user group is assigned to a user. |
| 121 |
* |
| 122 |
* @param string $objectType |
| 123 |
* @param string $objectId |
| 124 |
* @param string $fromDate |
| 125 |
* @param string $toDate |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
* |
| 129 |
* @throws UserGroupAssignmentException |
| 130 |
*/ |
| 131 |
public function addObject($objectType, $objectId, $fromDate = null, $toDate = null) |
| 132 |
{ |
| 133 |
if ($this->objectHandler->getGeneralObjectType($objectType) === ObjectHandler::GENERAL_USER_OBJECT_TYPE) { |
| 134 |
throw new UserGroupAssignmentException('Dynamic user groups can\'t be assigned to user.'); |
| 135 |
} |
| 136 |
|
| 137 |
return parent::addObject($objectType, $objectId, $fromDate, $toDate); |
| 138 |
} |
| 139 |
} |
| 140 |
|