Skip to content

Commit

Permalink
[FIX] respect Accounts_Registration_Users_Default_Roles setting (#2…
Browse files Browse the repository at this point in the history
…4173)

* Fix role handling if setup wizard pending and no admin found

Signed-off-by: Debdut Chakraborty <debdut.chakraborty@rocket.chat>

* [FIX] Create `findOneByRolesAndType` function (#24308)

* Create findOneByRolesAndType function and stop adding user role by default

* Remove logs

* Remove default user role in userHandler.js

* Update app/authentication/server/startup/index.js

Co-authored-by: Debdut Chakraborty <debdut.chakraborty@rocket.chat>

* fix lint issues

* Check if the server has an admin user before assigning the admin role on server startup

Co-authored-by: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com>
Co-authored-by: matheusbsilva137 <matheus_barbosa137@hotmail.com>
  • Loading branch information
3 people committed Feb 18, 2022
1 parent ee7dd16 commit a6a04cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
36 changes: 13 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,26 +276,18 @@ 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 no admin's been found,
* and existing role list doesn't include admin
* create this user admin.
* count this as the completion of setup wizard step 1.
*/
const hasAdmin = Users.findOneByRolesAndType('admin', 'user', { fields: { _id: 1 } });
if (!roles.includes('admin') && !hasAdmin) {
roles.push('admin');
if (settings.get('Show_Setup_Wizard') === 'pending') {
Settings.updateValueById('Show_Setup_Wizard', 'in_progress');
}
}

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

0 comments on commit a6a04cb

Please sign in to comment.