Skip to content

Commit

Permalink
[FIX] Anonymous chat read (#14717)
Browse files Browse the repository at this point in the history
* Prevent errors when allowing anonymous read

* getSingleMessage for anonymous users

* Fix register user when allow read and write anonymous is enabled

* Fix anonymous with sidebar extended as default

Co-authored-by: ubergeekzone <ubergeekzone@gmail.com>
  • Loading branch information
sampaiodiego and ubergeekzone authored Jun 18, 2019
1 parent 0dec989 commit d7a9968
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/emoji/client/emojiPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getEmojiElement = (emoji, image) => image && `<li class="emoji-${ emoji }

const createEmojiList = (category, actualTone) => {
const html = Object.values(emoji.packages).map((emojiPackage) => {
if (!emojiPackage.emojisByCategory[category]) {
if (!emojiPackage.emojisByCategory || !emojiPackage.emojisByCategory[category]) {
return;
}

Expand Down
4 changes: 0 additions & 4 deletions app/lib/server/methods/getSingleMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ Meteor.methods({
getSingleMessage(msgId) {
check(msgId, String);

if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getSingleMessage' });
}

const msg = Messages.findOneById(msgId);

if (!msg || !msg.rid) {
Expand Down
4 changes: 2 additions & 2 deletions app/ui-master/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Template.main.helpers({
hasUsername() {
const uid = Meteor.userId();
const user = uid && Users.findOne({ _id: uid }, { fields: { username: 1 } });
return (user && user.username) || settings.get('Accounts_AllowAnonymousRead');
return (user && user.username) || (!uid && settings.get('Accounts_AllowAnonymousRead'));
},
requirePasswordChange() {
const user = Meteor.user();
Expand All @@ -194,7 +194,7 @@ Template.main.helpers({
const user = Meteor.user();

// User is already using 2fa
if (user.services.totp !== undefined && user.services.totp.enabled) {
if (!user || (user.services.totp !== undefined && user.services.totp.enabled)) {
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions app/ui-sidenav/client/roomList.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Template.roomList.helpers({

noSubscriptionText() {
const instance = Template.instance();
if (instance.data.anonymous) {
return 'No_channels_yet';
}
return roomTypes.roomTypes[instance.data.identifier].getUiText(UiTextContext.NO_ROOMS_SUBSCRIBED) || 'No_channels_yet';
},

Expand Down
4 changes: 1 addition & 3 deletions app/ui-sidenav/client/sidebarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ Template.sidebarItem.onCreated(function() {
this.lastMessageTs = new ReactiveVar();
this.timeAgoInterval;

// console.log('sidebarItem.onCreated');

this.autorun(() => {
const currentData = Template.currentData();

Expand All @@ -98,7 +96,7 @@ Template.sidebarItem.onCreated(function() {

const otherUser = settings.get('UI_Use_Real_Name') ? currentData.lastMessage.u.name || currentData.lastMessage.u.username : currentData.lastMessage.u.username;
const renderedMessage = renderMessageBody(currentData.lastMessage).replace(/<br\s?\\?>/g, ' ');
const sender = this.user._id === currentData.lastMessage.u._id ? t('You') : otherUser;
const sender = this.user && this.user._id === currentData.lastMessage.u._id ? t('You') : otherUser;

if (currentData.t === 'd' && Meteor.userId() !== currentData.lastMessage.u._id) {
this.renderedMessage = currentData.lastMessage.msg === '' ? t('Sent_an_attachment') : renderedMessage;
Expand Down
2 changes: 1 addition & 1 deletion app/ui-utils/client/lib/messageContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AutoTranslate } from '../../../autotranslate/client';
export function messageContext({ rid } = Template.instance()) {
const uid = Meteor.userId();
return {
u: Users.findOne({ _id: uid }, { fields: { name: 1, username: 1 } }),
u: Users.findOne({ _id: uid }, { fields: { name: 1, username: 1 } }) || {},
room: Rooms.findOne({ _id: rid }, {
reactive: false,
fields: {
Expand Down
10 changes: 9 additions & 1 deletion server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import s from 'underscore.string';
import { hasPermission } from '../../app/authorization';
import { Rooms, Users } from '../../app/models';
import { Federation } from '../../app/federation/server';
import { settings } from '../../app/settings/server';

const sortChannels = function(field, direction) {
switch (field) {
Expand Down Expand Up @@ -57,11 +58,13 @@ Meteor.methods({
limit,
};

const canViewAnonymous = settings.get('Accounts_AllowAnonymousRead') === true;

const user = Meteor.user();

if (type === 'channels') {
const sort = sortChannels(sortBy, sortDirection);
if (!hasPermission(user._id, 'view-c-room')) {
if ((!user && !canViewAnonymous) || (user && !hasPermission(user._id, 'view-c-room'))) {
return;
}

Expand All @@ -85,6 +88,11 @@ Meteor.methods({
};
}

// non-logged id user
if (!user) {
return;
}

// type === users
if (!hasPermission(user._id, 'view-outside-room') || !hasPermission(user._id, 'view-d-room')) {
return;
Expand Down

0 comments on commit d7a9968

Please sign in to comment.