Skip to content

Commit

Permalink
Merge pull request #410 from nextcloud/fix-guest-entries-in-sidebar
Browse files Browse the repository at this point in the history
Fix guest entries in sidebar
  • Loading branch information
Ivansss authored Sep 20, 2017
2 parents 293a554 + bcbc315 commit e185152
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 22 deletions.
9 changes: 9 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@
'token' => '^[a-z0-9]{4,30}$',
],
],
[
'name' => 'Room#removeGuestFromRoom',
'url' => '/api/{apiVersion}/room/{token}/participants/guests',
'verb' => 'DELETE',
'requirements' => [
'apiVersion' => 'v1',
'token' => '^[a-z0-9]{4,30}$',
],
],
[
'name' => 'Room#promoteModerator',
'url' => '/api/{apiVersion}/room/{token}/moderators',
Expand Down
44 changes: 22 additions & 22 deletions js/views/participantview.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
var ITEM_TEMPLATE = '' +
'<a class="participant-entry-link {{#if isOffline}}participant-offline{{/if}}" href="#" data-sessionId="{{sessionId}}">' +
'<div class="avatar" data-user-id="{{userId}}" data-displayname="{{displayName}}"></div>' +
' {{displayName}}' +
' {{name}}' +
'{{#if participantIsOwner}}<span class="participant-moderator-indicator">(' + t('spreed', 'moderator') + ')</span>{{/if}}' +
'{{#if participantIsModerator}}<span class="participant-moderator-indicator">(' + t('spreed', 'moderator') + ')</span>{{/if}}' +
'</a>'+
Expand Down Expand Up @@ -101,28 +101,27 @@
});
},
templateContext: function() {
console.log(this.model.get('userId'));
console.log(this.model.get('participantType') !== 1);
console.log(this.model.get('userId') !== oc_current_user);
var canModerate = this.model.get('participantType') !== 1 && // can not moderate owners
var canModerate = this.model.get('participantType') !== OCA.SpreedMe.app.OWNER && // can not moderate owners
this.model.get('userId') !== oc_current_user && // can not moderate yourself
(OCA.SpreedMe.app.activeRoom.get('participantType') === 1 || // current user must be owner
OCA.SpreedMe.app.activeRoom.get('participantType') === 2); // or moderator.
(OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.OWNER || // current user must be owner
OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.MODERATOR); // or moderator.

return {
canModerate: canModerate,
participantIsUser: this.model.get('participantType') === 3,
participantIsModerator: this.model.get('participantType') === 2,
participantIsOwner: this.model.get('participantType') === 1
name: this.model.get('userId').length ? this.model.get('displayName') : t('spreed', 'Guest'),
participantIsUser: this.model.get('participantType') === OCA.SpreedMe.app.USER,
participantIsModerator: this.model.get('participantType') === OCA.SpreedMe.app.MODERATOR,
participantIsOwner: this.model.get('participantType') === OCA.SpreedMe.app.OWNER
};
},
onRender: function() {
this.$el.find('.avatar').each(function() {
var element = $(this);
if (element.data('displayname')) {
if (element.data('displayname').length) {
element.avatar(element.data('user-id'), 32, undefined, false, undefined, element.data('displayname'));
} else {
element.avatar(element.data('user-id'), 32);
element.imageplaceholder('?', undefined, 32);
element.css('background-color', '#b9b9b9');
}
});

Expand Down Expand Up @@ -157,7 +156,7 @@
this.ui.menu.toggleClass('open', this.menuShown);
},
promoteToModerator: function() {
if (this.model.get('participantType') !== 3) {
if (this.model.get('participantType') !== OCA.SpreedMe.app.USER) {
return;
}

Expand All @@ -179,7 +178,7 @@
});
},
demoteFromModerator: function() {
if (this.model.get('participantType') !== 2) {
if (this.model.get('participantType') !== OCA.SpreedMe.app.MODERATOR) {
return;
}

Expand All @@ -201,21 +200,22 @@
});
},
removeParticipant: function() {
if (this.model.get('participantType') === 1) {
if (this.model.get('participantType') === OCA.SpreedMe.app.OWNER) {
return;
}

if (this.model.get('userId') === '') {
console.log('Guests can currently not be deleted');
return;
}
var self = this,
participantId = this.model.get('userId'),
endpoint = '/participants';

var participantId = this.model.get('userId'),
self = this;
if (this.model.get('participantType') === OCA.SpreedMe.app.GUEST) {
participantId = this.model.get('sessionId');
endpoint += '/guests';
}

$.ajax({
type: 'DELETE',
url: OC.linkToOCS('apps/spreed/api/v1/room', 2) + OCA.SpreedMe.app.activeRoom.get('token') + '/participants',
url: OC.linkToOCS('apps/spreed/api/v1/room', 2) + OCA.SpreedMe.app.activeRoom.get('token') + endpoint,
data: {
participant: participantId
},
Expand Down
35 changes: 35 additions & 0 deletions lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,41 @@ public function removeSelfFromRoom($token) {
return new DataResponse([]);
}

/**
* @NoAdminRequired
*
* @param string $token
* @param string $participant
* @return DataResponse
*/
public function removeGuestFromRoom($token, $participant) {
try {
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
$currentParticipant = $room->getParticipant($this->userId);
} catch (RoomNotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\RuntimeException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

if (!in_array($currentParticipant->getParticipantType(), [Participant::OWNER, Participant::MODERATOR], true)) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

try {
$targetParticipant = $room->getParticipantBySession($participant);
} catch (\RuntimeException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

if ($targetParticipant->getParticipantType() !== Participant::GUEST) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

$room->removeParticipantBySession($targetParticipant);
return new DataResponse([]);
}

/**
* @NoAdminRequired
*
Expand Down
37 changes: 37 additions & 0 deletions lib/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ public function getParticipant($userId) {
return new Participant($this->db, $this, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId']);
}

/**
* @param string $sessionId
* @return Participant
* @throws \RuntimeException When the user is not a participant
*/
public function getParticipantBySession($sessionId) {
if (!is_string($sessionId) || $sessionId === '') {
throw new \RuntimeException('Not a user');
}

$query = $this->db->getQueryBuilder();
$query->select('*')
->from('spreedme_room_participants')
->where($query->expr()->eq('sessionId', $query->createNamedParameter($sessionId)))
->andWhere($query->expr()->eq('roomId', $query->createNamedParameter($this->getId())));
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();

if ($row === false) {
throw new \RuntimeException('User is not a participant');
}

return new Participant($this->db, $this, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId']);
}

public function deleteRoom() {
$query = $this->db->getQueryBuilder();

Expand Down Expand Up @@ -274,6 +300,17 @@ public function removeUser(IUser $user) {
$query->execute();
}

/**
* @param Participant $participant
*/
public function removeParticipantBySession(Participant $participant) {
$query = $this->db->getQueryBuilder();
$query->delete('spreedme_room_participants')
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('sessionId', $query->createNamedParameter($participant->getSessionId())));
$query->execute();
}

/**
* @param string $userId
* @return string
Expand Down

0 comments on commit e185152

Please sign in to comment.