Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding group display name support in group backends #26750

Merged
merged 3 commits into from Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/files_sharing/lib/API/Share20OCS.php
Expand Up @@ -144,8 +144,9 @@ protected function formatShare(\OCP\Share\IShare $share) {
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$group = $this->groupManager->get($share->getSharedWith());
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = $share->getSharedWith();
$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {

$result['share_with'] = $share->getPassword();
Expand Down
30 changes: 19 additions & 11 deletions apps/files_sharing/lib/Controller/ShareesController.php
Expand Up @@ -158,9 +158,10 @@ protected function getUsers($search) {
}

$foundUserById = false;
$lowerSearch = strtolower($search);
foreach ($users as $uid => $userDisplayName) {
if (strtolower($uid) === strtolower($search) || strtolower($userDisplayName) === strtolower($search)) {
if (strtolower($uid) === strtolower($search)) {
if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
if (strtolower($uid) === $lowerSearch) {
$foundUserById = true;
}
$this->result['exact']['users'][] = [
Expand Down Expand Up @@ -218,7 +219,7 @@ protected function getGroups($search) {
$this->result['groups'] = $this->result['exact']['groups'] = [];

$groups = $this->groupManager->search($search, $this->limit, $this->offset);
$groups = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);

if (!$this->shareeEnumeration || sizeof($groups) < $this->limit) {
$this->reachedEndFor[] = 'groups';
Expand All @@ -229,21 +230,27 @@ protected function getGroups($search) {
// Intersect all the groups that match with the groups this user is a member of
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
$groups = array_intersect($groups, $userGroups);
$groupIds = array_intersect($groupIds, $userGroups);
}

foreach ($groups as $gid) {
if (strtolower($gid) === strtolower($search)) {
$lowerSearch = strtolower($search);
foreach ($groups as $group) {
// FIXME: use a more efficient approach
$gid = $group->getGID();
if (!in_array($gid, $groupIds)) {
continue;
}
if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
$this->result['exact']['groups'][] = [
'label' => $gid,
'label' => $group->getDisplayName(),
'value' => [
'shareType' => Share::SHARE_TYPE_GROUP,
'shareWith' => $gid,
],
];
} else {
$this->result['groups'][] = [
'label' => $gid,
'label' => $group->getDisplayName(),
'value' => [
'shareType' => Share::SHARE_TYPE_GROUP,
'shareWith' => $gid,
Expand All @@ -258,7 +265,7 @@ protected function getGroups($search) {
$group = $this->groupManager->get($search);
if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
array_push($this->result['exact']['groups'], [
'label' => $group->getGID(),
'label' => $group->getDisplayName(),
'value' => [
'shareType' => Share::SHARE_TYPE_GROUP,
'shareWith' => $group->getGID(),
Expand Down Expand Up @@ -292,10 +299,11 @@ protected function getRemote($search) {
if (!is_array($cloudIds)) {
$cloudIds = [$cloudIds];
}
$lowerSearch = strtolower($search);
foreach ($cloudIds as $cloudId) {
list(, $serverUrl) = $this->splitUserRemote($cloudId);
if (strtolower($contact['FN']) === strtolower($search) || strtolower($cloudId) === strtolower($search)) {
if (strtolower($cloudId) === strtolower($search)) {
if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
if (strtolower($cloudId) === $lowerSearch) {
$foundRemoteById = true;
}
$this->result['exact']['remotes'][] = [
Expand Down
53 changes: 50 additions & 3 deletions apps/files_sharing/tests/API/Share20OCSTest.php
Expand Up @@ -2080,9 +2080,10 @@ public function dataFormatShare() {
], $share, [], false
];

// with existing group
$share = \OC::$server->getShareManager()->newShare();
$share->setShareType(Share::SHARE_TYPE_GROUP)
->setSharedWith('recipient')
->setSharedWith('recipientGroup')
->setSharedBy('initiator')
->setShareOwner('owner')
->setPermissions(\OCP\Constants::PERMISSION_READ)
Expand Down Expand Up @@ -2112,8 +2113,47 @@ public function dataFormatShare() {
'file_source' => 3,
'file_parent' => 1,
'file_target' => 'myTarget',
'share_with' => 'recipient',
'share_with_displayname' => 'recipient',
'share_with' => 'recipientGroup',
'share_with_displayname' => 'recipientGroupDisplayName',
'mail_send' => 0,
'mimetype' => 'myMimeType',
], $share, [], false
];

// with unknown group / no group backend
$share = \OC::$server->getShareManager()->newShare();
$share->setShareType(Share::SHARE_TYPE_GROUP)
->setSharedWith('recipientGroup2')
->setSharedBy('initiator')
->setShareOwner('owner')
->setPermissions(\OCP\Constants::PERMISSION_READ)
->setNode($file)
->setShareTime(new \DateTime('2000-01-01T00:01:02'))
->setTarget('myTarget')
->setId(42);
$result[] = [
[
'id' => 42,
'share_type' => Share::SHARE_TYPE_GROUP,
'uid_owner' => 'initiator',
'displayname_owner' => 'initiator',
'permissions' => 1,
'stime' => 946684862,
'parent' => null,
'expiration' => null,
'token' => null,
'uid_file_owner' => 'owner',
'displayname_file_owner' => 'owner',
'path' => 'file',
'item_type' => 'file',
'storage_id' => 'storageId',
'storage' => 100,
'item_source' => 3,
'file_source' => 3,
'file_parent' => 1,
'file_target' => 'myTarget',
'share_with' => 'recipientGroup2',
'share_with_displayname' => 'recipientGroup2',
'mail_send' => 0,
'mimetype' => 'myMimeType',
], $share, [], false
Expand Down Expand Up @@ -2229,6 +2269,13 @@ public function dataFormatShare() {
*/
public function testFormatShare(array $expects, \OCP\Share\IShare $share, array $users, $exception) {
$this->userManager->method('get')->will($this->returnValueMap($users));

$recipientGroup = $this->createMock('\OCP\IGroup');
$recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
$this->groupManager->method('get')->will($this->returnValueMap([
['recipientGroup', $recipientGroup],
]));

$this->urlGenerator->method('linkToRouteAbsolute')
->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
->willReturn('myLink');
Expand Down
42 changes: 41 additions & 1 deletion apps/files_sharing/tests/API/ShareesTest.php
Expand Up @@ -134,7 +134,7 @@ protected function getUserMock($uid, $displayName) {
* @param string $gid
* @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getGroupMock($gid) {
protected function getGroupMock($gid, $displayName = null) {
$group = $this->getMockBuilder(IGroup::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -143,6 +143,15 @@ protected function getGroupMock($gid) {
->method('getGID')
->willReturn($gid);

if (is_null($displayName)) {
// note: this is how the Group class behaves
$displayName = $gid;
}

$group->expects($this->any())
->method('getDisplayName')
->willReturn($displayName);

return $group;
}

Expand Down Expand Up @@ -468,6 +477,7 @@ public function dataGetGroups() {
return [
['test', false, true, [], [], [], [], true, false],
['test', false, false, [], [], [], [], true, false],
// group without display name
[
'test', false, true,
[$this->getGroupMock('test1')],
Expand All @@ -477,6 +487,36 @@ public function dataGetGroups() {
true,
false,
],
// group with display name, search by id
[
'test', false, true,
[$this->getGroupMock('test1', 'Test One')],
[],
[],
[['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
true,
false,
],
// group with display name, search by display name
[
'one', false, true,
[$this->getGroupMock('test1', 'Test One')],
[],
[],
[['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
true,
false,
],
// group with display name, search by display name, exact expected
[
'Test One', false, true,
[$this->getGroupMock('test1', 'Test One')],
[],
[['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
[],
true,
false,
],
[
'test', false, false,
[$this->getGroupMock('test1')],
Expand Down
2 changes: 1 addition & 1 deletion core/js/sharedialogresharerinfoview.js
Expand Up @@ -78,7 +78,7 @@
'core',
'Shared with you and the group {group} by {owner}',
{
group: this.model.getReshareWith(),
group: this.model.getReshareWithDisplayName(),
owner: ownerDisplayName
}
);
Expand Down
8 changes: 8 additions & 0 deletions core/js/shareitemmodel.js
Expand Up @@ -337,6 +337,14 @@
return this.get('reshare').share_with;
},

/**
* @returns {string}
*/
getReshareWithDisplayName: function() {
var reshare = this.get('reshare');
return reshare.share_with_displayname || reshare.share_with;
},

/**
* @returns {number}
*/
Expand Down
23 changes: 21 additions & 2 deletions core/js/tests/specs/sharedialogviewSpec.js
Expand Up @@ -1038,16 +1038,35 @@ describe('OC.Share.ShareDialogView', function() {
dialog.render();
expect(dialog.$el.find('.shareWithField').prop('disabled')).toEqual(true);
});
it('shows reshare owner', function() {
it('shows reshare owner for single user share', function() {
shareModel.set({
reshare: {
uid_owner: 'user1'
uid_owner: 'user1',
displayname_owner: 'User One',
share_type: OC.Share.SHARE_TYPE_USER
},
shares: [],
permissions: OC.PERMISSION_READ
});
dialog.render();
expect(dialog.$el.find('.resharerInfoView .reshare').length).toEqual(1);
expect(dialog.$el.find('.resharerInfoView .reshare').text().trim()).toEqual('Shared with you by User One');
});
it('shows reshare owner for single user share', function() {
shareModel.set({
reshare: {
uid_owner: 'user1',
displayname_owner: 'User One',
share_with: 'group2',
share_with_displayname: 'Group Two',
share_type: OC.Share.SHARE_TYPE_GROUP
},
shares: [],
permissions: OC.PERMISSION_READ
});
dialog.render();
expect(dialog.$el.find('.resharerInfoView .reshare').length).toEqual(1);
expect(dialog.$el.find('.resharerInfoView .reshare').text().trim()).toEqual('Shared with you and the group Group Two by User One');
});
it('does not show reshare owner if owner is current user', function() {
shareModel.set({
Expand Down
5 changes: 5 additions & 0 deletions core/js/tests/specs/shareitemmodelSpec.js
Expand Up @@ -190,6 +190,7 @@ describe('OC.Share.ShareItemModel', function() {
uid_owner: 'owner',
displayname_owner: 'Owner',
share_with: 'root',
share_with_displayname: 'Wurzel',
permissions: 1
},
{
Expand Down Expand Up @@ -221,7 +222,11 @@ describe('OC.Share.ShareItemModel', function() {
// user share has higher priority
expect(reshare.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
expect(reshare.share_with).toEqual('root');
expect(reshare.share_with_displayname).toEqual('Wurzel');
expect(reshare.id).toEqual('1');

expect(model.getReshareWith()).toEqual('root');
expect(model.getReshareWithDisplayName()).toEqual('Wurzel');
});
it('does not parse link share when for a different file', function() {
/* jshint camelcase: false */
Expand Down
11 changes: 1 addition & 10 deletions lib/private/Group/Backend.php
Expand Up @@ -30,22 +30,13 @@ abstract class Backend implements \OCP\GroupInterface {
*/
const NOT_IMPLEMENTED = -501;

/**
* actions that user backends can define
*/
const CREATE_GROUP = 0x00000001;
const DELETE_GROUP = 0x00000010;
const ADD_TO_GROUP = 0x00000100;
const REMOVE_FROM_GOUP = 0x00001000;
//OBSOLETE const GET_DISPLAYNAME = 0x00010000;
const COUNT_USERS = 0x00100000;

protected $possibleActions = [
self::CREATE_GROUP => 'createGroup',
self::DELETE_GROUP => 'deleteGroup',
self::ADD_TO_GROUP => 'addToGroup',
self::REMOVE_FROM_GOUP => 'removeFromGroup',
self::COUNT_USERS => 'countUsersInGroup',
self::GROUP_DETAILS => 'getGroupDetails',
];

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/private/Group/Group.php
Expand Up @@ -65,18 +65,27 @@ class Group implements IGroup {
* @param \OC\Group\Backend[] $backends
* @param \OC\User\Manager $userManager
* @param \OC\Hooks\PublicEmitter $emitter
* @param string $displayName
*/
public function __construct($gid, $backends, $userManager, $emitter = null) {
public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
$this->gid = $gid;
$this->backends = $backends;
$this->userManager = $userManager;
$this->emitter = $emitter;
$this->displayName = $displayName;
}

public function getGID() {
return $this->gid;
}

public function getDisplayName() {
if (is_null($this->displayName)) {
return $this->gid;
}
return $this->displayName;
}

/**
* get all users in the group
*
Expand Down