# Conflicts: # server/startup/migrations/v084.jspull/5366/head
commit
8249b96d58
@ -0,0 +1,44 @@ |
||||
RocketChat.API.v1.addRoute('statistics', { authRequired: true }, { |
||||
get() { |
||||
let refresh = false; |
||||
if (typeof this.queryParams.refresh !== 'undefined' && this.queryParams.refresh === 'true') { |
||||
refresh = true; |
||||
} |
||||
|
||||
let stats; |
||||
Meteor.runAsUser(this.userId, () => { |
||||
stats = Meteor.call('getStatistics', refresh); |
||||
}); |
||||
|
||||
return RocketChat.API.v1.success({ |
||||
statistics: stats |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
RocketChat.API.v1.addRoute('statistics.list', { authRequired: true }, { |
||||
get() { |
||||
if (!RocketChat.authz.hasPermission(this.userId, 'view-statistics')) { |
||||
return RocketChat.API.v1.unauthorized(); |
||||
} |
||||
|
||||
const { offset, count } = this.getPaginationItems(); |
||||
const { sort, fields, query } = this.parseJsonQuery(); |
||||
|
||||
const ourQuery = Object.assign({}, query); |
||||
|
||||
const statistics = RocketChat.models.Statistics.find(ourQuery, { |
||||
sort: sort ? sort : { name: 1 }, |
||||
skip: offset, |
||||
limit: count, |
||||
fields: Object.assign({}, fields, RocketChat.API.v1.defaultFieldsToExclude) |
||||
}).fetch(); |
||||
|
||||
return RocketChat.API.v1.success({ |
||||
statistics, |
||||
count: statistics.length, |
||||
offset, |
||||
total: RocketChat.models.Statistics.find(ourQuery).count() |
||||
}); |
||||
} |
||||
}); |
@ -1,36 +1,8 @@ |
||||
Meteor.methods({ |
||||
addUserToRoom(data) { |
||||
if (!Meteor.userId()) { |
||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'addUserToRoom' }); |
||||
} |
||||
|
||||
if (!Match.test(data && data.rid, String)) { |
||||
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'addUserToRoom' }); |
||||
} |
||||
|
||||
if (!Match.test(data && data.username, String)) { |
||||
throw new Meteor.Error('error-invalid-username', 'Invalid username', { method: 'addUserToRoom' }); |
||||
} |
||||
|
||||
const room = RocketChat.models.Rooms.findOneById(data.rid); |
||||
|
||||
if (room.usernames.indexOf(Meteor.user().username) === -1) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'addUserToRoom' }); |
||||
} |
||||
|
||||
const fromId = Meteor.userId(); |
||||
if (!RocketChat.authz.hasPermission(fromId, 'add-user-to-room', room._id)) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'addUserToRoom' }); |
||||
} |
||||
|
||||
if (room.t === 'd') { |
||||
throw new Meteor.Error('error-cant-invite-for-direct-room', 'Can\'t invite user to direct rooms', { method: 'addUserToRoom' }); |
||||
} |
||||
|
||||
|
||||
const newUser = RocketChat.models.Users.findOneByUsername(data.username); |
||||
RocketChat.addUserToRoom(data.rid, newUser, Meteor.user()); |
||||
|
||||
return true; |
||||
addUserToRoom: function(data) { |
||||
return Meteor.call('addUsersToRoom', { |
||||
rid: data.rid, |
||||
users: [ data.username ] |
||||
}); |
||||
} |
||||
}); |
||||
|
@ -1,40 +1,67 @@ |
||||
Meteor.methods({ |
||||
addUsersToRoom: function(data) { |
||||
var fromId, room; |
||||
addUsersToRoom(data = {}) { |
||||
// Validate user and room
|
||||
if (!Meteor.userId()) { |
||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { |
||||
method: 'addUserToRoom' |
||||
method: 'addUsersToRoom' |
||||
}); |
||||
} |
||||
if (!Match.test(data != null ? data.rid : void 0, String)) { |
||||
|
||||
if (!Match.test(data.rid, String)) { |
||||
throw new Meteor.Error('error-invalid-room', 'Invalid room', { |
||||
method: 'addUserToRoom' |
||||
method: 'addUsersToRoom' |
||||
}); |
||||
} |
||||
|
||||
room = RocketChat.models.Rooms.findOneById(data.rid); |
||||
if (room.usernames.indexOf(Meteor.user().username) === -1) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { |
||||
method: 'addUserToRoom' |
||||
// Get user and room details
|
||||
const room = RocketChat.models.Rooms.findOneById(data.rid); |
||||
const userId = Meteor.userId(); |
||||
const user = Meteor.user(); |
||||
const userInRoom = Array.isArray(room.usernames) && room.usernames.includes(user.username); |
||||
|
||||
// Can't add to direct room ever
|
||||
if (room.t === 'd') { |
||||
throw new Meteor.Error('error-cant-invite-for-direct-room', 'Can\'t invite user to direct rooms', { |
||||
method: 'addUsersToRoom' |
||||
}); |
||||
} |
||||
|
||||
fromId = Meteor.userId(); |
||||
if (!RocketChat.authz.hasPermission(fromId, 'add-user-to-room', room._id)) { |
||||
// Can add to any room you're in, with permission, otherwise need specific room type permission
|
||||
let canAddUser = false; |
||||
if (userInRoom && RocketChat.authz.hasPermission(userId, 'add-user-to-joined-room', room._id)) { |
||||
canAddUser = true; |
||||
} else if (room.t === 'c' && RocketChat.authz.hasPermission(userId, 'add-user-to-any-c-room')) { |
||||
canAddUser = true; |
||||
} else if (room.t === 'p' && RocketChat.authz.hasPermission(userId, 'add-user-to-any-p-room')) { |
||||
canAddUser = true; |
||||
} |
||||
|
||||
// Adding wasn't allowed
|
||||
if (!canAddUser) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { |
||||
method: 'addUserToRoom' |
||||
method: 'addUsersToRoom' |
||||
}); |
||||
} |
||||
if (room.t === 'd') { |
||||
throw new Meteor.Error('error-cant-invite-for-direct-room', 'Can\'t invite user to direct rooms', { |
||||
method: 'addUserToRoom' |
||||
|
||||
// Missing the users to be added
|
||||
if (!Array.isArray(data.users)) { |
||||
throw new Meteor.Error('error-invalid-arguments', 'Invalid arguments', { |
||||
method: 'addUsersToRoom' |
||||
}); |
||||
} |
||||
data.users = Array.isArray(data.users) ? data.users : []; |
||||
data.users.forEach(function(username) { |
||||
let newUser = RocketChat.models.Users.findOneByUsername(username); |
||||
RocketChat.addUserToRoom(data.rid, newUser, Meteor.user()); |
||||
|
||||
// Validate each user, then add to room
|
||||
data.users.forEach((username) => { |
||||
const newUser = RocketChat.models.Users.findOneByUsername(username); |
||||
if (!newUser) { |
||||
throw new Meteor.Error('error-invalid-username', 'Invalid username', { |
||||
method: 'addUsersToRoom' |
||||
}); |
||||
} |
||||
|
||||
RocketChat.addUserToRoom(data.rid, newUser, user); |
||||
}); |
||||
|
||||
return true; |
||||
} |
||||
}); |
||||
}); |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue