group_ldap.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. use OCA\user_ldap\lib\Access;
  24. use OCA\user_ldap\lib\BackendUtility;
  25. class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
  26. protected $enabled = false;
  27. public function __construct(Access $access) {
  28. parent::__construct($access);
  29. $filter = $this->access->connection->ldapGroupFilter;
  30. $gassoc = $this->access->connection->ldapGroupMemberAssocAttr;
  31. if(!empty($filter) && !empty($gassoc)) {
  32. $this->enabled = true;
  33. }
  34. }
  35. /**
  36. * @brief is user in group?
  37. * @param $uid uid of the user
  38. * @param $gid gid of the group
  39. * @returns true/false
  40. *
  41. * Checks whether the user is member of a group or not.
  42. */
  43. public function inGroup($uid, $gid) {
  44. if(!$this->enabled) {
  45. return false;
  46. }
  47. if($this->access->connection->isCached('inGroup'.$uid.':'.$gid)) {
  48. return $this->access->connection->getFromCache('inGroup'.$uid.':'.$gid);
  49. }
  50. $dn_user = $this->access->username2dn($uid);
  51. $dn_group = $this->access->groupname2dn($gid);
  52. // just in case
  53. if(!$dn_group || !$dn_user) {
  54. $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
  55. return false;
  56. }
  57. //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course.
  58. $members = $this->access->readAttribute($dn_group,
  59. $this->access->connection->ldapGroupMemberAssocAttr);
  60. if(!$members) {
  61. $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
  62. return false;
  63. }
  64. //extra work if we don't get back user DNs
  65. //TODO: this can be done with one LDAP query
  66. if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
  67. $dns = array();
  68. foreach($members as $mid) {
  69. $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter);
  70. $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
  71. if(count($ldap_users) < 1) {
  72. continue;
  73. }
  74. $dns[] = $ldap_users[0];
  75. }
  76. $members = $dns;
  77. }
  78. $isInGroup = in_array($dn_user, $members);
  79. $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup);
  80. return $isInGroup;
  81. }
  82. /**
  83. * @brief Get all groups a user belongs to
  84. * @param $uid Name of the user
  85. * @returns array with group names
  86. *
  87. * This function fetches all groups a user belongs to. It does not check
  88. * if the user exists at all.
  89. */
  90. public function getUserGroups($uid) {
  91. if(!$this->enabled) {
  92. return array();
  93. }
  94. $cacheKey = 'getUserGroups'.$uid;
  95. if($this->access->connection->isCached($cacheKey)) {
  96. return $this->access->connection->getFromCache($cacheKey);
  97. }
  98. $userDN = $this->access->username2dn($uid);
  99. if(!$userDN) {
  100. $this->access->connection->writeToCache($cacheKey, array());
  101. return array();
  102. }
  103. //uniqueMember takes DN, memberuid the uid, so we need to distinguish
  104. if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember')
  105. || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member')
  106. ) {
  107. $uid = $userDN;
  108. } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
  109. $result = $this->access->readAttribute($userDN, 'uid');
  110. $uid = $result[0];
  111. } else {
  112. // just in case
  113. $uid = $userDN;
  114. }
  115. $filter = $this->access->combineFilterWithAnd(array(
  116. $this->access->connection->ldapGroupFilter,
  117. $this->access->connection->ldapGroupMemberAssocAttr.'='.$uid
  118. ));
  119. $groups = $this->access->fetchListOfGroups($filter,
  120. array($this->access->connection->ldapGroupDisplayName, 'dn'));
  121. $groups = array_unique($this->access->ownCloudGroupNames($groups), SORT_LOCALE_STRING);
  122. $this->access->connection->writeToCache($cacheKey, $groups);
  123. return $groups;
  124. }
  125. /**
  126. * @brief get a list of all users in a group
  127. * @returns array with user ids
  128. */
  129. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  130. if(!$this->enabled) {
  131. return array();
  132. }
  133. if(!$this->groupExists($gid)) {
  134. return array();
  135. }
  136. $cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
  137. // check for cache of the exact query
  138. $groupUsers = $this->access->connection->getFromCache($cachekey);
  139. if(!is_null($groupUsers)) {
  140. return $groupUsers;
  141. }
  142. // check for cache of the query without limit and offset
  143. $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
  144. if(!is_null($groupUsers)) {
  145. $groupUsers = array_slice($groupUsers, $offset, $limit);
  146. $this->access->connection->writeToCache($cachekey, $groupUsers);
  147. return $groupUsers;
  148. }
  149. if($limit === -1) {
  150. $limit = null;
  151. }
  152. $groupDN = $this->access->groupname2dn($gid);
  153. if(!$groupDN) {
  154. // group couldn't be found, return empty resultset
  155. $this->access->connection->writeToCache($cachekey, array());
  156. return array();
  157. }
  158. $members = $this->access->readAttribute($groupDN,
  159. $this->access->connection->ldapGroupMemberAssocAttr);
  160. if(!$members) {
  161. //in case users could not be retrieved, return empty resultset
  162. $this->access->connection->writeToCache($cachekey, array());
  163. return array();
  164. }
  165. $groupUsers = array();
  166. $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid');
  167. foreach($members as $member) {
  168. if($isMemberUid) {
  169. //we got uids, need to get their DNs to 'tranlsate' them to usernames
  170. $filter = $this->access->combineFilterWithAnd(array(
  171. \OCP\Util::mb_str_replace('%uid', $member,
  172. $this->access->connection->ldapLoginFilter, 'UTF-8'),
  173. $this->access->getFilterPartForUserSearch($search)
  174. ));
  175. $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
  176. if(count($ldap_users) < 1) {
  177. continue;
  178. }
  179. $groupUsers[] = $this->access->dn2username($ldap_users[0]);
  180. } else {
  181. //we got DNs, check if we need to filter by search or we can give back all of them
  182. if(!empty($search)) {
  183. if(!$this->access->readAttribute($member,
  184. $this->access->connection->ldapUserDisplayName,
  185. $this->access->getFilterPartForUserSearch($search))) {
  186. continue;
  187. }
  188. }
  189. // dn2username will also check if the users belong to the allowed base
  190. if($ocname = $this->access->dn2username($member)) {
  191. $groupUsers[] = $ocname;
  192. }
  193. }
  194. }
  195. natsort($groupUsers);
  196. $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
  197. $groupUsers = array_slice($groupUsers, $offset, $limit);
  198. $this->access->connection->writeToCache($cachekey, $groupUsers);
  199. return $groupUsers;
  200. }
  201. /**
  202. * @brief get a list of all display names in a group
  203. * @returns array with display names (value) and user ids(key)
  204. */
  205. public function displayNamesInGroup($gid, $search, $limit, $offset) {
  206. if(!$this->enabled) {
  207. return array();
  208. }
  209. if(!$this->groupExists($gid)) {
  210. return array();
  211. }
  212. $users = $this->usersInGroup($gid, $search, $limit, $offset);
  213. $displayNames = array();
  214. foreach($users as $user) {
  215. $displayNames[$user] = \OC_User::getDisplayName($user);
  216. }
  217. return $displayNames;
  218. }
  219. /**
  220. * @brief get a list of all groups
  221. * @returns array with group names
  222. *
  223. * Returns a list with all groups
  224. */
  225. public function getGroups($search = '', $limit = -1, $offset = 0) {
  226. if(!$this->enabled) {
  227. return array();
  228. }
  229. $cachekey = 'getGroups-'.$search.'-'.$limit.'-'.$offset;
  230. //Check cache before driving unnecessary searches
  231. \OCP\Util::writeLog('user_ldap', 'getGroups '.$cachekey, \OCP\Util::DEBUG);
  232. $ldap_groups = $this->access->connection->getFromCache($cachekey);
  233. if(!is_null($ldap_groups)) {
  234. return $ldap_groups;
  235. }
  236. // if we'd pass -1 to LDAP search, we'd end up in a Protocol
  237. // error. With a limit of 0, we get 0 results. So we pass null.
  238. if($limit <= 0) {
  239. $limit = null;
  240. }
  241. $filter = $this->access->combineFilterWithAnd(array(
  242. $this->access->connection->ldapGroupFilter,
  243. $this->access->getFilterPartForGroupSearch($search)
  244. ));
  245. \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG);
  246. $ldap_groups = $this->access->fetchListOfGroups($filter,
  247. array($this->access->connection->ldapGroupDisplayName, 'dn'),
  248. $limit,
  249. $offset);
  250. $ldap_groups = $this->access->ownCloudGroupNames($ldap_groups);
  251. $this->access->connection->writeToCache($cachekey, $ldap_groups);
  252. return $ldap_groups;
  253. }
  254. public function groupMatchesFilter($group) {
  255. return (strripos($group, $this->groupSearch) !== false);
  256. }
  257. /**
  258. * check if a group exists
  259. * @param string $gid
  260. * @return bool
  261. */
  262. public function groupExists($gid) {
  263. if($this->access->connection->isCached('groupExists'.$gid)) {
  264. return $this->access->connection->getFromCache('groupExists'.$gid);
  265. }
  266. //getting dn, if false the group does not exist. If dn, it may be mapped
  267. //only, requires more checking.
  268. $dn = $this->access->groupname2dn($gid);
  269. if(!$dn) {
  270. $this->access->connection->writeToCache('groupExists'.$gid, false);
  271. return false;
  272. }
  273. //if group really still exists, we will be able to read its objectclass
  274. $objcs = $this->access->readAttribute($dn, 'objectclass');
  275. if(!$objcs || empty($objcs)) {
  276. $this->access->connection->writeToCache('groupExists'.$gid, false);
  277. return false;
  278. }
  279. $this->access->connection->writeToCache('groupExists'.$gid, true);
  280. return true;
  281. }
  282. /**
  283. * @brief Check if backend implements actions
  284. * @param $actions bitwise-or'ed actions
  285. * @returns boolean
  286. *
  287. * Returns the supported actions as int to be
  288. * compared with OC_USER_BACKEND_CREATE_USER etc.
  289. */
  290. public function implementsActions($actions) {
  291. return (bool)(OC_GROUP_BACKEND_GET_DISPLAYNAME & $actions);
  292. }
  293. }