-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #953 from arawa/backport/nextcloud-37677/exclude-s…
…ome-groups-from-sharing-with-users/stable3.0 backport: Limit the search to members
- Loading branch information
Showing
3 changed files
with
136 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace OCA\Workspace\Share\Group; | ||
|
||
use OCP\IGroupManager; | ||
use OCP\IUserSession; | ||
use OCP\Share\IManager; | ||
|
||
class GroupMembersOnlyChecker { | ||
public function __construct( | ||
private IGroupManager $groupManager, | ||
private IManager $shareManager, | ||
private IUserSession $userSession | ||
) { | ||
} | ||
|
||
public function checkboxIsChecked(): bool { | ||
return $this->shareManager->shareWithGroupMembersOnly(); | ||
} | ||
|
||
public function groupsExcludeSelected(): bool { | ||
if ( | ||
method_exists( | ||
$this->shareManager, | ||
"shareWithGroupMembersOnlyExcludeGroupsList" | ||
) | ||
) { | ||
return $this->shareManager->shareWithGroupMembersOnly() | ||
&& !empty($this->shareManager->shareWithGroupMembersOnlyExcludeGroupsList()); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function checkMemberInGroupExcluded(): bool { | ||
$excludedGroups = $this->shareManager->shareWithGroupMembersOnlyExcludeGroupsList(); | ||
|
||
$userSession = $this->userSession->getUser(); | ||
$uid = $userSession->getUID(); | ||
|
||
foreach ($excludedGroups as $gid) { | ||
if ($this->groupManager->isInGroup($uid, $gid)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace OCA\Workspace\Share\Group; | ||
|
||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
use OCP\IUserSession; | ||
use OCP\Share\IManager; | ||
|
||
class ShareMembersOnlyFilter { | ||
public function __construct( | ||
private IGroupManager $groupManager, | ||
private IManager $shareManager, | ||
private IUserSession $userSession | ||
) { | ||
} | ||
|
||
/** | ||
* @param IUser[] $users | ||
* @return IUser[] | ||
* Return users with groups in common. | ||
* Except some listed in the drop-down list | ||
* (`Settings > Share > Restrict users to only share with users in their groups > Ingore the following groups when cheking group membership`). | ||
*/ | ||
public function excludeGroupsList(array $users): array { | ||
$usersNotExclude = []; | ||
|
||
if (method_exists($this->shareManager, "shareWithGroupMembersOnlyExcludeGroupsList")) { | ||
$excludedGroups = $this->shareManager->shareWithGroupMembersOnlyExcludeGroupsList(); | ||
$userSession = $this->userSession->getUser(); | ||
$groupsOfUserSession = $this->groupManager->getUserGroups($userSession); | ||
$groupnamesOfUserSession = array_map(fn ($group) => $group->getGID(), $groupsOfUserSession); | ||
|
||
if (!empty($excludedGroups)) { | ||
$usersNotExclude = array_filter($users, function ($user) use ($excludedGroups, $groupnamesOfUserSession) { | ||
$groups = $this->groupManager->getUserGroups($user); | ||
$groupnames = array_values(array_map(fn ($group) => $group->getGID(), $groups)); | ||
$commonGroups = array_intersect($groupnamesOfUserSession, $groupnames); | ||
return count(array_diff($commonGroups, $excludedGroups)) !== 0; | ||
}); | ||
} | ||
|
||
return $usersNotExclude; | ||
} | ||
} | ||
|
||
/** | ||
* @param IUser[] $users | ||
* | ||
* @return IUser[] | ||
*/ | ||
public function filterUsersGroupOnly(array $users): array { | ||
$usersInTheSameGroup = []; | ||
$userSession = $this->userSession->getUser(); | ||
$groupsOfUserSession = $this->groupManager->getUserGroups($userSession); | ||
|
||
foreach ($groupsOfUserSession as $group) { | ||
$usersInTheSameGroup = array_merge($usersInTheSameGroup, $group->getUsers()); | ||
} | ||
|
||
$usersInTheSameGroup = array_filter( | ||
$users, | ||
function ($user) use ($usersInTheSameGroup) { | ||
$usernames = array_values(array_map(fn ($user) => $user->getUID(), $usersInTheSameGroup)); | ||
return in_array($user->getUID(), $usernames); | ||
}); | ||
|
||
return $usersInTheSameGroup; | ||
} | ||
} |