Blame view

sources/lib/private/group.php 7.59 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  <?php
  /**
   * ownCloud
   *
   * @author Frank Karlitschek
   * @copyright 2012 Frank Karlitschek frank@owncloud.org
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  /**
   * This class provides all methods needed for managing groups.
   *
   * Hooks provided:
   *   pre_createGroup(&run, gid)
   *   post_createGroup(gid)
   *   pre_deleteGroup(&run, gid)
   *   post_deleteGroup(gid)
   *   pre_addToGroup(&run, uid, gid)
   *   post_addToGroup(uid, gid)
   *   pre_removeFromGroup(&run, uid, gid)
   *   post_removeFromGroup(uid, gid)
   */
  class OC_Group {
31b7f2792   Kload   Upgrade to ownclo...
37
38
  
  	/**
f7d878ff1   kload   [enh] Update to 7...
39
  	 * @return \OC\Group\Manager
31b7f2792   Kload   Upgrade to ownclo...
40
  	 */
f7d878ff1   kload   [enh] Update to 7...
41
42
43
  	public static function getManager() {
  		return \OC::$server->getGroupManager();
  	}
31b7f2792   Kload   Upgrade to ownclo...
44
45
  
  	/**
f7d878ff1   kload   [enh] Update to 7...
46
  	 * @return \OC\User\Manager
31b7f2792   Kload   Upgrade to ownclo...
47
  	 */
f7d878ff1   kload   [enh] Update to 7...
48
49
  	private static function getUserManager() {
  		return \OC::$server->getUserManager();
31b7f2792   Kload   Upgrade to ownclo...
50
  	}
03e52840d   Kload   Init
51
52
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
53
54
  	 * set the group backend
  	 * @param \OC_Group_Backend $backend  The backend to use for user managment
03e52840d   Kload   Init
55
56
  	 * @return bool
  	 */
31b7f2792   Kload   Upgrade to ownclo...
57
58
59
  	public static function useBackend($backend) {
  		self::getManager()->addBackend($backend);
  		return true;
03e52840d   Kload   Init
60
61
62
63
64
65
  	}
  
  	/**
  	 * remove all used backends
  	 */
  	public static function clearBackends() {
31b7f2792   Kload   Upgrade to ownclo...
66
  		self::getManager()->clearBackends();
03e52840d   Kload   Init
67
68
69
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
70
  	 * Try to create a new group
03e52840d   Kload   Init
71
72
73
74
75
76
  	 * @param string $gid The name of the group to create
  	 * @return bool
  	 *
  	 * Tries to create a new group. If the group name already exists, false will
  	 * be returned. Basic checking of Group name
  	 */
31b7f2792   Kload   Upgrade to ownclo...
77
78
  	public static function createGroup($gid) {
  		OC_Hook::emit("OC_Group", "pre_createGroup", array("run" => true, "gid" => $gid));
03e52840d   Kload   Init
79

31b7f2792   Kload   Upgrade to ownclo...
80
81
82
83
  		if (self::getManager()->createGroup($gid)) {
  			OC_Hook::emit("OC_User", "post_createGroup", array("gid" => $gid));
  			return true;
  		} else {
03e52840d   Kload   Init
84
85
86
87
88
  			return false;
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
89
  	 * delete a group
03e52840d   Kload   Init
90
91
92
93
94
  	 * @param string $gid gid of the group to delete
  	 * @return bool
  	 *
  	 * Deletes a group and removes it from the group_user-table
  	 */
31b7f2792   Kload   Upgrade to ownclo...
95
  	public static function deleteGroup($gid) {
03e52840d   Kload   Init
96
  		// Prevent users from deleting group admin
31b7f2792   Kload   Upgrade to ownclo...
97
  		if ($gid == "admin") {
03e52840d   Kload   Init
98
99
  			return false;
  		}
31b7f2792   Kload   Upgrade to ownclo...
100
  		OC_Hook::emit("OC_Group", "pre_deleteGroup", array("run" => true, "gid" => $gid));
03e52840d   Kload   Init
101

31b7f2792   Kload   Upgrade to ownclo...
102
103
104
105
  		$group = self::getManager()->get($gid);
  		if ($group) {
  			if ($group->delete()) {
  				OC_Hook::emit("OC_User", "post_deleteGroup", array("gid" => $gid));
03e52840d   Kload   Init
106
107
  				return true;
  			}
03e52840d   Kload   Init
108
  		}
31b7f2792   Kload   Upgrade to ownclo...
109
  		return false;
03e52840d   Kload   Init
110
111
112
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
113
  	 * is user in group?
03e52840d   Kload   Init
114
115
116
117
118
119
  	 * @param string $uid uid of the user
  	 * @param string $gid gid of the group
  	 * @return bool
  	 *
  	 * Checks whether the user is member of a group or not.
  	 */
31b7f2792   Kload   Upgrade to ownclo...
120
121
  	public static function inGroup($uid, $gid) {
  		$group = self::getManager()->get($gid);
f7d878ff1   kload   [enh] Update to 7...
122
  		$user = self::getUserManager()->get($uid);
31b7f2792   Kload   Upgrade to ownclo...
123
124
  		if ($group and $user) {
  			return $group->inGroup($user);
03e52840d   Kload   Init
125
126
127
128
129
  		}
  		return false;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
130
  	 * Add a user to a group
03e52840d   Kload   Init
131
132
133
134
135
136
  	 * @param string $uid Name of the user to add to group
  	 * @param string $gid Name of the group in which add the user
  	 * @return bool
  	 *
  	 * Adds a user to a group.
  	 */
31b7f2792   Kload   Upgrade to ownclo...
137
138
  	public static function addToGroup($uid, $gid) {
  		$group = self::getManager()->get($gid);
f7d878ff1   kload   [enh] Update to 7...
139
  		$user = self::getUserManager()->get($uid);
31b7f2792   Kload   Upgrade to ownclo...
140
141
142
143
144
145
  		if ($group and $user) {
  			OC_Hook::emit("OC_Group", "pre_addToGroup", array("run" => true, "uid" => $uid, "gid" => $gid));
  			$group->addUser($user);
  			OC_Hook::emit("OC_User", "post_addToGroup", array("uid" => $uid, "gid" => $gid));
  			return true;
  		} else {
03e52840d   Kload   Init
146
147
148
149
150
  			return false;
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
151
  	 * Removes a user from a group
03e52840d   Kload   Init
152
153
154
155
156
157
  	 * @param string $uid Name of the user to remove from group
  	 * @param string $gid Name of the group from which remove the user
  	 * @return bool
  	 *
  	 * removes the user from a group.
  	 */
31b7f2792   Kload   Upgrade to ownclo...
158
159
  	public static function removeFromGroup($uid, $gid) {
  		$group = self::getManager()->get($gid);
f7d878ff1   kload   [enh] Update to 7...
160
  		$user = self::getUserManager()->get($uid);
31b7f2792   Kload   Upgrade to ownclo...
161
162
163
164
  		if ($group and $user) {
  			OC_Hook::emit("OC_Group", "pre_removeFromGroup", array("run" => true, "uid" => $uid, "gid" => $gid));
  			$group->removeUser($user);
  			OC_Hook::emit("OC_User", "post_removeFromGroup", array("uid" => $uid, "gid" => $gid));
03e52840d   Kload   Init
165
  			return true;
31b7f2792   Kload   Upgrade to ownclo...
166
  		} else {
03e52840d   Kload   Init
167
168
169
170
171
  			return false;
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
172
  	 * Get all groups a user belongs to
03e52840d   Kload   Init
173
  	 * @param string $uid Name of the user
6d9380f96   Cédric Dupont   Update sources OC...
174
  	 * @return array an array of group names
03e52840d   Kload   Init
175
176
177
178
  	 *
  	 * This function fetches all groups a user belongs to. It does not check
  	 * if the user exists at all.
  	 */
31b7f2792   Kload   Upgrade to ownclo...
179
  	public static function getUserGroups($uid) {
f7d878ff1   kload   [enh] Update to 7...
180
  		$user = self::getUserManager()->get($uid);
31b7f2792   Kload   Upgrade to ownclo...
181
  		if ($user) {
6d9380f96   Cédric Dupont   Update sources OC...
182
  			return self::getManager()->getUserGroupIds($user);
31b7f2792   Kload   Upgrade to ownclo...
183
184
  		} else {
  			return array();
03e52840d   Kload   Init
185
  		}
03e52840d   Kload   Init
186
187
188
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
189
190
191
192
193
  	 * get a list of all groups
  	 * @param string $search
  	 * @param int|null $limit
  	 * @param int|null $offset
  	 * @return array an array of group names
03e52840d   Kload   Init
194
195
196
  	 *
  	 * Returns a list with all groups
  	 */
31b7f2792   Kload   Upgrade to ownclo...
197
198
199
200
201
  	public static function getGroups($search = '', $limit = null, $offset = null) {
  		$groups = self::getManager()->search($search, $limit, $offset);
  		$groupIds = array();
  		foreach ($groups as $group) {
  			$groupIds[] = $group->getGID();
03e52840d   Kload   Init
202
  		}
31b7f2792   Kload   Upgrade to ownclo...
203
  		return $groupIds;
03e52840d   Kload   Init
204
205
206
207
  	}
  
  	/**
  	 * check if a group exists
31b7f2792   Kload   Upgrade to ownclo...
208
  	 *
03e52840d   Kload   Init
209
210
211
212
  	 * @param string $gid
  	 * @return bool
  	 */
  	public static function groupExists($gid) {
31b7f2792   Kload   Upgrade to ownclo...
213
  		return self::getManager()->groupExists($gid);
03e52840d   Kload   Init
214
215
216
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
217
218
219
220
221
222
  	 * get a list of all users in a group
  	 * @param string $gid
  	 * @param string $search
  	 * @param int $limit
  	 * @param int $offset
  	 * @return array an array of user ids
03e52840d   Kload   Init
223
224
  	 */
  	public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
31b7f2792   Kload   Upgrade to ownclo...
225
226
227
228
229
230
231
232
233
234
  		$group = self::getManager()->get($gid);
  		if ($group) {
  			$users = $group->searchUsers($search, $limit, $offset);
  			$userIds = array();
  			foreach ($users as $user) {
  				$userIds[] = $user->getUID();
  			}
  			return $userIds;
  		} else {
  			return array();
03e52840d   Kload   Init
235
  		}
03e52840d   Kload   Init
236
237
238
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
239
240
  	 * get a list of all users in several groups
  	 * @param string[] $gids
03e52840d   Kload   Init
241
242
243
  	 * @param string $search
  	 * @param int $limit
  	 * @param int $offset
6d9380f96   Cédric Dupont   Update sources OC...
244
  	 * @return array an array of user ids
03e52840d   Kload   Init
245
246
247
248
249
250
251
252
253
254
255
  	 */
  	public static function usersInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  		$users = array();
  		foreach ($gids as $gid) {
  			// TODO Need to apply limits to groups as total
  			$users = array_merge(array_diff(self::usersInGroup($gid, $search, $limit, $offset), $users), $users);
  		}
  		return $users;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
256
257
258
259
260
261
  	 * get a list of all display names in a group
  	 * @param string $gid
  	 * @param string $search
  	 * @param int $limit
  	 * @param int $offset
  	 * @return array an array of display names (value) and user ids(key)
03e52840d   Kload   Init
262
263
  	 */
  	public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
837968727   Kload   [enh] Upgrade to ...
264
  		return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset);
03e52840d   Kload   Init
265
266
267
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
268
  	 * get a list of all display names in several groups
03e52840d   Kload   Init
269
270
271
272
  	 * @param array $gids
  	 * @param string $search
  	 * @param int $limit
  	 * @param int $offset
6d9380f96   Cédric Dupont   Update sources OC...
273
  	 * @return array an array of display names (Key) user ids (value)
03e52840d   Kload   Init
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  	 */
  	public static function displayNamesInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  		$displayNames = array();
  		foreach ($gids as $gid) {
  			// TODO Need to apply limits to groups as total
  			$diff = array_diff(
  				self::displayNamesInGroup($gid, $search, $limit, $offset),
  				$displayNames
  			);
  			if ($diff) {
  				$displayNames = array_merge($diff, $displayNames);
  			}
  		}
  		return $displayNames;
  	}
  }