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

πŸ›ƒ Chat permission API + Web #7235

Merged
merged 7 commits into from
Apr 29, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m

]]></description>

<version>15.0.0-dev.2</version>
<version>15.0.0-dev.3</version>
<licence>agpl</licence>

<author>Aleksandra Lazarević</author>
Expand Down
3 changes: 3 additions & 0 deletions docs/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ title: Capabilities
* `reactions` - Api reactions to chat message
* `rich-object-list-media` - When the API to get the chat messages for shared media is available
* `rich-object-delete` - When the API allows to delete chat messages which are file or rich object shares

## 15
* `chat-permission` - When permission 128 is required to post chat messages, reaction or share items to the conversation
1 change: 1 addition & 0 deletions docs/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ title: Constants
* `16` Can publish audio stream
* `32` Can publish video stream
* `64` Can publish screen sharing stream
* `128` Can post chat message, share items and do reactions

### Attendee permission modifications
* `set` - Setting this permission set.
Expand Down
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function getCapabilities(): array {
'conversation-permissions',
'rich-object-list-media',
'rich-object-delete',
'chat-permission',
],
'config' => [
'attachments' => [
Expand Down
8 changes: 8 additions & 0 deletions lib/Collaboration/Collaborators/RoomPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace OCA\Talk\Collaboration\Collaborators;

use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
Expand Down Expand Up @@ -62,6 +64,12 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
continue;
}

$participant = $room->getParticipant($userId, false);
if (!$participant instanceof Participant || !($participant->getPermissions() & Attendee::PERMISSIONS_CHAT)) {
// No chat permissions is like read-only
continue;
}

if (stripos($room->getDisplayName($userId), $search) !== false) {
$item = $this->roomToSearchResultItem($room, $userId);

Expand Down
4 changes: 4 additions & 0 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function parseCommentToResponse(IComment $comment, Message $parentMessage
* @PublicPage
* @RequireParticipant
* @RequireReadWriteConversation
* @RequirePermissions(permissions=chat)
* @RequireModeratorOrNoLobby
*
* Sends a new chat message to the given room.
Expand Down Expand Up @@ -235,6 +236,7 @@ public function sendMessage(string $message, string $actorDisplayName = '', stri
* @PublicPage
* @RequireParticipant
* @RequireReadWriteConversation
* @RequirePermissions(permissions=chat)
* @RequireModeratorOrNoLobby
*
* Sends a rich-object to the given room.
Expand Down Expand Up @@ -575,6 +577,7 @@ protected function loadSelfReactions(array $messages, array $commentIdToIndex):
* @NoAdminRequired
* @RequireParticipant
* @RequireReadWriteConversation
* @RequirePermissions(permissions=chat)
* @RequireModeratorOrNoLobby
*
* @param int $messageId
Expand Down Expand Up @@ -825,6 +828,7 @@ protected function getMessagesForRoom(Room $room, array $messageIds): array {
* @PublicPage
* @RequireParticipant
* @RequireReadWriteConversation
* @RequirePermissions(permissions=chat)
* @RequireModeratorOrNoLobby
*
* @param string $search
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/ReactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(string $appName,
* @PublicPage
* @RequireParticipant
* @RequireReadWriteConversation
* @RequirePermissions(permissions=chat)
* @RequireModeratorOrNoLobby
*
* @param int $messageId for reaction
Expand Down Expand Up @@ -78,6 +79,7 @@ public function react(int $messageId, string $reaction): DataResponse {
* @PublicPage
* @RequireParticipant
* @RequireReadWriteConversation
* @RequirePermissions(permissions=chat)
* @RequireModeratorOrNoLobby
*
* @param int $messageId for reaction
Expand Down
28 changes: 28 additions & 0 deletions lib/Exceptions/PermissionsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Julien Veyssier <eneiluj@posteo.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Talk\Exceptions;

class PermissionsException extends \Exception {
}
27 changes: 26 additions & 1 deletion lib/Middleware/InjectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Talk\Controller\AEnvironmentAwareController;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\PermissionsException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Middleware\Exceptions\LobbyException;
Expand Down Expand Up @@ -108,6 +109,11 @@ public function beforeController($controller, $methodName): void {
if ($this->reflector->hasAnnotation('RequireModeratorOrNoLobby')) {
$this->checkLobbyState($controller);
}

$requiredPermissions = $this->reflector->getAnnotationParameter('RequirePermissions', 'permissions');
if ($requiredPermissions) {
$this->checkPermissions($controller, $requiredPermissions);
}
}

/**
Expand Down Expand Up @@ -188,6 +194,24 @@ protected function checkReadOnlyState(AEnvironmentAwareController $controller):
}
}

/**
* @param AEnvironmentAwareController $controller
* @throws PermissionsException
*/
protected function checkPermissions(AEnvironmentAwareController $controller, string $permissions): void {
$textPermissions = explode(',', $permissions);
$participant = $controller->getParticipant();
if (!$participant instanceof Participant) {
throw new PermissionsException();
}

foreach ($textPermissions as $textPermission) {
if ($textPermission === 'chat' && !($participant->getPermissions() & Attendee::PERMISSIONS_CHAT)) {
throw new PermissionsException();
}
}
}

/**
* @param AEnvironmentAwareController $controller
* @throws LobbyException
Expand Down Expand Up @@ -238,7 +262,8 @@ public function afterException($controller, $methodName, \Exception $exception):
}

if ($exception instanceof NotAModeratorException ||
$exception instanceof ReadOnlyException) {
$exception instanceof ReadOnlyException ||
$exception instanceof PermissionsException) {
if ($controller instanceof OCSController) {
throw new OCSException('', Http::STATUS_FORBIDDEN);
}
Expand Down
113 changes: 113 additions & 0 deletions lib/Migration/Version15000Date20220427183026.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Migration;

use Closure;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version15000Date20220427183026 extends SimpleMigrationStep {
protected IDBConnection $connection;

public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}

/**
* Update existing permissions by adding the chat permissions when set to none default
*
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `\OCP\DB\ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$update = $this->connection->getQueryBuilder();
$update->update('talk_rooms')
->set('default_permissions', $update->func()->add(
'default_permissions',
$update->createNamedParameter(128, IQueryBuilder::PARAM_INT) // Attendee::PERMISSION_CHAT
))
->where($update->expr()->neq('default_permissions', $update->createNamedParameter(0, IQueryBuilder::PARAM_INT))) // Attendee::PERMISSIONS_DEFAULT
->andWhere(
$update->expr()->neq(
$update->expr()->castColumn(
$update->expr()->bitwiseAnd(
'default_permissions',
128 // Attendee::PERMISSION_CHAT
),
IQueryBuilder::PARAM_INT
),
$update->createNamedParameter(128, IQueryBuilder::PARAM_INT) // Attendee::PERMISSION_CHAT
)
);
$update->executeStatement();

$update = $this->connection->getQueryBuilder();
$update->update('talk_rooms')
->set('call_permissions', $update->func()->add(
'call_permissions',
$update->createNamedParameter(128, IQueryBuilder::PARAM_INT) // Attendee::PERMISSION_CHAT
))
->where($update->expr()->neq('call_permissions', $update->createNamedParameter(0, IQueryBuilder::PARAM_INT))) // Attendee::PERMISSIONS_DEFAULT
->andWhere(
$update->expr()->neq(
$update->expr()->castColumn(
$update->expr()->bitwiseAnd(
'call_permissions',
128 // Attendee::PERMISSION_CHAT
),
IQueryBuilder::PARAM_INT
),
$update->createNamedParameter(128, IQueryBuilder::PARAM_INT) // Attendee::PERMISSION_CHAT
)
);
$update->executeStatement();

$update = $this->connection->getQueryBuilder();
$update->update('talk_attendees')
->set('permissions', $update->func()->add(
'permissions',
$update->createNamedParameter(128, IQueryBuilder::PARAM_INT) // Attendee::PERMISSION_CHAT
))
->where($update->expr()->neq('permissions', $update->createNamedParameter(0, IQueryBuilder::PARAM_INT))) // Attendee::PERMISSIONS_DEFAULT
->andWhere(
$update->expr()->neq(
$update->expr()->castColumn(
$update->expr()->bitwiseAnd(
'permissions',
128 // Attendee::PERMISSION_CHAT
),
IQueryBuilder::PARAM_INT
),
$update->createNamedParameter(128, IQueryBuilder::PARAM_INT) // Attendee::PERMISSION_CHAT
)
);
$update->executeStatement();
}
}
2 changes: 2 additions & 0 deletions lib/Model/Attendee.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ class Attendee extends Entity {
public const PERMISSIONS_PUBLISH_AUDIO = 16;
public const PERMISSIONS_PUBLISH_VIDEO = 32;
public const PERMISSIONS_PUBLISH_SCREEN = 64;
public const PERMISSIONS_CHAT = 128;
public const PERMISSIONS_MAX_DEFAULT = // Max int (when all permissions are granted as default)
self::PERMISSIONS_CALL_START
| self::PERMISSIONS_CALL_JOIN
| self::PERMISSIONS_LOBBY_IGNORE
| self::PERMISSIONS_PUBLISH_AUDIO
| self::PERMISSIONS_PUBLISH_VIDEO
| self::PERMISSIONS_PUBLISH_SCREEN
| self::PERMISSIONS_CHAT
;
public const PERMISSIONS_MAX_CUSTOM = self::PERMISSIONS_MAX_DEFAULT | self::PERMISSIONS_CUSTOM; // Max int (when all permissions are granted as custom)

Expand Down
8 changes: 7 additions & 1 deletion lib/Share/RoomShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -142,13 +143,18 @@ public function create(IShare $share): IShare {
}

try {
$room->getParticipant($share->getSharedBy(), false);
$participant = $room->getParticipant($share->getSharedBy(), false);
} catch (ParticipantNotFoundException $e) {
// If the sharer is not a participant of the room even if the room
// exists the error is still "Room not found".
throw new GenericShareException('Room not found', $this->l->t('Conversation not found'), 404);
}

if (!($participant->getPermissions() & Attendee::PERMISSIONS_CHAT)) {
// No chat permissions is like read-only
throw new GenericShareException('Room not found', $this->l->t('Conversation not found'), 404);
}

$existingShares = $this->getSharesByPath($share->getNode());
foreach ($existingShares as $existingShare) {
if ($existingShare->getSharedWith() === $share->getSharedWith()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createLocalVue, mount, shallowMount } from '@vue/test-utils'
import { cloneDeep } from 'lodash'
import { EventBus } from '../../../../services/EventBus'
import storeConfig from '../../../../store/storeConfig'
import { CONVERSATION, ATTENDEE } from '../../../../constants'
import { CONVERSATION, ATTENDEE, PARTICIPANT } from '../../../../constants'

// Components
import Check from 'vue-material-design-icons/Check'
Expand Down Expand Up @@ -50,6 +50,7 @@ describe('Message.vue', () => {
lastCommonReadMessage: 0,
type: CONVERSATION.TYPE.GROUP,
readOnly: CONVERSATION.STATE.READ_WRITE,
permissions: PARTICIPANT.PERMISSIONS.MAX_DEFAULT,
}

testStoreConfig = cloneDeep(storeConfig)
Expand Down Expand Up @@ -806,6 +807,38 @@ describe('Message.vue', () => {
expect(reactionButtons.wrappers[1].text()).toBe('πŸ‘ 7')
})

test('shows reaction buttons with the right emoji count but without emoji placeholder when no chat permission', () => {
const conversationProps = {
token: TOKEN,
lastCommonReadMessage: 0,
type: CONVERSATION.TYPE.GROUP,
readOnly: CONVERSATION.STATE.READ_WRITE,
permissions: PARTICIPANT.PERMISSIONS.MAX_DEFAULT - PARTICIPANT.PERMISSIONS.CHAT,
}
testStoreConfig.modules.conversationsStore.getters.conversation
= jest.fn().mockReturnValue((token) => conversationProps)
store = new Store(testStoreConfig)

const wrapper = shallowMount(Message, {
localVue,
store,
propsData: messageProps,
})

const reactionsBar = wrapper.find('.message-body__reactions')

// Array of buttons
const reactionButtons = reactionsBar.findAll('.reaction-button')

// Number of buttons, 2 passed into the getter and 1 is the emoji
// picker
expect(reactionButtons.length).toBe(2)

// Text of the buttons
expect(reactionButtons.wrappers[0].text()).toBe('❀️ 1')
expect(reactionButtons.wrappers[1].text()).toBe('πŸ‘ 7')
})

test('dispatches store action upon picking an emoji from the emojipicker', () => {
const addReactionToMessageAction = jest.fn()
const userHasReactedGetter = jest.fn().mockReturnValue(() => false)
Expand Down
Loading