Chore: Move getUserRoles to service and add cache (#22345)
parent
4a28c1282b
commit
f21aceef45
@ -1,33 +1,13 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import _ from 'underscore'; |
||||
|
||||
import { Roles, Users } from '../../../models'; |
||||
import { Authorization } from '../../../../server/sdk'; |
||||
|
||||
Meteor.methods({ |
||||
getUserRoles() { |
||||
async getUserRoles() { |
||||
if (!Meteor.userId()) { |
||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getUserRoles' }); |
||||
} |
||||
|
||||
const options = { |
||||
sort: { |
||||
username: 1, |
||||
}, |
||||
fields: { |
||||
username: 1, |
||||
roles: 1, |
||||
}, |
||||
}; |
||||
|
||||
const roles = Roles.find({ scope: 'Users', description: { $exists: 1, $ne: '' } }).fetch(); |
||||
const roleIds = _.pluck(roles, '_id'); |
||||
|
||||
// Security issue: we should not send all user's roles to all clients, only the 'public' roles
|
||||
// We must remove all roles that are not part of the query from the returned users
|
||||
const users = Users.findUsersInRoles(roleIds, null, options).fetch(); |
||||
for (const user of users) { |
||||
user.roles = _.intersection(user.roles, roleIds); |
||||
} |
||||
return users; |
||||
return Authorization.getUsersFromPublicRoles(); |
||||
}, |
||||
}); |
||||
|
||||
Loading…
Reference in new issue