Skip to content

Commit

Permalink
Merge pull request #5986 from RocketChat/improvements/anonymous
Browse files Browse the repository at this point in the history
Anonymous use
  • Loading branch information
rodrigok authored Apr 20, 2017
2 parents 193553c + e864b18 commit ddc6822
Show file tree
Hide file tree
Showing 37 changed files with 236 additions and 65 deletions.
2 changes: 1 addition & 1 deletion client/routes/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ FlowRouter.route('/', {
Tracker.autorun(function(c) {
if (FlowRouter.subsReady() === true) {
Meteor.defer(function() {
if (Meteor.user().defaultRoom) {
if (Meteor.user() && Meteor.user().defaultRoom) {
const room = Meteor.user().defaultRoom.split('/');
FlowRouter.go(room[0], { name: room[1] }, FlowRouter.current().queryParams);
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"email": "support@rocket.chat"
},
"devDependencies": {
"chimp": "^0.47.2",
"chimp": "^0.48.0",
"eslint": "^3.19.0",
"stylelint": "^7.10.1",
"supertest": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
RocketChat.authz.cachedCollection = new RocketChat.CachedCollection({
name: 'permissions',
eventType: 'onLogged'
eventType: 'onLogged',
userRelated: false
});
RocketChat.authz.cachedCollection.init();

this.ChatPermissions = RocketChat.authz.cachedCollection.collection;
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/* globals RocketChat */
RocketChat.authz.roomAccessValidators = [
function(room, user) {
function(room, user = {}) {
if (room.t === 'c') {
if (!user._id && RocketChat.settings.get('Accounts_AllowAnonymousAccess') === true) {
return true;
}

return RocketChat.authz.hasPermission(user._id, 'view-c-room');
}
},
function(room, user = {}) {
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user._id);
if (subscription) {
return subscription._room;
}
},
function(room, user) {
if (room.t === 'c') {
return RocketChat.authz.hasPermission(user._id, 'view-c-room');
}
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Meteor.startup(() => {
RocketChat.TabBar.addButton({
groups: ['channel', 'group', 'direct'],
id: 'channel-settings',
anonymous: true,
i18nTitle: 'Room_Info',
icon: 'icon-info-circled',
template: 'channelSettings',
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Accessing_permissions": "Accessing permissions",
"Account_SID": "Account SID",
"Accounts": "Accounts",
"Accounts_AllowAnonymousAccess": "Allow anonymous access",
"Accounts_AllowDeleteOwnAccount": "Allow users to delete own account",
"Accounts_AllowedDomainsList": "Allowed Domains List",
"Accounts_AllowedDomainsList_Description": "Comma-separated list of allowed domains",
Expand Down Expand Up @@ -1227,6 +1228,7 @@
"Register": "Register a new account",
"Registration": "Registration",
"Registration_Succeeded": "Registration Succeeded",
"Register_or_login_to_send_messages": "Register or login to send messages",
"Registration_via_Admin": "Registration via Admin",
"Regular_Expressions": "Regular Expressions",
"Release": "Release",
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/client/lib/cachedCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class CachedCollection {
}

localforage.getItem(this.name, (error, data) => {
if (data && (data.version < this.version || data.token !== this.getToken())) {
if (data && (data.version < this.version || data.token !== this.getToken() || this.getToken() === undefined)) {
this.clearCache();
callback(false);
return;
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-lib/client/lib/openRoom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ currentTracker = undefined

Meteor.defer ->
currentTracker = Tracker.autorun (c) ->
if RoomManager.open(type + name).ready() isnt true
BlazeLayout.render 'main', { modal: RocketChat.Layout.isEmbedded(), center: 'loading' }
user = Meteor.user()
if (user? and not user.username?) or (not user? and RocketChat.settings.get('Accounts_AllowAnonymousAccess') is false)
BlazeLayout.render 'main'
return

user = Meteor.user()
unless user?.username
if RoomManager.open(type + name).ready() isnt true
BlazeLayout.render 'main', { modal: RocketChat.Layout.isEmbedded(), center: 'loading' }
return

currentTracker = undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/server/methods/getRoomRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Meteor.methods({

check(rid, String);

if (!Meteor.userId()) {
if (!Meteor.userId() && RocketChat.settings.get('Accounts_AllowAnonymousAccess') === false) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getRoomRoles' });
}

Expand Down
16 changes: 16 additions & 0 deletions packages/rocketchat-lib/server/models/Rooms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ class ModelRooms extends RocketChat.models._Base

return @find query, options

findByNameAndType: (name, type, options) ->
query =
t: type
name: name

return @find query, options

findByNameAndTypeNotDefault: (name, type, options) ->
query =
t: type
name: name
default:
$ne: true

return @find query, options

findByNameAndTypeNotContainingUsername: (name, type, username, options) ->
query =
t: type
Expand Down
4 changes: 4 additions & 0 deletions packages/rocketchat-lib/server/startup/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ RocketChat.settings.add('uniqueID', process.env.DEPLOYMENT_ID || Random.id(), {
// if you add a node to the i18n.json with the same setting name but with `_Description` it will automatically work.

RocketChat.settings.addGroup('Accounts', function() {
this.add('Accounts_AllowAnonymousAccess', false, {
type: 'boolean',
public: true
});
this.add('Accounts_AllowDeleteOwnAccount', false, {
type: 'boolean',
'public': true,
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/startup/defaultRoomTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RocketChat.roomTypes.add('c', 10, {
},

condition() {
return RocketChat.authz.hasAtLeastOnePermission(['view-c-room', 'view-joined-room']);
return RocketChat.authz.hasAtLeastOnePermission(['view-c-room', 'view-joined-room']) || RocketChat.settings.get('Accounts_AllowAnonymousAccess') === true;
},

showJoinLink(roomId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-theme/client/imports/base.less
Original file line number Diff line number Diff line change
Expand Up @@ -2685,7 +2685,7 @@ label.required::after {
margin: 60px 20px 0 0;
overflow: hidden;
width: 100%;
height: calc(~'100% - 120px');
height: calc(~'100% - 130px');

.message-cog-container {
.message-action {
Expand Down
4 changes: 4 additions & 0 deletions packages/rocketchat-ui-flextab/client/flexTabBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Template.flexTabBar.helpers({
},

visible() {
if (!Meteor.userId() && !this.anonymous) {
return 'hidden';
}

if (this.groups.indexOf(Template.instance().tabBar.currentGroup()) === -1) {
return 'hidden';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-login/client/login/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<form id="login-card" class="content-background-color color-primary-font-color" method='/' novalidate>
{{#if state 'wait-activation'}}
<header>
<h2>{{{_ "Registration_Succeeded"}}}</h2>
<h2 data-i18n="Registration_Succeeded">{{{_ "Registration_Succeeded"}}}</h2>
<p>{{{_ "Wait_activation_warning"}}}</p>
<p>{{{_ "Please_wait_activation"}}}</p>
</header>
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-ui-login/client/login/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Template.loginForm.events({
return instance.state.set('login');
} else if (error && error.error === 'error-user-is-not-activated') {
return instance.state.set('wait-activation');
} else {
Session.set('forceLogin', false);
}
});
});
Expand All @@ -130,6 +132,7 @@ Template.loginForm.events({
}
return;
}
Session.set('forceLogin', false);
if (user && user.language) {
localStorage.setItem('userLanguage', user.language);
return setLanguage(Meteor.user().language);
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-ui-master/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Template.main.helpers({
return RocketChat.settings.get('Site_Name');
},
logged() {
if (Meteor.userId() != null) {
if (Meteor.userId() != null || (RocketChat.settings.get('Accounts_AllowAnonymousAccess') === true && Session.get('forceLogin') !== true)) {
$('html').addClass('noscroll').removeClass('scroll');
return true;
} else {
Expand All @@ -134,7 +134,7 @@ Template.main.helpers({
return ready;
},
hasUsername() {
return (Meteor.userId() != null) && (Meteor.user().username != null);
return (Meteor.userId() != null && Meteor.user().username != null) || (Meteor.userId() == null && RocketChat.settings.get('Accounts_AllowAnonymousAccess') === true);
},
requirePasswordChange() {
const user = Meteor.user();
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-message/client/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Template.message.helpers

reactions: ->
msgReactions = []
userUsername = Meteor.user().username
userUsername = Meteor.user()?.username

for emoji, reaction of @reactions
total = reaction.usernames.length
Expand Down
10 changes: 9 additions & 1 deletion packages/rocketchat-ui-message/client/messageBox.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Template.messageBox.helpers
showFormattingTips: ->
return RocketChat.settings.get('Message_ShowFormattingTips') and (RocketChat.Markdown or RocketChat.MarkdownCode or katexSyntax())
canJoin: ->
return RocketChat.roomTypes.verifyShowJoinLink @_id
return Meteor.userId()? and RocketChat.roomTypes.verifyShowJoinLink @_id
joinCodeRequired: ->
return Session.get('roomData' + this._id)?.joinCodeRequired
subscribed: ->
Expand Down Expand Up @@ -124,6 +124,9 @@ Template.messageBox.helpers
showSandstorm: ->
return Meteor.settings.public.sandstorm && !Meteor.isCordova

isAnonymous: ->
return not Meteor.userId()? and RocketChat.settings.get('Accounts_AllowAnonymousAccess') is true

firefoxPasteUpload = (fn) ->
user = navigator.userAgent.match(/Firefox\/(\d+)\.\d/)
if !user or user[1] > 49
Expand Down Expand Up @@ -178,6 +181,11 @@ Template.messageBox.events
RoomHistoryManager.getRoom(@_id).loaded = undefined
RoomManager.computation.invalidate()

'click .register': (event) ->
event.stopPropagation()
event.preventDefault()
Session.set('forceLogin', true)

'focus .input-message': (event, instance) ->
KonchatNotification.removeRoomNotification @_id
chatMessages[@_id].input = instance.find('.input-message')
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-ui-message/client/messageBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
<button class="button join"><span><i class="icon-login"></i> {{_ "join"}}</span></button>
</div>
{{/if}}
{{#if isAnonymous}}
<div>
<button class="button register"><span><i class="icon-login"></i> {{_ "Register_or_login_to_send_messages"}}</span></button>
</div>
{{/if}}
{{/with}}
{{/if}}
</template>
13 changes: 13 additions & 0 deletions packages/rocketchat-ui-sidenav/client/accountBox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
Template.accountBox.helpers({
myUserInfo() {
if (Meteor.user() == null && RocketChat.settings.get('Accounts_AllowAnonymousAccess')) {
return {
name: t('Anonymous'),
status: 'online',
visualStatus: t('online'),
username: 'anonymous'
};
}

let visualStatus = 'online';
const user = Meteor.user() || {};
const { name, username } = user;
Expand Down Expand Up @@ -41,6 +50,10 @@ Template.accountBox.events({
},

'click .account-box'() {
if (Meteor.userId() == null && RocketChat.settings.get('Accounts_AllowAnonymousAccess')) {
return;
}

return AccountBox.toggle();
},

Expand Down
13 changes: 13 additions & 0 deletions packages/rocketchat-ui-sidenav/client/channelsAnonymous.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template name="channelsAnonymous">
<h3 class="add-room background-transparent-darker-hover {{isActive}}">
{{_ "Channels"}} <span class="room-count-small">({{rooms.count}})</span>
</h3>
<ul>
{{#each rooms}}
{{> chatRoomItem }}
{{else}}
<p class="empty">{{_ "No_channels_yet" }}</p>
{{/each}}
</ul>
<button class="more more-channels background-transparent-darker-hover">{{_ "More_channels"}}...</button>
</template>
23 changes: 23 additions & 0 deletions packages/rocketchat-ui-sidenav/client/channelsAnonymous.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Template.channelsAnonymous.helpers({
isActive() {
const currentRoom = RocketChat.models.Rooms.findOne({ _id: Session.get('openedRoom') });
if (currentRoom) {
return 'active';
}
},

rooms() {
const query = {
t: 'c'
};

return RocketChat.models.Rooms.find(query, { sort: { name: 1 } });
}
});

Template.channelsAnonymous.events({
'click .more-channels'() {
SideNav.setFlex('listChannelsFlex');
SideNav.openFlex();
}
});
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-sidenav/client/chatRoomItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Template.chatRoomItem.helpers({
},

active() {
if (Session.get('openedRoom') === this.rid) {
if (Session.get('openedRoom') && Session.get('openedRoom') === this.rid || Session.get('openedRoom') === this._id) {
return 'active';
}
},
Expand Down
8 changes: 7 additions & 1 deletion packages/rocketchat-ui-sidenav/client/sideNav.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<header class="header">
{{> accountBox }}
</header>
{{> toolbar}}
{{#if currentUser}}
{{> toolbar}}
<div class="unread-rooms background-primary-action-color color-primary-action-contrast top-unread-rooms hidden">
{{_ "More_unreads"}} <i class="icon-up-big"></i>
</div>
Expand Down Expand Up @@ -34,6 +34,12 @@ <h3 class="history-div">
</section>
</div>
<span class="arrow bottom"></span>
{{else}}
<div class="rooms-list" aria-label="{{_ "Channels"}}" role="region">
<div class="wrapper">
{{> channelsAnonymous}}
</div>
</div>
{{/if}}
<footer class="footer">
{{{footer}}}
Expand Down
Loading

0 comments on commit ddc6822

Please sign in to comment.