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

[FIX] respect Accounts_Registration_Users_Default_Roles setting #24173

Merged
merged 14 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
34 changes: 11 additions & 23 deletions app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { escapeRegExp, escapeHTML } from '@rocket.chat/string-helpers';
import * as Mailer from '../../../mailer/server/api';
import { settings } from '../../../settings/server';
import { callbacks } from '../../../../lib/callbacks';
import { Users, Settings } from '../../../models/server';
import { Settings, Users } from '../../../models/server';
import { Roles, Users as UsersRaw } from '../../../models/server/raw';
import { addUserRoles } from '../../../authorization/server';
import { getAvatarSuggestionForUser } from '../../../lib/server/functions/getAvatarSuggestionForUser';
Expand Down Expand Up @@ -213,8 +213,6 @@ Accounts.onCreateUser(function (options, user = {}) {
});

Accounts.insertUserDoc = _.wrap(Accounts.insertUserDoc, function (insertUserDoc, options, user) {
const noRoles = !user?.hasOwnProperty('globalRoles');

const globalRoles = [];

if (Match.test(user.globalRoles, [String]) && user.globalRoles.length > 0) {
Expand Down Expand Up @@ -278,27 +276,17 @@ Accounts.insertUserDoc = _.wrap(Accounts.insertUserDoc, function (insertUserDoc,
}
}

if (noRoles || roles.length === 0) {
const hasAdmin = Users.findOne(
{
roles: 'admin',
type: 'user',
},
{
fields: {
_id: 1,
},
},
);

if (hasAdmin) {
roles.push('user');
} else {
roles.push('admin');
if (settings.get('Show_Setup_Wizard') === 'pending') {
Settings.updateValueById('Show_Setup_Wizard', 'in_progress');
}
/**
* if settings shows setup wizard to be pending
* and existing role list doesn't include admin
* create this user admin.
* count this as the completion of setup wizard step 1.
*/
if (settings.get('Show_Setup_Wizard') === 'pending') {
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
if (!roles.includes('admin')) {
roles.push('admin');
}
Settings.updateValueById('Show_Setup_Wizard', 'in_progress');
}

addUserRoles(_id, roles);
Expand Down
6 changes: 6 additions & 0 deletions app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,12 @@ export class Users extends Base {
return this.findOne(query, options);
}

findOneByRolesAndType(roles, type, options) {
const query = { roles, type };

return this.findOne(query, options);
}

// FIND
findByIds(users, options) {
const query = { _id: { $in: users } };
Expand Down