123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- namespace OC\Group;
- use OC\Hooks\PublicEmitter;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\GroupInterface;
- use OCP\ICacheFactory;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IUser;
- use Psr\Log\LoggerInterface;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- class Manager extends PublicEmitter implements IGroupManager {
-
- private $backends = [];
-
- private $userManager;
-
- private $dispatcher;
- private LoggerInterface $logger;
-
- private $cachedGroups = [];
-
- private $cachedUserGroups = [];
-
- private $subAdmin = null;
- private DisplayNameCache $displayNameCache;
- public function __construct(\OC\User\Manager $userManager,
- EventDispatcherInterface $dispatcher,
- LoggerInterface $logger,
- ICacheFactory $cacheFactory) {
- $this->userManager = $userManager;
- $this->dispatcher = $dispatcher;
- $this->logger = $logger;
- $this->displayNameCache = new DisplayNameCache($cacheFactory, $this);
- $cachedGroups = &$this->cachedGroups;
- $cachedUserGroups = &$this->cachedUserGroups;
- $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
-
- unset($cachedGroups[$group->getGID()]);
- $cachedUserGroups = [];
- });
- $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
-
- $cachedUserGroups = [];
- });
- $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
-
- $cachedUserGroups = [];
- });
- }
-
- public function isBackendUsed($backendClass) {
- $backendClass = strtolower(ltrim($backendClass, '\\'));
- foreach ($this->backends as $backend) {
- if (strtolower(get_class($backend)) === $backendClass) {
- return true;
- }
- }
- return false;
- }
-
- public function addBackend($backend) {
- $this->backends[] = $backend;
- $this->clearCaches();
- }
- public function clearBackends() {
- $this->backends = [];
- $this->clearCaches();
- }
-
- public function getBackends() {
- return $this->backends;
- }
- protected function clearCaches() {
- $this->cachedGroups = [];
- $this->cachedUserGroups = [];
- }
-
- public function get($gid) {
- if (isset($this->cachedGroups[$gid])) {
- return $this->cachedGroups[$gid];
- }
- return $this->getGroupObject($gid);
- }
-
- protected function getGroupObject($gid, $displayName = null) {
- $backends = [];
- foreach ($this->backends as $backend) {
- if ($backend->implementsActions(Backend::GROUP_DETAILS)) {
- $groupData = $backend->getGroupDetails($gid);
- if (is_array($groupData) && !empty($groupData)) {
-
- if (is_null($displayName) && isset($groupData['displayName'])) {
- $displayName = $groupData['displayName'];
- }
- $backends[] = $backend;
- }
- } elseif ($backend->groupExists($gid)) {
- $backends[] = $backend;
- }
- }
- if (count($backends) === 0) {
- return null;
- }
- $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName);
- return $this->cachedGroups[$gid];
- }
-
- public function groupExists($gid) {
- return $this->get($gid) instanceof IGroup;
- }
-
- public function createGroup($gid) {
- if ($gid === '' || $gid === null) {
- return null;
- } elseif ($group = $this->get($gid)) {
- return $group;
- } else {
- $this->emit('\OC\Group', 'preCreate', [$gid]);
- foreach ($this->backends as $backend) {
- if ($backend->implementsActions(Backend::CREATE_GROUP)) {
- if ($backend->createGroup($gid)) {
- $group = $this->getGroupObject($gid);
- $this->emit('\OC\Group', 'postCreate', [$group]);
- return $group;
- }
- }
- }
- return null;
- }
- }
-
- public function search(string $search, ?int $limit = null, ?int $offset = 0) {
- $groups = [];
- foreach ($this->backends as $backend) {
- $groupIds = $backend->getGroups($search, $limit ?? -1, $offset ?? 0);
- foreach ($groupIds as $groupId) {
- $aGroup = $this->get($groupId);
- if ($aGroup instanceof IGroup) {
- $groups[$groupId] = $aGroup;
- } else {
- $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']);
- }
- }
- if (!is_null($limit) and $limit <= 0) {
- return array_values($groups);
- }
- }
- return array_values($groups);
- }
-
- public function getUserGroups(IUser $user = null) {
- if (!$user instanceof IUser) {
- return [];
- }
- return $this->getUserIdGroups($user->getUID());
- }
-
- public function getUserIdGroups(string $uid): array {
- $groups = [];
- foreach ($this->getUserIdGroupIds($uid) as $groupId) {
- $aGroup = $this->get($groupId);
- if ($aGroup instanceof IGroup) {
- $groups[$groupId] = $aGroup;
- } else {
- $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
- }
- }
- return $groups;
- }
-
- public function isAdmin($userId) {
- foreach ($this->backends as $backend) {
- if ($backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
- return true;
- }
- }
- return $this->isInGroup($userId, 'admin');
- }
-
- public function isInGroup($userId, $group) {
- return array_search($group, $this->getUserIdGroupIds($userId)) !== false;
- }
-
- public function getUserGroupIds(IUser $user): array {
- return $this->getUserIdGroupIds($user->getUID());
- }
-
- private function getUserIdGroupIds(string $uid): array {
- if (!isset($this->cachedUserGroups[$uid])) {
- $groups = [];
- foreach ($this->backends as $backend) {
- if ($groupIds = $backend->getUserGroups($uid)) {
- $groups = array_merge($groups, $groupIds);
- }
- }
- $this->cachedUserGroups[$uid] = $groups;
- }
- return $this->cachedUserGroups[$uid];
- }
-
- public function getDisplayName(string $groupId): ?string {
- return $this->displayNameCache->getDisplayName($groupId);
- }
-
- public function getUserGroupNames(IUser $user) {
- return array_map(function ($group) {
- return ['displayName' => $this->displayNameCache->getDisplayName($group->getGID())];
- }, $this->getUserGroups($user));
- }
-
- public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
- $group = $this->get($gid);
- if (is_null($group)) {
- return [];
- }
- $search = trim($search);
- $groupUsers = [];
- if (!empty($search)) {
-
- $searchOffset = 0;
- $searchLimit = $limit * 100;
- if ($limit === -1) {
- $searchLimit = 500;
- }
- do {
- $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
- foreach ($filteredUsers as $filteredUser) {
- if ($group->inGroup($filteredUser)) {
- $groupUsers[] = $filteredUser;
- }
- }
- $searchOffset += $searchLimit;
- } while (count($groupUsers) < $searchLimit + $offset && count($filteredUsers) >= $searchLimit);
- if ($limit === -1) {
- $groupUsers = array_slice($groupUsers, $offset);
- } else {
- $groupUsers = array_slice($groupUsers, $offset, $limit);
- }
- } else {
- $groupUsers = $group->searchUsers('', $limit, $offset);
- }
- $matchingUsers = [];
- foreach ($groupUsers as $groupUser) {
- $matchingUsers[(string) $groupUser->getUID()] = $groupUser->getDisplayName();
- }
- return $matchingUsers;
- }
-
- public function getSubAdmin() {
- if (!$this->subAdmin) {
- $this->subAdmin = new \OC\SubAdmin(
- $this->userManager,
- $this,
- \OC::$server->getDatabaseConnection(),
- \OC::$server->get(IEventDispatcher::class)
- );
- }
- return $this->subAdmin;
- }
- }
|