PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/user_ldap/group_ldap.php

https://github.com/jlgg/simple_trash
PHP | 290 lines | 185 code | 28 blank | 77 comment | 43 complexity | 1097c5f241ea732ca992cae400bd05be MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ownCloud – LDAP group backend
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 2012 Arthur Schiwon blizzz@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\user_ldap;
  23. class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
  24. protected $enabled = false;
  25. public function setConnector(lib\Connection &$connection) {
  26. parent::setConnector($connection);
  27. $filter = $this->connection->ldapGroupFilter;
  28. $gassoc = $this->connection->ldapGroupMemberAssocAttr;
  29. if(!empty($filter) && !empty($gassoc)) {
  30. $this->enabled = true;
  31. }
  32. }
  33. /**
  34. * @brief is user in group?
  35. * @param $uid uid of the user
  36. * @param $gid gid of the group
  37. * @returns true/false
  38. *
  39. * Checks whether the user is member of a group or not.
  40. */
  41. public function inGroup($uid, $gid) {
  42. if(!$this->enabled) {
  43. return false;
  44. }
  45. if($this->connection->isCached('inGroup'.$uid.':'.$gid)) {
  46. return $this->connection->getFromCache('inGroup'.$uid.':'.$gid);
  47. }
  48. $dn_user = $this->username2dn($uid);
  49. $dn_group = $this->groupname2dn($gid);
  50. // just in case
  51. if(!$dn_group || !$dn_user) {
  52. $this->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
  53. return false;
  54. }
  55. //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course.
  56. $members = $this->readAttribute($dn_group, $this->connection->ldapGroupMemberAssocAttr);
  57. if(!$members) {
  58. $this->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
  59. return false;
  60. }
  61. //extra work if we don't get back user DNs
  62. //TODO: this can be done with one LDAP query
  63. if(strtolower($this->connection->ldapGroupMemberAssocAttr) == 'memberuid') {
  64. $dns = array();
  65. foreach($members as $mid) {
  66. $filter = str_replace('%uid', $mid, $this->connection->ldapLoginFilter);
  67. $ldap_users = $this->fetchListOfUsers($filter, 'dn');
  68. if(count($ldap_users) < 1) {
  69. continue;
  70. }
  71. $dns[] = $ldap_users[0];
  72. }
  73. $members = $dns;
  74. }
  75. $isInGroup = in_array($dn_user, $members);
  76. $this->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup);
  77. return $isInGroup;
  78. }
  79. /**
  80. * @brief Get all groups a user belongs to
  81. * @param $uid Name of the user
  82. * @returns array with group names
  83. *
  84. * This function fetches all groups a user belongs to. It does not check
  85. * if the user exists at all.
  86. */
  87. public function getUserGroups($uid) {
  88. if(!$this->enabled) {
  89. return array();
  90. }
  91. $cacheKey = 'getUserGroups'.$uid;
  92. if($this->connection->isCached($cacheKey)) {
  93. return $this->connection->getFromCache($cacheKey);
  94. }
  95. $userDN = $this->username2dn($uid);
  96. if(!$userDN) {
  97. $this->connection->writeToCache($cacheKey, array());
  98. return array();
  99. }
  100. //uniqueMember takes DN, memberuid the uid, so we need to distinguish
  101. if((strtolower($this->connection->ldapGroupMemberAssocAttr) == 'uniquemember')
  102. || (strtolower($this->connection->ldapGroupMemberAssocAttr) == 'member')
  103. ) {
  104. $uid = $userDN;
  105. } else if(strtolower($this->connection->ldapGroupMemberAssocAttr) == 'memberuid') {
  106. $result = $this->readAttribute($userDN, 'uid');
  107. $uid = $result[0];
  108. } else {
  109. // just in case
  110. $uid = $userDN;
  111. }
  112. $filter = $this->combineFilterWithAnd(array(
  113. $this->connection->ldapGroupFilter,
  114. $this->connection->ldapGroupMemberAssocAttr.'='.$uid
  115. ));
  116. $groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'));
  117. $groups = array_unique($this->ownCloudGroupNames($groups), SORT_LOCALE_STRING);
  118. $this->connection->writeToCache($cacheKey, $groups);
  119. return $groups;
  120. }
  121. /**
  122. * @brief get a list of all users in a group
  123. * @returns array with user ids
  124. */
  125. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  126. if(!$this->enabled) {
  127. return array();
  128. }
  129. $cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
  130. // check for cache of the exact query
  131. $groupUsers = $this->connection->getFromCache($cachekey);
  132. if(!is_null($groupUsers)) {
  133. return $groupUsers;
  134. }
  135. // check for cache of the query without limit and offset
  136. $groupUsers = $this->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
  137. if(!is_null($groupUsers)) {
  138. $groupUsers = array_slice($groupUsers, $offset, $limit);
  139. $this->connection->writeToCache($cachekey, $groupUsers);
  140. return $groupUsers;
  141. }
  142. if($limit == -1) {
  143. $limit = null;
  144. }
  145. $groupDN = $this->groupname2dn($gid);
  146. if(!$groupDN) {
  147. // group couldn't be found, return empty resultset
  148. $this->connection->writeToCache($cachekey, array());
  149. return array();
  150. }
  151. $members = $this->readAttribute($groupDN, $this->connection->ldapGroupMemberAssocAttr);
  152. if(!$members) {
  153. //in case users could not be retrieved, return empty resultset
  154. $this->connection->writeToCache($cachekey, array());
  155. return array();
  156. }
  157. $search = empty($search) ? '*' : '*'.$search.'*';
  158. $groupUsers = array();
  159. $isMemberUid = (strtolower($this->connection->ldapGroupMemberAssocAttr) == 'memberuid');
  160. foreach($members as $member) {
  161. if($isMemberUid) {
  162. //we got uids, need to get their DNs to 'tranlsate' them to usernames
  163. $filter = $this->combineFilterWithAnd(array(
  164. \OCP\Util::mb_str_replace('%uid', $member, $this->connection>ldapLoginFilter, 'UTF-8'),
  165. $this->connection->ldapUserDisplayName.'='.$search
  166. ));
  167. $ldap_users = $this->fetchListOfUsers($filter, 'dn');
  168. if(count($ldap_users) < 1) {
  169. continue;
  170. }
  171. $groupUsers[] = $this->dn2username($ldap_users[0]);
  172. } else {
  173. //we got DNs, check if we need to filter by search or we can give back all of them
  174. if($search != '*') {
  175. if(!$this->readAttribute($member, $this->connection->ldapUserDisplayName, $this->connection->ldapUserDisplayName.'='.$search)) {
  176. continue;
  177. }
  178. }
  179. // dn2username will also check if the users belong to the allowed base
  180. if($ocname = $this->dn2username($member)) {
  181. $groupUsers[] = $ocname;
  182. }
  183. }
  184. }
  185. natsort($groupUsers);
  186. $this->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
  187. $groupUsers = array_slice($groupUsers, $offset, $limit);
  188. $this->connection->writeToCache($cachekey, $groupUsers);
  189. return $groupUsers;
  190. }
  191. /**
  192. * @brief get a list of all groups
  193. * @returns array with group names
  194. *
  195. * Returns a list with all groups
  196. */
  197. public function getGroups($search = '', $limit = -1, $offset = 0) {
  198. if(!$this->enabled) {
  199. return array();
  200. }
  201. $cachekey = 'getGroups-'.$search.'-'.$limit.'-'.$offset;
  202. //Check cache before driving unnecessary searches
  203. \OCP\Util::writeLog('user_ldap', 'getGroups '.$cachekey, \OCP\Util::DEBUG);
  204. $ldap_groups = $this->connection->getFromCache($cachekey);
  205. if(!is_null($ldap_groups)) {
  206. return $ldap_groups;
  207. }
  208. // if we'd pass -1 to LDAP search, we'd end up in a Protocol error. With a limit of 0, we get 0 results. So we pass null.
  209. if($limit <= 0) {
  210. $limit = null;
  211. }
  212. $search = empty($search) ? '*' : '*'.$search.'*';
  213. $filter = $this->combineFilterWithAnd(array(
  214. $this->connection->ldapGroupFilter,
  215. $this->connection->ldapGroupDisplayName.'='.$search
  216. ));
  217. \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG);
  218. $ldap_groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'), $limit, $offset);
  219. $ldap_groups = $this->ownCloudGroupNames($ldap_groups);
  220. $this->connection->writeToCache($cachekey, $ldap_groups);
  221. return $ldap_groups;
  222. }
  223. public function groupMatchesFilter($group) {
  224. return (strripos($group, $this->groupSearch) !== false);
  225. }
  226. /**
  227. * check if a group exists
  228. * @param string $gid
  229. * @return bool
  230. */
  231. public function groupExists($gid) {
  232. if($this->connection->isCached('groupExists'.$gid)) {
  233. return $this->connection->getFromCache('groupExists'.$gid);
  234. }
  235. //getting dn, if false the group does not exist. If dn, it may be mapped only, requires more checking.
  236. $dn = $this->groupname2dn($gid);
  237. if(!$dn) {
  238. $this->connection->writeToCache('groupExists'.$gid, false);
  239. return false;
  240. }
  241. //if group really still exists, we will be able to read its objectclass
  242. $objcs = $this->readAttribute($dn, 'objectclass');
  243. if(!$objcs || empty($objcs)) {
  244. $this->connection->writeToCache('groupExists'.$gid, false);
  245. return false;
  246. }
  247. $this->connection->writeToCache('groupExists'.$gid, true);
  248. return true;
  249. }
  250. /**
  251. * @brief Check if backend implements actions
  252. * @param $actions bitwise-or'ed actions
  253. * @returns boolean
  254. *
  255. * Returns the supported actions as int to be
  256. * compared with OC_USER_BACKEND_CREATE_USER etc.
  257. */
  258. public function implementsActions($actions) {
  259. //always returns false, because possible actions are modifying actions. We do not write to LDAP, at least for now.
  260. return false;
  261. }
  262. }