You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
8.2 KiB
260 lines
8.2 KiB
|
6 days ago
|
import { UserStatus, type IUser } from '@rocket.chat/core-typings';
|
||
|
1 year ago
|
import { Settings, Rooms, Users, Roles } from '@rocket.chat/models';
|
||
|
2 weeks ago
|
import { validateEmail } from '@rocket.chat/tools';
|
||
|
3 years ago
|
import colors from 'colors/safe';
|
||
|
2 years ago
|
import { Accounts } from 'meteor/accounts-base';
|
||
|
|
import { Meteor } from 'meteor/meteor';
|
||
|
7 years ago
|
|
||
|
3 weeks ago
|
import { addCallHistoryTestData } from './callHistoryTestData';
|
||
|
2 years ago
|
import { RocketChatFile } from '../../app/file/server';
|
||
|
1 year ago
|
import { FileUpload } from '../../app/file-upload/server';
|
||
|
2 years ago
|
import { addUserToDefaultChannels } from '../../app/lib/server/functions/addUserToDefaultChannels';
|
||
|
3 years ago
|
import { checkUsernameAvailability } from '../../app/lib/server/functions/checkUsernameAvailability';
|
||
|
2 years ago
|
import { notifyOnSettingChangedById } from '../../app/lib/server/lib/notifyListener';
|
||
|
2 years ago
|
import { settings } from '../../app/settings/server';
|
||
|
|
import { addUserRolesAsync } from '../lib/roles/addUserRoles';
|
||
|
4 years ago
|
|
||
|
2 years ago
|
export async function insertAdminUserFromEnv() {
|
||
|
|
if (process.env.ADMIN_PASS) {
|
||
|
1 year ago
|
if ((await Roles.countUsersInRole('admin')) === 0) {
|
||
|
6 days ago
|
const adminUser: Partial<IUser> = {
|
||
|
|
name: process.env.ADMIN_NAME || 'Administrator',
|
||
|
2 years ago
|
username: 'admin',
|
||
|
6 days ago
|
status: UserStatus.OFFLINE,
|
||
|
|
statusDefault: UserStatus.ONLINE,
|
||
|
2 years ago
|
utcOffset: 0,
|
||
|
|
active: true,
|
||
|
6 days ago
|
type: 'user',
|
||
|
2 years ago
|
};
|
||
|
|
|
||
|
|
console.log(colors.green(`Name: ${adminUser.name}`));
|
||
|
|
|
||
|
|
if (process.env.ADMIN_EMAIL) {
|
||
|
|
if (validateEmail(process.env.ADMIN_EMAIL)) {
|
||
|
|
if (!(await Users.findOneByEmailAddress(process.env.ADMIN_EMAIL))) {
|
||
|
|
adminUser.emails = [
|
||
|
|
{
|
||
|
|
address: process.env.ADMIN_EMAIL,
|
||
|
|
verified: process.env.ADMIN_EMAIL_VERIFIED === 'true',
|
||
|
|
},
|
||
|
|
];
|
||
|
|
|
||
|
|
console.log(colors.green(`Email: ${process.env.ADMIN_EMAIL}`));
|
||
|
|
} else {
|
||
|
|
console.log(colors.red('Email provided already exists; Ignoring environment variables ADMIN_EMAIL'));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log(colors.red('Email provided is invalid; Ignoring environment variables ADMIN_EMAIL'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (process.env.ADMIN_USERNAME) {
|
||
|
|
let nameValidation;
|
||
|
|
|
||
|
|
try {
|
||
|
|
nameValidation = new RegExp(`^${settings.get('UTF8_User_Names_Validation')}$`);
|
||
|
|
} catch (error) {
|
||
|
|
nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (nameValidation.test(process.env.ADMIN_USERNAME)) {
|
||
|
|
try {
|
||
|
|
await checkUsernameAvailability(process.env.ADMIN_USERNAME);
|
||
|
|
adminUser.username = process.env.ADMIN_USERNAME;
|
||
|
|
} catch (error) {
|
||
|
|
console.log(
|
||
|
|
colors.red('Username provided already exists or is blocked from usage; Ignoring environment variables ADMIN_USERNAME'),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log(colors.red('Username provided is invalid; Ignoring environment variables ADMIN_USERNAME'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(colors.green(`Username: ${adminUser.username}`));
|
||
|
|
|
||
|
|
const { insertedId: userId } = await Users.create(adminUser);
|
||
|
|
|
||
|
|
await Accounts.setPasswordAsync(userId, process.env.ADMIN_PASS);
|
||
|
|
|
||
|
|
await addUserRolesAsync(userId, ['admin']);
|
||
|
|
} else {
|
||
|
|
console.log(colors.red('Users with admin role already exist; Ignoring environment variables ADMIN_PASS'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
2 years ago
|
Meteor.startup(async () => {
|
||
|
2 years ago
|
const dynamicImport = {
|
||
|
|
'dynamic-import': {
|
||
|
|
useLocationOrigin: true,
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
if (!Meteor.settings) {
|
||
|
|
Meteor.settings = {
|
||
|
|
public: {
|
||
|
|
packages: {
|
||
|
|
'dynamic-import': dynamicImport,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!Meteor.settings.public) {
|
||
|
|
Meteor.settings.public = {
|
||
|
|
packages: {
|
||
|
|
'dynamic-import': dynamicImport,
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!Meteor.settings.public.packages) {
|
||
|
|
Meteor.settings.public.packages = dynamicImport;
|
||
|
|
}
|
||
|
|
|
||
|
|
Meteor.settings.public.packages['dynamic-import'] = dynamicImport['dynamic-import'];
|
||
|
|
|
||
|
4 years ago
|
if (!settings.get('Initial_Channel_Created')) {
|
||
|
3 years ago
|
const exists = await Rooms.findOneById('GENERAL', { projection: { _id: 1 } });
|
||
|
4 years ago
|
if (!exists) {
|
||
|
3 years ago
|
await Rooms.createWithIdTypeAndName('GENERAL', 'c', 'general', {
|
||
|
4 years ago
|
default: true,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
2 years ago
|
(await Settings.updateValueById('Initial_Channel_Created', true)).modifiedCount &&
|
||
|
|
void notifyOnSettingChangedById('Initial_Channel_Created');
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
2 years ago
|
try {
|
||
|
|
if (!(await Users.findOneById('rocket.cat', { projection: { _id: 1 } }))) {
|
||
|
|
await Users.create({
|
||
|
|
_id: 'rocket.cat',
|
||
|
|
name: 'Rocket.Cat',
|
||
|
|
username: 'rocket.cat',
|
||
|
6 days ago
|
status: UserStatus.ONLINE,
|
||
|
|
statusDefault: UserStatus.ONLINE,
|
||
|
2 years ago
|
utcOffset: 0,
|
||
|
|
active: true,
|
||
|
|
type: 'bot',
|
||
|
|
});
|
||
|
|
|
||
|
|
await addUserRolesAsync('rocket.cat', ['bot']);
|
||
|
4 years ago
|
|
||
|
6 days ago
|
const asset = await Assets.getBinaryAsync('avatars/rocketcat.png');
|
||
|
|
if (asset) {
|
||
|
|
const buffer = Buffer.from(asset);
|
||
|
2 years ago
|
|
||
|
6 days ago
|
const rs = RocketChatFile.bufferToStream(buffer);
|
||
|
|
const fileStore = FileUpload.getStore('Avatars');
|
||
|
|
await fileStore.deleteByName('rocket.cat');
|
||
|
2 years ago
|
|
||
|
6 days ago
|
const file = {
|
||
|
|
userId: 'rocket.cat',
|
||
|
|
type: 'image/png',
|
||
|
|
size: buffer.length,
|
||
|
|
};
|
||
|
2 years ago
|
|
||
|
6 days ago
|
const upload = await fileStore.insert(file, rs);
|
||
|
|
await Users.setAvatarData('rocket.cat', 'local', upload.etag);
|
||
|
|
}
|
||
|
2 years ago
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.log(
|
||
|
|
'Error creating default `rocket.cat` user, if you created a user with this username please remove it and restart the server',
|
||
|
|
);
|
||
|
|
throw error;
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
2 years ago
|
await insertAdminUserFromEnv();
|
||
|
4 years ago
|
|
||
|
|
if (typeof process.env.INITIAL_USER === 'string' && process.env.INITIAL_USER.length > 0) {
|
||
|
|
try {
|
||
|
|
const initialUser = JSON.parse(process.env.INITIAL_USER);
|
||
|
|
|
||
|
|
if (!initialUser._id) {
|
||
|
3 years ago
|
console.log(colors.red('No _id provided; Ignoring environment variable INITIAL_USER'));
|
||
|
3 years ago
|
} else if (!(await Users.findOneById(initialUser._id))) {
|
||
|
3 years ago
|
console.log(colors.green('Inserting initial user:'));
|
||
|
|
console.log(colors.green(JSON.stringify(initialUser, null, 2)));
|
||
|
3 years ago
|
await Users.create(initialUser);
|
||
|
4 years ago
|
|
||
|
|
await addUserToDefaultChannels(initialUser, true);
|
||
|
9 years ago
|
}
|
||
|
4 years ago
|
} catch (e) {
|
||
|
3 years ago
|
console.log(colors.red('Error processing environment variable INITIAL_USER'), e);
|
||
|
9 years ago
|
}
|
||
|
4 years ago
|
}
|
||
|
9 years ago
|
|
||
|
1 year ago
|
if ((await Roles.countUsersInRole('admin')) === 0) {
|
||
|
3 years ago
|
const oldestUser = await Users.getOldest({ projection: { _id: 1, username: 1, name: 1 } });
|
||
|
9 years ago
|
|
||
|
4 years ago
|
if (oldestUser) {
|
||
|
4 years ago
|
await addUserRolesAsync(oldestUser._id, ['admin']);
|
||
|
4 years ago
|
console.log(`No admins are found. Set ${oldestUser.username || oldestUser.name} as admin for being the oldest user`);
|
||
|
8 years ago
|
}
|
||
|
4 years ago
|
}
|
||
|
8 years ago
|
|
||
|
1 year ago
|
if ((await Roles.countUsersInRole('admin')) !== 0) {
|
||
|
4 years ago
|
if (settings.get('Show_Setup_Wizard') === 'pending') {
|
||
|
|
console.log('Setting Setup Wizard to "in_progress" because, at least, one admin was found');
|
||
|
2 years ago
|
|
||
|
|
(await Settings.updateValueById('Show_Setup_Wizard', 'in_progress')).modifiedCount &&
|
||
|
|
void notifyOnSettingChangedById('Show_Setup_Wizard');
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
await Users.removeById('rocketchat.internal.admin.test');
|
||
|
4 years ago
|
|
||
|
|
if (process.env.TEST_MODE === 'true') {
|
||
|
3 years ago
|
console.log(colors.green('Inserting admin test user:'));
|
||
|
4 years ago
|
|
||
|
6 days ago
|
const adminUser: Omit<IUser, 'createdAt' | 'roles' | '_updatedAt'> = {
|
||
|
4 years ago
|
_id: 'rocketchat.internal.admin.test',
|
||
|
|
name: 'RocketChat Internal Admin Test',
|
||
|
|
username: 'rocketchat.internal.admin.test',
|
||
|
|
emails: [
|
||
|
|
{
|
||
|
|
address: 'rocketchat.internal.admin.test@rocket.chat',
|
||
|
9 months ago
|
verified: true,
|
||
|
4 years ago
|
},
|
||
|
|
],
|
||
|
6 days ago
|
status: UserStatus.OFFLINE,
|
||
|
|
statusDefault: UserStatus.ONLINE,
|
||
|
4 years ago
|
utcOffset: 0,
|
||
|
|
active: true,
|
||
|
|
type: 'user',
|
||
|
|
};
|
||
|
|
|
||
|
3 years ago
|
console.log(colors.green(`Name: ${adminUser.name}`));
|
||
|
6 days ago
|
console.log(colors.green(`Email: ${adminUser.emails![0].address}`));
|
||
|
3 years ago
|
console.log(colors.green(`Username: ${adminUser.username}`));
|
||
|
|
console.log(colors.green(`Password: ${adminUser._id}`));
|
||
|
4 years ago
|
|
||
|
6 days ago
|
if (await Users.findOneByEmailAddress(adminUser.emails![0].address)) {
|
||
|
|
throw new Meteor.Error(`Email ${adminUser.emails![0].address} already exists`, "Rocket.Chat can't run in test mode");
|
||
|
9 years ago
|
}
|
||
|
|
|
||
|
6 days ago
|
if (!(await checkUsernameAvailability(adminUser.username!))) {
|
||
|
4 years ago
|
throw new Meteor.Error(`Username ${adminUser.username} already exists`, "Rocket.Chat can't run in test mode");
|
||
|
4 years ago
|
}
|
||
|
9 years ago
|
|
||
|
3 years ago
|
await Users.create(adminUser);
|
||
|
9 years ago
|
|
||
|
3 years ago
|
await Accounts.setPasswordAsync(adminUser._id, adminUser._id);
|
||
|
9 years ago
|
|
||
|
4 years ago
|
await addUserRolesAsync(adminUser._id, ['admin']);
|
||
|
8 years ago
|
|
||
|
4 years ago
|
if (settings.get('Show_Setup_Wizard') === 'pending') {
|
||
|
2 years ago
|
(await Settings.updateValueById('Show_Setup_Wizard', 'in_progress')).modifiedCount &&
|
||
|
|
void notifyOnSettingChangedById('Show_Setup_Wizard');
|
||
|
9 years ago
|
}
|
||
|
4 years ago
|
|
||
|
6 days ago
|
await addUserToDefaultChannels(adminUser as IUser, true);
|
||
|
3 weeks ago
|
|
||
|
|
// Create sample call history for API tests
|
||
|
|
return addCallHistoryTestData('rocketchat.internal.admin.test', 'rocket.cat');
|
||
|
4 years ago
|
}
|
||
|
9 years ago
|
});
|