group_ldap.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. if(!$this->groupExists($gid)) {
  130. return array();
  131. }
  132. $cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
  133. // check for cache of the exact query
  134. $groupUsers = $this->connection->getFromCache($cachekey);
  135. if(!is_null($groupUsers)) {
  136. return $groupUsers;
  137. }
  138. // check for cache of the query without limit and offset
  139. $groupUsers = $this->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
  140. if(!is_null($groupUsers)) {
  141. $groupUsers = array_slice($groupUsers, $offset, $limit);
  142. $this->connection->writeToCache($cachekey, $groupUsers);
  143. return $groupUsers;
  144. }
  145. if($limit === -1) {
  146. $limit = null;
  147. }
  148. $groupDN = $this->groupname2dn($gid);
  149. if(!$groupDN) {
  150. // group couldn't be found, return empty resultset
  151. $this->connection->writeToCache($cachekey, array());
  152. return array();
  153. }
  154. $members = $this->readAttribute($groupDN, $this->connection->ldapGroupMemberAssocAttr);
  155. if(!$members) {
  156. //in case users could not be retrieved, return empty resultset
  157. $this->connection->writeToCache($cachekey, array());
  158. return array();
  159. }
  160. $groupUsers = array();
  161. $isMemberUid = (strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid');
  162. foreach($members as $member) {
  163. if($isMemberUid) {
  164. //we got uids, need to get their DNs to 'tranlsate' them to usernames
  165. $filter = $this->combineFilterWithAnd(array(
  166. \OCP\Util::mb_str_replace('%uid', $member,
  167. $this->connection->ldapLoginFilter, 'UTF-8'),
  168. $this->getFilterPartForUserSearch($search)
  169. ));
  170. $ldap_users = $this->fetchListOfUsers($filter, 'dn');
  171. if(count($ldap_users) < 1) {
  172. continue;
  173. }
  174. $groupUsers[] = $this->dn2username($ldap_users[0]);
  175. } else {
  176. //we got DNs, check if we need to filter by search or we can give back all of them
  177. if(!empty($search)) {
  178. if(!$this->readAttribute($member,
  179. $this->connection->ldapUserDisplayName,
  180. $this->getFilterPartForUserSearch($search))) {
  181. continue;
  182. }
  183. }
  184. // dn2username will also check if the users belong to the allowed base
  185. if($ocname = $this->dn2username($member)) {
  186. $groupUsers[] = $ocname;
  187. }
  188. }
  189. }
  190. natsort($groupUsers);
  191. $this->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
  192. $groupUsers = array_slice($groupUsers, $offset, $limit);
  193. $this->connection->writeToCache($cachekey, $groupUsers);
  194. return $groupUsers;
  195. }
  196. /**
  197. * @brief get a list of all display names in a group
  198. * @returns array with display names (value) and user ids(key)
  199. */
  200. public function displayNamesInGroup($gid, $search, $limit, $offset) {
  201. if(!$this->enabled) {
  202. return array();
  203. }
  204. if(!$this->groupExists($gid)) {
  205. return array();
  206. }
  207. $users = $this->usersInGroup($gid, $search, $limit, $offset);
  208. $displayNames = array();
  209. foreach($users as $user) {
  210. $displayNames[$user] = \OC_User::getDisplayName($user);
  211. }
  212. return $displayNames;
  213. }
  214. /**
  215. * @brief get a list of all groups
  216. * @returns array with group names
  217. *
  218. * Returns a list with all groups
  219. */
  220. public function getGroups($search = '', $limit = -1, $offset = 0) {
  221. if(!$this->enabled) {
  222. return array();
  223. }
  224. $cachekey = 'getGroups-'.$search.'-'.$limit.'-'.$offset;
  225. //Check cache before driving unnecessary searches
  226. \OCP\Util::writeLog('user_ldap', 'getGroups '.$cachekey, \OCP\Util::DEBUG);
  227. $ldap_groups = $this->connection->getFromCache($cachekey);
  228. if(!is_null($ldap_groups)) {
  229. return $ldap_groups;
  230. }
  231. // if we'd pass -1 to LDAP search, we'd end up in a Protocol
  232. // error. With a limit of 0, we get 0 results. So we pass null.
  233. if($limit <= 0) {
  234. $limit = null;
  235. }
  236. $filter = $this->combineFilterWithAnd(array(
  237. $this->connection->ldapGroupFilter,
  238. $this->getFilterPartForGroupSearch($search)
  239. ));
  240. \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG);
  241. $ldap_groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'),
  242. $limit, $offset);
  243. $ldap_groups = $this->ownCloudGroupNames($ldap_groups);
  244. $this->connection->writeToCache($cachekey, $ldap_groups);
  245. return $ldap_groups;
  246. }
  247. public function groupMatchesFilter($group) {
  248. return (strripos($group, $this->groupSearch) !== false);
  249. }
  250. /**
  251. * check if a group exists
  252. * @param string $gid
  253. * @return bool
  254. */
  255. public function groupExists($gid) {
  256. if($this->connection->isCached('groupExists'.$gid)) {
  257. return $this->connection->getFromCache('groupExists'.$gid);
  258. }
  259. //getting dn, if false the group does not exist. If dn, it may be mapped only, requires more checking.
  260. $dn = $this->groupname2dn($gid);
  261. if(!$dn) {
  262. $this->connection->writeToCache('groupExists'.$gid, false);
  263. return false;
  264. }
  265. //if group really still exists, we will be able to read its objectclass
  266. $objcs = $this->readAttribute($dn, 'objectclass');
  267. if(!$objcs || empty($objcs)) {
  268. $this->connection->writeToCache('groupExists'.$gid, false);
  269. return false;
  270. }
  271. $this->connection->writeToCache('groupExists'.$gid, true);
  272. return true;
  273. }
  274. /**
  275. * @brief Check if backend implements actions
  276. * @param $actions bitwise-or'ed actions
  277. * @returns boolean
  278. *
  279. * Returns the supported actions as int to be
  280. * compared with OC_USER_BACKEND_CREATE_USER etc.
  281. */
  282. public function implementsActions($actions) {
  283. return (bool)(OC_GROUP_BACKEND_GET_DISPLAYNAME & $actions);
  284. }
  285. }