@@ -30,8 +30,8 @@ |
||
| 30 | 30 | use OCP\IConfig; |
| 31 | 31 | |
| 32 | 32 | class UUIDFixGroup extends UUIDFix { |
| 33 | - public function __construct(GroupMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) { |
|
| 34 | - $this->mapper = $mapper; |
|
| 35 | - $this->proxy = new User_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config); |
|
| 36 | - } |
|
| 33 | + public function __construct(GroupMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) { |
|
| 34 | + $this->mapper = $mapper; |
|
| 35 | + $this->proxy = new User_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -30,8 +30,8 @@ |
||
| 30 | 30 | use OCP\IConfig; |
| 31 | 31 | |
| 32 | 32 | class UUIDFixUser extends UUIDFix { |
| 33 | - public function __construct(UserMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) { |
|
| 34 | - $this->mapper = $mapper; |
|
| 35 | - $this->proxy = new Group_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config); |
|
| 36 | - } |
|
| 33 | + public function __construct(UserMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) { |
|
| 34 | + $this->mapper = $mapper; |
|
| 35 | + $this->proxy = new Group_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -30,31 +30,31 @@ |
||
| 30 | 30 | use OCA\User_LDAP\User_Proxy; |
| 31 | 31 | |
| 32 | 32 | abstract class UUIDFix extends QueuedJob { |
| 33 | - /** @var AbstractMapping */ |
|
| 34 | - protected $mapper; |
|
| 33 | + /** @var AbstractMapping */ |
|
| 34 | + protected $mapper; |
|
| 35 | 35 | |
| 36 | - /** @var Proxy */ |
|
| 37 | - protected $proxy; |
|
| 36 | + /** @var Proxy */ |
|
| 37 | + protected $proxy; |
|
| 38 | 38 | |
| 39 | - public function run($argument) { |
|
| 40 | - $isUser = $this->proxy instanceof User_Proxy; |
|
| 41 | - foreach($argument['records'] as $record) { |
|
| 42 | - $access = $this->proxy->getLDAPAccess($record['name']); |
|
| 43 | - $uuid = $access->getUUID($record['dn'], $isUser); |
|
| 44 | - if($uuid === false) { |
|
| 45 | - // record not found, no prob, continue with the next |
|
| 46 | - continue; |
|
| 47 | - } |
|
| 48 | - if($uuid !== $record['uuid']) { |
|
| 49 | - $this->mapper->setUUIDbyDN($uuid, $record['dn']); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - } |
|
| 39 | + public function run($argument) { |
|
| 40 | + $isUser = $this->proxy instanceof User_Proxy; |
|
| 41 | + foreach($argument['records'] as $record) { |
|
| 42 | + $access = $this->proxy->getLDAPAccess($record['name']); |
|
| 43 | + $uuid = $access->getUUID($record['dn'], $isUser); |
|
| 44 | + if($uuid === false) { |
|
| 45 | + // record not found, no prob, continue with the next |
|
| 46 | + continue; |
|
| 47 | + } |
|
| 48 | + if($uuid !== $record['uuid']) { |
|
| 49 | + $this->mapper->setUUIDbyDN($uuid, $record['dn']); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @param Proxy $proxy |
|
| 56 | - */ |
|
| 57 | - public function overrideProxy(Proxy $proxy) { |
|
| 58 | - $this->proxy = $proxy; |
|
| 59 | - } |
|
| 54 | + /** |
|
| 55 | + * @param Proxy $proxy |
|
| 56 | + */ |
|
| 57 | + public function overrideProxy(Proxy $proxy) { |
|
| 58 | + $this->proxy = $proxy; |
|
| 59 | + } |
|
| 60 | 60 | } |
@@ -38,14 +38,14 @@ |
||
| 38 | 38 | |
| 39 | 39 | public function run($argument) { |
| 40 | 40 | $isUser = $this->proxy instanceof User_Proxy; |
| 41 | - foreach($argument['records'] as $record) { |
|
| 41 | + foreach ($argument['records'] as $record) { |
|
| 42 | 42 | $access = $this->proxy->getLDAPAccess($record['name']); |
| 43 | 43 | $uuid = $access->getUUID($record['dn'], $isUser); |
| 44 | - if($uuid === false) { |
|
| 44 | + if ($uuid === false) { |
|
| 45 | 45 | // record not found, no prob, continue with the next |
| 46 | 46 | continue; |
| 47 | 47 | } |
| 48 | - if($uuid !== $record['uuid']) { |
|
| 48 | + if ($uuid !== $record['uuid']) { |
|
| 49 | 49 | $this->mapper->setUUIDbyDN($uuid, $record['dn']); |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -40,874 +40,874 @@ |
||
| 40 | 40 | use OC\Cache\CappedMemoryCache; |
| 41 | 41 | |
| 42 | 42 | class Group_LDAP extends BackendUtility implements \OCP\GroupInterface { |
| 43 | - protected $enabled = false; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var string[] $cachedGroupMembers array of users with gid as key |
|
| 47 | - */ |
|
| 48 | - protected $cachedGroupMembers; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
| 52 | - */ |
|
| 53 | - protected $cachedGroupsByMember; |
|
| 54 | - |
|
| 55 | - public function __construct(Access $access) { |
|
| 56 | - parent::__construct($access); |
|
| 57 | - $filter = $this->access->connection->ldapGroupFilter; |
|
| 58 | - $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
| 59 | - if(!empty($filter) && !empty($gassoc)) { |
|
| 60 | - $this->enabled = true; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - $this->cachedGroupMembers = new CappedMemoryCache(); |
|
| 64 | - $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * is user in group? |
|
| 69 | - * @param string $uid uid of the user |
|
| 70 | - * @param string $gid gid of the group |
|
| 71 | - * @return bool |
|
| 72 | - * |
|
| 73 | - * Checks whether the user is member of a group or not. |
|
| 74 | - */ |
|
| 75 | - public function inGroup($uid, $gid) { |
|
| 76 | - if(!$this->enabled) { |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
| 80 | - $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
| 81 | - if(!is_null($inGroup)) { |
|
| 82 | - return (bool)$inGroup; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $userDN = $this->access->username2dn($uid); |
|
| 86 | - |
|
| 87 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
| 88 | - $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
| 89 | - return $isInGroup; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
| 93 | - $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
| 94 | - if(!is_null($members)) { |
|
| 95 | - $this->cachedGroupMembers[$gid] = $members; |
|
| 96 | - $isInGroup = in_array($userDN, $members); |
|
| 97 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 98 | - return $isInGroup; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 102 | - // just in case |
|
| 103 | - if(!$groupDN || !$userDN) { |
|
| 104 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - //check primary group first |
|
| 109 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 110 | - $this->access->connection->writeToCache($cacheKey, true); |
|
| 111 | - return true; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
| 115 | - $members = $this->_groupMembers($groupDN); |
|
| 116 | - $members = array_keys($members); // uids are returned as keys |
|
| 117 | - if(!is_array($members) || count($members) === 0) { |
|
| 118 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - //extra work if we don't get back user DNs |
|
| 123 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 124 | - $dns = array(); |
|
| 125 | - $filterParts = array(); |
|
| 126 | - $bytes = 0; |
|
| 127 | - foreach($members as $mid) { |
|
| 128 | - $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
| 129 | - $filterParts[] = $filter; |
|
| 130 | - $bytes += strlen($filter); |
|
| 131 | - if($bytes >= 9000000) { |
|
| 132 | - // AD has a default input buffer of 10 MB, we do not want |
|
| 133 | - // to take even the chance to exceed it |
|
| 134 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 135 | - $bytes = 0; |
|
| 136 | - $filterParts = array(); |
|
| 137 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 138 | - $dns = array_merge($dns, $users); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - if(count($filterParts) > 0) { |
|
| 142 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 143 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 144 | - $dns = array_merge($dns, $users); |
|
| 145 | - } |
|
| 146 | - $members = $dns; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $isInGroup = in_array($userDN, $members); |
|
| 150 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 151 | - $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
| 152 | - $this->cachedGroupMembers[$gid] = $members; |
|
| 153 | - |
|
| 154 | - return $isInGroup; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @param string $dnGroup |
|
| 159 | - * @return array |
|
| 160 | - * |
|
| 161 | - * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
| 162 | - * that match the search url otherwise returns an empty array. |
|
| 163 | - */ |
|
| 164 | - public function getDynamicGroupMembers($dnGroup) { |
|
| 165 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 166 | - |
|
| 167 | - if (empty($dynamicGroupMemberURL)) { |
|
| 168 | - return array(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $dynamicMembers = array(); |
|
| 172 | - $memberURLs = $this->access->readAttribute( |
|
| 173 | - $dnGroup, |
|
| 174 | - $dynamicGroupMemberURL, |
|
| 175 | - $this->access->connection->ldapGroupFilter |
|
| 176 | - ); |
|
| 177 | - if ($memberURLs !== false) { |
|
| 178 | - // this group has the 'memberURL' attribute so this is a dynamic group |
|
| 179 | - // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
| 180 | - // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
| 181 | - $pos = strpos($memberURLs[0], '('); |
|
| 182 | - if ($pos !== false) { |
|
| 183 | - $memberUrlFilter = substr($memberURLs[0], $pos); |
|
| 184 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 185 | - $dynamicMembers = array(); |
|
| 186 | - foreach($foundMembers as $value) { |
|
| 187 | - $dynamicMembers[$value['dn'][0]] = 1; |
|
| 188 | - } |
|
| 189 | - } else { |
|
| 190 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 191 | - 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - return $dynamicMembers; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param string $dnGroup |
|
| 199 | - * @param array|null &$seen |
|
| 200 | - * @return array|mixed|null |
|
| 201 | - */ |
|
| 202 | - private function _groupMembers($dnGroup, &$seen = null) { |
|
| 203 | - if ($seen === null) { |
|
| 204 | - $seen = array(); |
|
| 205 | - } |
|
| 206 | - $allMembers = array(); |
|
| 207 | - if (array_key_exists($dnGroup, $seen)) { |
|
| 208 | - // avoid loops |
|
| 209 | - return array(); |
|
| 210 | - } |
|
| 211 | - // used extensively in cron job, caching makes sense for nested groups |
|
| 212 | - $cacheKey = '_groupMembers'.$dnGroup; |
|
| 213 | - $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
| 214 | - if(!is_null($groupMembers)) { |
|
| 215 | - return $groupMembers; |
|
| 216 | - } |
|
| 217 | - $seen[$dnGroup] = 1; |
|
| 218 | - $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr, |
|
| 219 | - $this->access->connection->ldapGroupFilter); |
|
| 220 | - if (is_array($members)) { |
|
| 221 | - foreach ($members as $memberDN) { |
|
| 222 | - $allMembers[$memberDN] = 1; |
|
| 223 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 224 | - if (!empty($nestedGroups)) { |
|
| 225 | - $subMembers = $this->_groupMembers($memberDN, $seen); |
|
| 226 | - if ($subMembers) { |
|
| 227 | - $allMembers = array_merge($allMembers, $subMembers); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 43 | + protected $enabled = false; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var string[] $cachedGroupMembers array of users with gid as key |
|
| 47 | + */ |
|
| 48 | + protected $cachedGroupMembers; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
| 52 | + */ |
|
| 53 | + protected $cachedGroupsByMember; |
|
| 54 | + |
|
| 55 | + public function __construct(Access $access) { |
|
| 56 | + parent::__construct($access); |
|
| 57 | + $filter = $this->access->connection->ldapGroupFilter; |
|
| 58 | + $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
| 59 | + if(!empty($filter) && !empty($gassoc)) { |
|
| 60 | + $this->enabled = true; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + $this->cachedGroupMembers = new CappedMemoryCache(); |
|
| 64 | + $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * is user in group? |
|
| 69 | + * @param string $uid uid of the user |
|
| 70 | + * @param string $gid gid of the group |
|
| 71 | + * @return bool |
|
| 72 | + * |
|
| 73 | + * Checks whether the user is member of a group or not. |
|
| 74 | + */ |
|
| 75 | + public function inGroup($uid, $gid) { |
|
| 76 | + if(!$this->enabled) { |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
| 80 | + $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
| 81 | + if(!is_null($inGroup)) { |
|
| 82 | + return (bool)$inGroup; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $userDN = $this->access->username2dn($uid); |
|
| 86 | + |
|
| 87 | + if(isset($this->cachedGroupMembers[$gid])) { |
|
| 88 | + $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
| 89 | + return $isInGroup; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
| 93 | + $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
| 94 | + if(!is_null($members)) { |
|
| 95 | + $this->cachedGroupMembers[$gid] = $members; |
|
| 96 | + $isInGroup = in_array($userDN, $members); |
|
| 97 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 98 | + return $isInGroup; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 102 | + // just in case |
|
| 103 | + if(!$groupDN || !$userDN) { |
|
| 104 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + //check primary group first |
|
| 109 | + if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 110 | + $this->access->connection->writeToCache($cacheKey, true); |
|
| 111 | + return true; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
| 115 | + $members = $this->_groupMembers($groupDN); |
|
| 116 | + $members = array_keys($members); // uids are returned as keys |
|
| 117 | + if(!is_array($members) || count($members) === 0) { |
|
| 118 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + //extra work if we don't get back user DNs |
|
| 123 | + if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 124 | + $dns = array(); |
|
| 125 | + $filterParts = array(); |
|
| 126 | + $bytes = 0; |
|
| 127 | + foreach($members as $mid) { |
|
| 128 | + $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
| 129 | + $filterParts[] = $filter; |
|
| 130 | + $bytes += strlen($filter); |
|
| 131 | + if($bytes >= 9000000) { |
|
| 132 | + // AD has a default input buffer of 10 MB, we do not want |
|
| 133 | + // to take even the chance to exceed it |
|
| 134 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 135 | + $bytes = 0; |
|
| 136 | + $filterParts = array(); |
|
| 137 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 138 | + $dns = array_merge($dns, $users); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + if(count($filterParts) > 0) { |
|
| 142 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 143 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 144 | + $dns = array_merge($dns, $users); |
|
| 145 | + } |
|
| 146 | + $members = $dns; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $isInGroup = in_array($userDN, $members); |
|
| 150 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 151 | + $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
| 152 | + $this->cachedGroupMembers[$gid] = $members; |
|
| 153 | + |
|
| 154 | + return $isInGroup; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @param string $dnGroup |
|
| 159 | + * @return array |
|
| 160 | + * |
|
| 161 | + * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
| 162 | + * that match the search url otherwise returns an empty array. |
|
| 163 | + */ |
|
| 164 | + public function getDynamicGroupMembers($dnGroup) { |
|
| 165 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 166 | + |
|
| 167 | + if (empty($dynamicGroupMemberURL)) { |
|
| 168 | + return array(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $dynamicMembers = array(); |
|
| 172 | + $memberURLs = $this->access->readAttribute( |
|
| 173 | + $dnGroup, |
|
| 174 | + $dynamicGroupMemberURL, |
|
| 175 | + $this->access->connection->ldapGroupFilter |
|
| 176 | + ); |
|
| 177 | + if ($memberURLs !== false) { |
|
| 178 | + // this group has the 'memberURL' attribute so this is a dynamic group |
|
| 179 | + // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
| 180 | + // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
| 181 | + $pos = strpos($memberURLs[0], '('); |
|
| 182 | + if ($pos !== false) { |
|
| 183 | + $memberUrlFilter = substr($memberURLs[0], $pos); |
|
| 184 | + $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 185 | + $dynamicMembers = array(); |
|
| 186 | + foreach($foundMembers as $value) { |
|
| 187 | + $dynamicMembers[$value['dn'][0]] = 1; |
|
| 188 | + } |
|
| 189 | + } else { |
|
| 190 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 191 | + 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + return $dynamicMembers; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param string $dnGroup |
|
| 199 | + * @param array|null &$seen |
|
| 200 | + * @return array|mixed|null |
|
| 201 | + */ |
|
| 202 | + private function _groupMembers($dnGroup, &$seen = null) { |
|
| 203 | + if ($seen === null) { |
|
| 204 | + $seen = array(); |
|
| 205 | + } |
|
| 206 | + $allMembers = array(); |
|
| 207 | + if (array_key_exists($dnGroup, $seen)) { |
|
| 208 | + // avoid loops |
|
| 209 | + return array(); |
|
| 210 | + } |
|
| 211 | + // used extensively in cron job, caching makes sense for nested groups |
|
| 212 | + $cacheKey = '_groupMembers'.$dnGroup; |
|
| 213 | + $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
| 214 | + if(!is_null($groupMembers)) { |
|
| 215 | + return $groupMembers; |
|
| 216 | + } |
|
| 217 | + $seen[$dnGroup] = 1; |
|
| 218 | + $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr, |
|
| 219 | + $this->access->connection->ldapGroupFilter); |
|
| 220 | + if (is_array($members)) { |
|
| 221 | + foreach ($members as $memberDN) { |
|
| 222 | + $allMembers[$memberDN] = 1; |
|
| 223 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 224 | + if (!empty($nestedGroups)) { |
|
| 225 | + $subMembers = $this->_groupMembers($memberDN, $seen); |
|
| 226 | + if ($subMembers) { |
|
| 227 | + $allMembers = array_merge($allMembers, $subMembers); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - $allMembers = array_merge($allMembers, $this->getDynamicGroupMembers($dnGroup)); |
|
| 233 | + $allMembers = array_merge($allMembers, $this->getDynamicGroupMembers($dnGroup)); |
|
| 234 | 234 | |
| 235 | - $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
| 236 | - return $allMembers; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @param string $DN |
|
| 241 | - * @param array|null &$seen |
|
| 242 | - * @return array |
|
| 243 | - */ |
|
| 244 | - private function _getGroupDNsFromMemberOf($DN, &$seen = null) { |
|
| 245 | - if ($seen === null) { |
|
| 246 | - $seen = array(); |
|
| 247 | - } |
|
| 248 | - if (array_key_exists($DN, $seen)) { |
|
| 249 | - // avoid loops |
|
| 250 | - return array(); |
|
| 251 | - } |
|
| 252 | - $seen[$DN] = 1; |
|
| 253 | - $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
| 254 | - if (!is_array($groups)) { |
|
| 255 | - return array(); |
|
| 256 | - } |
|
| 257 | - $groups = $this->access->groupsMatchFilter($groups); |
|
| 258 | - $allGroups = $groups; |
|
| 259 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 260 | - if (intval($nestedGroups) === 1) { |
|
| 261 | - foreach ($groups as $group) { |
|
| 262 | - $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
|
| 263 | - $allGroups = array_merge($allGroups, $subGroups); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - return $allGroups; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * translates a primary group ID into an ownCloud internal name |
|
| 271 | - * @param string $gid as given by primaryGroupID on AD |
|
| 272 | - * @param string $dn a DN that belongs to the same domain as the group |
|
| 273 | - * @return string|bool |
|
| 274 | - */ |
|
| 275 | - public function primaryGroupID2Name($gid, $dn) { |
|
| 276 | - $cacheKey = 'primaryGroupIDtoName'; |
|
| 277 | - $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
| 278 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 279 | - return $groupNames[$gid]; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - $domainObjectSid = $this->access->getSID($dn); |
|
| 283 | - if($domainObjectSid === false) { |
|
| 284 | - return false; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - //we need to get the DN from LDAP |
|
| 288 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 289 | - $this->access->connection->ldapGroupFilter, |
|
| 290 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 291 | - )); |
|
| 292 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 293 | - if(empty($result)) { |
|
| 294 | - return false; |
|
| 295 | - } |
|
| 296 | - $dn = $result[0]['dn'][0]; |
|
| 297 | - |
|
| 298 | - //and now the group name |
|
| 299 | - //NOTE once we have separate ownCloud group IDs and group names we can |
|
| 300 | - //directly read the display name attribute instead of the DN |
|
| 301 | - $name = $this->access->dn2groupname($dn); |
|
| 302 | - |
|
| 303 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
| 304 | - |
|
| 305 | - return $name; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * returns the entry's primary group ID |
|
| 310 | - * @param string $dn |
|
| 311 | - * @param string $attribute |
|
| 312 | - * @return string|bool |
|
| 313 | - */ |
|
| 314 | - private function getEntryGroupID($dn, $attribute) { |
|
| 315 | - $value = $this->access->readAttribute($dn, $attribute); |
|
| 316 | - if(is_array($value) && !empty($value)) { |
|
| 317 | - return $value[0]; |
|
| 318 | - } |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * returns the group's primary ID |
|
| 324 | - * @param string $dn |
|
| 325 | - * @return string|bool |
|
| 326 | - */ |
|
| 327 | - public function getGroupPrimaryGroupID($dn) { |
|
| 328 | - return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * returns the user's primary group ID |
|
| 333 | - * @param string $dn |
|
| 334 | - * @return string|bool |
|
| 335 | - */ |
|
| 336 | - public function getUserPrimaryGroupIDs($dn) { |
|
| 337 | - $primaryGroupID = false; |
|
| 338 | - if($this->access->connection->hasPrimaryGroups) { |
|
| 339 | - $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
| 340 | - if($primaryGroupID === false) { |
|
| 341 | - $this->access->connection->hasPrimaryGroups = false; |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - return $primaryGroupID; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * returns a filter for a "users in primary group" search or count operation |
|
| 349 | - * |
|
| 350 | - * @param string $groupDN |
|
| 351 | - * @param string $search |
|
| 352 | - * @return string |
|
| 353 | - * @throws \Exception |
|
| 354 | - */ |
|
| 355 | - private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
| 356 | - $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
| 357 | - if($groupID === false) { |
|
| 358 | - throw new \Exception('Not a valid group'); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - $filterParts = []; |
|
| 362 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 363 | - if ($search !== '') { |
|
| 364 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 365 | - } |
|
| 366 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 367 | - |
|
| 368 | - $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 369 | - |
|
| 370 | - return $filter; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * returns a list of users that have the given group as primary group |
|
| 375 | - * |
|
| 376 | - * @param string $groupDN |
|
| 377 | - * @param string $search |
|
| 378 | - * @param int $limit |
|
| 379 | - * @param int $offset |
|
| 380 | - * @return string[] |
|
| 381 | - */ |
|
| 382 | - public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 383 | - try { |
|
| 384 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 385 | - $users = $this->access->fetchListOfUsers( |
|
| 386 | - $filter, |
|
| 387 | - array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
| 388 | - $limit, |
|
| 389 | - $offset |
|
| 390 | - ); |
|
| 391 | - return $this->access->ownCloudUserNames($users); |
|
| 392 | - } catch (\Exception $e) { |
|
| 393 | - return array(); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * returns the number of users that have the given group as primary group |
|
| 399 | - * |
|
| 400 | - * @param string $groupDN |
|
| 401 | - * @param string $search |
|
| 402 | - * @param int $limit |
|
| 403 | - * @param int $offset |
|
| 404 | - * @return int |
|
| 405 | - */ |
|
| 406 | - public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 407 | - try { |
|
| 408 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 409 | - $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
| 410 | - return (int)$users; |
|
| 411 | - } catch (\Exception $e) { |
|
| 412 | - return 0; |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * gets the primary group of a user |
|
| 418 | - * @param string $dn |
|
| 419 | - * @return string |
|
| 420 | - */ |
|
| 421 | - public function getUserPrimaryGroup($dn) { |
|
| 422 | - $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
| 423 | - if($groupID !== false) { |
|
| 424 | - $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
| 425 | - if($groupName !== false) { |
|
| 426 | - return $groupName; |
|
| 427 | - } |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - return false; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Get all groups a user belongs to |
|
| 435 | - * @param string $uid Name of the user |
|
| 436 | - * @return array with group names |
|
| 437 | - * |
|
| 438 | - * This function fetches all groups a user belongs to. It does not check |
|
| 439 | - * if the user exists at all. |
|
| 440 | - * |
|
| 441 | - * This function includes groups based on dynamic group membership. |
|
| 442 | - */ |
|
| 443 | - public function getUserGroups($uid) { |
|
| 444 | - if(!$this->enabled) { |
|
| 445 | - return array(); |
|
| 446 | - } |
|
| 447 | - $cacheKey = 'getUserGroups'.$uid; |
|
| 448 | - $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
| 449 | - if(!is_null($userGroups)) { |
|
| 450 | - return $userGroups; |
|
| 451 | - } |
|
| 452 | - $userDN = $this->access->username2dn($uid); |
|
| 453 | - if(!$userDN) { |
|
| 454 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 455 | - return array(); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $groups = []; |
|
| 459 | - $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
| 460 | - |
|
| 461 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 462 | - |
|
| 463 | - if (!empty($dynamicGroupMemberURL)) { |
|
| 464 | - // look through dynamic groups to add them to the result array if needed |
|
| 465 | - $groupsToMatch = $this->access->fetchListOfGroups( |
|
| 466 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 467 | - foreach($groupsToMatch as $dynamicGroup) { |
|
| 468 | - if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
| 469 | - continue; |
|
| 470 | - } |
|
| 471 | - $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
| 472 | - if ($pos !== false) { |
|
| 473 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 474 | - // apply filter via ldap search to see if this user is in this |
|
| 475 | - // dynamic group |
|
| 476 | - $userMatch = $this->access->readAttribute( |
|
| 477 | - $userDN, |
|
| 478 | - $this->access->connection->ldapUserDisplayName, |
|
| 479 | - $memberUrlFilter |
|
| 480 | - ); |
|
| 481 | - if ($userMatch !== false) { |
|
| 482 | - // match found so this user is in this group |
|
| 483 | - $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
| 484 | - if(is_string($groupName)) { |
|
| 485 | - // be sure to never return false if the dn could not be |
|
| 486 | - // resolved to a name, for whatever reason. |
|
| 487 | - $groups[] = $groupName; |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } else { |
|
| 491 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 492 | - 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - // if possible, read out membership via memberOf. It's far faster than |
|
| 498 | - // performing a search, which still is a fallback later. |
|
| 499 | - if(intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 500 | - && intval($this->access->connection->useMemberOfToDetectMembership) === 1 |
|
| 501 | - ) { |
|
| 502 | - $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
| 503 | - if (is_array($groupDNs)) { |
|
| 504 | - foreach ($groupDNs as $dn) { |
|
| 505 | - $groupName = $this->access->dn2groupname($dn); |
|
| 506 | - if(is_string($groupName)) { |
|
| 507 | - // be sure to never return false if the dn could not be |
|
| 508 | - // resolved to a name, for whatever reason. |
|
| 509 | - $groups[] = $groupName; |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - } |
|
| 235 | + $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
| 236 | + return $allMembers; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @param string $DN |
|
| 241 | + * @param array|null &$seen |
|
| 242 | + * @return array |
|
| 243 | + */ |
|
| 244 | + private function _getGroupDNsFromMemberOf($DN, &$seen = null) { |
|
| 245 | + if ($seen === null) { |
|
| 246 | + $seen = array(); |
|
| 247 | + } |
|
| 248 | + if (array_key_exists($DN, $seen)) { |
|
| 249 | + // avoid loops |
|
| 250 | + return array(); |
|
| 251 | + } |
|
| 252 | + $seen[$DN] = 1; |
|
| 253 | + $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
| 254 | + if (!is_array($groups)) { |
|
| 255 | + return array(); |
|
| 256 | + } |
|
| 257 | + $groups = $this->access->groupsMatchFilter($groups); |
|
| 258 | + $allGroups = $groups; |
|
| 259 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 260 | + if (intval($nestedGroups) === 1) { |
|
| 261 | + foreach ($groups as $group) { |
|
| 262 | + $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
|
| 263 | + $allGroups = array_merge($allGroups, $subGroups); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + return $allGroups; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * translates a primary group ID into an ownCloud internal name |
|
| 271 | + * @param string $gid as given by primaryGroupID on AD |
|
| 272 | + * @param string $dn a DN that belongs to the same domain as the group |
|
| 273 | + * @return string|bool |
|
| 274 | + */ |
|
| 275 | + public function primaryGroupID2Name($gid, $dn) { |
|
| 276 | + $cacheKey = 'primaryGroupIDtoName'; |
|
| 277 | + $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
| 278 | + if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 279 | + return $groupNames[$gid]; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + $domainObjectSid = $this->access->getSID($dn); |
|
| 283 | + if($domainObjectSid === false) { |
|
| 284 | + return false; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + //we need to get the DN from LDAP |
|
| 288 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 289 | + $this->access->connection->ldapGroupFilter, |
|
| 290 | + 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 291 | + )); |
|
| 292 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 293 | + if(empty($result)) { |
|
| 294 | + return false; |
|
| 295 | + } |
|
| 296 | + $dn = $result[0]['dn'][0]; |
|
| 297 | + |
|
| 298 | + //and now the group name |
|
| 299 | + //NOTE once we have separate ownCloud group IDs and group names we can |
|
| 300 | + //directly read the display name attribute instead of the DN |
|
| 301 | + $name = $this->access->dn2groupname($dn); |
|
| 302 | + |
|
| 303 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
| 304 | + |
|
| 305 | + return $name; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * returns the entry's primary group ID |
|
| 310 | + * @param string $dn |
|
| 311 | + * @param string $attribute |
|
| 312 | + * @return string|bool |
|
| 313 | + */ |
|
| 314 | + private function getEntryGroupID($dn, $attribute) { |
|
| 315 | + $value = $this->access->readAttribute($dn, $attribute); |
|
| 316 | + if(is_array($value) && !empty($value)) { |
|
| 317 | + return $value[0]; |
|
| 318 | + } |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * returns the group's primary ID |
|
| 324 | + * @param string $dn |
|
| 325 | + * @return string|bool |
|
| 326 | + */ |
|
| 327 | + public function getGroupPrimaryGroupID($dn) { |
|
| 328 | + return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * returns the user's primary group ID |
|
| 333 | + * @param string $dn |
|
| 334 | + * @return string|bool |
|
| 335 | + */ |
|
| 336 | + public function getUserPrimaryGroupIDs($dn) { |
|
| 337 | + $primaryGroupID = false; |
|
| 338 | + if($this->access->connection->hasPrimaryGroups) { |
|
| 339 | + $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
| 340 | + if($primaryGroupID === false) { |
|
| 341 | + $this->access->connection->hasPrimaryGroups = false; |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + return $primaryGroupID; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * returns a filter for a "users in primary group" search or count operation |
|
| 349 | + * |
|
| 350 | + * @param string $groupDN |
|
| 351 | + * @param string $search |
|
| 352 | + * @return string |
|
| 353 | + * @throws \Exception |
|
| 354 | + */ |
|
| 355 | + private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
| 356 | + $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
| 357 | + if($groupID === false) { |
|
| 358 | + throw new \Exception('Not a valid group'); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + $filterParts = []; |
|
| 362 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 363 | + if ($search !== '') { |
|
| 364 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 365 | + } |
|
| 366 | + $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 367 | + |
|
| 368 | + $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 369 | + |
|
| 370 | + return $filter; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * returns a list of users that have the given group as primary group |
|
| 375 | + * |
|
| 376 | + * @param string $groupDN |
|
| 377 | + * @param string $search |
|
| 378 | + * @param int $limit |
|
| 379 | + * @param int $offset |
|
| 380 | + * @return string[] |
|
| 381 | + */ |
|
| 382 | + public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 383 | + try { |
|
| 384 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 385 | + $users = $this->access->fetchListOfUsers( |
|
| 386 | + $filter, |
|
| 387 | + array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
| 388 | + $limit, |
|
| 389 | + $offset |
|
| 390 | + ); |
|
| 391 | + return $this->access->ownCloudUserNames($users); |
|
| 392 | + } catch (\Exception $e) { |
|
| 393 | + return array(); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * returns the number of users that have the given group as primary group |
|
| 399 | + * |
|
| 400 | + * @param string $groupDN |
|
| 401 | + * @param string $search |
|
| 402 | + * @param int $limit |
|
| 403 | + * @param int $offset |
|
| 404 | + * @return int |
|
| 405 | + */ |
|
| 406 | + public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 407 | + try { |
|
| 408 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 409 | + $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
| 410 | + return (int)$users; |
|
| 411 | + } catch (\Exception $e) { |
|
| 412 | + return 0; |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * gets the primary group of a user |
|
| 418 | + * @param string $dn |
|
| 419 | + * @return string |
|
| 420 | + */ |
|
| 421 | + public function getUserPrimaryGroup($dn) { |
|
| 422 | + $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
| 423 | + if($groupID !== false) { |
|
| 424 | + $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
| 425 | + if($groupName !== false) { |
|
| 426 | + return $groupName; |
|
| 427 | + } |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + return false; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Get all groups a user belongs to |
|
| 435 | + * @param string $uid Name of the user |
|
| 436 | + * @return array with group names |
|
| 437 | + * |
|
| 438 | + * This function fetches all groups a user belongs to. It does not check |
|
| 439 | + * if the user exists at all. |
|
| 440 | + * |
|
| 441 | + * This function includes groups based on dynamic group membership. |
|
| 442 | + */ |
|
| 443 | + public function getUserGroups($uid) { |
|
| 444 | + if(!$this->enabled) { |
|
| 445 | + return array(); |
|
| 446 | + } |
|
| 447 | + $cacheKey = 'getUserGroups'.$uid; |
|
| 448 | + $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
| 449 | + if(!is_null($userGroups)) { |
|
| 450 | + return $userGroups; |
|
| 451 | + } |
|
| 452 | + $userDN = $this->access->username2dn($uid); |
|
| 453 | + if(!$userDN) { |
|
| 454 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 455 | + return array(); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $groups = []; |
|
| 459 | + $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
| 460 | + |
|
| 461 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 462 | + |
|
| 463 | + if (!empty($dynamicGroupMemberURL)) { |
|
| 464 | + // look through dynamic groups to add them to the result array if needed |
|
| 465 | + $groupsToMatch = $this->access->fetchListOfGroups( |
|
| 466 | + $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 467 | + foreach($groupsToMatch as $dynamicGroup) { |
|
| 468 | + if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
| 469 | + continue; |
|
| 470 | + } |
|
| 471 | + $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
| 472 | + if ($pos !== false) { |
|
| 473 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 474 | + // apply filter via ldap search to see if this user is in this |
|
| 475 | + // dynamic group |
|
| 476 | + $userMatch = $this->access->readAttribute( |
|
| 477 | + $userDN, |
|
| 478 | + $this->access->connection->ldapUserDisplayName, |
|
| 479 | + $memberUrlFilter |
|
| 480 | + ); |
|
| 481 | + if ($userMatch !== false) { |
|
| 482 | + // match found so this user is in this group |
|
| 483 | + $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
| 484 | + if(is_string($groupName)) { |
|
| 485 | + // be sure to never return false if the dn could not be |
|
| 486 | + // resolved to a name, for whatever reason. |
|
| 487 | + $groups[] = $groupName; |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } else { |
|
| 491 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 492 | + 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + // if possible, read out membership via memberOf. It's far faster than |
|
| 498 | + // performing a search, which still is a fallback later. |
|
| 499 | + if(intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 500 | + && intval($this->access->connection->useMemberOfToDetectMembership) === 1 |
|
| 501 | + ) { |
|
| 502 | + $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
| 503 | + if (is_array($groupDNs)) { |
|
| 504 | + foreach ($groupDNs as $dn) { |
|
| 505 | + $groupName = $this->access->dn2groupname($dn); |
|
| 506 | + if(is_string($groupName)) { |
|
| 507 | + // be sure to never return false if the dn could not be |
|
| 508 | + // resolved to a name, for whatever reason. |
|
| 509 | + $groups[] = $groupName; |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - if($primaryGroup !== false) { |
|
| 515 | - $groups[] = $primaryGroup; |
|
| 516 | - } |
|
| 517 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 518 | - return $groups; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
| 522 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 523 | - || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
| 524 | - ) { |
|
| 525 | - $uid = $userDN; |
|
| 526 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 527 | - $result = $this->access->readAttribute($userDN, 'uid'); |
|
| 528 | - if ($result === false) { |
|
| 529 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 530 | - $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
|
| 531 | - } |
|
| 532 | - $uid = $result[0]; |
|
| 533 | - } else { |
|
| 534 | - // just in case |
|
| 535 | - $uid = $userDN; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 539 | - $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
| 540 | - } else { |
|
| 541 | - $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
| 542 | - $groupsByMember = $this->access->ownCloudGroupNames($groupsByMember); |
|
| 543 | - $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
| 544 | - $groups = array_merge($groups, $groupsByMember); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - if($primaryGroup !== false) { |
|
| 548 | - $groups[] = $primaryGroup; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
| 552 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 553 | - |
|
| 554 | - return $groups; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @param string $dn |
|
| 559 | - * @param array|null &$seen |
|
| 560 | - * @return array |
|
| 561 | - */ |
|
| 562 | - private function getGroupsByMember($dn, &$seen = null) { |
|
| 563 | - if ($seen === null) { |
|
| 564 | - $seen = array(); |
|
| 565 | - } |
|
| 566 | - $allGroups = array(); |
|
| 567 | - if (array_key_exists($dn, $seen)) { |
|
| 568 | - // avoid loops |
|
| 569 | - return array(); |
|
| 570 | - } |
|
| 571 | - $seen[$dn] = true; |
|
| 572 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 573 | - $this->access->connection->ldapGroupFilter, |
|
| 574 | - $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn |
|
| 575 | - )); |
|
| 576 | - $groups = $this->access->fetchListOfGroups($filter, |
|
| 577 | - array($this->access->connection->ldapGroupDisplayName, 'dn')); |
|
| 578 | - if (is_array($groups)) { |
|
| 579 | - foreach ($groups as $groupobj) { |
|
| 580 | - $groupDN = $groupobj['dn'][0]; |
|
| 581 | - $allGroups[$groupDN] = $groupobj; |
|
| 582 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 583 | - if (!empty($nestedGroups)) { |
|
| 584 | - $supergroups = $this->getGroupsByMember($groupDN, $seen); |
|
| 585 | - if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 586 | - $allGroups = array_merge($allGroups, $supergroups); |
|
| 587 | - } |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - return $allGroups; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * get a list of all users in a group |
|
| 596 | - * |
|
| 597 | - * @param string $gid |
|
| 598 | - * @param string $search |
|
| 599 | - * @param int $limit |
|
| 600 | - * @param int $offset |
|
| 601 | - * @return array with user ids |
|
| 602 | - */ |
|
| 603 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 604 | - if(!$this->enabled) { |
|
| 605 | - return array(); |
|
| 606 | - } |
|
| 607 | - if(!$this->groupExists($gid)) { |
|
| 608 | - return array(); |
|
| 609 | - } |
|
| 610 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 611 | - $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
| 612 | - // check for cache of the exact query |
|
| 613 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 614 | - if(!is_null($groupUsers)) { |
|
| 615 | - return $groupUsers; |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - // check for cache of the query without limit and offset |
|
| 619 | - $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
| 620 | - if(!is_null($groupUsers)) { |
|
| 621 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 622 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 623 | - return $groupUsers; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - if($limit === -1) { |
|
| 627 | - $limit = null; |
|
| 628 | - } |
|
| 629 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 630 | - if(!$groupDN) { |
|
| 631 | - // group couldn't be found, return empty resultset |
|
| 632 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 633 | - return array(); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
| 637 | - $members = array_keys($this->_groupMembers($groupDN)); |
|
| 638 | - if(!$members && empty($primaryUsers)) { |
|
| 639 | - //in case users could not be retrieved, return empty result set |
|
| 640 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 641 | - return array(); |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - $groupUsers = array(); |
|
| 645 | - $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
| 646 | - $attrs = $this->access->userManager->getAttributes(true); |
|
| 647 | - foreach($members as $member) { |
|
| 648 | - if($isMemberUid) { |
|
| 649 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
| 650 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 651 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 652 | - $this->access->getFilterPartForUserSearch($search) |
|
| 653 | - )); |
|
| 654 | - $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
| 655 | - if(count($ldap_users) < 1) { |
|
| 656 | - continue; |
|
| 657 | - } |
|
| 658 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
| 659 | - } else { |
|
| 660 | - //we got DNs, check if we need to filter by search or we can give back all of them |
|
| 661 | - if ($search !== '') { |
|
| 662 | - if(!$this->access->readAttribute($member, |
|
| 663 | - $this->access->connection->ldapUserDisplayName, |
|
| 664 | - $this->access->getFilterPartForUserSearch($search))) { |
|
| 665 | - continue; |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - // dn2username will also check if the users belong to the allowed base |
|
| 669 | - if($ocname = $this->access->dn2username($member)) { |
|
| 670 | - $groupUsers[] = $ocname; |
|
| 671 | - } |
|
| 672 | - } |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers)); |
|
| 676 | - natsort($groupUsers); |
|
| 677 | - $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
| 678 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 679 | - |
|
| 680 | - |
|
| 681 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 682 | - |
|
| 683 | - return $groupUsers; |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * returns the number of users in a group, who match the search term |
|
| 688 | - * @param string $gid the internal group name |
|
| 689 | - * @param string $search optional, a search string |
|
| 690 | - * @return int|bool |
|
| 691 | - */ |
|
| 692 | - public function countUsersInGroup($gid, $search = '') { |
|
| 693 | - $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
| 694 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 695 | - return false; |
|
| 696 | - } |
|
| 697 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 698 | - if(!is_null($groupUsers)) { |
|
| 699 | - return $groupUsers; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 703 | - if(!$groupDN) { |
|
| 704 | - // group couldn't be found, return empty result set |
|
| 705 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 706 | - return false; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - $members = array_keys($this->_groupMembers($groupDN)); |
|
| 710 | - $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
| 711 | - if(!$members && $primaryUserCount === 0) { |
|
| 712 | - //in case users could not be retrieved, return empty result set |
|
| 713 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 714 | - return false; |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - if ($search === '') { |
|
| 718 | - $groupUsers = count($members) + $primaryUserCount; |
|
| 719 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 720 | - return $groupUsers; |
|
| 721 | - } |
|
| 722 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 723 | - $isMemberUid = |
|
| 724 | - (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
| 725 | - === 'memberuid'); |
|
| 726 | - |
|
| 727 | - //we need to apply the search filter |
|
| 728 | - //alternatives that need to be checked: |
|
| 729 | - //a) get all users by search filter and array_intersect them |
|
| 730 | - //b) a, but only when less than 1k 10k ?k users like it is |
|
| 731 | - //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
| 732 | - // and let it count. |
|
| 733 | - //For now this is not important, because the only use of this method |
|
| 734 | - //does not supply a search string |
|
| 735 | - $groupUsers = array(); |
|
| 736 | - foreach($members as $member) { |
|
| 737 | - if($isMemberUid) { |
|
| 738 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
| 739 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 740 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 741 | - $this->access->getFilterPartForUserSearch($search) |
|
| 742 | - )); |
|
| 743 | - $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
| 744 | - if(count($ldap_users) < 1) { |
|
| 745 | - continue; |
|
| 746 | - } |
|
| 747 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
| 748 | - } else { |
|
| 749 | - //we need to apply the search filter now |
|
| 750 | - if(!$this->access->readAttribute($member, |
|
| 751 | - $this->access->connection->ldapUserDisplayName, |
|
| 752 | - $this->access->getFilterPartForUserSearch($search))) { |
|
| 753 | - continue; |
|
| 754 | - } |
|
| 755 | - // dn2username will also check if the users belong to the allowed base |
|
| 756 | - if($ocname = $this->access->dn2username($member)) { |
|
| 757 | - $groupUsers[] = $ocname; |
|
| 758 | - } |
|
| 759 | - } |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - //and get users that have the group as primary |
|
| 763 | - $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
| 764 | - |
|
| 765 | - return count($groupUsers) + $primaryUsers; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - /** |
|
| 769 | - * get a list of all groups |
|
| 770 | - * |
|
| 771 | - * @param string $search |
|
| 772 | - * @param $limit |
|
| 773 | - * @param int $offset |
|
| 774 | - * @return array with group names |
|
| 775 | - * |
|
| 776 | - * Returns a list with all groups (used by getGroups) |
|
| 777 | - */ |
|
| 778 | - protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
| 779 | - if(!$this->enabled) { |
|
| 780 | - return array(); |
|
| 781 | - } |
|
| 782 | - $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
| 783 | - |
|
| 784 | - //Check cache before driving unnecessary searches |
|
| 785 | - \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
|
| 786 | - $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
| 787 | - if(!is_null($ldap_groups)) { |
|
| 788 | - return $ldap_groups; |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 792 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 793 | - if($limit <= 0) { |
|
| 794 | - $limit = null; |
|
| 795 | - } |
|
| 796 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 797 | - $this->access->connection->ldapGroupFilter, |
|
| 798 | - $this->access->getFilterPartForGroupSearch($search) |
|
| 799 | - )); |
|
| 800 | - \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); |
|
| 801 | - $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
| 802 | - array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
| 803 | - $limit, |
|
| 804 | - $offset); |
|
| 805 | - $ldap_groups = $this->access->ownCloudGroupNames($ldap_groups); |
|
| 806 | - |
|
| 807 | - $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
| 808 | - return $ldap_groups; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * get a list of all groups using a paged search |
|
| 813 | - * |
|
| 814 | - * @param string $search |
|
| 815 | - * @param int $limit |
|
| 816 | - * @param int $offset |
|
| 817 | - * @return array with group names |
|
| 818 | - * |
|
| 819 | - * Returns a list with all groups |
|
| 820 | - * Uses a paged search if available to override a |
|
| 821 | - * server side search limit. |
|
| 822 | - * (active directory has a limit of 1000 by default) |
|
| 823 | - */ |
|
| 824 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 825 | - if(!$this->enabled) { |
|
| 826 | - return array(); |
|
| 827 | - } |
|
| 828 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 829 | - $pagingSize = intval($this->access->connection->ldapPagingSize); |
|
| 830 | - if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
|
| 831 | - return $this->getGroupsChunk($search, $limit, $offset); |
|
| 832 | - } |
|
| 833 | - $maxGroups = 100000; // limit max results (just for safety reasons) |
|
| 834 | - if ($limit > -1) { |
|
| 835 | - $overallLimit = min($limit + $offset, $maxGroups); |
|
| 836 | - } else { |
|
| 837 | - $overallLimit = $maxGroups; |
|
| 838 | - } |
|
| 839 | - $chunkOffset = $offset; |
|
| 840 | - $allGroups = array(); |
|
| 841 | - while ($chunkOffset < $overallLimit) { |
|
| 842 | - $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
| 843 | - $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
| 844 | - $nread = count($ldapGroups); |
|
| 845 | - \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG); |
|
| 846 | - if ($nread) { |
|
| 847 | - $allGroups = array_merge($allGroups, $ldapGroups); |
|
| 848 | - $chunkOffset += $nread; |
|
| 849 | - } |
|
| 850 | - if ($nread < $chunkLimit) { |
|
| 851 | - break; |
|
| 852 | - } |
|
| 853 | - } |
|
| 854 | - return $allGroups; |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * @param string $group |
|
| 859 | - * @return bool |
|
| 860 | - */ |
|
| 861 | - public function groupMatchesFilter($group) { |
|
| 862 | - return (strripos($group, $this->groupSearch) !== false); |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - /** |
|
| 866 | - * check if a group exists |
|
| 867 | - * @param string $gid |
|
| 868 | - * @return bool |
|
| 869 | - */ |
|
| 870 | - public function groupExists($gid) { |
|
| 871 | - $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
| 872 | - if(!is_null($groupExists)) { |
|
| 873 | - return (bool)$groupExists; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - //getting dn, if false the group does not exist. If dn, it may be mapped |
|
| 877 | - //only, requires more checking. |
|
| 878 | - $dn = $this->access->groupname2dn($gid); |
|
| 879 | - if(!$dn) { |
|
| 880 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 881 | - return false; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - //if group really still exists, we will be able to read its objectclass |
|
| 885 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 886 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 887 | - return false; |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
| 891 | - return true; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - /** |
|
| 895 | - * Check if backend implements actions |
|
| 896 | - * @param int $actions bitwise-or'ed actions |
|
| 897 | - * @return boolean |
|
| 898 | - * |
|
| 899 | - * Returns the supported actions as int to be |
|
| 900 | - * compared with OC_USER_BACKEND_CREATE_USER etc. |
|
| 901 | - */ |
|
| 902 | - public function implementsActions($actions) { |
|
| 903 | - return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Return access for LDAP interaction. |
|
| 908 | - * @return Access instance of Access for LDAP interaction |
|
| 909 | - */ |
|
| 910 | - public function getLDAPAccess() { |
|
| 911 | - return $this->access; |
|
| 912 | - } |
|
| 514 | + if($primaryGroup !== false) { |
|
| 515 | + $groups[] = $primaryGroup; |
|
| 516 | + } |
|
| 517 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 518 | + return $groups; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
| 522 | + if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 523 | + || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
| 524 | + ) { |
|
| 525 | + $uid = $userDN; |
|
| 526 | + } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 527 | + $result = $this->access->readAttribute($userDN, 'uid'); |
|
| 528 | + if ($result === false) { |
|
| 529 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 530 | + $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
|
| 531 | + } |
|
| 532 | + $uid = $result[0]; |
|
| 533 | + } else { |
|
| 534 | + // just in case |
|
| 535 | + $uid = $userDN; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 539 | + $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
| 540 | + } else { |
|
| 541 | + $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
| 542 | + $groupsByMember = $this->access->ownCloudGroupNames($groupsByMember); |
|
| 543 | + $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
| 544 | + $groups = array_merge($groups, $groupsByMember); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + if($primaryGroup !== false) { |
|
| 548 | + $groups[] = $primaryGroup; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
| 552 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 553 | + |
|
| 554 | + return $groups; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @param string $dn |
|
| 559 | + * @param array|null &$seen |
|
| 560 | + * @return array |
|
| 561 | + */ |
|
| 562 | + private function getGroupsByMember($dn, &$seen = null) { |
|
| 563 | + if ($seen === null) { |
|
| 564 | + $seen = array(); |
|
| 565 | + } |
|
| 566 | + $allGroups = array(); |
|
| 567 | + if (array_key_exists($dn, $seen)) { |
|
| 568 | + // avoid loops |
|
| 569 | + return array(); |
|
| 570 | + } |
|
| 571 | + $seen[$dn] = true; |
|
| 572 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 573 | + $this->access->connection->ldapGroupFilter, |
|
| 574 | + $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn |
|
| 575 | + )); |
|
| 576 | + $groups = $this->access->fetchListOfGroups($filter, |
|
| 577 | + array($this->access->connection->ldapGroupDisplayName, 'dn')); |
|
| 578 | + if (is_array($groups)) { |
|
| 579 | + foreach ($groups as $groupobj) { |
|
| 580 | + $groupDN = $groupobj['dn'][0]; |
|
| 581 | + $allGroups[$groupDN] = $groupobj; |
|
| 582 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 583 | + if (!empty($nestedGroups)) { |
|
| 584 | + $supergroups = $this->getGroupsByMember($groupDN, $seen); |
|
| 585 | + if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 586 | + $allGroups = array_merge($allGroups, $supergroups); |
|
| 587 | + } |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + return $allGroups; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * get a list of all users in a group |
|
| 596 | + * |
|
| 597 | + * @param string $gid |
|
| 598 | + * @param string $search |
|
| 599 | + * @param int $limit |
|
| 600 | + * @param int $offset |
|
| 601 | + * @return array with user ids |
|
| 602 | + */ |
|
| 603 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 604 | + if(!$this->enabled) { |
|
| 605 | + return array(); |
|
| 606 | + } |
|
| 607 | + if(!$this->groupExists($gid)) { |
|
| 608 | + return array(); |
|
| 609 | + } |
|
| 610 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 611 | + $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
| 612 | + // check for cache of the exact query |
|
| 613 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 614 | + if(!is_null($groupUsers)) { |
|
| 615 | + return $groupUsers; |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + // check for cache of the query without limit and offset |
|
| 619 | + $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
| 620 | + if(!is_null($groupUsers)) { |
|
| 621 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 622 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 623 | + return $groupUsers; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + if($limit === -1) { |
|
| 627 | + $limit = null; |
|
| 628 | + } |
|
| 629 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 630 | + if(!$groupDN) { |
|
| 631 | + // group couldn't be found, return empty resultset |
|
| 632 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 633 | + return array(); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
| 637 | + $members = array_keys($this->_groupMembers($groupDN)); |
|
| 638 | + if(!$members && empty($primaryUsers)) { |
|
| 639 | + //in case users could not be retrieved, return empty result set |
|
| 640 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 641 | + return array(); |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + $groupUsers = array(); |
|
| 645 | + $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
| 646 | + $attrs = $this->access->userManager->getAttributes(true); |
|
| 647 | + foreach($members as $member) { |
|
| 648 | + if($isMemberUid) { |
|
| 649 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
| 650 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 651 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 652 | + $this->access->getFilterPartForUserSearch($search) |
|
| 653 | + )); |
|
| 654 | + $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
| 655 | + if(count($ldap_users) < 1) { |
|
| 656 | + continue; |
|
| 657 | + } |
|
| 658 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
| 659 | + } else { |
|
| 660 | + //we got DNs, check if we need to filter by search or we can give back all of them |
|
| 661 | + if ($search !== '') { |
|
| 662 | + if(!$this->access->readAttribute($member, |
|
| 663 | + $this->access->connection->ldapUserDisplayName, |
|
| 664 | + $this->access->getFilterPartForUserSearch($search))) { |
|
| 665 | + continue; |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + // dn2username will also check if the users belong to the allowed base |
|
| 669 | + if($ocname = $this->access->dn2username($member)) { |
|
| 670 | + $groupUsers[] = $ocname; |
|
| 671 | + } |
|
| 672 | + } |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers)); |
|
| 676 | + natsort($groupUsers); |
|
| 677 | + $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
| 678 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 679 | + |
|
| 680 | + |
|
| 681 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 682 | + |
|
| 683 | + return $groupUsers; |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * returns the number of users in a group, who match the search term |
|
| 688 | + * @param string $gid the internal group name |
|
| 689 | + * @param string $search optional, a search string |
|
| 690 | + * @return int|bool |
|
| 691 | + */ |
|
| 692 | + public function countUsersInGroup($gid, $search = '') { |
|
| 693 | + $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
| 694 | + if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 695 | + return false; |
|
| 696 | + } |
|
| 697 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 698 | + if(!is_null($groupUsers)) { |
|
| 699 | + return $groupUsers; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 703 | + if(!$groupDN) { |
|
| 704 | + // group couldn't be found, return empty result set |
|
| 705 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 706 | + return false; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + $members = array_keys($this->_groupMembers($groupDN)); |
|
| 710 | + $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
| 711 | + if(!$members && $primaryUserCount === 0) { |
|
| 712 | + //in case users could not be retrieved, return empty result set |
|
| 713 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 714 | + return false; |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + if ($search === '') { |
|
| 718 | + $groupUsers = count($members) + $primaryUserCount; |
|
| 719 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 720 | + return $groupUsers; |
|
| 721 | + } |
|
| 722 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 723 | + $isMemberUid = |
|
| 724 | + (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
| 725 | + === 'memberuid'); |
|
| 726 | + |
|
| 727 | + //we need to apply the search filter |
|
| 728 | + //alternatives that need to be checked: |
|
| 729 | + //a) get all users by search filter and array_intersect them |
|
| 730 | + //b) a, but only when less than 1k 10k ?k users like it is |
|
| 731 | + //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
| 732 | + // and let it count. |
|
| 733 | + //For now this is not important, because the only use of this method |
|
| 734 | + //does not supply a search string |
|
| 735 | + $groupUsers = array(); |
|
| 736 | + foreach($members as $member) { |
|
| 737 | + if($isMemberUid) { |
|
| 738 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
| 739 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 740 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 741 | + $this->access->getFilterPartForUserSearch($search) |
|
| 742 | + )); |
|
| 743 | + $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
| 744 | + if(count($ldap_users) < 1) { |
|
| 745 | + continue; |
|
| 746 | + } |
|
| 747 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
| 748 | + } else { |
|
| 749 | + //we need to apply the search filter now |
|
| 750 | + if(!$this->access->readAttribute($member, |
|
| 751 | + $this->access->connection->ldapUserDisplayName, |
|
| 752 | + $this->access->getFilterPartForUserSearch($search))) { |
|
| 753 | + continue; |
|
| 754 | + } |
|
| 755 | + // dn2username will also check if the users belong to the allowed base |
|
| 756 | + if($ocname = $this->access->dn2username($member)) { |
|
| 757 | + $groupUsers[] = $ocname; |
|
| 758 | + } |
|
| 759 | + } |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + //and get users that have the group as primary |
|
| 763 | + $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
| 764 | + |
|
| 765 | + return count($groupUsers) + $primaryUsers; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + /** |
|
| 769 | + * get a list of all groups |
|
| 770 | + * |
|
| 771 | + * @param string $search |
|
| 772 | + * @param $limit |
|
| 773 | + * @param int $offset |
|
| 774 | + * @return array with group names |
|
| 775 | + * |
|
| 776 | + * Returns a list with all groups (used by getGroups) |
|
| 777 | + */ |
|
| 778 | + protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
| 779 | + if(!$this->enabled) { |
|
| 780 | + return array(); |
|
| 781 | + } |
|
| 782 | + $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
| 783 | + |
|
| 784 | + //Check cache before driving unnecessary searches |
|
| 785 | + \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
|
| 786 | + $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
| 787 | + if(!is_null($ldap_groups)) { |
|
| 788 | + return $ldap_groups; |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 792 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 793 | + if($limit <= 0) { |
|
| 794 | + $limit = null; |
|
| 795 | + } |
|
| 796 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 797 | + $this->access->connection->ldapGroupFilter, |
|
| 798 | + $this->access->getFilterPartForGroupSearch($search) |
|
| 799 | + )); |
|
| 800 | + \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); |
|
| 801 | + $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
| 802 | + array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
| 803 | + $limit, |
|
| 804 | + $offset); |
|
| 805 | + $ldap_groups = $this->access->ownCloudGroupNames($ldap_groups); |
|
| 806 | + |
|
| 807 | + $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
| 808 | + return $ldap_groups; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * get a list of all groups using a paged search |
|
| 813 | + * |
|
| 814 | + * @param string $search |
|
| 815 | + * @param int $limit |
|
| 816 | + * @param int $offset |
|
| 817 | + * @return array with group names |
|
| 818 | + * |
|
| 819 | + * Returns a list with all groups |
|
| 820 | + * Uses a paged search if available to override a |
|
| 821 | + * server side search limit. |
|
| 822 | + * (active directory has a limit of 1000 by default) |
|
| 823 | + */ |
|
| 824 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 825 | + if(!$this->enabled) { |
|
| 826 | + return array(); |
|
| 827 | + } |
|
| 828 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 829 | + $pagingSize = intval($this->access->connection->ldapPagingSize); |
|
| 830 | + if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
|
| 831 | + return $this->getGroupsChunk($search, $limit, $offset); |
|
| 832 | + } |
|
| 833 | + $maxGroups = 100000; // limit max results (just for safety reasons) |
|
| 834 | + if ($limit > -1) { |
|
| 835 | + $overallLimit = min($limit + $offset, $maxGroups); |
|
| 836 | + } else { |
|
| 837 | + $overallLimit = $maxGroups; |
|
| 838 | + } |
|
| 839 | + $chunkOffset = $offset; |
|
| 840 | + $allGroups = array(); |
|
| 841 | + while ($chunkOffset < $overallLimit) { |
|
| 842 | + $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
| 843 | + $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
| 844 | + $nread = count($ldapGroups); |
|
| 845 | + \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG); |
|
| 846 | + if ($nread) { |
|
| 847 | + $allGroups = array_merge($allGroups, $ldapGroups); |
|
| 848 | + $chunkOffset += $nread; |
|
| 849 | + } |
|
| 850 | + if ($nread < $chunkLimit) { |
|
| 851 | + break; |
|
| 852 | + } |
|
| 853 | + } |
|
| 854 | + return $allGroups; |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * @param string $group |
|
| 859 | + * @return bool |
|
| 860 | + */ |
|
| 861 | + public function groupMatchesFilter($group) { |
|
| 862 | + return (strripos($group, $this->groupSearch) !== false); |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + /** |
|
| 866 | + * check if a group exists |
|
| 867 | + * @param string $gid |
|
| 868 | + * @return bool |
|
| 869 | + */ |
|
| 870 | + public function groupExists($gid) { |
|
| 871 | + $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
| 872 | + if(!is_null($groupExists)) { |
|
| 873 | + return (bool)$groupExists; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + //getting dn, if false the group does not exist. If dn, it may be mapped |
|
| 877 | + //only, requires more checking. |
|
| 878 | + $dn = $this->access->groupname2dn($gid); |
|
| 879 | + if(!$dn) { |
|
| 880 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 881 | + return false; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + //if group really still exists, we will be able to read its objectclass |
|
| 885 | + if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 886 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 887 | + return false; |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
| 891 | + return true; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + /** |
|
| 895 | + * Check if backend implements actions |
|
| 896 | + * @param int $actions bitwise-or'ed actions |
|
| 897 | + * @return boolean |
|
| 898 | + * |
|
| 899 | + * Returns the supported actions as int to be |
|
| 900 | + * compared with OC_USER_BACKEND_CREATE_USER etc. |
|
| 901 | + */ |
|
| 902 | + public function implementsActions($actions) { |
|
| 903 | + return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Return access for LDAP interaction. |
|
| 908 | + * @return Access instance of Access for LDAP interaction |
|
| 909 | + */ |
|
| 910 | + public function getLDAPAccess() { |
|
| 911 | + return $this->access; |
|
| 912 | + } |
|
| 913 | 913 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | parent::__construct($access); |
| 57 | 57 | $filter = $this->access->connection->ldapGroupFilter; |
| 58 | 58 | $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
| 59 | - if(!empty($filter) && !empty($gassoc)) { |
|
| 59 | + if (!empty($filter) && !empty($gassoc)) { |
|
| 60 | 60 | $this->enabled = true; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -73,25 +73,25 @@ discard block |
||
| 73 | 73 | * Checks whether the user is member of a group or not. |
| 74 | 74 | */ |
| 75 | 75 | public function inGroup($uid, $gid) { |
| 76 | - if(!$this->enabled) { |
|
| 76 | + if (!$this->enabled) { |
|
| 77 | 77 | return false; |
| 78 | 78 | } |
| 79 | 79 | $cacheKey = 'inGroup'.$uid.':'.$gid; |
| 80 | 80 | $inGroup = $this->access->connection->getFromCache($cacheKey); |
| 81 | - if(!is_null($inGroup)) { |
|
| 82 | - return (bool)$inGroup; |
|
| 81 | + if (!is_null($inGroup)) { |
|
| 82 | + return (bool) $inGroup; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $userDN = $this->access->username2dn($uid); |
| 86 | 86 | |
| 87 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
| 87 | + if (isset($this->cachedGroupMembers[$gid])) { |
|
| 88 | 88 | $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
| 89 | 89 | return $isInGroup; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $cacheKeyMembers = 'inGroup-members:'.$gid; |
| 93 | 93 | $members = $this->access->connection->getFromCache($cacheKeyMembers); |
| 94 | - if(!is_null($members)) { |
|
| 94 | + if (!is_null($members)) { |
|
| 95 | 95 | $this->cachedGroupMembers[$gid] = $members; |
| 96 | 96 | $isInGroup = in_array($userDN, $members); |
| 97 | 97 | $this->access->connection->writeToCache($cacheKey, $isInGroup); |
@@ -100,13 +100,13 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | $groupDN = $this->access->groupname2dn($gid); |
| 102 | 102 | // just in case |
| 103 | - if(!$groupDN || !$userDN) { |
|
| 103 | + if (!$groupDN || !$userDN) { |
|
| 104 | 104 | $this->access->connection->writeToCache($cacheKey, false); |
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | //check primary group first |
| 109 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 109 | + if ($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 110 | 110 | $this->access->connection->writeToCache($cacheKey, true); |
| 111 | 111 | return true; |
| 112 | 112 | } |
@@ -114,21 +114,21 @@ discard block |
||
| 114 | 114 | //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
| 115 | 115 | $members = $this->_groupMembers($groupDN); |
| 116 | 116 | $members = array_keys($members); // uids are returned as keys |
| 117 | - if(!is_array($members) || count($members) === 0) { |
|
| 117 | + if (!is_array($members) || count($members) === 0) { |
|
| 118 | 118 | $this->access->connection->writeToCache($cacheKey, false); |
| 119 | 119 | return false; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | //extra work if we don't get back user DNs |
| 123 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 123 | + if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 124 | 124 | $dns = array(); |
| 125 | 125 | $filterParts = array(); |
| 126 | 126 | $bytes = 0; |
| 127 | - foreach($members as $mid) { |
|
| 127 | + foreach ($members as $mid) { |
|
| 128 | 128 | $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
| 129 | 129 | $filterParts[] = $filter; |
| 130 | 130 | $bytes += strlen($filter); |
| 131 | - if($bytes >= 9000000) { |
|
| 131 | + if ($bytes >= 9000000) { |
|
| 132 | 132 | // AD has a default input buffer of 10 MB, we do not want |
| 133 | 133 | // to take even the chance to exceed it |
| 134 | 134 | $filter = $this->access->combineFilterWithOr($filterParts); |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | $dns = array_merge($dns, $users); |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | - if(count($filterParts) > 0) { |
|
| 141 | + if (count($filterParts) > 0) { |
|
| 142 | 142 | $filter = $this->access->combineFilterWithOr($filterParts); |
| 143 | 143 | $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
| 144 | 144 | $dns = array_merge($dns, $users); |
@@ -181,14 +181,14 @@ discard block |
||
| 181 | 181 | $pos = strpos($memberURLs[0], '('); |
| 182 | 182 | if ($pos !== false) { |
| 183 | 183 | $memberUrlFilter = substr($memberURLs[0], $pos); |
| 184 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 184 | + $foundMembers = $this->access->searchUsers($memberUrlFilter, 'dn'); |
|
| 185 | 185 | $dynamicMembers = array(); |
| 186 | - foreach($foundMembers as $value) { |
|
| 186 | + foreach ($foundMembers as $value) { |
|
| 187 | 187 | $dynamicMembers[$value['dn'][0]] = 1; |
| 188 | 188 | } |
| 189 | 189 | } else { |
| 190 | 190 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
| 191 | - 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 191 | + 'of group '.$dnGroup, \OCP\Util::DEBUG); |
|
| 192 | 192 | } |
| 193 | 193 | } |
| 194 | 194 | return $dynamicMembers; |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | // used extensively in cron job, caching makes sense for nested groups |
| 212 | 212 | $cacheKey = '_groupMembers'.$dnGroup; |
| 213 | 213 | $groupMembers = $this->access->connection->getFromCache($cacheKey); |
| 214 | - if(!is_null($groupMembers)) { |
|
| 214 | + if (!is_null($groupMembers)) { |
|
| 215 | 215 | return $groupMembers; |
| 216 | 216 | } |
| 217 | 217 | $seen[$dnGroup] = 1; |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | return array(); |
| 256 | 256 | } |
| 257 | 257 | $groups = $this->access->groupsMatchFilter($groups); |
| 258 | - $allGroups = $groups; |
|
| 258 | + $allGroups = $groups; |
|
| 259 | 259 | $nestedGroups = $this->access->connection->ldapNestedGroups; |
| 260 | 260 | if (intval($nestedGroups) === 1) { |
| 261 | 261 | foreach ($groups as $group) { |
@@ -275,22 +275,22 @@ discard block |
||
| 275 | 275 | public function primaryGroupID2Name($gid, $dn) { |
| 276 | 276 | $cacheKey = 'primaryGroupIDtoName'; |
| 277 | 277 | $groupNames = $this->access->connection->getFromCache($cacheKey); |
| 278 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 278 | + if (!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 279 | 279 | return $groupNames[$gid]; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | $domainObjectSid = $this->access->getSID($dn); |
| 283 | - if($domainObjectSid === false) { |
|
| 283 | + if ($domainObjectSid === false) { |
|
| 284 | 284 | return false; |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | //we need to get the DN from LDAP |
| 288 | 288 | $filter = $this->access->combineFilterWithAnd(array( |
| 289 | 289 | $this->access->connection->ldapGroupFilter, |
| 290 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 290 | + 'objectsid='.$domainObjectSid.'-'.$gid |
|
| 291 | 291 | )); |
| 292 | 292 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
| 293 | - if(empty($result)) { |
|
| 293 | + if (empty($result)) { |
|
| 294 | 294 | return false; |
| 295 | 295 | } |
| 296 | 296 | $dn = $result[0]['dn'][0]; |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | */ |
| 314 | 314 | private function getEntryGroupID($dn, $attribute) { |
| 315 | 315 | $value = $this->access->readAttribute($dn, $attribute); |
| 316 | - if(is_array($value) && !empty($value)) { |
|
| 316 | + if (is_array($value) && !empty($value)) { |
|
| 317 | 317 | return $value[0]; |
| 318 | 318 | } |
| 319 | 319 | return false; |
@@ -335,9 +335,9 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | public function getUserPrimaryGroupIDs($dn) { |
| 337 | 337 | $primaryGroupID = false; |
| 338 | - if($this->access->connection->hasPrimaryGroups) { |
|
| 338 | + if ($this->access->connection->hasPrimaryGroups) { |
|
| 339 | 339 | $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
| 340 | - if($primaryGroupID === false) { |
|
| 340 | + if ($primaryGroupID === false) { |
|
| 341 | 341 | $this->access->connection->hasPrimaryGroups = false; |
| 342 | 342 | } |
| 343 | 343 | } |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | */ |
| 355 | 355 | private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
| 356 | 356 | $groupID = $this->getGroupPrimaryGroupID($groupDN); |
| 357 | - if($groupID === false) { |
|
| 357 | + if ($groupID === false) { |
|
| 358 | 358 | throw new \Exception('Not a valid group'); |
| 359 | 359 | } |
| 360 | 360 | |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | if ($search !== '') { |
| 364 | 364 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
| 365 | 365 | } |
| 366 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 366 | + $filterParts[] = 'primaryGroupID='.$groupID; |
|
| 367 | 367 | |
| 368 | 368 | $filter = $this->access->combineFilterWithAnd($filterParts); |
| 369 | 369 | |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | try { |
| 408 | 408 | $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
| 409 | 409 | $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
| 410 | - return (int)$users; |
|
| 410 | + return (int) $users; |
|
| 411 | 411 | } catch (\Exception $e) { |
| 412 | 412 | return 0; |
| 413 | 413 | } |
@@ -420,9 +420,9 @@ discard block |
||
| 420 | 420 | */ |
| 421 | 421 | public function getUserPrimaryGroup($dn) { |
| 422 | 422 | $groupID = $this->getUserPrimaryGroupIDs($dn); |
| 423 | - if($groupID !== false) { |
|
| 423 | + if ($groupID !== false) { |
|
| 424 | 424 | $groupName = $this->primaryGroupID2Name($groupID, $dn); |
| 425 | - if($groupName !== false) { |
|
| 425 | + if ($groupName !== false) { |
|
| 426 | 426 | return $groupName; |
| 427 | 427 | } |
| 428 | 428 | } |
@@ -441,16 +441,16 @@ discard block |
||
| 441 | 441 | * This function includes groups based on dynamic group membership. |
| 442 | 442 | */ |
| 443 | 443 | public function getUserGroups($uid) { |
| 444 | - if(!$this->enabled) { |
|
| 444 | + if (!$this->enabled) { |
|
| 445 | 445 | return array(); |
| 446 | 446 | } |
| 447 | 447 | $cacheKey = 'getUserGroups'.$uid; |
| 448 | 448 | $userGroups = $this->access->connection->getFromCache($cacheKey); |
| 449 | - if(!is_null($userGroups)) { |
|
| 449 | + if (!is_null($userGroups)) { |
|
| 450 | 450 | return $userGroups; |
| 451 | 451 | } |
| 452 | 452 | $userDN = $this->access->username2dn($uid); |
| 453 | - if(!$userDN) { |
|
| 453 | + if (!$userDN) { |
|
| 454 | 454 | $this->access->connection->writeToCache($cacheKey, array()); |
| 455 | 455 | return array(); |
| 456 | 456 | } |
@@ -463,14 +463,14 @@ discard block |
||
| 463 | 463 | if (!empty($dynamicGroupMemberURL)) { |
| 464 | 464 | // look through dynamic groups to add them to the result array if needed |
| 465 | 465 | $groupsToMatch = $this->access->fetchListOfGroups( |
| 466 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 467 | - foreach($groupsToMatch as $dynamicGroup) { |
|
| 466 | + $this->access->connection->ldapGroupFilter, array('dn', $dynamicGroupMemberURL)); |
|
| 467 | + foreach ($groupsToMatch as $dynamicGroup) { |
|
| 468 | 468 | if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
| 469 | 469 | continue; |
| 470 | 470 | } |
| 471 | 471 | $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
| 472 | 472 | if ($pos !== false) { |
| 473 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 473 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0], $pos); |
|
| 474 | 474 | // apply filter via ldap search to see if this user is in this |
| 475 | 475 | // dynamic group |
| 476 | 476 | $userMatch = $this->access->readAttribute( |
@@ -481,7 +481,7 @@ discard block |
||
| 481 | 481 | if ($userMatch !== false) { |
| 482 | 482 | // match found so this user is in this group |
| 483 | 483 | $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
| 484 | - if(is_string($groupName)) { |
|
| 484 | + if (is_string($groupName)) { |
|
| 485 | 485 | // be sure to never return false if the dn could not be |
| 486 | 486 | // resolved to a name, for whatever reason. |
| 487 | 487 | $groups[] = $groupName; |
@@ -489,21 +489,21 @@ discard block |
||
| 489 | 489 | } |
| 490 | 490 | } else { |
| 491 | 491 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
| 492 | - 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 492 | + 'of group '.print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 493 | 493 | } |
| 494 | 494 | } |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | // if possible, read out membership via memberOf. It's far faster than |
| 498 | 498 | // performing a search, which still is a fallback later. |
| 499 | - if(intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 499 | + if (intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 500 | 500 | && intval($this->access->connection->useMemberOfToDetectMembership) === 1 |
| 501 | 501 | ) { |
| 502 | 502 | $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
| 503 | 503 | if (is_array($groupDNs)) { |
| 504 | 504 | foreach ($groupDNs as $dn) { |
| 505 | 505 | $groupName = $this->access->dn2groupname($dn); |
| 506 | - if(is_string($groupName)) { |
|
| 506 | + if (is_string($groupName)) { |
|
| 507 | 507 | // be sure to never return false if the dn could not be |
| 508 | 508 | // resolved to a name, for whatever reason. |
| 509 | 509 | $groups[] = $groupName; |
@@ -511,7 +511,7 @@ discard block |
||
| 511 | 511 | } |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | - if($primaryGroup !== false) { |
|
| 514 | + if ($primaryGroup !== false) { |
|
| 515 | 515 | $groups[] = $primaryGroup; |
| 516 | 516 | } |
| 517 | 517 | $this->access->connection->writeToCache($cacheKey, $groups); |
@@ -519,14 +519,14 @@ discard block |
||
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
| 522 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 522 | + if ((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 523 | 523 | || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
| 524 | 524 | ) { |
| 525 | 525 | $uid = $userDN; |
| 526 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 526 | + } else if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 527 | 527 | $result = $this->access->readAttribute($userDN, 'uid'); |
| 528 | 528 | if ($result === false) { |
| 529 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 529 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN '.$userDN.' on '. |
|
| 530 | 530 | $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
| 531 | 531 | } |
| 532 | 532 | $uid = $result[0]; |
@@ -535,7 +535,7 @@ discard block |
||
| 535 | 535 | $uid = $userDN; |
| 536 | 536 | } |
| 537 | 537 | |
| 538 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 538 | + if (isset($this->cachedGroupsByMember[$uid])) { |
|
| 539 | 539 | $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
| 540 | 540 | } else { |
| 541 | 541 | $groupsByMember = array_values($this->getGroupsByMember($uid)); |
@@ -544,7 +544,7 @@ discard block |
||
| 544 | 544 | $groups = array_merge($groups, $groupsByMember); |
| 545 | 545 | } |
| 546 | 546 | |
| 547 | - if($primaryGroup !== false) { |
|
| 547 | + if ($primaryGroup !== false) { |
|
| 548 | 548 | $groups[] = $primaryGroup; |
| 549 | 549 | } |
| 550 | 550 | |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | $nestedGroups = $this->access->connection->ldapNestedGroups; |
| 583 | 583 | if (!empty($nestedGroups)) { |
| 584 | 584 | $supergroups = $this->getGroupsByMember($groupDN, $seen); |
| 585 | - if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 585 | + if (is_array($supergroups) && (count($supergroups) > 0)) { |
|
| 586 | 586 | $allGroups = array_merge($allGroups, $supergroups); |
| 587 | 587 | } |
| 588 | 588 | } |
@@ -601,33 +601,33 @@ discard block |
||
| 601 | 601 | * @return array with user ids |
| 602 | 602 | */ |
| 603 | 603 | public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
| 604 | - if(!$this->enabled) { |
|
| 604 | + if (!$this->enabled) { |
|
| 605 | 605 | return array(); |
| 606 | 606 | } |
| 607 | - if(!$this->groupExists($gid)) { |
|
| 607 | + if (!$this->groupExists($gid)) { |
|
| 608 | 608 | return array(); |
| 609 | 609 | } |
| 610 | 610 | $search = $this->access->escapeFilterPart($search, true); |
| 611 | 611 | $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
| 612 | 612 | // check for cache of the exact query |
| 613 | 613 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
| 614 | - if(!is_null($groupUsers)) { |
|
| 614 | + if (!is_null($groupUsers)) { |
|
| 615 | 615 | return $groupUsers; |
| 616 | 616 | } |
| 617 | 617 | |
| 618 | 618 | // check for cache of the query without limit and offset |
| 619 | 619 | $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
| 620 | - if(!is_null($groupUsers)) { |
|
| 620 | + if (!is_null($groupUsers)) { |
|
| 621 | 621 | $groupUsers = array_slice($groupUsers, $offset, $limit); |
| 622 | 622 | $this->access->connection->writeToCache($cacheKey, $groupUsers); |
| 623 | 623 | return $groupUsers; |
| 624 | 624 | } |
| 625 | 625 | |
| 626 | - if($limit === -1) { |
|
| 626 | + if ($limit === -1) { |
|
| 627 | 627 | $limit = null; |
| 628 | 628 | } |
| 629 | 629 | $groupDN = $this->access->groupname2dn($gid); |
| 630 | - if(!$groupDN) { |
|
| 630 | + if (!$groupDN) { |
|
| 631 | 631 | // group couldn't be found, return empty resultset |
| 632 | 632 | $this->access->connection->writeToCache($cacheKey, array()); |
| 633 | 633 | return array(); |
@@ -635,7 +635,7 @@ discard block |
||
| 635 | 635 | |
| 636 | 636 | $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
| 637 | 637 | $members = array_keys($this->_groupMembers($groupDN)); |
| 638 | - if(!$members && empty($primaryUsers)) { |
|
| 638 | + if (!$members && empty($primaryUsers)) { |
|
| 639 | 639 | //in case users could not be retrieved, return empty result set |
| 640 | 640 | $this->access->connection->writeToCache($cacheKey, array()); |
| 641 | 641 | return array(); |
@@ -644,29 +644,29 @@ discard block |
||
| 644 | 644 | $groupUsers = array(); |
| 645 | 645 | $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
| 646 | 646 | $attrs = $this->access->userManager->getAttributes(true); |
| 647 | - foreach($members as $member) { |
|
| 648 | - if($isMemberUid) { |
|
| 647 | + foreach ($members as $member) { |
|
| 648 | + if ($isMemberUid) { |
|
| 649 | 649 | //we got uids, need to get their DNs to 'translate' them to user names |
| 650 | 650 | $filter = $this->access->combineFilterWithAnd(array( |
| 651 | 651 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
| 652 | 652 | $this->access->getFilterPartForUserSearch($search) |
| 653 | 653 | )); |
| 654 | 654 | $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
| 655 | - if(count($ldap_users) < 1) { |
|
| 655 | + if (count($ldap_users) < 1) { |
|
| 656 | 656 | continue; |
| 657 | 657 | } |
| 658 | 658 | $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
| 659 | 659 | } else { |
| 660 | 660 | //we got DNs, check if we need to filter by search or we can give back all of them |
| 661 | 661 | if ($search !== '') { |
| 662 | - if(!$this->access->readAttribute($member, |
|
| 662 | + if (!$this->access->readAttribute($member, |
|
| 663 | 663 | $this->access->connection->ldapUserDisplayName, |
| 664 | 664 | $this->access->getFilterPartForUserSearch($search))) { |
| 665 | 665 | continue; |
| 666 | 666 | } |
| 667 | 667 | } |
| 668 | 668 | // dn2username will also check if the users belong to the allowed base |
| 669 | - if($ocname = $this->access->dn2username($member)) { |
|
| 669 | + if ($ocname = $this->access->dn2username($member)) { |
|
| 670 | 670 | $groupUsers[] = $ocname; |
| 671 | 671 | } |
| 672 | 672 | } |
@@ -691,16 +691,16 @@ discard block |
||
| 691 | 691 | */ |
| 692 | 692 | public function countUsersInGroup($gid, $search = '') { |
| 693 | 693 | $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
| 694 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 694 | + if (!$this->enabled || !$this->groupExists($gid)) { |
|
| 695 | 695 | return false; |
| 696 | 696 | } |
| 697 | 697 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
| 698 | - if(!is_null($groupUsers)) { |
|
| 698 | + if (!is_null($groupUsers)) { |
|
| 699 | 699 | return $groupUsers; |
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | $groupDN = $this->access->groupname2dn($gid); |
| 703 | - if(!$groupDN) { |
|
| 703 | + if (!$groupDN) { |
|
| 704 | 704 | // group couldn't be found, return empty result set |
| 705 | 705 | $this->access->connection->writeToCache($cacheKey, false); |
| 706 | 706 | return false; |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | |
| 709 | 709 | $members = array_keys($this->_groupMembers($groupDN)); |
| 710 | 710 | $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
| 711 | - if(!$members && $primaryUserCount === 0) { |
|
| 711 | + if (!$members && $primaryUserCount === 0) { |
|
| 712 | 712 | //in case users could not be retrieved, return empty result set |
| 713 | 713 | $this->access->connection->writeToCache($cacheKey, false); |
| 714 | 714 | return false; |
@@ -733,27 +733,27 @@ discard block |
||
| 733 | 733 | //For now this is not important, because the only use of this method |
| 734 | 734 | //does not supply a search string |
| 735 | 735 | $groupUsers = array(); |
| 736 | - foreach($members as $member) { |
|
| 737 | - if($isMemberUid) { |
|
| 736 | + foreach ($members as $member) { |
|
| 737 | + if ($isMemberUid) { |
|
| 738 | 738 | //we got uids, need to get their DNs to 'translate' them to user names |
| 739 | 739 | $filter = $this->access->combineFilterWithAnd(array( |
| 740 | 740 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
| 741 | 741 | $this->access->getFilterPartForUserSearch($search) |
| 742 | 742 | )); |
| 743 | 743 | $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
| 744 | - if(count($ldap_users) < 1) { |
|
| 744 | + if (count($ldap_users) < 1) { |
|
| 745 | 745 | continue; |
| 746 | 746 | } |
| 747 | 747 | $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
| 748 | 748 | } else { |
| 749 | 749 | //we need to apply the search filter now |
| 750 | - if(!$this->access->readAttribute($member, |
|
| 750 | + if (!$this->access->readAttribute($member, |
|
| 751 | 751 | $this->access->connection->ldapUserDisplayName, |
| 752 | 752 | $this->access->getFilterPartForUserSearch($search))) { |
| 753 | 753 | continue; |
| 754 | 754 | } |
| 755 | 755 | // dn2username will also check if the users belong to the allowed base |
| 756 | - if($ocname = $this->access->dn2username($member)) { |
|
| 756 | + if ($ocname = $this->access->dn2username($member)) { |
|
| 757 | 757 | $groupUsers[] = $ocname; |
| 758 | 758 | } |
| 759 | 759 | } |
@@ -776,7 +776,7 @@ discard block |
||
| 776 | 776 | * Returns a list with all groups (used by getGroups) |
| 777 | 777 | */ |
| 778 | 778 | protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
| 779 | - if(!$this->enabled) { |
|
| 779 | + if (!$this->enabled) { |
|
| 780 | 780 | return array(); |
| 781 | 781 | } |
| 782 | 782 | $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
@@ -784,13 +784,13 @@ discard block |
||
| 784 | 784 | //Check cache before driving unnecessary searches |
| 785 | 785 | \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
| 786 | 786 | $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
| 787 | - if(!is_null($ldap_groups)) { |
|
| 787 | + if (!is_null($ldap_groups)) { |
|
| 788 | 788 | return $ldap_groups; |
| 789 | 789 | } |
| 790 | 790 | |
| 791 | 791 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
| 792 | 792 | // error. With a limit of 0, we get 0 results. So we pass null. |
| 793 | - if($limit <= 0) { |
|
| 793 | + if ($limit <= 0) { |
|
| 794 | 794 | $limit = null; |
| 795 | 795 | } |
| 796 | 796 | $filter = $this->access->combineFilterWithAnd(array( |
@@ -822,7 +822,7 @@ discard block |
||
| 822 | 822 | * (active directory has a limit of 1000 by default) |
| 823 | 823 | */ |
| 824 | 824 | public function getGroups($search = '', $limit = -1, $offset = 0) { |
| 825 | - if(!$this->enabled) { |
|
| 825 | + if (!$this->enabled) { |
|
| 826 | 826 | return array(); |
| 827 | 827 | } |
| 828 | 828 | $search = $this->access->escapeFilterPart($search, true); |
@@ -869,20 +869,20 @@ discard block |
||
| 869 | 869 | */ |
| 870 | 870 | public function groupExists($gid) { |
| 871 | 871 | $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
| 872 | - if(!is_null($groupExists)) { |
|
| 873 | - return (bool)$groupExists; |
|
| 872 | + if (!is_null($groupExists)) { |
|
| 873 | + return (bool) $groupExists; |
|
| 874 | 874 | } |
| 875 | 875 | |
| 876 | 876 | //getting dn, if false the group does not exist. If dn, it may be mapped |
| 877 | 877 | //only, requires more checking. |
| 878 | 878 | $dn = $this->access->groupname2dn($gid); |
| 879 | - if(!$dn) { |
|
| 879 | + if (!$dn) { |
|
| 880 | 880 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
| 881 | 881 | return false; |
| 882 | 882 | } |
| 883 | 883 | |
| 884 | 884 | //if group really still exists, we will be able to read its objectclass |
| 885 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 885 | + if (!is_array($this->access->readAttribute($dn, ''))) { |
|
| 886 | 886 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
| 887 | 887 | return false; |
| 888 | 888 | } |
@@ -900,7 +900,7 @@ discard block |
||
| 900 | 900 | * compared with OC_USER_BACKEND_CREATE_USER etc. |
| 901 | 901 | */ |
| 902 | 902 | public function implementsActions($actions) { |
| 903 | - return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 903 | + return (bool) (\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 904 | 904 | } |
| 905 | 905 | |
| 906 | 906 | /** |
@@ -36,101 +36,101 @@ |
||
| 36 | 36 | use OCA\User_LDAP\User_Proxy; |
| 37 | 37 | |
| 38 | 38 | class CheckUser extends Command { |
| 39 | - /** @var \OCA\User_LDAP\User_Proxy */ |
|
| 40 | - protected $backend; |
|
| 41 | - |
|
| 42 | - /** @var \OCA\User_LDAP\Helper */ |
|
| 43 | - protected $helper; |
|
| 44 | - |
|
| 45 | - /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ |
|
| 46 | - protected $dui; |
|
| 47 | - |
|
| 48 | - /** @var \OCA\User_LDAP\Mapping\UserMapping */ |
|
| 49 | - protected $mapping; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param User_Proxy $uBackend |
|
| 53 | - * @param LDAPHelper $helper |
|
| 54 | - * @param DeletedUsersIndex $dui |
|
| 55 | - * @param UserMapping $mapping |
|
| 56 | - */ |
|
| 57 | - public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) { |
|
| 58 | - $this->backend = $uBackend; |
|
| 59 | - $this->helper = $helper; |
|
| 60 | - $this->dui = $dui; |
|
| 61 | - $this->mapping = $mapping; |
|
| 62 | - parent::__construct(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - protected function configure() { |
|
| 66 | - $this |
|
| 67 | - ->setName('ldap:check-user') |
|
| 68 | - ->setDescription('checks whether a user exists on LDAP.') |
|
| 69 | - ->addArgument( |
|
| 70 | - 'ocName', |
|
| 71 | - InputArgument::REQUIRED, |
|
| 72 | - 'the user name as used in ownCloud' |
|
| 73 | - ) |
|
| 74 | - ->addOption( |
|
| 75 | - 'force', |
|
| 76 | - null, |
|
| 77 | - InputOption::VALUE_NONE, |
|
| 78 | - 'ignores disabled LDAP configuration' |
|
| 79 | - ) |
|
| 80 | - ; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 84 | - try { |
|
| 85 | - $uid = $input->getArgument('ocName'); |
|
| 86 | - $this->isAllowed($input->getOption('force')); |
|
| 87 | - $this->confirmUserIsMapped($uid); |
|
| 88 | - $exists = $this->backend->userExistsOnLDAP($uid); |
|
| 89 | - if($exists === true) { |
|
| 90 | - $output->writeln('The user is still available on LDAP.'); |
|
| 91 | - return; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $this->dui->markUser($uid); |
|
| 95 | - $output->writeln('The user does not exists on LDAP anymore.'); |
|
| 96 | - $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "' |
|
| 97 | - . $uid . '"'); |
|
| 98 | - } catch (\Exception $e) { |
|
| 99 | - $output->writeln('<error>' . $e->getMessage(). '</error>'); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * checks whether a user is actually mapped |
|
| 105 | - * @param string $ocName the username as used in ownCloud |
|
| 106 | - * @throws \Exception |
|
| 107 | - * @return true |
|
| 108 | - */ |
|
| 109 | - protected function confirmUserIsMapped($ocName) { |
|
| 110 | - $dn = $this->mapping->getDNByName($ocName); |
|
| 111 | - if ($dn === false) { |
|
| 112 | - throw new \Exception('The given user is not a recognized LDAP user.'); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return true; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * checks whether the setup allows reliable checking of LDAP user existence |
|
| 120 | - * @throws \Exception |
|
| 121 | - * @return true |
|
| 122 | - */ |
|
| 123 | - protected function isAllowed($force) { |
|
| 124 | - if($this->helper->haveDisabledConfigurations() && !$force) { |
|
| 125 | - throw new \Exception('Cannot check user existence, because ' |
|
| 126 | - . 'disabled LDAP configurations are present.'); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // we don't check ldapUserCleanupInterval from config.php because this |
|
| 130 | - // action is triggered manually, while the setting only controls the |
|
| 131 | - // background job. |
|
| 132 | - |
|
| 133 | - return true; |
|
| 134 | - } |
|
| 39 | + /** @var \OCA\User_LDAP\User_Proxy */ |
|
| 40 | + protected $backend; |
|
| 41 | + |
|
| 42 | + /** @var \OCA\User_LDAP\Helper */ |
|
| 43 | + protected $helper; |
|
| 44 | + |
|
| 45 | + /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ |
|
| 46 | + protected $dui; |
|
| 47 | + |
|
| 48 | + /** @var \OCA\User_LDAP\Mapping\UserMapping */ |
|
| 49 | + protected $mapping; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param User_Proxy $uBackend |
|
| 53 | + * @param LDAPHelper $helper |
|
| 54 | + * @param DeletedUsersIndex $dui |
|
| 55 | + * @param UserMapping $mapping |
|
| 56 | + */ |
|
| 57 | + public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) { |
|
| 58 | + $this->backend = $uBackend; |
|
| 59 | + $this->helper = $helper; |
|
| 60 | + $this->dui = $dui; |
|
| 61 | + $this->mapping = $mapping; |
|
| 62 | + parent::__construct(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + protected function configure() { |
|
| 66 | + $this |
|
| 67 | + ->setName('ldap:check-user') |
|
| 68 | + ->setDescription('checks whether a user exists on LDAP.') |
|
| 69 | + ->addArgument( |
|
| 70 | + 'ocName', |
|
| 71 | + InputArgument::REQUIRED, |
|
| 72 | + 'the user name as used in ownCloud' |
|
| 73 | + ) |
|
| 74 | + ->addOption( |
|
| 75 | + 'force', |
|
| 76 | + null, |
|
| 77 | + InputOption::VALUE_NONE, |
|
| 78 | + 'ignores disabled LDAP configuration' |
|
| 79 | + ) |
|
| 80 | + ; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 84 | + try { |
|
| 85 | + $uid = $input->getArgument('ocName'); |
|
| 86 | + $this->isAllowed($input->getOption('force')); |
|
| 87 | + $this->confirmUserIsMapped($uid); |
|
| 88 | + $exists = $this->backend->userExistsOnLDAP($uid); |
|
| 89 | + if($exists === true) { |
|
| 90 | + $output->writeln('The user is still available on LDAP.'); |
|
| 91 | + return; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $this->dui->markUser($uid); |
|
| 95 | + $output->writeln('The user does not exists on LDAP anymore.'); |
|
| 96 | + $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "' |
|
| 97 | + . $uid . '"'); |
|
| 98 | + } catch (\Exception $e) { |
|
| 99 | + $output->writeln('<error>' . $e->getMessage(). '</error>'); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * checks whether a user is actually mapped |
|
| 105 | + * @param string $ocName the username as used in ownCloud |
|
| 106 | + * @throws \Exception |
|
| 107 | + * @return true |
|
| 108 | + */ |
|
| 109 | + protected function confirmUserIsMapped($ocName) { |
|
| 110 | + $dn = $this->mapping->getDNByName($ocName); |
|
| 111 | + if ($dn === false) { |
|
| 112 | + throw new \Exception('The given user is not a recognized LDAP user.'); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return true; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * checks whether the setup allows reliable checking of LDAP user existence |
|
| 120 | + * @throws \Exception |
|
| 121 | + * @return true |
|
| 122 | + */ |
|
| 123 | + protected function isAllowed($force) { |
|
| 124 | + if($this->helper->haveDisabledConfigurations() && !$force) { |
|
| 125 | + throw new \Exception('Cannot check user existence, because ' |
|
| 126 | + . 'disabled LDAP configurations are present.'); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // we don't check ldapUserCleanupInterval from config.php because this |
|
| 130 | + // action is triggered manually, while the setting only controls the |
|
| 131 | + // background job. |
|
| 132 | + |
|
| 133 | + return true; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | 136 | } |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | $this->isAllowed($input->getOption('force')); |
| 87 | 87 | $this->confirmUserIsMapped($uid); |
| 88 | 88 | $exists = $this->backend->userExistsOnLDAP($uid); |
| 89 | - if($exists === true) { |
|
| 89 | + if ($exists === true) { |
|
| 90 | 90 | $output->writeln('The user is still available on LDAP.'); |
| 91 | 91 | return; |
| 92 | 92 | } |
@@ -94,9 +94,9 @@ discard block |
||
| 94 | 94 | $this->dui->markUser($uid); |
| 95 | 95 | $output->writeln('The user does not exists on LDAP anymore.'); |
| 96 | 96 | $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "' |
| 97 | - . $uid . '"'); |
|
| 97 | + . $uid.'"'); |
|
| 98 | 98 | } catch (\Exception $e) { |
| 99 | - $output->writeln('<error>' . $e->getMessage(). '</error>'); |
|
| 99 | + $output->writeln('<error>'.$e->getMessage().'</error>'); |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | * @return true |
| 122 | 122 | */ |
| 123 | 123 | protected function isAllowed($force) { |
| 124 | - if($this->helper->haveDisabledConfigurations() && !$force) { |
|
| 124 | + if ($this->helper->haveDisabledConfigurations() && !$force) { |
|
| 125 | 125 | throw new \Exception('Cannot check user existence, because ' |
| 126 | 126 | . 'disabled LDAP configurations are present.'); |
| 127 | 127 | } |
@@ -37,93 +37,93 @@ |
||
| 37 | 37 | use OCP\IConfig; |
| 38 | 38 | |
| 39 | 39 | class Search extends Command { |
| 40 | - /** @var \OCP\IConfig */ |
|
| 41 | - protected $ocConfig; |
|
| 40 | + /** @var \OCP\IConfig */ |
|
| 41 | + protected $ocConfig; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param \OCP\IConfig $ocConfig |
|
| 45 | - */ |
|
| 46 | - public function __construct(IConfig $ocConfig) { |
|
| 47 | - $this->ocConfig = $ocConfig; |
|
| 48 | - parent::__construct(); |
|
| 49 | - } |
|
| 43 | + /** |
|
| 44 | + * @param \OCP\IConfig $ocConfig |
|
| 45 | + */ |
|
| 46 | + public function __construct(IConfig $ocConfig) { |
|
| 47 | + $this->ocConfig = $ocConfig; |
|
| 48 | + parent::__construct(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - protected function configure() { |
|
| 52 | - $this |
|
| 53 | - ->setName('ldap:search') |
|
| 54 | - ->setDescription('executes a user or group search') |
|
| 55 | - ->addArgument( |
|
| 56 | - 'search', |
|
| 57 | - InputArgument::REQUIRED, |
|
| 58 | - 'the search string (can be empty)' |
|
| 59 | - ) |
|
| 60 | - ->addOption( |
|
| 61 | - 'group', |
|
| 62 | - null, |
|
| 63 | - InputOption::VALUE_NONE, |
|
| 64 | - 'searches groups instead of users' |
|
| 65 | - ) |
|
| 66 | - ->addOption( |
|
| 67 | - 'offset', |
|
| 68 | - null, |
|
| 69 | - InputOption::VALUE_REQUIRED, |
|
| 70 | - 'The offset of the result set. Needs to be a multiple of limit. defaults to 0.', |
|
| 71 | - 0 |
|
| 72 | - ) |
|
| 73 | - ->addOption( |
|
| 74 | - 'limit', |
|
| 75 | - null, |
|
| 76 | - InputOption::VALUE_REQUIRED, |
|
| 77 | - 'limit the results. 0 means no limit, defaults to 15', |
|
| 78 | - 15 |
|
| 79 | - ) |
|
| 80 | - ; |
|
| 81 | - } |
|
| 51 | + protected function configure() { |
|
| 52 | + $this |
|
| 53 | + ->setName('ldap:search') |
|
| 54 | + ->setDescription('executes a user or group search') |
|
| 55 | + ->addArgument( |
|
| 56 | + 'search', |
|
| 57 | + InputArgument::REQUIRED, |
|
| 58 | + 'the search string (can be empty)' |
|
| 59 | + ) |
|
| 60 | + ->addOption( |
|
| 61 | + 'group', |
|
| 62 | + null, |
|
| 63 | + InputOption::VALUE_NONE, |
|
| 64 | + 'searches groups instead of users' |
|
| 65 | + ) |
|
| 66 | + ->addOption( |
|
| 67 | + 'offset', |
|
| 68 | + null, |
|
| 69 | + InputOption::VALUE_REQUIRED, |
|
| 70 | + 'The offset of the result set. Needs to be a multiple of limit. defaults to 0.', |
|
| 71 | + 0 |
|
| 72 | + ) |
|
| 73 | + ->addOption( |
|
| 74 | + 'limit', |
|
| 75 | + null, |
|
| 76 | + InputOption::VALUE_REQUIRED, |
|
| 77 | + 'limit the results. 0 means no limit, defaults to 15', |
|
| 78 | + 15 |
|
| 79 | + ) |
|
| 80 | + ; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Tests whether the offset and limit options are valid |
|
| 85 | - * @param int $offset |
|
| 86 | - * @param int $limit |
|
| 87 | - * @throws \InvalidArgumentException |
|
| 88 | - */ |
|
| 89 | - protected function validateOffsetAndLimit($offset, $limit) { |
|
| 90 | - if($limit < 0) { |
|
| 91 | - throw new \InvalidArgumentException('limit must be 0 or greater'); |
|
| 92 | - } |
|
| 93 | - if($offset < 0) { |
|
| 94 | - throw new \InvalidArgumentException('offset must be 0 or greater'); |
|
| 95 | - } |
|
| 96 | - if($limit === 0 && $offset !== 0) { |
|
| 97 | - throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0'); |
|
| 98 | - } |
|
| 99 | - if($offset > 0 && ($offset % $limit !== 0)) { |
|
| 100 | - throw new \InvalidArgumentException('offset must be a multiple of limit'); |
|
| 101 | - } |
|
| 102 | - } |
|
| 83 | + /** |
|
| 84 | + * Tests whether the offset and limit options are valid |
|
| 85 | + * @param int $offset |
|
| 86 | + * @param int $limit |
|
| 87 | + * @throws \InvalidArgumentException |
|
| 88 | + */ |
|
| 89 | + protected function validateOffsetAndLimit($offset, $limit) { |
|
| 90 | + if($limit < 0) { |
|
| 91 | + throw new \InvalidArgumentException('limit must be 0 or greater'); |
|
| 92 | + } |
|
| 93 | + if($offset < 0) { |
|
| 94 | + throw new \InvalidArgumentException('offset must be 0 or greater'); |
|
| 95 | + } |
|
| 96 | + if($limit === 0 && $offset !== 0) { |
|
| 97 | + throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0'); |
|
| 98 | + } |
|
| 99 | + if($offset > 0 && ($offset % $limit !== 0)) { |
|
| 100 | + throw new \InvalidArgumentException('offset must be a multiple of limit'); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 105 | - $helper = new Helper($this->ocConfig); |
|
| 106 | - $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
| 107 | - $ldapWrapper = new LDAP(); |
|
| 104 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 105 | + $helper = new Helper($this->ocConfig); |
|
| 106 | + $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
| 107 | + $ldapWrapper = new LDAP(); |
|
| 108 | 108 | |
| 109 | - $offset = intval($input->getOption('offset')); |
|
| 110 | - $limit = intval($input->getOption('limit')); |
|
| 111 | - $this->validateOffsetAndLimit($offset, $limit); |
|
| 109 | + $offset = intval($input->getOption('offset')); |
|
| 110 | + $limit = intval($input->getOption('limit')); |
|
| 111 | + $this->validateOffsetAndLimit($offset, $limit); |
|
| 112 | 112 | |
| 113 | - if($input->getOption('group')) { |
|
| 114 | - $proxy = new Group_Proxy($configPrefixes, $ldapWrapper); |
|
| 115 | - $getMethod = 'getGroups'; |
|
| 116 | - $printID = false; |
|
| 117 | - } else { |
|
| 118 | - $proxy = new User_Proxy($configPrefixes, $ldapWrapper, $this->ocConfig); |
|
| 119 | - $getMethod = 'getDisplayNames'; |
|
| 120 | - $printID = true; |
|
| 121 | - } |
|
| 113 | + if($input->getOption('group')) { |
|
| 114 | + $proxy = new Group_Proxy($configPrefixes, $ldapWrapper); |
|
| 115 | + $getMethod = 'getGroups'; |
|
| 116 | + $printID = false; |
|
| 117 | + } else { |
|
| 118 | + $proxy = new User_Proxy($configPrefixes, $ldapWrapper, $this->ocConfig); |
|
| 119 | + $getMethod = 'getDisplayNames'; |
|
| 120 | + $printID = true; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset); |
|
| 124 | - foreach($result as $id => $name) { |
|
| 125 | - $line = $name . ($printID ? ' ('.$id.')' : ''); |
|
| 126 | - $output->writeln($line); |
|
| 127 | - } |
|
| 128 | - } |
|
| 123 | + $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset); |
|
| 124 | + foreach($result as $id => $name) { |
|
| 125 | + $line = $name . ($printID ? ' ('.$id.')' : ''); |
|
| 126 | + $output->writeln($line); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -87,16 +87,16 @@ discard block |
||
| 87 | 87 | * @throws \InvalidArgumentException |
| 88 | 88 | */ |
| 89 | 89 | protected function validateOffsetAndLimit($offset, $limit) { |
| 90 | - if($limit < 0) { |
|
| 90 | + if ($limit < 0) { |
|
| 91 | 91 | throw new \InvalidArgumentException('limit must be 0 or greater'); |
| 92 | 92 | } |
| 93 | - if($offset < 0) { |
|
| 93 | + if ($offset < 0) { |
|
| 94 | 94 | throw new \InvalidArgumentException('offset must be 0 or greater'); |
| 95 | 95 | } |
| 96 | - if($limit === 0 && $offset !== 0) { |
|
| 96 | + if ($limit === 0 && $offset !== 0) { |
|
| 97 | 97 | throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0'); |
| 98 | 98 | } |
| 99 | - if($offset > 0 && ($offset % $limit !== 0)) { |
|
| 99 | + if ($offset > 0 && ($offset % $limit !== 0)) { |
|
| 100 | 100 | throw new \InvalidArgumentException('offset must be a multiple of limit'); |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $limit = intval($input->getOption('limit')); |
| 111 | 111 | $this->validateOffsetAndLimit($offset, $limit); |
| 112 | 112 | |
| 113 | - if($input->getOption('group')) { |
|
| 113 | + if ($input->getOption('group')) { |
|
| 114 | 114 | $proxy = new Group_Proxy($configPrefixes, $ldapWrapper); |
| 115 | 115 | $getMethod = 'getGroups'; |
| 116 | 116 | $printID = false; |
@@ -121,8 +121,8 @@ discard block |
||
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset); |
| 124 | - foreach($result as $id => $name) { |
|
| 125 | - $line = $name . ($printID ? ' ('.$id.')' : ''); |
|
| 124 | + foreach ($result as $id => $name) { |
|
| 125 | + $line = $name.($printID ? ' ('.$id.')' : ''); |
|
| 126 | 126 | $output->writeln($line); |
| 127 | 127 | } |
| 128 | 128 | } |
@@ -33,39 +33,39 @@ |
||
| 33 | 33 | use Symfony\Component\Console\Output\OutputInterface; |
| 34 | 34 | |
| 35 | 35 | class DeleteConfig extends Command { |
| 36 | - /** @var \OCA\User_LDAP\Helper */ |
|
| 37 | - protected $helper; |
|
| 36 | + /** @var \OCA\User_LDAP\Helper */ |
|
| 37 | + protected $helper; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param Helper $helper |
|
| 41 | - */ |
|
| 42 | - public function __construct(Helper $helper) { |
|
| 43 | - $this->helper = $helper; |
|
| 44 | - parent::__construct(); |
|
| 45 | - } |
|
| 39 | + /** |
|
| 40 | + * @param Helper $helper |
|
| 41 | + */ |
|
| 42 | + public function __construct(Helper $helper) { |
|
| 43 | + $this->helper = $helper; |
|
| 44 | + parent::__construct(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - protected function configure() { |
|
| 48 | - $this |
|
| 49 | - ->setName('ldap:delete-config') |
|
| 50 | - ->setDescription('deletes an existing LDAP configuration') |
|
| 51 | - ->addArgument( |
|
| 52 | - 'configID', |
|
| 53 | - InputArgument::REQUIRED, |
|
| 54 | - 'the configuration ID' |
|
| 55 | - ) |
|
| 56 | - ; |
|
| 57 | - } |
|
| 47 | + protected function configure() { |
|
| 48 | + $this |
|
| 49 | + ->setName('ldap:delete-config') |
|
| 50 | + ->setDescription('deletes an existing LDAP configuration') |
|
| 51 | + ->addArgument( |
|
| 52 | + 'configID', |
|
| 53 | + InputArgument::REQUIRED, |
|
| 54 | + 'the configuration ID' |
|
| 55 | + ) |
|
| 56 | + ; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 61 | - $configPrefix = $input->getArgument('configID'); |
|
| 60 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 61 | + $configPrefix = $input->getArgument('configID'); |
|
| 62 | 62 | |
| 63 | - $success = $this->helper->deleteServerConfiguration($configPrefix); |
|
| 63 | + $success = $this->helper->deleteServerConfiguration($configPrefix); |
|
| 64 | 64 | |
| 65 | - if($success) { |
|
| 66 | - $output->writeln("Deleted configuration with configID '{$configPrefix}'"); |
|
| 67 | - } else { |
|
| 68 | - $output->writeln("Cannot delete configuration with configID '{$configPrefix}'"); |
|
| 69 | - } |
|
| 70 | - } |
|
| 65 | + if($success) { |
|
| 66 | + $output->writeln("Deleted configuration with configID '{$configPrefix}'"); |
|
| 67 | + } else { |
|
| 68 | + $output->writeln("Cannot delete configuration with configID '{$configPrefix}'"); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -62,7 +62,7 @@ |
||
| 62 | 62 | |
| 63 | 63 | $success = $this->helper->deleteServerConfiguration($configPrefix); |
| 64 | 64 | |
| 65 | - if($success) { |
|
| 65 | + if ($success) { |
|
| 66 | 66 | $output->writeln("Deleted configuration with configID '{$configPrefix}'"); |
| 67 | 67 | } else { |
| 68 | 68 | $output->writeln("Cannot delete configuration with configID '{$configPrefix}'"); |
@@ -34,59 +34,59 @@ |
||
| 34 | 34 | |
| 35 | 35 | class TestConfig extends Command { |
| 36 | 36 | |
| 37 | - protected function configure() { |
|
| 38 | - $this |
|
| 39 | - ->setName('ldap:test-config') |
|
| 40 | - ->setDescription('tests an LDAP configuration') |
|
| 41 | - ->addArgument( |
|
| 42 | - 'configID', |
|
| 43 | - InputArgument::REQUIRED, |
|
| 44 | - 'the configuration ID' |
|
| 45 | - ) |
|
| 46 | - ; |
|
| 47 | - } |
|
| 37 | + protected function configure() { |
|
| 38 | + $this |
|
| 39 | + ->setName('ldap:test-config') |
|
| 40 | + ->setDescription('tests an LDAP configuration') |
|
| 41 | + ->addArgument( |
|
| 42 | + 'configID', |
|
| 43 | + InputArgument::REQUIRED, |
|
| 44 | + 'the configuration ID' |
|
| 45 | + ) |
|
| 46 | + ; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 50 | - $helper = new Helper(\OC::$server->getConfig()); |
|
| 51 | - $availableConfigs = $helper->getServerConfigurationPrefixes(); |
|
| 52 | - $configID = $input->getArgument('configID'); |
|
| 53 | - if(!in_array($configID, $availableConfigs)) { |
|
| 54 | - $output->writeln("Invalid configID"); |
|
| 55 | - return; |
|
| 56 | - } |
|
| 49 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 50 | + $helper = new Helper(\OC::$server->getConfig()); |
|
| 51 | + $availableConfigs = $helper->getServerConfigurationPrefixes(); |
|
| 52 | + $configID = $input->getArgument('configID'); |
|
| 53 | + if(!in_array($configID, $availableConfigs)) { |
|
| 54 | + $output->writeln("Invalid configID"); |
|
| 55 | + return; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $result = $this->testConfig($configID); |
|
| 59 | - if($result === 0) { |
|
| 60 | - $output->writeln('The configuration is valid and the connection could be established!'); |
|
| 61 | - } else if($result === 1) { |
|
| 62 | - $output->writeln('The configuration is invalid. Please have a look at the logs for further details.'); |
|
| 63 | - } else if($result === 2) { |
|
| 64 | - $output->writeln('The configuration is valid, but the Bind failed. Please check the server settings and credentials.'); |
|
| 65 | - } else { |
|
| 66 | - $output->writeln('Your LDAP server was kidnapped by aliens.'); |
|
| 67 | - } |
|
| 68 | - } |
|
| 58 | + $result = $this->testConfig($configID); |
|
| 59 | + if($result === 0) { |
|
| 60 | + $output->writeln('The configuration is valid and the connection could be established!'); |
|
| 61 | + } else if($result === 1) { |
|
| 62 | + $output->writeln('The configuration is invalid. Please have a look at the logs for further details.'); |
|
| 63 | + } else if($result === 2) { |
|
| 64 | + $output->writeln('The configuration is valid, but the Bind failed. Please check the server settings and credentials.'); |
|
| 65 | + } else { |
|
| 66 | + $output->writeln('Your LDAP server was kidnapped by aliens.'); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * tests the specified connection |
|
| 72 | - * @param string $configID |
|
| 73 | - * @return int |
|
| 74 | - */ |
|
| 75 | - protected function testConfig($configID) { |
|
| 76 | - $lw = new \OCA\User_LDAP\LDAP(); |
|
| 77 | - $connection = new Connection($lw, $configID); |
|
| 70 | + /** |
|
| 71 | + * tests the specified connection |
|
| 72 | + * @param string $configID |
|
| 73 | + * @return int |
|
| 74 | + */ |
|
| 75 | + protected function testConfig($configID) { |
|
| 76 | + $lw = new \OCA\User_LDAP\LDAP(); |
|
| 77 | + $connection = new Connection($lw, $configID); |
|
| 78 | 78 | |
| 79 | - //ensure validation is run before we attempt the bind |
|
| 80 | - $connection->getConfiguration(); |
|
| 79 | + //ensure validation is run before we attempt the bind |
|
| 80 | + $connection->getConfiguration(); |
|
| 81 | 81 | |
| 82 | - if(!$connection->setConfiguration(array( |
|
| 83 | - 'ldap_configuration_active' => 1, |
|
| 84 | - ))) { |
|
| 85 | - return 1; |
|
| 86 | - } |
|
| 87 | - if($connection->bind()) { |
|
| 88 | - return 0; |
|
| 89 | - } |
|
| 90 | - return 2; |
|
| 91 | - } |
|
| 82 | + if(!$connection->setConfiguration(array( |
|
| 83 | + 'ldap_configuration_active' => 1, |
|
| 84 | + ))) { |
|
| 85 | + return 1; |
|
| 86 | + } |
|
| 87 | + if($connection->bind()) { |
|
| 88 | + return 0; |
|
| 89 | + } |
|
| 90 | + return 2; |
|
| 91 | + } |
|
| 92 | 92 | } |
@@ -50,17 +50,17 @@ discard block |
||
| 50 | 50 | $helper = new Helper(\OC::$server->getConfig()); |
| 51 | 51 | $availableConfigs = $helper->getServerConfigurationPrefixes(); |
| 52 | 52 | $configID = $input->getArgument('configID'); |
| 53 | - if(!in_array($configID, $availableConfigs)) { |
|
| 53 | + if (!in_array($configID, $availableConfigs)) { |
|
| 54 | 54 | $output->writeln("Invalid configID"); |
| 55 | 55 | return; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | $result = $this->testConfig($configID); |
| 59 | - if($result === 0) { |
|
| 59 | + if ($result === 0) { |
|
| 60 | 60 | $output->writeln('The configuration is valid and the connection could be established!'); |
| 61 | - } else if($result === 1) { |
|
| 61 | + } else if ($result === 1) { |
|
| 62 | 62 | $output->writeln('The configuration is invalid. Please have a look at the logs for further details.'); |
| 63 | - } else if($result === 2) { |
|
| 63 | + } else if ($result === 2) { |
|
| 64 | 64 | $output->writeln('The configuration is valid, but the Bind failed. Please check the server settings and credentials.'); |
| 65 | 65 | } else { |
| 66 | 66 | $output->writeln('Your LDAP server was kidnapped by aliens.'); |
@@ -79,12 +79,12 @@ discard block |
||
| 79 | 79 | //ensure validation is run before we attempt the bind |
| 80 | 80 | $connection->getConfiguration(); |
| 81 | 81 | |
| 82 | - if(!$connection->setConfiguration(array( |
|
| 82 | + if (!$connection->setConfiguration(array( |
|
| 83 | 83 | 'ldap_configuration_active' => 1, |
| 84 | 84 | ))) { |
| 85 | 85 | return 1; |
| 86 | 86 | } |
| 87 | - if($connection->bind()) { |
|
| 87 | + if ($connection->bind()) { |
|
| 88 | 88 | return 0; |
| 89 | 89 | } |
| 90 | 90 | return 2; |
@@ -35,61 +35,61 @@ |
||
| 35 | 35 | use OCP\IDateTimeFormatter; |
| 36 | 36 | |
| 37 | 37 | class ShowRemnants extends Command { |
| 38 | - /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ |
|
| 39 | - protected $dui; |
|
| 38 | + /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ |
|
| 39 | + protected $dui; |
|
| 40 | 40 | |
| 41 | - /** @var \OCP\IDateTimeFormatter */ |
|
| 42 | - protected $dateFormatter; |
|
| 41 | + /** @var \OCP\IDateTimeFormatter */ |
|
| 42 | + protected $dateFormatter; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param DeletedUsersIndex $dui |
|
| 46 | - * @param IDateTimeFormatter $dateFormatter |
|
| 47 | - */ |
|
| 48 | - public function __construct(DeletedUsersIndex $dui, IDateTimeFormatter $dateFormatter) { |
|
| 49 | - $this->dui = $dui; |
|
| 50 | - $this->dateFormatter = $dateFormatter; |
|
| 51 | - parent::__construct(); |
|
| 52 | - } |
|
| 44 | + /** |
|
| 45 | + * @param DeletedUsersIndex $dui |
|
| 46 | + * @param IDateTimeFormatter $dateFormatter |
|
| 47 | + */ |
|
| 48 | + public function __construct(DeletedUsersIndex $dui, IDateTimeFormatter $dateFormatter) { |
|
| 49 | + $this->dui = $dui; |
|
| 50 | + $this->dateFormatter = $dateFormatter; |
|
| 51 | + parent::__construct(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - protected function configure() { |
|
| 55 | - $this |
|
| 56 | - ->setName('ldap:show-remnants') |
|
| 57 | - ->setDescription('shows which users are not available on LDAP anymore, but have remnants in ownCloud.') |
|
| 58 | - ->addOption('json', null, InputOption::VALUE_NONE, 'return JSON array instead of pretty table.'); |
|
| 59 | - } |
|
| 54 | + protected function configure() { |
|
| 55 | + $this |
|
| 56 | + ->setName('ldap:show-remnants') |
|
| 57 | + ->setDescription('shows which users are not available on LDAP anymore, but have remnants in ownCloud.') |
|
| 58 | + ->addOption('json', null, InputOption::VALUE_NONE, 'return JSON array instead of pretty table.'); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * executes the command, i.e. creeates and outputs a table of LDAP users marked as deleted |
|
| 63 | - * |
|
| 64 | - * {@inheritdoc} |
|
| 65 | - */ |
|
| 66 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 67 | - /** @var \Symfony\Component\Console\Helper\Table $table */ |
|
| 68 | - $table = new Table($output); |
|
| 69 | - $table->setHeaders(array( |
|
| 70 | - 'ownCloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login', |
|
| 71 | - 'Dir', 'Sharer')); |
|
| 72 | - $rows = array(); |
|
| 73 | - $resultSet = $this->dui->getUsers(); |
|
| 74 | - foreach($resultSet as $user) { |
|
| 75 | - $hAS = $user->getHasActiveShares() ? 'Y' : 'N'; |
|
| 76 | - $lastLogin = ($user->getLastLogin() > 0) ? |
|
| 77 | - $this->dateFormatter->formatDate($user->getLastLogin()) : '-'; |
|
| 78 | - $rows[] = array('ocName' => $user->getOCName(), |
|
| 79 | - 'displayName' => $user->getDisplayName(), |
|
| 80 | - 'uid' => $user->getUID(), |
|
| 81 | - 'dn' => $user->getDN(), |
|
| 82 | - 'lastLogin' => $lastLogin, |
|
| 83 | - 'homePath' => $user->getHomePath(), |
|
| 84 | - 'sharer' => $hAS |
|
| 85 | - ); |
|
| 86 | - } |
|
| 61 | + /** |
|
| 62 | + * executes the command, i.e. creeates and outputs a table of LDAP users marked as deleted |
|
| 63 | + * |
|
| 64 | + * {@inheritdoc} |
|
| 65 | + */ |
|
| 66 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 67 | + /** @var \Symfony\Component\Console\Helper\Table $table */ |
|
| 68 | + $table = new Table($output); |
|
| 69 | + $table->setHeaders(array( |
|
| 70 | + 'ownCloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login', |
|
| 71 | + 'Dir', 'Sharer')); |
|
| 72 | + $rows = array(); |
|
| 73 | + $resultSet = $this->dui->getUsers(); |
|
| 74 | + foreach($resultSet as $user) { |
|
| 75 | + $hAS = $user->getHasActiveShares() ? 'Y' : 'N'; |
|
| 76 | + $lastLogin = ($user->getLastLogin() > 0) ? |
|
| 77 | + $this->dateFormatter->formatDate($user->getLastLogin()) : '-'; |
|
| 78 | + $rows[] = array('ocName' => $user->getOCName(), |
|
| 79 | + 'displayName' => $user->getDisplayName(), |
|
| 80 | + 'uid' => $user->getUID(), |
|
| 81 | + 'dn' => $user->getDN(), |
|
| 82 | + 'lastLogin' => $lastLogin, |
|
| 83 | + 'homePath' => $user->getHomePath(), |
|
| 84 | + 'sharer' => $hAS |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - if ($input->getOption('json')) { |
|
| 89 | - $output->writeln(json_encode($rows)); |
|
| 90 | - } else { |
|
| 91 | - $table->setRows($rows); |
|
| 92 | - $table->render($output); |
|
| 93 | - } |
|
| 94 | - } |
|
| 88 | + if ($input->getOption('json')) { |
|
| 89 | + $output->writeln(json_encode($rows)); |
|
| 90 | + } else { |
|
| 91 | + $table->setRows($rows); |
|
| 92 | + $table->render($output); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -71,7 +71,7 @@ |
||
| 71 | 71 | 'Dir', 'Sharer')); |
| 72 | 72 | $rows = array(); |
| 73 | 73 | $resultSet = $this->dui->getUsers(); |
| 74 | - foreach($resultSet as $user) { |
|
| 74 | + foreach ($resultSet as $user) { |
|
| 75 | 75 | $hAS = $user->getHasActiveShares() ? 'Y' : 'N'; |
| 76 | 76 | $lastLogin = ($user->getLastLogin() > 0) ? |
| 77 | 77 | $this->dateFormatter->formatDate($user->getLastLogin()) : '-'; |