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

Anonymous use #5986

Merged
merged 24 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6da69c4
Init work with anonymous use
rodrigok Feb 10, 2017
94f5806
Show username window when necessary
rodrigok Feb 11, 2017
9427501
Code improvement
rodrigok Feb 11, 2017
66ec972
Use setting instead of permission
rodrigok Feb 11, 2017
29c16f8
Back old code
rodrigok Feb 11, 2017
0cf06d5
Back old code
rodrigok Feb 11, 2017
c5bad07
Show account information
rodrigok Feb 11, 2017
2d534a2
Make sportlight work for anonymous
rodrigok Feb 11, 2017
70198c9
Add button to go to login when anonymous
rodrigok Feb 11, 2017
a4d71ef
Reload current room after user registration
rodrigok Feb 13, 2017
fe807cd
List default channels for anonymous users
rodrigok Feb 14, 2017
28a8c8b
Rename channels-anonymous
rodrigok Feb 14, 2017
1455f88
Code improvement
rodrigok Feb 14, 2017
97162d8
Reload current room on login
rodrigok Feb 14, 2017
fd5e2bb
Do not show user information for anonymous users
rodrigok Feb 14, 2017
c7cf9ef
Improve spotlight search for anonymous users
rodrigok Feb 14, 2017
b0b804e
Merge remote-tracking branch 'origin/develop' into improvements/anony…
rodrigok Feb 14, 2017
3b389c5
Merge remote-tracking branch 'origin/develop' into improvements/anony…
rodrigok Feb 17, 2017
3d4aaa1
Merge remote-tracking branch 'origin/develop' into improvements/anony…
rodrigok Feb 17, 2017
cd30525
Clear session var `forceLogin` on login/register
rodrigok Feb 17, 2017
5b6c0c2
Merge remote-tracking branch 'origin/develop' into improvements/anony…
rodrigok Apr 20, 2017
59680c3
Remove lesshat references
rodrigok Apr 20, 2017
5fe89ae
Update test lib and stop on first fail
rodrigok Apr 20, 2017
e864b18
Fix tests
rodrigok Apr 20, 2017
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
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 @@ -1184,6 +1185,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
1 change: 1 addition & 0 deletions packages/rocketchat-lib/server/startup/settings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ RocketChat.settings.add('uniqueID', process.env.DEPLOYMENT_ID or Random.id(), {
# When you define a setting and want to add a description, you don't need to automatically define the i18nDescription
# 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', ->
@add 'Accounts_AllowAnonymousAccess', false, { type: 'boolean', public: true}
@add 'Accounts_AllowDeleteOwnAccount', false, { type: 'boolean', public: true, enableQuery: { _id: 'Accounts_AllowUserProfileChange', value: true } }
@add 'Accounts_AllowUserProfileChange', true, { type: 'boolean', public: true }
@add 'Accounts_AllowUserAvatarChange', true, { 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 @@ -29,7 +29,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 @@ -2559,7 +2559,7 @@ label.required::after {
margin: 60px 20px 0 0;
overflow: hidden;
width: 100%;
.calc(height, ~'100% - 120px');
.calc(height, ~'100% - 130px');

.message-cog-container {
.message-action {
Expand Down
4 changes: 4 additions & 0 deletions packages/rocketchat-ui-flextab/flex-tab/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
4 changes: 2 additions & 2 deletions packages/rocketchat-ui-master/master/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Template.main.helpers
return RocketChat.settings.get 'Site_Name'

logged: ->
if Meteor.userId()?
if Meteor.userId()? || (RocketChat.settings.get('Accounts_AllowAnonymousAccess') is true && Session.get('forceLogin') isnt true)
$('html').addClass("noscroll").removeClass("scroll")
return true
else
Expand All @@ -102,7 +102,7 @@ Template.main.helpers
return ready

hasUsername: ->
return Meteor.userId()? and Meteor.user().username?
return (Meteor.userId()? and Meteor.user().username?) || (!Meteor.userId()? && RocketChat.settings.get('Accounts_AllowAnonymousAccess') is true)

requirePasswordChange: ->
return Meteor.user()?.requirePasswordChange is true
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-message/message/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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/message/messageBox.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -116,6 +116,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


Template.messageBox.events
'click .join': (event) ->
Expand All @@ -131,6 +134,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/message/messageBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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>
11 changes: 11 additions & 0 deletions packages/rocketchat-ui-sidenav/client/accountBox.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Template.accountBox.helpers
myUserInfo: ->
if not Meteor.user()? and RocketChat.settings.get('Accounts_AllowAnonymousAccess')
return {
name: t('Anonymous')
status: 'online'
visualStatus: t('online')
username: 'anonymous'
}

visualStatus = "online"
username = Meteor.user()?.username
switch Session.get('user_' + username + '_status')
Expand Down Expand Up @@ -30,6 +38,9 @@ Template.accountBox.events
RocketChat.callbacks.run('userStatusManuallySet', event.currentTarget.dataset.status)

'click .account-box': (event) ->
if not Meteor.userId()? and RocketChat.settings.get('Accounts_AllowAnonymousAccess')
return

AccountBox.toggle()

'click #logout': (event) ->
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.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Template.chatRoomItem.helpers
return RocketChat.roomTypes.getIcon this.t

active: ->
if Session.get('openedRoom') is this.rid
if Session.get('openedRoom') and (Session.get('openedRoom') is this.rid or Session.get('openedRoom') is this._id)
return 'active'

canLeave: ->
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
19 changes: 16 additions & 3 deletions packages/rocketchat-ui-sidenav/client/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ const getFromServer = (cb, type) => {

if (roomsLength) {
for (let i = 0; i < roomsLength; i++) {
const alreadyOnClient = resultsFromClient.find(item => item._id === results.rooms[i]._id);
if (alreadyOnClient) {
continue;
}

resultsFromServer.push({
_id: results.rooms[i]._id,
t: results.rooms[i].t,
Expand Down Expand Up @@ -112,7 +117,7 @@ Template.toolbar.helpers({

const config = {
cls: 'search-results-list',
collection: RocketChat.models.Subscriptions,
collection: Meteor.userId() ? RocketChat.models.Subscriptions : RocketChat.models.Rooms,
template: 'toolbarSearchList',
emptyTemplate: 'toolbarSearchListEmpty',
input: '.toolbar-search__input',
Expand All @@ -135,6 +140,11 @@ Template.toolbar.helpers({
}
};

if (!Meteor.userId()) {
query._id = query.rid;
delete query.rid;
}

if (filterText[0] === '#') {
filterText = filterText.slice(1);
type.users = false;
Expand All @@ -152,7 +162,10 @@ Template.toolbar.helpers({
resultsFromClient = collection.find(query, {limit: 20, sort: {unread: -1, ls: -1}}).fetch();

const resultsFromClientLength = resultsFromClient.length;
usernamesFromClient = [Meteor.user().username];
const user = Meteor.user();
if (user) {
usernamesFromClient = [user];
}

for (let i = 0; i < resultsFromClientLength; i++) {
if (resultsFromClient[i].t === 'd') {
Expand All @@ -163,7 +176,7 @@ Template.toolbar.helpers({
cb(resultsFromClient);

// Use `filter` here to get results for `#` or `@` filter only
if (filter.trim() !== '' && resultsFromClient.length < 20) {
if (resultsFromClient.length < 20) {
getFromServerDebounced(cb, type);
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-ui-sidenav/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Package.onUse(function(api) {
api.addFiles('client/combined.html', 'client');
api.addFiles('client/chatRoomItem.html', 'client');
api.addFiles('client/channels.html', 'client');
api.addFiles('client/channelsAnonymous.html', 'client');
api.addFiles('client/createCombinedFlex.html', 'client');
api.addFiles('client/directMessages.html', 'client');
api.addFiles('client/listChannelsFlex.html', 'client');
Expand All @@ -41,6 +42,7 @@ Package.onUse(function(api) {
api.addFiles('client/combined.coffee', 'client');
api.addFiles('client/chatRoomItem.coffee', 'client');
api.addFiles('client/channels.coffee', 'client');
api.addFiles('client/channelsAnonymous.js', 'client');
api.addFiles('client/createCombinedFlex.coffee', 'client');
api.addFiles('client/directMessages.coffee', 'client');
api.addFiles('client/listChannelsFlex.coffee', 'client');
Expand Down
Loading