diff --git a/apps/testing/lib/AlternativeHomeUserBackend.php b/apps/testing/lib/AlternativeHomeUserBackend.php index 975f76c84b797..d67bbfa6dec78 100644 --- a/apps/testing/lib/AlternativeHomeUserBackend.php +++ b/apps/testing/lib/AlternativeHomeUserBackend.php @@ -27,13 +27,9 @@ class AlternativeHomeUserBackend extends Database { public function __construct() { parent::__construct(); } - /** - * get the user's home directory - * @param string $uid the username - * @return string|false - */ + #[\Override] - public function getHome($uid) { + public function getHome(string $uid): string|false { if ($this->userExists($uid)) { // workaround to avoid killing the admin if ($uid !== 'admin') { diff --git a/apps/user_ldap/lib/Command/UpdateUUID.php b/apps/user_ldap/lib/Command/UpdateUUID.php index 07f0a4a95dcf2..7a33e2594483a 100644 --- a/apps/user_ldap/lib/Command/UpdateUUID.php +++ b/apps/user_ldap/lib/Command/UpdateUUID.php @@ -9,7 +9,6 @@ namespace OCA\User_LDAP\Command; -use OCA\User_LDAP\Access; use OCA\User_LDAP\Group_Proxy; use OCA\User_LDAP\Mapping\AbstractMapping; use OCA\User_LDAP\Mapping\GroupMapping; @@ -293,8 +292,7 @@ protected function handleUpdatesByList(AbstractMapping $mapping, array $list): \ foreach ($list as $row) { $access = $backendProxy->getLDAPAccess($row['name']); - if ($access instanceof Access - && $dn = $mapping->getDNByName($row['name'])) { + if ($dn = $mapping->getDNByName($row['name'])) { if ($uuid = $access->getUUID($dn, $isUser)) { if ($uuid !== $row['uuid']) { if ($this->dryRun || $mapping->setUUIDbyDN($uuid, $dn)) { diff --git a/apps/user_ldap/lib/GroupPluginManager.php b/apps/user_ldap/lib/GroupPluginManager.php index 8ec7c1bf7af11..f34ffcb224f1f 100644 --- a/apps/user_ldap/lib/GroupPluginManager.php +++ b/apps/user_ldap/lib/GroupPluginManager.php @@ -20,41 +20,30 @@ class GroupPluginManager { GroupInterface::DELETE_GROUP => null, GroupInterface::ADD_TO_GROUP => null, GroupInterface::REMOVE_FROM_GROUP => null, - GroupInterface::COUNT_USERS => null, - GroupInterface::GROUP_DETAILS => null ]; private bool $suppressDeletion = false; - /** - * @return int All implemented actions - */ - public function getImplementedActions() { + public function getImplementedActions(): int { return $this->respondToActions; } /** * Registers a group plugin that may implement some actions, overriding User_LDAP's group actions. - * @param ILDAPGroupPlugin $plugin */ - public function register(ILDAPGroupPlugin $plugin) { + public function register(ILDAPGroupPlugin $plugin): void { $respondToActions = $plugin->respondToActions(); $this->respondToActions |= $respondToActions; foreach ($this->which as $action => $v) { - if ((bool)($respondToActions & $action)) { + if ($respondToActions & $action) { $this->which[$action] = $plugin; Server::get(LoggerInterface::class)->debug('Registered action ' . $action . ' to plugin ' . get_class($plugin), ['app' => 'user_ldap']); } } } - /** - * Signal if there is a registered plugin that implements some given actions - * @param int $actions Actions defined in \OCP\GroupInterface, like GroupInterface::REMOVE_FROM_GROUP - * @return bool - */ - public function implementsActions($actions) { + public function implementsActions(int $actions): bool { return ($actions & $this->respondToActions) == $actions; } @@ -64,11 +53,11 @@ public function implementsActions($actions) { * @return string | null The group DN if group creation was successful. * @throws \Exception */ - public function createGroup($gid) { + public function createGroup(string $name): ?string { $plugin = $this->which[GroupInterface::CREATE_GROUP]; if ($plugin) { - return $plugin->createGroup($gid); + return $plugin->createGroup($name); } throw new \Exception('No plugin implements createGroup in this LDAP Backend.'); } @@ -112,7 +101,7 @@ public function deleteGroup(string $gid): bool { * * Adds a user to a group. */ - public function addToGroup($uid, $gid) { + public function addToGroup(string $uid, string $gid): bool { $plugin = $this->which[GroupInterface::ADD_TO_GROUP]; if ($plugin) { @@ -130,7 +119,7 @@ public function addToGroup($uid, $gid) { * * removes the user from a group. */ - public function removeFromGroup($uid, $gid) { + public function removeFromGroup(string $uid, string $gid): bool { $plugin = $this->which[GroupInterface::REMOVE_FROM_GROUP]; if ($plugin) { @@ -138,35 +127,4 @@ public function removeFromGroup($uid, $gid) { } throw new \Exception('No plugin implements removeFromGroup in this LDAP Backend.'); } - - /** - * get the number of all users matching the search string in a group - * @param string $gid ID of the group - * @param string $search query string - * @return int|false - * @throws \Exception - */ - public function countUsersInGroup($gid, $search = '') { - $plugin = $this->which[GroupInterface::COUNT_USERS]; - - if ($plugin) { - return $plugin->countUsersInGroup($gid, $search); - } - throw new \Exception('No plugin implements countUsersInGroup in this LDAP Backend.'); - } - - /** - * get an array with group details - * @param string $gid - * @return array|false - * @throws \Exception - */ - public function getGroupDetails($gid) { - $plugin = $this->which[GroupInterface::GROUP_DETAILS]; - - if ($plugin) { - return $plugin->getGroupDetails($gid); - } - throw new \Exception('No plugin implements getGroupDetails in this LDAP Backend.'); - } } diff --git a/apps/user_ldap/lib/Group_LDAP.php b/apps/user_ldap/lib/Group_LDAP.php index abeac906a42c8..3a2c2c1c28571 100644 --- a/apps/user_ldap/lib/Group_LDAP.php +++ b/apps/user_ldap/lib/Group_LDAP.php @@ -14,15 +14,30 @@ use OCP\Cache\CappedMemoryCache; use OCP\Config\IUserConfig; use OCP\Group\Backend\ABackend; +use OCP\Group\Backend\IAddToGroupBackend; +use OCP\Group\Backend\ICountUsersBackend; +use OCP\Group\Backend\ICreateNamedGroupBackend; use OCP\Group\Backend\IDeleteGroupBackend; use OCP\Group\Backend\IGetDisplayNameBackend; +use OCP\Group\Backend\IGroupDetailsBackend; use OCP\Group\Backend\IIsAdminBackend; +use OCP\Group\Backend\IRemoveFromGroupBackend; use OCP\GroupInterface; use OCP\IUserManager; use OCP\Server; use Psr\Log\LoggerInterface; -class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, IDeleteGroupBackend, IIsAdminBackend { +class Group_LDAP extends ABackend implements + GroupInterface, + IGroupLDAP, + IGetDisplayNameBackend, + IDeleteGroupBackend, + IIsAdminBackend, + ICountUsersBackend, + IGroupDetailsBackend, + ICreateNamedGroupBackend, + IAddToGroupBackend, + IRemoveFromGroupBackend { protected bool $enabled = false; /** @var CappedMemoryCache $cachedGroupMembers array of user DN with gid as key */ @@ -57,16 +72,8 @@ public function __construct( $this->ldapGroupMemberAssocAttr = strtolower((string)$gAssoc); } - /** - * Check if user is in group - * - * @param string $uid uid of the user - * @param string $gid gid of the group - * @throws Exception - * @throws ServerNotAvailableException - */ #[\Override] - public function inGroup($uid, $gid): bool { + public function inGroup(string $uid, string $gid): bool { if (!$this->enabled) { return false; } @@ -384,10 +391,10 @@ private function getNameOfGroup(string $filter, string $cacheKey) { } /** - * @return string|bool The entry's gidNumber + * @return string|false The entry's gidNumber * @throws ServerNotAvailableException */ - private function getEntryGidNumber(string $dn, string $attribute) { + private function getEntryGidNumber(string $dn, string $attribute): string|false { $value = $this->access->readAttribute($dn, $attribute); if (is_array($value) && !empty($value)) { return $value[0]; @@ -396,18 +403,18 @@ private function getEntryGidNumber(string $dn, string $attribute) { } /** - * @return string|bool The group's gidNumber + * @return string|false The group's gidNumber * @throws ServerNotAvailableException */ - public function getGroupGidNumber(string $dn) { + public function getGroupGidNumber(string $dn): string|false { return $this->getEntryGidNumber($dn, 'gidNumber'); } /** - * @return string|bool The user's gidNumber + * @return string|false The user's gidNumber * @throws ServerNotAvailableException */ - public function getUserGidNumber(string $dn) { + public function getUserGidNumber(string $dn): string|false { $gidNumber = false; if ($this->access->connection->hasGidNumber) { // FIXME: when $dn does not exist on LDAP anymore, this will be set wrongly to false :/ @@ -512,7 +519,7 @@ public function primaryGroupID2Name(string $gid, string $dn) { * @return string|false The entry's group Id * @throws ServerNotAvailableException */ - private function getEntryGroupID(string $dn, string $attribute) { + private function getEntryGroupID(string $dn, string $attribute): string|false { $value = $this->access->readAttribute($dn, $attribute); if (is_array($value) && !empty($value)) { return $value[0]; @@ -524,15 +531,14 @@ private function getEntryGroupID(string $dn, string $attribute) { * @return string|false The entry's primary group Id * @throws ServerNotAvailableException */ - public function getGroupPrimaryGroupID(string $dn) { + public function getGroupPrimaryGroupID(string $dn): string|false { return $this->getEntryGroupID($dn, 'primaryGroupToken'); } /** - * @return string|false * @throws ServerNotAvailableException */ - public function getUserPrimaryGroupIDs(string $dn) { + public function getUserPrimaryGroupIDs(string $dn): string|false { $primaryGroupID = false; if ($this->access->connection->hasPrimaryGroups) { $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); @@ -604,16 +610,15 @@ public function countUsersInPrimaryGroup( return (int)$users; } catch (ServerNotAvailableException $e) { throw $e; - } catch (Exception $e) { + } catch (Exception) { return 0; } } /** - * @return string|false * @throws ServerNotAvailableException */ - public function getUserPrimaryGroup(string $dn) { + public function getUserPrimaryGroup(string $dn): string|false { $groupID = $this->getUserPrimaryGroupIDs($dn); if ($groupID !== false) { $groupName = $this->primaryGroupID2Name($groupID, $dn); @@ -649,19 +654,8 @@ protected function getCachedGroupsForUserId(string $uid): array { return $cache; } - /** - * This function fetches all groups a user belongs to. It does not check - * if the user exists at all. - * - * This function includes groups based on dynamic group membership. - * - * @param string $uid Name of the user - * @return list Group names - * @throws Exception - * @throws ServerNotAvailableException - */ #[\Override] - public function getUserGroups($uid): array { + public function getUserGroups(string $uid): array { if (!$this->enabled) { return []; } @@ -854,19 +848,8 @@ private function getGroupsByMember(string $dn, array &$seen = []): array { return $visibleGroups; } - /** - * get a list of all users in a group - * - * @param string $gid - * @param string $search - * @param int $limit - * @param int $offset - * @return array user ids - * @throws Exception - * @throws ServerNotAvailableException - */ #[\Override] - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + public function usersInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array { if (!$this->enabled) { return []; } @@ -978,23 +961,11 @@ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { return $groupUsers; } - /** - * returns the number of users in a group, who match the search term - * - * @param string $gid the internal group name - * @param string $search optional, a search string - * @return int|bool - * @throws Exception - * @throws ServerNotAvailableException - */ - public function countUsersInGroup($gid, $search = '') { - if ($this->groupPluginManager->implementsActions(GroupInterface::COUNT_USERS)) { - return $this->groupPluginManager->countUsersInGroup($gid, $search); - } - + #[\Override] + public function countUsersInGroup(string $gid, string $search = ''): int { $cacheKey = 'countUsersInGroup-' . $gid . '-' . $search; if (!$this->enabled || !$this->groupExists($gid)) { - return false; + return 0; } $groupUsers = $this->access->connection->getFromCache($cacheKey); if (!is_null($groupUsers)) { @@ -1005,7 +976,7 @@ public function countUsersInGroup($gid, $search = '') { if (!$groupDN) { // group couldn't be found, return empty result set $this->access->connection->writeToCache($cacheKey, false); - return false; + return 0; } $members = $this->_groupMembers($groupDN); @@ -1013,7 +984,7 @@ public function countUsersInGroup($gid, $search = '') { if (!$members && $primaryUserCount === 0) { //in case users could not be retrieved, return empty result set $this->access->connection->writeToCache($cacheKey, false); - return false; + return 0; } if ($search === '') { @@ -1072,22 +1043,8 @@ public function countUsersInGroup($gid, $search = '') { return count($groupUsers) + $primaryUsers; } - /** - * get a list of all groups using a paged search - * - * @param string $search - * @param int $limit - * @param int $offset - * @return array with group names - * - * Returns a list with all groups - * Uses a paged search if available to override a - * server side search limit. - * (active directory has a limit of 1000 by default) - * @throws Exception - */ #[\Override] - public function getGroups($search = '', $limit = -1, $offset = 0) { + public function getGroups(string $search = '', int $limit = -1, int $offset = 0): array { if (!$this->enabled) { return []; } @@ -1119,15 +1076,8 @@ public function getGroups($search = '', $limit = -1, $offset = 0) { return $ldap_groups; } - /** - * check if a group exists - * - * @param string $gid - * @return bool - * @throws ServerNotAvailableException - */ #[\Override] - public function groupExists($gid) { + public function groupExists(string $gid): bool { return $this->groupExistsOnLDAP($gid, false); } @@ -1211,51 +1161,38 @@ public function implementsActions($actions): bool { | $this->groupPluginManager->getImplementedActions()) & $actions); } - /** - * Return access for LDAP interaction. - * - * @return Access instance of Access for LDAP interaction - */ #[\Override] - public function getLDAPAccess($gid) { + public function getLDAPAccess(string $name): Access { return $this->access; } - /** - * create a group - * - * @param string $gid - * @return bool - * @throws Exception - * @throws ServerNotAvailableException - */ - public function createGroup($gid) { - if ($this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) { - if ($dn = $this->groupPluginManager->createGroup($gid)) { - //updates group mapping - $uuid = $this->access->getUUID($dn, false); - if (is_string($uuid)) { - $this->access->mapAndAnnounceIfApplicable( - $this->access->getGroupMapper(), - $dn, - $gid, - $uuid, - false - ); - $this->access->cacheGroupExists($gid); - } - } - return $dn != null; + #[\Override] + public function createGroup(string $name): ?string { + if (!$this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) { + throw new Exception('Could not create group in LDAP backend.'); + } + + $dn = $this->groupPluginManager->createGroup($name); + if ($dn === null) { + return null; + } + + // updates group mapping + $uuid = $this->access->getUUID($dn, false); + if (is_string($uuid)) { + $this->access->mapAndAnnounceIfApplicable( + $this->access->getGroupMapper(), + $dn, + $name, + $uuid, + false + ); + $this->access->cacheGroupExists($name); } - throw new Exception('Could not create group in LDAP backend.'); + + return $name; } - /** - * delete a group - * - * @param string $gid gid of the group to delete - * @throws Exception - */ #[\Override] public function deleteGroup(string $gid): bool { if ($this->groupPluginManager->canDeleteGroup()) { @@ -1283,69 +1220,44 @@ public function deleteGroup(string $gid): bool { throw new Exception('Could not delete existing group ' . $gid . ' in LDAP backend.'); } - /** - * Add a user to a group - * - * @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 - * @throws Exception - */ - public function addToGroup($uid, $gid) { - if ($this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) { - if ($ret = $this->groupPluginManager->addToGroup($uid, $gid)) { - $this->access->connection->clearCache(); - unset($this->cachedGroupMembers[$gid]); - } - return $ret; + #[\Override] + public function addToGroup(string $uid, string $gid): bool { + if (!$this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) { + throw new Exception('Could not add user to group in LDAP backend.'); + } + + if ($ret = $this->groupPluginManager->addToGroup($uid, $gid)) { + $this->access->connection->clearCache(); + unset($this->cachedGroupMembers[$gid]); } - throw new Exception('Could not add user to group in LDAP backend.'); + return $ret; } - /** - * Removes a user from a group - * - * @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 - * @throws Exception - */ - public function removeFromGroup($uid, $gid) { - if ($this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) { - if ($ret = $this->groupPluginManager->removeFromGroup($uid, $gid)) { - $this->access->connection->clearCache(); - unset($this->cachedGroupMembers[$gid]); - } - return $ret; + #[\Override] + public function removeFromGroup(string $uid, string $gid): bool { + if (!$this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) { + throw new Exception('Could not remove user from group in LDAP backend. You need to install ldap_write_support to get write support for LDAP groups.'); } - throw new Exception('Could not remove user from group in LDAP backend.'); + + if ($ret = $this->groupPluginManager->removeFromGroup($uid, $gid)) { + $this->access->connection->clearCache(); + unset($this->cachedGroupMembers[$gid]); + } + return $ret; } - /** - * Gets group details - * - * @param string $gid Name of the group - * @return array|false - * @throws Exception - */ - public function getGroupDetails($gid) { - if ($this->groupPluginManager->implementsActions(GroupInterface::GROUP_DETAILS)) { - return $this->groupPluginManager->getGroupDetails($gid); + #[\Override] + public function getGroupDetails(string $gid): array { + $displayName = $this->getDisplayName($gid); + if ($displayName !== '') { + return ['displayName' => $displayName]; } - throw new Exception('Could not get group details in LDAP backend.'); + + return []; } - /** - * Return LDAP connection resource from a cloned connection. - * The cloned connection needs to be closed manually. - * of the current access. - * - * @param string $gid - * @return \LDAP\Connection The LDAP connection - * @throws ServerNotAvailableException - */ #[\Override] - public function getNewLDAPConnection($gid): \LDAP\Connection { + public function getNewLDAPConnection(string $name): \LDAP\Connection { $connection = clone $this->access->getConnection(); return $connection->getConnectionResource(); } diff --git a/apps/user_ldap/lib/Group_Proxy.php b/apps/user_ldap/lib/Group_Proxy.php index 507d90182601d..7be9398b843e8 100644 --- a/apps/user_ldap/lib/Group_Proxy.php +++ b/apps/user_ldap/lib/Group_Proxy.php @@ -10,19 +10,25 @@ use OC\ServerNotAvailableException; use OCP\Config\IUserConfig; +use OCP\Group\Backend\IAddToGroupBackend; use OCP\Group\Backend\IBatchMethodsBackend; +use OCP\Group\Backend\ICountUsersBackend; +use OCP\Group\Backend\ICreateNamedGroupBackend; use OCP\Group\Backend\IDeleteGroupBackend; use OCP\Group\Backend\IGetDisplayNameBackend; use OCP\Group\Backend\IGroupDetailsBackend; use OCP\Group\Backend\IIsAdminBackend; use OCP\Group\Backend\INamedBackend; +use OCP\Group\Backend\IRemoveFromGroupBackend; use OCP\GroupInterface; use OCP\IUserManager; /** * @template-extends Proxy + * + * @note This class implements a few more interface (e.g. ICreateNamedGroupBackend) via plugins. */ -class Group_Proxy extends Proxy implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend, IIsAdminBackend { +class Group_Proxy extends Proxy implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend, IIsAdminBackend, IGroupDetailsBackend, ICountUsersBackend { public function __construct( Helper $helper, ILDAPWrapper $ldap, @@ -39,16 +45,8 @@ protected function newInstance(string $configPrefix): Group_LDAP { return new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager, $this->userConfig, $this->ncUserManager); } - /** - * Tries the backends one after the other until a positive result is returned from the specified method - * - * @param string $id the gid connected to the request - * @param string $method the method of the group backend that shall be called - * @param array $parameters an array of parameters to be passed - * @return mixed the result of the method or false - */ #[\Override] - protected function walkBackends($id, $method, $parameters) { + protected function walkBackends(string $id, string $method, array $parameters): mixed { $this->setup(); $gid = $id; @@ -64,17 +62,8 @@ protected function walkBackends($id, $method, $parameters) { return false; } - /** - * Asks the backend connected to the server that supposely takes care of the gid from the request. - * - * @param string $id the gid connected to the request - * @param string $method the method of the group backend that shall be called - * @param array $parameters an array of parameters to be passed - * @param mixed $passOnWhen the result matches this variable - * @return mixed the result of the method or false - */ #[\Override] - protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) { + protected function callOnLastSeenOn(string $id, string $method, array $parameters, bool $passOnWhen): mixed { $this->setup(); $gid = $id; @@ -107,31 +96,13 @@ protected function activeBackends(): int { return count($this->backends); } - /** - * is user in group? - * - * @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. - */ #[\Override] - public function inGroup($uid, $gid) { + public function inGroup(string $uid, string $gid): bool { return $this->handleRequest($gid, 'inGroup', [$uid, $gid]); } - /** - * Get all groups a user belongs to - * - * @param string $uid Name of the user - * @return list with group names - * - * This function fetches all groups a user belongs to. It does not check - * if the user exists at all. - */ #[\Override] - public function getUserGroups($uid) { + public function getUserGroups(string $uid): array { $this->setup(); $groups = []; @@ -143,13 +114,8 @@ public function getUserGroups($uid) { return array_values(array_unique($groups)); } - /** - * get a list of all users in a group - * - * @return array user ids - */ #[\Override] - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + public function usersInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array { $this->setup(); $users = []; @@ -164,17 +130,15 @@ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { } /** - * @param string $gid - * @return bool + * Doesn't overwrite because dynamically implemented via ILDAPGroupPlugin + * + * @see ICreateNamedGroupBackend */ - public function createGroup($gid) { + public function createGroup(string $name): ?string { return $this->handleRequest( - $gid, 'createGroup', [$gid]); + $name, 'createGroup', [$name]); } - /** - * delete a group - */ #[\Override] public function deleteGroup(string $gid): bool { return $this->handleRequest( @@ -182,52 +146,33 @@ public function deleteGroup(string $gid): bool { } /** - * Add a user to a group - * - * @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 + * Doesn't overwrite because dynamically implemented via ILDAPGroupPlugin * - * Adds a user to a group. + * @see IAddToGroupBackend */ - public function addToGroup($uid, $gid) { + public function addToGroup(string $uid, string $gid): bool { return $this->handleRequest( $gid, 'addToGroup', [$uid, $gid]); } /** - * Removes a user from a group + * Doesn't overwrite because dynamically implemented via ILDAPGroupPlugin * - * @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. + * @see IRemoveFromGroupBackend */ - public function removeFromGroup($uid, $gid) { + public function removeFromGroup(string $uid, string $gid): bool { return $this->handleRequest( $gid, 'removeFromGroup', [$uid, $gid]); } - /** - * returns the number of users in a group, who match the search term - * - * @param string $gid the internal group name - * @param string $search optional, a search string - * @return int|bool - */ - public function countUsersInGroup($gid, $search = '') { + #[\Override] + public function countUsersInGroup(string $gid, string $search = ''): int { return $this->handleRequest( $gid, 'countUsersInGroup', [$gid, $search]); } - /** - * get an array with group details - * - * @param string $gid - * @return array|false - */ - public function getGroupDetails($gid) { + #[\Override] + public function getGroupDetails(string $gid): array { return $this->handleRequest( $gid, 'getGroupDetails', [$gid]); } @@ -248,36 +193,21 @@ public function getGroupsDetails(array $gids): array { return $groupData; } - /** - * get a list of all groups - * - * @return string[] with group names - * - * Returns a list with all groups - */ #[\Override] - public function getGroups($search = '', $limit = -1, $offset = 0) { + public function getGroups(string $search = '', int $limit = -1, int $offset = 0): array { $this->setup(); $groups = []; foreach ($this->backends as $backend) { $backendGroups = $backend->getGroups($search, $limit, $offset); - if (is_array($backendGroups)) { - $groups = array_merge($groups, $backendGroups); - } + $groups = array_merge($groups, $backendGroups); } return $groups; } - /** - * check if a group exists - * - * @param string $gid - * @return bool - */ #[\Override] - public function groupExists($gid) { + public function groupExists(string $gid): bool { return $this->handleRequest($gid, 'groupExists', [$gid]); } @@ -319,33 +249,20 @@ public function groupsExists(array $gids): array { * compared with \OCP\GroupInterface::CREATE_GROUP etc. */ #[\Override] - public function implementsActions($actions) { + public function implementsActions(int $actions): bool { $this->setup(); //it's the same across all our user backends obviously return $this->refBackend->implementsActions($actions); } - /** - * Return access for LDAP interaction. - * - * @param string $gid - * @return Access instance of Access for LDAP interaction - */ #[\Override] - public function getLDAPAccess($gid) { - return $this->handleRequest($gid, 'getLDAPAccess', [$gid]); + public function getLDAPAccess(string $name): Access { + return $this->handleRequest($name, 'getLDAPAccess', [$name]); } - /** - * Return a new LDAP connection for the specified group. - * The connection needs to be closed manually. - * - * @param string $gid - * @return \LDAP\Connection The LDAP connection - */ #[\Override] - public function getNewLDAPConnection($gid): \LDAP\Connection { - return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]); + public function getNewLDAPConnection(string $name): \LDAP\Connection { + return $this->handleRequest($name, 'getNewLDAPConnection', [$name]); } #[\Override] diff --git a/apps/user_ldap/lib/Handler/ExtStorageConfigHandler.php b/apps/user_ldap/lib/Handler/ExtStorageConfigHandler.php index 6ca2b7712005a..a8043cd4f9ee1 100644 --- a/apps/user_ldap/lib/Handler/ExtStorageConfigHandler.php +++ b/apps/user_ldap/lib/Handler/ExtStorageConfigHandler.php @@ -36,9 +36,6 @@ public function handle($optionValue) { } $access = $backend->getLDAPAccess($user->getUID()); - if (!$access) { - return $optionValue; - } $attribute = $access->connection->ldapExtStorageHomeAttribute; if (empty($attribute)) { diff --git a/apps/user_ldap/lib/IGroupLDAP.php b/apps/user_ldap/lib/IGroupLDAP.php index f36aeff60237e..0cecf3392e69b 100644 --- a/apps/user_ldap/lib/IGroupLDAP.php +++ b/apps/user_ldap/lib/IGroupLDAP.php @@ -9,21 +9,21 @@ namespace OCA\User_LDAP; +/** + * Interface defining methods used by the LDAPProvider + */ interface IGroupLDAP { - - //Used by LDAPProvider - /** * Return access for LDAP interaction. - * @param string $gid + * * @return Access instance of Access for LDAP interaction */ - public function getLDAPAccess($gid); + public function getLDAPAccess(string $name): Access; /** * Return a new LDAP connection for the specified group. - * @param string $gid + * * @return \LDAP\Connection The LDAP connection */ - public function getNewLDAPConnection($gid); + public function getNewLDAPConnection(string $name): \LDAP\Connection; } diff --git a/apps/user_ldap/lib/ILDAPGroupPlugin.php b/apps/user_ldap/lib/ILDAPGroupPlugin.php index c6241699147c5..20ed6320a3eb3 100644 --- a/apps/user_ldap/lib/ILDAPGroupPlugin.php +++ b/apps/user_ldap/lib/ILDAPGroupPlugin.php @@ -9,6 +9,10 @@ namespace OCA\User_LDAP; +/** + * Implemented by the ldap_write_support to provide write operations from Nextcloud + * to the LDAP server. + */ interface ILDAPGroupPlugin { /** @@ -24,14 +28,14 @@ public function respondToActions(); * @param string $gid * @return string|null The group DN if group creation was successful. */ - public function createGroup($gid); + public function createGroup(string $gid); /** * delete a group * @param string $gid gid of the group to delete * @return bool */ - public function deleteGroup($gid); + public function deleteGroup(string $gid); /** * Add a user to a group @@ -41,7 +45,7 @@ public function deleteGroup($gid); * * Adds a user to a group. */ - public function addToGroup($uid, $gid); + public function addToGroup(string $uid, string $gid); /** * Removes a user from a group @@ -51,20 +55,5 @@ public function addToGroup($uid, $gid); * * removes the user from a group. */ - public function removeFromGroup($uid, $gid); - - /** - * get the number of all users matching the search string in a group - * @param string $gid - * @param string $search - * @return int|false - */ - public function countUsersInGroup($gid, $search = ''); - - /** - * get an array with group details - * @param string $gid - * @return array|false - */ - public function getGroupDetails($gid); + public function removeFromGroup(string $uid, string $gid); } diff --git a/apps/user_ldap/lib/IUserLDAP.php b/apps/user_ldap/lib/IUserLDAP.php index 260576cdaa665..9049654ffc4a2 100644 --- a/apps/user_ldap/lib/IUserLDAP.php +++ b/apps/user_ldap/lib/IUserLDAP.php @@ -9,6 +9,7 @@ namespace OCA\User_LDAP; +use OCP\AppFramework\Attribute\Consumable; use OCP\LDAP\Exceptions\MultipleUsersReturnedException; interface IUserLDAP { @@ -17,24 +18,24 @@ interface IUserLDAP { /** * Return access for LDAP interaction. - * @param string $uid + * @param string $name * @return Access instance of Access for LDAP interaction */ - public function getLDAPAccess($uid); + public function getLDAPAccess(string $name): Access; /** * Return a new LDAP connection for the specified user. - * @param string $uid + * @param string $name * @return \LDAP\Connection of the LDAP connection */ - public function getNewLDAPConnection($uid); + public function getNewLDAPConnection(string $name): \LDAP\Connection; /** * Return the username for the given LDAP DN, if available. * @param string $dn * @return string|false with the username */ - public function dn2UserName($dn); + public function dn2UserName(string $dn): string|false; /** * Fetches one user from LDAP based on a filter or a custom attribute and search term. diff --git a/apps/user_ldap/lib/Proxy.php b/apps/user_ldap/lib/Proxy.php index 72afee9a574e1..7053b1ecb00a8 100644 --- a/apps/user_ldap/lib/Proxy.php +++ b/apps/user_ldap/lib/Proxy.php @@ -71,44 +71,28 @@ protected function getAccess(string $configPrefix): Access { return $this->accessFactory->getAccessForPrefix($configPrefix); } - /** - * @param string $uid - * @return string - */ - protected function getUserCacheKey($uid) { + protected function getUserCacheKey(string $uid): string { return 'user-' . $uid . '-lastSeenOn'; } - /** - * @param string $gid - * @return string - */ - protected function getGroupCacheKey($gid) { + protected function getGroupCacheKey(string $gid): string { return 'group-' . $gid . '-lastSeenOn'; } /** - * @param string $id - * @param string $method - * @param array $parameters - * @param bool $passOnWhen - * @return mixed + * Asks the backend connected to the server that supposely takes care of the gid from the request. + * + * @param string $id the gid connected to the request + * @param string $method the method of the group backend that shall be called + * @param array $parameters an array of parameters to be passed + * @param bool $passOnWhen the result matches this variable + * @return mixed the result of the method or false */ - abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); + abstract protected function callOnLastSeenOn(string $id, string $method, array $parameters, bool $passOnWhen): mixed; - /** - * @param string $id - * @param string $method - * @param array $parameters - * @return mixed - */ - abstract protected function walkBackends($id, $method, $parameters); + abstract protected function walkBackends(string $id, string $method, array $parameters): mixed; - /** - * @param string $id - * @return Access - */ - abstract public function getLDAPAccess($id); + abstract public function getLDAPAccess(string $name): Access; abstract protected function activeBackends(): int; @@ -122,13 +106,11 @@ protected function isSingleBackend(): bool { /** * Takes care of the request to the User backend * - * @param string $id * @param string $method string, the method of the user backend that shall be called * @param array $parameters an array of parameters to be passed - * @param bool $passOnWhen * @return mixed the result of the specified method */ - protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { + protected function handleRequest(string $id, string $method, array $parameters, bool $passOnWhen = false): mixed { if (!$this->isSingleBackend()) { $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); } @@ -138,11 +120,7 @@ protected function handleRequest($id, $method, $parameters, $passOnWhen = false) return $result; } - /** - * @param string|null $key - * @return string - */ - private function getCacheKey($key) { + private function getCacheKey(?string $key): string { $prefix = 'LDAP-Proxy-'; if ($key === null) { return $prefix; @@ -151,10 +129,9 @@ private function getCacheKey($key) { } /** - * @param string $key * @return mixed|null */ - public function getFromCache($key) { + public function getFromCache(string $key) { if ($this->cache === null) { return null; } @@ -168,11 +145,7 @@ public function getFromCache($key) { return json_decode(base64_decode($value)); } - /** - * @param string $key - * @param mixed $value - */ - public function writeToCache($key, $value) { + public function writeToCache(string $key, mixed $value): void { if ($this->cache === null) { return; } @@ -181,7 +154,7 @@ public function writeToCache($key, $value) { $this->cache->set($key, $value, 2592000); } - public function clearCache() { + public function clearCache(): void { if ($this->cache === null) { return; } diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index 7f4f6e6c24c8d..ff44bd0277da4 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -45,7 +45,7 @@ public function __construct( * @return boolean either the user can or cannot * @throws \Exception */ - public function canChangeAvatar($uid) { + public function canChangeAvatar($uid): bool { if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { return $this->userPluginManager->canChangeAvatar($uid); } @@ -103,14 +103,8 @@ public function loginName2UserName($loginName, bool $forceLdapRefetch = false) { } } - /** - * returns the username for the given LDAP DN, if available - * - * @param string $dn - * @return string|false with the username - */ #[\Override] - public function dn2UserName($dn) { + public function dn2UserName(string $dn): string|false { return $this->access->dn2username($dn); } @@ -542,16 +536,8 @@ public function getDisplayNames($search = '', $limit = null, $offset = null) { return $displayNames; } - /** - * Check if backend implements actions - * @param int $actions bitwise-or'ed actions - * @return boolean - * - * Returns the supported actions as int to be - * compared with \OC\User\Backend::CREATE_USER etc. - */ #[\Override] - public function implementsActions($actions) { + public function implementsActions(int $actions): bool { return (bool)((Backend::CHECK_PASSWORD | Backend::GET_HOME | Backend::GET_DISPLAYNAME @@ -562,11 +548,8 @@ public function implementsActions($actions) { & $actions); } - /** - * @return bool - */ #[\Override] - public function hasUserListings() { + public function hasUserListings(): bool { return true; } @@ -594,34 +577,18 @@ public function countMappedUsers(): int { return $this->access->getUserMapper()->count(); } - /** - * Backend name to be shown in user management - * @return string the name of the backend to be shown - */ #[\Override] - public function getBackendName() { + public function getBackendName(): string { return 'LDAP'; } - /** - * Return access for LDAP interaction. - * @param string $uid - * @return Access instance of Access for LDAP interaction - */ #[\Override] - public function getLDAPAccess($uid) { + public function getLDAPAccess(string $name): Access { return $this->access; } - /** - * Return LDAP connection resource from a cloned connection. - * The cloned connection needs to be closed manually. - * of the current access. - * @param string $uid - * @return \LDAP\Connection The LDAP connection - */ #[\Override] - public function getNewLDAPConnection($uid) { + public function getNewLDAPConnection(string $name): \LDAP\Connection { $connection = clone $this->access->getConnection(); return $connection->getConnectionResource(); } diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index 080f828e0a22d..896091bbb0d27 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -50,16 +50,8 @@ protected function newInstance(string $configPrefix): User_LDAP { ); } - /** - * Tries the backends one after the other until a positive result is returned from the specified method - * - * @param string $id the uid connected to the request - * @param string $method the method of the user backend that shall be called - * @param array $parameters an array of parameters to be passed - * @return mixed the result of the method or false - */ #[\Override] - protected function walkBackends($id, $method, $parameters) { + protected function walkBackends(string $id, string $method, array $parameters): mixed { $this->setup(); $uid = $id; @@ -80,17 +72,8 @@ protected function walkBackends($id, $method, $parameters) { return false; } - /** - * Asks the backend connected to the server that supposely takes care of the uid from the request. - * - * @param string $id the uid connected to the request - * @param string $method the method of the user backend that shall be called - * @param array $parameters an array of parameters to be passed - * @param mixed $passOnWhen the result matches this variable - * @return mixed the result of the method or false - */ #[\Override] - protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) { + protected function callOnLastSeenOn(string $id, string $method, array $parameters, bool $passOnWhen): mixed { $this->setup(); $uid = $id; @@ -128,29 +111,15 @@ protected function activeBackends(): int { return count($this->backends); } - /** - * Check if backend implements actions - * - * @param int $actions bitwise-or'ed actions - * @return boolean - * - * Returns the supported actions as int to be - * compared with \OC\User\Backend::CREATE_USER etc. - */ #[\Override] - public function implementsActions($actions) { + public function implementsActions(int $actions): bool { $this->setup(); //it's the same across all our user backends obviously return $this->refBackend->implementsActions($actions); } - /** - * Backend name to be shown in user management - * - * @return string the name of the backend to be shown - */ #[\Override] - public function getBackendName() { + public function getBackendName(): string { $this->setup(); return $this->refBackend->getBackendName(); } @@ -345,7 +314,7 @@ public function setPassword($uid, $password) { * @return bool */ #[\Override] - public function hasUserListings() { + public function hasUserListings(): bool { $this->setup(); return $this->refBackend->hasUserListings(); } @@ -387,15 +356,9 @@ public function countMappedUsers(): int { return $users; } - /** - * Return access for LDAP interaction. - * - * @param string $uid - * @return Access instance of Access for LDAP interaction - */ #[\Override] - public function getLDAPAccess($uid) { - return $this->handleRequest($uid, 'getLDAPAccess', [$uid]); + public function getLDAPAccess(string $name): Access { + return $this->handleRequest($name, 'getLDAPAccess', [$name]); } /** diff --git a/apps/user_ldap/tests/LDAPGroupPluginDummy.php b/apps/user_ldap/tests/LDAPGroupPluginDummy.php index a106772c66ad2..b9b78100f29af 100644 --- a/apps/user_ldap/tests/LDAPGroupPluginDummy.php +++ b/apps/user_ldap/tests/LDAPGroupPluginDummy.php @@ -11,31 +11,23 @@ use OCA\User_LDAP\ILDAPGroupPlugin; class LDAPGroupPluginDummy implements ILDAPGroupPlugin { - public function respondToActions() { - return null; - } - - public function createGroup($gid) { - return null; - } - - public function deleteGroup($gid) { - return null; + public function respondToActions(): int { + return 0; } - public function addToGroup($uid, $gid) { + public function createGroup(string $gid): ?string { return null; } - public function removeFromGroup($uid, $gid) { - return null; + public function deleteGroup(string $gid): false { + return false; } - public function countUsersInGroup($gid, $search = '') { - return null; + public function addToGroup(string $uid, string $gid): false { + return false; } - public function getGroupDetails($gid) { - return null; + public function removeFromGroup(string $uid, string $gid): false { + return false; } } diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 2a63a40c58416..45da266e30061 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -2774,16 +2774,6 @@ break;]]> - - - - - - - - - - diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 83b5dbeb66a9e..56c40526f81ad 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1876,7 +1876,6 @@ 'OC\\FullTextSearch\\Model\\SearchRequestSimpleQuery' => $baseDir . '/lib/private/FullTextSearch/Model/SearchRequestSimpleQuery.php', 'OC\\FullTextSearch\\Model\\SearchTemplate' => $baseDir . '/lib/private/FullTextSearch/Model/SearchTemplate.php', 'OC\\GlobalScale\\Config' => $baseDir . '/lib/private/GlobalScale/Config.php', - 'OC\\Group\\Backend' => $baseDir . '/lib/private/Group/Backend.php', 'OC\\Group\\Database' => $baseDir . '/lib/private/Group/Database.php', 'OC\\Group\\DisplayNameCache' => $baseDir . '/lib/private/Group/DisplayNameCache.php', 'OC\\Group\\Group' => $baseDir . '/lib/private/Group/Group.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 1b0383a23142e..d606076e8bfe0 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1917,7 +1917,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\FullTextSearch\\Model\\SearchRequestSimpleQuery' => __DIR__ . '/../../..' . '/lib/private/FullTextSearch/Model/SearchRequestSimpleQuery.php', 'OC\\FullTextSearch\\Model\\SearchTemplate' => __DIR__ . '/../../..' . '/lib/private/FullTextSearch/Model/SearchTemplate.php', 'OC\\GlobalScale\\Config' => __DIR__ . '/../../..' . '/lib/private/GlobalScale/Config.php', - 'OC\\Group\\Backend' => __DIR__ . '/../../..' . '/lib/private/Group/Backend.php', 'OC\\Group\\Database' => __DIR__ . '/../../..' . '/lib/private/Group/Database.php', 'OC\\Group\\DisplayNameCache' => __DIR__ . '/../../..' . '/lib/private/Group/DisplayNameCache.php', 'OC\\Group\\Group' => __DIR__ . '/../../..' . '/lib/private/Group/Group.php', diff --git a/lib/private/Group/Backend.php b/lib/private/Group/Backend.php deleted file mode 100644 index 4dd3e273133b4..0000000000000 --- a/lib/private/Group/Backend.php +++ /dev/null @@ -1,126 +0,0 @@ - 'createGroup', - self::DELETE_GROUP => 'deleteGroup', - self::ADD_TO_GROUP => 'addToGroup', - self::REMOVE_FROM_GOUP => 'removeFromGroup', - self::COUNT_USERS => 'countUsersInGroup', - self::GROUP_DETAILS => 'getGroupDetails', - self::IS_ADMIN => 'isAdmin', - ]; - - /** - * Get all supported actions - * @return int bitwise-or'ed actions - * - * Returns the supported actions as int to be - * compared with \OC\Group\Backend::CREATE_GROUP etc. - */ - public function getSupportedActions() { - $actions = 0; - foreach ($this->possibleActions as $action => $methodName) { - if (method_exists($this, $methodName)) { - $actions |= $action; - } - } - - return $actions; - } - - /** - * Check if backend implements actions - * @param int $actions bitwise-or'ed actions - * @return bool - * - * Returns the supported actions as int to be - * compared with \OC\Group\Backend::CREATE_GROUP etc. - */ - #[\Override] - public function implementsActions($actions) { - return (bool)($this->getSupportedActions() & $actions); - } - - /** - * is user in group? - * @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. - */ - #[\Override] - public function inGroup($uid, $gid) { - return in_array($gid, $this->getUserGroups($uid)); - } - - /** - * Get all groups a user belongs to - * @param string $uid Name of the user - * @return list an array of group names - * - * This function fetches all groups a user belongs to. It does not check - * if the user exists at all. - */ - #[\Override] - public function getUserGroups($uid) { - return []; - } - - /** - * get a list of all groups - * @param string $search - * @param int $limit - * @param int $offset - * @return array an array of group names - * - * Returns a list with all groups - */ - - #[\Override] - public function getGroups($search = '', $limit = -1, $offset = 0) { - return []; - } - - /** - * check if a group exists - * @param string $gid - * @return bool - */ - #[\Override] - public function groupExists($gid) { - return in_array($gid, $this->getGroups($gid, 1)); - } - - /** - * 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 - */ - #[\Override] - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { - return []; - } -} diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index c3e6df7cc3038..ea381303c7d89 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -45,7 +45,7 @@ class Database extends ABackend implements IBatchMethodsBackend, INamedBackend { /** @var array */ - private $groupCache = []; + private array $groupCache = []; /** * \OC\Group\Database constructor. @@ -59,8 +59,9 @@ public function __construct( /** * FIXME: This function should not be required! + * @psalm-assert IDBConnection $this->dbConn */ - private function fixDI() { + private function fixDI(): void { if ($this->dbConn === null) { $this->dbConn = Server::get(IDBConnection::class); } @@ -130,16 +131,8 @@ public function deleteGroup(string $gid): bool { return true; } - /** - * is user in group? - * @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. - */ #[\Override] - public function inGroup($uid, $gid) { + public function inGroup(string $uid, string $gid): bool { $this->fixDI(); // check @@ -153,17 +146,9 @@ public function inGroup($uid, $gid) { $result = $cursor->fetch(); $cursor->closeCursor(); - return $result ? true : false; + return $result !== false; } - /** - * Add a user to a group - * @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. - */ #[\Override] public function addToGroup(string $uid, string $gid): bool { $this->fixDI(); @@ -181,14 +166,6 @@ public function addToGroup(string $uid, string $gid): bool { } } - /** - * Removes a user from a group - * @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. - */ #[\Override] public function removeFromGroup(string $uid, string $gid): bool { $this->fixDI(); @@ -202,21 +179,8 @@ public function removeFromGroup(string $uid, string $gid): bool { return true; } - /** - * Get all groups a user belongs to - * @param string $uid Name of the user - * @return list an array of group names - * - * This function fetches all groups a user belongs to. It does not check - * if the user exists at all. - */ #[\Override] - public function getUserGroups($uid) { - //guests has empty or null $uid - if ($uid === null || $uid === '') { - return []; - } - + public function getUserGroups(string $uid): array { $this->fixDI(); // No magic! @@ -240,17 +204,8 @@ public function getUserGroups($uid) { return $groups; } - /** - * get a list of all groups - * @param string $search - * @param int $limit - * @param int $offset - * @return array an array of group names - * - * Returns a list with all groups - */ #[\Override] - public function getGroups(string $search = '', int $limit = -1, int $offset = 0) { + public function getGroups(string $search = '', int $limit = -1, int $offset = 0): array { $this->fixDI(); $query = $this->dbConn->getQueryBuilder(); @@ -288,13 +243,8 @@ public function getGroups(string $search = '', int $limit = -1, int $offset = 0) return $groups; } - /** - * check if a group exists - * @param string $gid - * @return bool - */ #[\Override] - public function groupExists($gid) { + public function groupExists(string $gid): bool { $this->fixDI(); // Check cache first @@ -320,9 +270,6 @@ public function groupExists($gid) { return false; } - /** - * {@inheritdoc} - */ #[\Override] public function groupsExists(array $gids): array { $notFoundGids = []; @@ -358,16 +305,8 @@ public function groupsExists(array $gids): array { return $existingGroups; } - /** - * 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 - */ #[\Override] - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0): array { + public function usersInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array { return array_values(array_map(fn ($user) => $user->getUid(), $this->searchInGroup($gid, $search, $limit, $offset))); } @@ -432,12 +371,6 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1, return $users; } - /** - * get the number of all users matching the search string in a group - * @param string $gid - * @param string $search - * @return int - */ #[\Override] public function countUsersInGroup(string $gid, string $search = ''): int { $this->fixDI(); @@ -466,13 +399,6 @@ public function countUsersInGroup(string $gid, string $search = ''): int { return $count; } - /** - * get the number of disabled users in a group - * - * @param string $search - * - * @return int - */ #[\Override] public function countDisabledInGroup(string $gid): int { $this->fixDI(); @@ -533,9 +459,6 @@ public function getGroupDetails(string $gid): array { return []; } - /** - * {@inheritdoc} - */ #[\Override] public function getGroupsDetails(array $gids): array { $notFoundGids = []; @@ -595,11 +518,6 @@ public function setDisplayName(string $gid, string $displayName): bool { return true; } - /** - * Backend name to be shown in group management - * @return string the name of the backend to be shown - * @since 21.0.0 - */ #[\Override] public function getBackendName(): string { return 'Database'; diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php index 65aff5d225b4a..83283d755474c 100644 --- a/lib/private/Group/Group.php +++ b/lib/private/Group/Group.php @@ -92,11 +92,6 @@ public function setDisplayName(string $displayName): bool { return false; } - /** - * get all users in the group - * - * @return array - */ #[\Override] public function getUsers(): array { if ($this->usersLoaded) { @@ -119,12 +114,6 @@ public function getUsers(): array { return $this->users; } - /** - * check if a user is in the group - * - * @param IUser $user - * @return bool - */ #[\Override] public function inGroup(IUser $user): bool { if (isset($this->users[$user->getUID()])) { @@ -139,11 +128,6 @@ public function inGroup(IUser $user): bool { return false; } - /** - * add a user to the group - * - * @param IUser $user - */ #[\Override] public function addUser(IUser $user): void { if ($this->inGroup($user)) { @@ -156,7 +140,7 @@ public function addUser(IUser $user): void { $this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]); } foreach ($this->backends as $backend) { - if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { + if ($backend->implementsActions(\OCP\GroupInterface::ADD_TO_GROUP)) { /** @var IAddToGroupBackend $backend */ $backend->addToGroup($user->getUID(), $this->gid); $this->users[$user->getUID()] = $user; @@ -171,9 +155,6 @@ public function addUser(IUser $user): void { } } - /** - * remove a user from the group - */ #[\Override] public function removeUser(IUser $user): void { $result = false; @@ -182,7 +163,7 @@ public function removeUser(IUser $user): void { $this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]); } foreach ($this->backends as $backend) { - if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) && $backend->inGroup($user->getUID(), $this->gid)) { + if ($backend->implementsActions(\OCP\GroupInterface::REMOVE_FROM_GOUP) && $backend->inGroup($user->getUID(), $this->gid)) { /** @var IRemoveFromGroupBackend $backend */ $backend->removeFromGroup($user->getUID(), $this->gid); $result = true; @@ -204,10 +185,6 @@ public function removeUser(IUser $user): void { } } - /** - * Search for users in the group by userid or display name - * @return IUser[] - */ #[\Override] public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array { $users = []; @@ -230,17 +207,11 @@ public function searchUsers(string $search, ?int $limit = null, ?int $offset = n return $users; } - /** - * returns the number of users matching the search string - * - * @param string $search - * @return int|bool - */ #[\Override] - public function count($search = ''): int|bool { + public function count($search = ''): int|false { $users = false; foreach ($this->backends as $backend) { - if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { + if ($backend->implementsActions(\OCP\GroupInterface::COUNT_USERS)) { /** @var ICountUsersBackend $backend */ if ($users === false) { //we could directly add to a bool variable, but this would @@ -253,13 +224,8 @@ public function count($search = ''): int|bool { return $users; } - /** - * returns the number of disabled users - * - * @return int|bool - */ #[\Override] - public function countDisabled(): int|bool { + public function countDisabled(): int|false { $users = false; foreach ($this->backends as $backend) { if ($backend instanceof ICountDisabledInGroup) { @@ -274,25 +240,11 @@ public function countDisabled(): int|bool { return $users; } - /** - * search for users in the group by displayname - * - * @param string $search - * @param int $limit - * @param int $offset - * @return IUser[] - * @deprecated 27.0.0 Use searchUsers instead (same implementation) - */ #[\Override] public function searchDisplayName(string $search, ?int $limit = null, ?int $offset = null): array { return $this->searchUsers($search, $limit, $offset); } - /** - * Get the names of the backend classes the group is connected to - * - * @return string[] - */ #[\Override] public function getBackendNames(): array { $backends = []; @@ -307,11 +259,6 @@ public function getBackendNames(): array { return $backends; } - /** - * Delete the group - * - * @return bool - */ #[\Override] public function delete(): bool { // Prevent users from deleting group admin @@ -325,7 +272,7 @@ public function delete(): bool { $this->emitter->emit('\OC\Group', 'preDelete', [$this]); } foreach ($this->backends as $backend) { - if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { + if ($backend->implementsActions(\OCP\GroupInterface::DELETE_GROUP)) { /** @var IDeleteGroupBackend $backend */ $result = $result || $backend->deleteGroup($this->gid); } @@ -355,10 +302,6 @@ private function getVerifiedUsers(array $userIds): array { return $users; } - /** - * @return bool - * @since 14.0.0 - */ #[\Override] public function canRemoveUser(): bool { foreach ($this->backends as $backend) { @@ -369,10 +312,6 @@ public function canRemoveUser(): bool { return false; } - /** - * @return bool - * @since 14.0.0 - */ #[\Override] public function canAddUser(): bool { foreach ($this->backends as $backend) { @@ -383,10 +322,6 @@ public function canAddUser(): bool { return false; } - /** - * @return bool - * @since 16.0.0 - */ #[\Override] public function hideFromCollaboration(): bool { return array_reduce($this->backends, function (bool $hide, GroupInterface $backend) { diff --git a/lib/private/User/Backend.php b/lib/private/User/Backend.php index e18afa0102bea..79374c9715df4 100644 --- a/lib/private/User/Backend.php +++ b/lib/private/User/Backend.php @@ -32,7 +32,7 @@ abstract class Backend implements UserInterface { public const PROVIDE_AVATAR = 16777216; // 1 << 24 public const COUNT_USERS = 268435456; // 1 << 28 - protected $possibleActions = [ + protected array $possibleActions = [ self::CREATE_USER => 'createUser', self::SET_PASSWORD => 'setPassword', self::CHECK_PASSWORD => 'checkPassword', @@ -50,7 +50,7 @@ abstract class Backend implements UserInterface { * Returns the supported actions as int to be * compared with self::CREATE_USER etc. */ - public function getSupportedActions() { + public function getSupportedActions(): int { $actions = 0; foreach ($this->possibleActions as $action => $methodName) { if (method_exists($this, $methodName)) { @@ -61,83 +61,37 @@ public function getSupportedActions() { return $actions; } - /** - * Check if backend implements actions - * @param int $actions bitwise-or'ed actions - * @return boolean - * - * Returns the supported actions as int to be - * compared with self::CREATE_USER etc. - */ #[\Override] - public function implementsActions($actions) { + public function implementsActions(int $actions): bool { return (bool)($this->getSupportedActions() & $actions); } - /** - * delete a user - * @param string $uid The username of the user to delete - * @return bool - * - * Deletes a user - */ #[\Override] - public function deleteUser($uid) { + public function deleteUser(string $uid): bool { return false; } - /** - * Get a list of all users - * - * @param string $search - * @param null|int $limit - * @param null|int $offset - * @return string[] an array of all uids - */ #[\Override] - public function getUsers($search = '', $limit = null, $offset = null) { + public function getUsers(string $search = '', ?int $limit = null, ?int $offset = null): array { return []; } - /** - * check if a user exists - * @param string $uid the username - * @return boolean - */ #[\Override] - public function userExists($uid) { + public function userExists(string $uid): bool { return false; } - /** - * get the user's home directory - * @param string $uid the username - * @return string|boolean - */ - public function getHome(string $uid) { + public function getHome(string $uid): string|false { return false; } - /** - * get display name of the user - * @param string $uid user ID of the user - * @return string display name - */ #[\Override] - public function getDisplayName($uid) { + public function getDisplayName(string $uid) { return $uid; } - /** - * Get a list of all display names and user ids. - * - * @param string $search - * @param int|null $limit - * @param int|null $offset - * @return array an array of all displayNames (value) and the corresponding uids (key) - */ #[\Override] - public function getDisplayNames($search = '', $limit = null, $offset = null) { + public function getDisplayNames(string $search = '', ?int $limit = null, ?int $offset = null) { $displayNames = []; $users = $this->getUsers($search, $limit, $offset); foreach ($users as $user) { @@ -146,12 +100,8 @@ public function getDisplayNames($search = '', $limit = null, $offset = null) { return $displayNames; } - /** - * Check if a user list is available or not - * @return boolean if users can be listed or not - */ #[\Override] - public function hasUserListings() { + public function hasUserListings(): bool { return false; } } diff --git a/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php b/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php index eae08736085e4..a0396217ddcd8 100644 --- a/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php +++ b/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php @@ -14,8 +14,8 @@ use OC\User\User; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; +use OCP\Config\IUserConfig; use OCP\EventDispatcher\IEventDispatcher; -use OCP\IConfig; use OCP\Server; use Psr\Log\LoggerInterface; @@ -23,7 +23,7 @@ class CleanupDeletedUsers extends TimedJob { public function __construct( ITimeFactory $time, private Manager $userManager, - private IConfig $config, + private IUserConfig $config, private LoggerInterface $logger, ) { parent::__construct($time); diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php index 10dee5c2eac77..9d1e25e01dbe2 100644 --- a/lib/private/User/Database.php +++ b/lib/private/User/Database.php @@ -456,25 +456,13 @@ public function getUsers($search = '', $limit = null, $offset = null) { return $userIds; } - /** - * check if a user exists - * - * @param string $uid the username - * @return boolean - */ #[\Override] - public function userExists($uid) { + public function userExists(string $uid): bool { return $this->loadUser($uid); } - /** - * get the user's home directory - * - * @param string $uid the username - * @return string|false - */ #[\Override] - public function getHome(string $uid) { + public function getHome(string $uid): string|false { if ($this->userExists($uid)) { return $this->config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid; } diff --git a/lib/private/User/PartiallyDeletedUsersBackend.php b/lib/private/User/PartiallyDeletedUsersBackend.php index d525dcf2988bc..2634f36c0ab8f 100644 --- a/lib/private/User/PartiallyDeletedUsersBackend.php +++ b/lib/private/User/PartiallyDeletedUsersBackend.php @@ -7,7 +7,7 @@ namespace OC\User; -use OCP\IConfig; +use OCP\Config\IUserConfig; use OCP\IUserBackend; use OCP\User\Backend\IGetHomeBackend; @@ -19,7 +19,7 @@ class PartiallyDeletedUsersBackend extends Backend implements IGetHomeBackend, IUserBackend { public function __construct( - private IConfig $config, + private IUserConfig $config, ) { } @@ -35,18 +35,18 @@ public function getBackendName(): string { } #[\Override] - public function userExists($uid) { - return $this->config->getUserValue($uid, 'core', 'deleted') === 'true'; + public function userExists(string $uid): bool { + return $this->config->getValueBool($uid, 'core', 'deleted'); } #[\Override] public function getHome(string $uid): string|false { - return $this->config->getUserValue($uid, 'core', 'deleted.home-path') ?: false; + return $this->config->getValueString($uid, 'core', 'deleted.home-path') ?: false; } #[\Override] - public function getUsers($search = '', $limit = null, $offset = null) { - return $this->config->getUsersForUserValue('core', 'deleted', 'true'); + public function getUsers(string $search = '', ?int $limit = null, ?int $offset = null): array { + return iterator_to_array($this->config->searchUsersByValueBool('core', 'deleted', true)); } /** @@ -55,8 +55,8 @@ public function getUsers($search = '', $limit = null, $offset = null) { * meaning the user still exists so we unmark them as it still can be accessed (and deleted) normally. */ public function unmarkUser(string $userId): void { - $this->config->deleteUserValue($userId, 'core', 'deleted'); - $this->config->deleteUserValue($userId, 'core', 'deleted.home-path'); + $this->config->deleteUserConfig($userId, 'core', 'deleted'); + $this->config->deleteUserConfig($userId, 'core', 'deleted.home-path'); } } diff --git a/lib/public/Group/Backend/ABackend.php b/lib/public/Group/Backend/ABackend.php index 93c7a21070319..a2c18d99b70df 100644 --- a/lib/public/Group/Backend/ABackend.php +++ b/lib/public/Group/Backend/ABackend.php @@ -23,7 +23,7 @@ abstract class ABackend implements GroupInterface, IBatchMethodsBackend { * @return bool */ #[\Override] - public function implementsActions($actions): bool { + public function implementsActions(int $actions): bool { $implements = 0; if ($this instanceof IAddToGroupBackend) { diff --git a/lib/public/GroupInterface.php b/lib/public/GroupInterface.php index cbfd74a068af0..0a188ebed5934 100644 --- a/lib/public/GroupInterface.php +++ b/lib/public/GroupInterface.php @@ -1,20 +1,23 @@ an array of group names * @since 4.5.0 * - * This function fetches all groups a user belongs to. It does not check - * if the user exists at all. */ - public function getUserGroups($uid); + public function getUserGroups(string $uid); /** - * @brief Get a list of all groups + * Get a list of all groups. * * @param string $search * @param int $limit * @param int $offset * @return array an array of group names * @since 4.5.0 - * - * Returns a list with all groups */ public function getGroups(string $search = '', int $limit = -1, int $offset = 0); /** - * @brief Check if a group exists + * Check if a group exists * - * @param string $gid + * @param non-empty-string $gid * @return bool * @since 4.5.0 */ - public function groupExists($gid); + public function groupExists(string $gid); /** - * @brief Get a list of user ids in a group matching the given search parameters. + * Get a list of user ids in a group matching the given search parameters. * - * @param string $gid + * @param non-empty-string $gid * @param string $search * @param int $limit * @param int $offset @@ -127,5 +127,5 @@ public function groupExists($gid); * @since 4.5.0 * @deprecated 27.0.0 Use searchInGroup instead, for performance reasons */ - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0); + public function usersInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0); } diff --git a/lib/public/User/Backend/IGetHomeBackend.php b/lib/public/User/Backend/IGetHomeBackend.php index bdd32781e8c3e..5aeef2a27e33e 100644 --- a/lib/public/User/Backend/IGetHomeBackend.php +++ b/lib/public/User/Backend/IGetHomeBackend.php @@ -17,7 +17,7 @@ interface IGetHomeBackend { * @since 14.0.0 * * @param string $uid the username - * @return string|bool Datadir on success false on failure + * @return string|false Datadir on success false on failure */ public function getHome(string $uid); } diff --git a/lib/public/UserInterface.php b/lib/public/UserInterface.php index 34e7a09feb7d5..e73bf6841cdd3 100644 --- a/lib/public/UserInterface.php +++ b/lib/public/UserInterface.php @@ -10,11 +10,12 @@ namespace OCP; +use OCP\AppFramework\Attribute\Implementable; + /** * TODO actually this is a IUserBackend - * - * @since 4.5.0 */ +#[Implementable(since: '4.5.0')] interface UserInterface { /** * Check if backend implements actions @@ -26,7 +27,7 @@ interface UserInterface { * @since 4.5.0 * @deprecated 14.0.0 Switch to the interfaces from OCP\User\Backend */ - public function implementsActions($actions); + public function implementsActions(int $actions); /** * delete a user @@ -34,7 +35,7 @@ public function implementsActions($actions); * @return bool * @since 4.5.0 */ - public function deleteUser($uid); + public function deleteUser(string $uid); /** * Get a list of all users @@ -45,23 +46,25 @@ public function deleteUser($uid); * @return string[] an array of all uids * @since 4.5.0 */ - public function getUsers($search = '', $limit = null, $offset = null); + public function getUsers(string $search = '', ?int $limit = null, ?int $offset = null); /** - * check if a user exists + * Check if a user exists. + * * @param string $uid the username - * @return boolean + * @return bool * @since 4.5.0 */ - public function userExists($uid); + public function userExists(string $uid); /** - * get display name of the user + * Get display name of the user. + * * @param string $uid user ID of the user * @return string display name * @since 4.5.0 */ - public function getDisplayName($uid); + public function getDisplayName(string $uid); /** * Get a list of all display names and user ids. @@ -72,7 +75,7 @@ public function getDisplayName($uid); * @return array an array of all displayNames (value) and the corresponding uids (key) * @since 4.5.0 */ - public function getDisplayNames($search = '', $limit = null, $offset = null); + public function getDisplayNames(string $search = '', ?int $limit = null, ?int $offset = null); /** * Check if a user list is available or not