summaryrefslogtreecommitdiffstats
path: root/lib/group
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2013-07-10 00:06:22 +0200
committerRobin Appelman <icewind@owncloud.com>2013-07-10 00:06:22 +0200
commita735105a224e83f502e1d49366509e2afa2989b4 (patch)
tree8e5a0faf8e362a0b8788834935897faf4b33f4a3 /lib/group
parent244d376a16c1ec6e4fd190aa75adaad3a10af5e1 (diff)
downloadnextcloud-server-a735105a224e83f502e1d49366509e2afa2989b4.tar.gz
nextcloud-server-a735105a224e83f502e1d49366509e2afa2989b4.zip
update group management
Diffstat (limited to 'lib/group')
-rw-r--r--lib/group/group.php238
-rw-r--r--lib/group/manager.php163
2 files changed, 401 insertions, 0 deletions
diff --git a/lib/group/group.php b/lib/group/group.php
new file mode 100644
index 00000000000..6d0cb8f316b
--- /dev/null
+++ b/lib/group/group.php
@@ -0,0 +1,238 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Group;
+
+class Group {
+ /**
+ * @var string $id
+ */
+ private $id;
+
+ /**
+ * @var \OC\User\User[] $users
+ */
+ private $users;
+
+ /**
+ * @var \OC_Group_Backend[] | \OC_Group_Database[] $backend
+ */
+ private $backends;
+
+ /**
+ * @var \OC\Hooks\PublicEmitter $emitter;
+ */
+ private $emitter;
+
+ /**
+ * @var \OC\User\Manager $userManager
+ */
+ private $userManager;
+
+ /**
+ * @param string $id
+ * @param \OC_Group_Backend[] $backends
+ * @param \OC\User\Manager $userManager
+ * @param \OC\Hooks\PublicEmitter $emitter
+ */
+ public function __construct($id, $backends, $userManager, $emitter = null) {
+ $this->id = $id;
+ $this->backends = $backends;
+ $this->userManager = $userManager;
+ $this->emitter = $emitter;
+ }
+
+ public function getGID() {
+ return $this->id;
+ }
+
+ /**
+ * get all users in the group
+ *
+ * @return \OC\User\User[]
+ */
+ public function getUsers() {
+ if ($this->users) {
+ return $this->users;
+ }
+
+ $users = array();
+ $userIds = array();
+ foreach ($this->backends as $backend) {
+ $diff = array_diff(
+ $backend->usersInGroup($this->id),
+ $userIds
+ );
+ if ($diff) {
+ $userIds = array_merge($userIds, $diff);
+ }
+ }
+
+ foreach ($userIds as $userId) {
+ $users[] = $this->userManager->get($userId);
+ }
+ $this->users = $users;
+ return $users;
+ }
+
+ /**
+ * check if a user is in the group
+ *
+ * @param \OC\User\User $user
+ * @return bool
+ */
+ public function inGroup($user) {
+ foreach ($this->backends as $backend) {
+ if ($backend->inGroup($user->getUID(), $this->id)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * add a user to the group
+ *
+ * @param \OC\User\User $user
+ */
+ public function addUser($user) {
+ if ($this->inGroup($user)) {
+ return;
+ }
+
+ if ($this->emitter) {
+ $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
+ }
+ foreach ($this->backends as $backend) {
+ if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) {
+ $backend->addToGroup($user->getUID(), $this->id);
+ if ($this->users) {
+ $this->users[] = $user;
+ }
+ if ($this->emitter) {
+ $this->emitter->emit('\OC\Group', 'addUser', array($this, $user));
+ }
+ return;
+ }
+ }
+ }
+
+ /**
+ * remove a user from the group
+ *
+ * @param \OC\User\User $user
+ */
+ public function removeUser($user) {
+ $result = false;
+ if ($this->emitter) {
+ $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
+ }
+ foreach ($this->backends as $backend) {
+ if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->id)) {
+ $backend->removeFromGroup($user->getUID(), $this->id);
+ $result = true;
+ }
+ }
+ if ($result) {
+ if ($this->emitter) {
+ $this->emitter->emit('\OC\Group', 'removeUser', array($this, $user));
+ }
+ if ($this->users) {
+ foreach ($this->users as $index => $groupUser) {
+ if ($groupUser->getUID() === $user->getUID()) {
+ unset($this->users[$index]);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * search for users in the group by userid
+ *
+ * @param string $search
+ * @param int $limit
+ * @param int $offset
+ * @return \OC\User\User[]
+ */
+ public function searchUsers($search = '', $limit = null, $offset = null) {
+ $users = array();
+ foreach ($this->backends as $backend) {
+ $userIds = $backend->usersInGroup($this->id, $search, $limit, $offset);
+ if (!is_null($limit)) {
+ $limit -= count($userIds);
+ }
+ if (!is_null($offset)) {
+ $offset -= count($userIds);
+ }
+ foreach ($userIds as $userId) {
+ $users[$userId] = $this->userManager->get($userId);
+ }
+ if (!is_null($limit) and $limit <= 0) {
+ return array_values($users);
+ }
+ }
+ return array_values($users);
+ }
+
+ /**
+ * search for users in the group by displayname
+ *
+ * @param string $search
+ * @param int $limit
+ * @param int $offset
+ * @return \OC\User\User[]
+ */
+ public function searchDisplayName($search = '', $limit = null, $offset = null) {
+ $users = array();
+ foreach ($this->backends as $backend) {
+ if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
+ $userIds = array_keys($backend->displayNamesInGroup($this->id, $search, $limit, $offset));
+ } else {
+ $userIds = $backend->usersInGroup($this->id, $search, $limit, $offset);
+ }
+ if (!is_null($limit)) {
+ $limit -= count($userIds);
+ }
+ if (!is_null($offset)) {
+ $offset -= count($userIds);
+ }
+ foreach ($userIds as $userId) {
+ $users[$userId] = $this->userManager->get($userId);
+ }
+ if (!is_null($limit) and $limit <= 0) {
+ return array_values($users);
+ }
+ }
+ return array_values($users);
+ }
+
+ /**
+ * delete the group
+ *
+ * @return bool
+ */
+ public function delete() {
+ $result = false;
+ if ($this->emitter) {
+ $this->emitter->emit('\OC\Group', 'preDelete', array($this));
+ }
+ foreach ($this->backends as $backend) {
+ if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP) and $backend->groupExists($this->id)) {
+ $result = true;
+ $backend->deleteGroup($this->id);
+ }
+ }
+ if ($result and $this->emitter) {
+ $this->emitter->emit('\OC\Group', 'delete', array($this));
+ }
+ return $result;
+ }
+}
diff --git a/lib/group/manager.php b/lib/group/manager.php
new file mode 100644
index 00000000000..7f96065c6d2
--- /dev/null
+++ b/lib/group/manager.php
@@ -0,0 +1,163 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Group;
+
+use OC\Hooks\PublicEmitter;
+
+/**
+ * Class Manager
+ *
+ * Hooks available in scope \OC\Group:
+ * - addUser(\OC\Group\Group $group, \OC\User\User $user)
+ * - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
+ * - removeUser(\OC\Group\Group $group, \OC\User\User $user)
+ * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
+ * - delete(\OC\Group\Group $group)
+ * - preDelete(\OC\Group\Group $group)
+ * - create(\OC\Group\Group $group)
+ * - preCreate(string $groupId)
+ *
+ * @package OC\Group
+ */
+class Manager extends PublicEmitter {
+ /**
+ * @var \OC_Group_Backend[] | \OC_Group_Database[] $backends
+ */
+ private $backends = array();
+
+ /**
+ * @var \OC\User\Manager $userManager
+ */
+ private $userManager;
+
+ /**
+ * @var \OC\Group\Group[]
+ */
+ private $cachedGroups;
+
+ /**
+ * @param \OC\User\Manager $userManager
+ */
+ public function __construct($userManager) {
+ $this->userManager = $userManager;
+ $manager = $this;
+ $this->listen('\OC\Group', 'delete', function ($group) use (&$manager) {
+ /**
+ * @var \OC\Group\Group $group
+ */
+ unset($manager->cachedGroups[$group->getGID()]);
+ });
+ }
+
+ /**
+ * @param \OC_Group_Backend $backend
+ */
+ public function addBackend($backend) {
+ $this->backends[] = $backend;
+ }
+
+ public function clearBackends() {
+ $this->backends = array();
+ $this->cachedGroups = array();
+ }
+
+ /**
+ * @param string $gid
+ * @return \OC\Group\Group
+ */
+ public function get($gid) {
+ if (isset($this->cachedGroups[$gid])) {
+ return $this->cachedGroups[$gid];
+ }
+ foreach ($this->backends as $backend) {
+ if ($backend->groupExists($gid)) {
+ return $this->createGroupObject($gid);
+ }
+ }
+ return null;
+ }
+
+ protected function createGroupObject($gid) {
+ $this->cachedGroups[$gid] = new Group($gid, $this->backends, $this->userManager, $this);
+ return $this->cachedGroups[$gid];
+ }
+
+ /**
+ * @param string $gid
+ * @return bool
+ */
+ public function exists($gid) {
+ return !is_null($this->get($gid));
+ }
+
+ /**
+ * @param string $gid
+ * @return \OC\Group\Group
+ */
+ public function create($gid) {
+ if (!$gid) {
+ return false;
+ } else if ($this->exists($gid)) {
+ return $this->get($gid);
+ } else {
+ $this->emit('\OC\Group', 'preCreate', array($gid));
+ foreach ($this->backends as $backend) {
+ if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) {
+ $backend->createGroup($gid);
+ $group = $this->createGroupObject($gid);
+ $this->emit('\OC\Group', 'create', array($group));
+ return $group;
+ }
+ }
+ return null;
+ }
+ }
+
+ /**
+ * @param string $search
+ * @param int $limit
+ * @param int $offset
+ * @return \OC\Group\Group[]
+ */
+ public function search($search = '', $limit = null, $offset = null) {
+ $groups = array();
+ foreach ($this->backends as $backend) {
+ $groupIds = $backend->getGroups($search, $limit, $offset);
+ if (!is_null($limit)) {
+ $limit -= count($groupIds);
+ }
+ if (!is_null($offset)) {
+ $offset -= count($groupIds);
+ }
+ foreach ($groupIds as $groupId) {
+ $groups[$groupId] = $this->createGroupObject($groupId);
+ }
+ if (!is_null($limit) and $limit <= 0) {
+ return array_values($groups);
+ }
+ }
+ return array_values($groups);
+ }
+
+ /**
+ * @param \OC\User\User $user
+ * @return \OC\Group\Group[]
+ */
+ public function getUserGroups($user) {
+ $groups = array();
+ foreach ($this->backends as $backend) {
+ $groupIds = $backend->getUserGroups($user->getUID());
+ foreach ($groupIds as $groupId) {
+ $groups[$groupId] = $this->createGroupObject($groupId);
+ }
+ }
+ return array_values($groups);
+ }
+}