From b4d8f8e279eb2e1d7a0e2dce96fdfe6320ca842b Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 15 Dec 2015 16:57:01 -0200 Subject: [PATCH] Allow pass an array of roles to user in Acctouns.createUser --- server/lib/accounts.coffee | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/server/lib/accounts.coffee b/server/lib/accounts.coffee index b9531f2ea1a..765e55ea8db 100644 --- a/server/lib/accounts.coffee +++ b/server/lib/accounts.coffee @@ -69,16 +69,25 @@ Accounts.onCreateUser (options, user) -> return user # Wrap insertUserDoc to allow executing code after Accounts.insertUserDoc is run -Accounts.insertUserDoc = _.wrap Accounts.insertUserDoc, (insertUserDoc) -> - options = arguments[1] - user = arguments[2] +Accounts.insertUserDoc = _.wrap Accounts.insertUserDoc, (insertUserDoc, options, user) -> + roles = [] + if Match.test(user.globalRoles, [String]) and user.globalRoles.length > 0 + roles = roles.concat user.globalRoles + + delete user.globalRoles + _id = insertUserDoc.call(Accounts, options, user) - # when inserting first user give them admin privileges otherwise make a regular user - firstUser = RocketChat.models.Users.findOne({ _id: { $ne: 'rocket.cat' }}, { sort: { createdAt: 1 }}) - roleName = if firstUser?._id is _id then 'admin' else 'user' + if roles.length is 0 + # when inserting first user give them admin privileges otherwise make a regular user + firstUser = RocketChat.models.Users.findOne({ _id: { $ne: 'rocket.cat' }}, { sort: { createdAt: 1 }}) + if firstUser?._id is _id + roles.push 'admin' + else + roles.push 'user' + + RocketChat.authz.addUsersToRoles(_id, roles) - RocketChat.authz.addUsersToRoles(_id, roleName) RocketChat.callbacks.run 'afterCreateUser', options, user return _id