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 1 commit
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
13 changes: 8 additions & 5 deletions packages/rocketchat-authorization/client/hasPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function atLeastOne(permissions = [], scope) {
const roles = (permission && permission.roles) || [];

return roles.some((roleName) => {
if (roleName === 'anonymous' && !Meteor.userId()) {
return true;
}

const role = RocketChat.models.Roles.findOne(roleName);
const roleScope = role && role.scope;
const model = RocketChat.models[roleScope];
Expand All @@ -21,6 +25,10 @@ function all(permissions = [], scope) {
const roles = (permission && permission.roles) || [];

return roles.some((roleName) => {
if (roleName === 'anonymous' && !Meteor.userId()) {
return true;
}

const role = RocketChat.models.Roles.findOne(roleName);
const roleScope = role && role.scope;
const model = RocketChat.models[roleScope];
Expand All @@ -31,11 +39,6 @@ function all(permissions = [], scope) {
}

function hasPermission(permissions, scope, strategy) {
const userId = Meteor.userId();
if (!userId) {
return false;
}

if (!RocketChat.authz.cachedCollection.ready.get()) {
return false;
}
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,14 +1,14 @@
/* globals RocketChat */
RocketChat.authz.roomAccessValidators = [
function(room, user) {
function(room, user = {}) {
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user._id);
if (subscription) {
return subscription._room;
}
},
function(room, user) {
function(room, user = {}) {
if (room.t === 'c') {
return RocketChat.authz.hasPermission(user._id, 'view-c-room');
return RocketChat.authz.hasPermission(user._id, 'view-c-room') || RocketChat.authz.hasPermission(user._id, 'preview-c-room');
}
}
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
function atLeastOne(userId, permissions = [], scope) {
return permissions.some((permissionId) => {
const permission = RocketChat.models.Permissions.findOne(permissionId);
if (!userId && permission.roles.includes('anonymous')) {
return true;
}
return RocketChat.models.Roles.isUserInRoles(userId, permission.roles, scope);
});
}

function all(userId, permissions = [], scope) {
return permissions.every((permissionId) => {
const permission = RocketChat.models.Permissions.findOne(permissionId);
if (!userId && permission.roles.includes('anonymous')) {
return true;
}
return RocketChat.models.Roles.isUserInRoles(userId, permission.roles, scope);
});
}

function hasPermission(userId, permissions, scope, strategy) {
if (!userId) {
return false;
}

permissions = [].concat(permissions);
return strategy(userId, permissions, scope);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/rocketchat-authorization/server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Meteor.startup(function() {
{ _id: 'view-room-administration', roles : ['admin'] },
{ _id: 'view-statistics', roles : ['admin'] },
{ _id: 'view-user-administration', roles : ['admin'] },
{ _id: 'preview-c-room', roles : ['admin', 'user'] }
{ _id: 'preview-c-room', roles : ['admin', 'user', 'anonymous'] }
];

for (const permission of permissions) {
Expand All @@ -73,7 +73,8 @@ Meteor.startup(function() {
{ name: 'owner', scope: 'Subscriptions', description: 'Owner' },
{ name: 'user', scope: 'Users', description: '' },
{ name: 'bot', scope: 'Users', description: '' },
{ name: 'guest', scope: 'Users', description: '' }
{ name: 'guest', scope: 'Users', description: '' },
{ name: 'anonymous', scope: 'Users', description: 'Anonymous' }
];

for (const role of defaultRoles) {
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
1 change: 1 addition & 0 deletions packages/rocketchat-lib/client/defaultTabBars.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RocketChat.TabBar.addButton({
RocketChat.TabBar.addButton({
groups: ['channel', 'group'],
id: 'members-list',
anonymous: true,
i18nTitle: 'Members_List',
icon: 'icon-users',
template: 'membersList',
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
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/client/lib/openRoom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ currentTracker = undefined
return

user = Meteor.user()
unless user?.username
if not user?.username and !RocketChat.authz.hasAllPermission('preview-c-room')
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.authz.hasPermission(undefined, 'preview-c-room')) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getRoomRoles' });
}

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.authz.hasAllPermission('preview-c-room')
$('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?) || RocketChat.authz.hasAllPermission('preview-c-room')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will prevent the username screen for normal users too


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
4 changes: 1 addition & 3 deletions packages/rocketchat-ui/lib/RoomManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ Tracker.autorun ->
do (typeName, record) ->

user = Meteor.user()
unless user?.username
return

if record.ready is true
return
Expand Down Expand Up @@ -158,7 +156,7 @@ Tracker.autorun ->
if openedRooms[typeName].ready
closeOlderRooms()

if CachedChatSubscription.ready.get() is true && Meteor.userId()
if CachedChatSubscription.ready.get() is true

if openedRooms[typeName].active isnt true
openedRooms[typeName].active = true
Expand Down
6 changes: 6 additions & 0 deletions packages/rocketchat-ui/lib/collections.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ RocketChat.models.Users = _.extend {}, RocketChat.models.Users, Meteor.users
RocketChat.models.Subscriptions = _.extend {}, RocketChat.models.Subscriptions, @ChatSubscription
RocketChat.models.Rooms = _.extend {}, RocketChat.models.Rooms, @ChatRoom
RocketChat.models.Messages = _.extend {}, RocketChat.models.Messages, @ChatMessage

Meteor.startup ->
Tracker.autorun ->
if !Meteor.userId() and RocketChat.authz.hasAllPermission('preview-c-room')
CachedChatRoom.ready.set(true)
CachedChatSubscription.ready.set(true)
28 changes: 20 additions & 8 deletions server/methods/canAccessRoom.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
Meteor.methods({
canAccessRoom(rid, userId) {
check(rid, String);
check(userId, String);
check(userId, Match.Maybe(String));

const user = RocketChat.models.Users.findOneById(userId, {
fields: {
username: 1
}
});
let user;

if (!user || !user.username) {
if (!userId && !RocketChat.authz.hasPermission(undefined, 'preview-c-room')) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'canAccessRoom'
});
}

if (userId) {
user = RocketChat.models.Users.findOneById(userId, {
fields: {
username: 1
}
});

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

if (!rid) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'canAccessRoom'
Expand All @@ -24,7 +34,9 @@ Meteor.methods({
const room = RocketChat.models.Rooms.findOneById(rid);
if (room) {
if (RocketChat.authz.canAccessRoom.call(this, room, user)) {
room.username = user.username;
if (user) {
room.username = user.username;
}
return room;
}

Expand Down
2 changes: 1 addition & 1 deletion server/methods/loadHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Meteor.methods({
loadHistory(rid, end, limit = 20, ls) {
check(rid, String);

if (!Meteor.userId()) {
if (!Meteor.userId() && !RocketChat.authz.hasAllPermission(undefined, 'preview-c-room')) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'loadHistory'
});
Expand Down
2 changes: 1 addition & 1 deletion server/publications/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Meteor.methods({
},

getRoomByTypeAndName(type, name) {
if (!Meteor.userId()) {
if (!Meteor.userId() && !RocketChat.authz.hasAllPermission(undefined, 'preview-c-room')) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getRoomByTypeAndName' });
}

Expand Down