mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-19 00:15:43 +01:00
fix: create admin users if enabled and zero users already
This commit is contained in:
parent
9ce3df8086
commit
3a35e16f1f
@ -87,6 +87,13 @@ class UserStore {
|
||||
this.logger = getLogger('user-store.js');
|
||||
}
|
||||
|
||||
async count(): Promise<number> {
|
||||
return this.db
|
||||
.count('*')
|
||||
.from(TABLE)
|
||||
.then((res) => Number(res[0].count));
|
||||
}
|
||||
|
||||
async update(id: number, fields: IUserUpdateFields): Promise<User> {
|
||||
await this.db(TABLE)
|
||||
.where('id', id)
|
||||
|
@ -114,9 +114,9 @@ class UserService {
|
||||
}
|
||||
|
||||
async initAdminUser(): Promise<void> {
|
||||
const hasAdminUser = await this.store.hasUser({ username: 'admin' });
|
||||
const userCount = await this.store.count();
|
||||
|
||||
if (!hasAdminUser) {
|
||||
if (userCount === 0) {
|
||||
// create default admin user
|
||||
try {
|
||||
const pwd = 'unleash4all';
|
||||
|
@ -55,6 +55,21 @@ test('should create initial admin user', async () => {
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should not init default user if we already have users', async () => {
|
||||
await userService.createUser({
|
||||
username: 'test',
|
||||
password: 'A very strange P4ssw0rd_',
|
||||
rootRole: adminRole.id,
|
||||
});
|
||||
await userService.initAdminUser();
|
||||
const users = await userService.getAll();
|
||||
expect(users).toHaveLength(1);
|
||||
expect(users[0].username).toBe('test');
|
||||
await expect(async () =>
|
||||
userService.loginUser('admin', 'unleash4all'),
|
||||
).rejects.toThrow(Error);
|
||||
});
|
||||
|
||||
test('should not be allowed to create existing user', async () => {
|
||||
await userStore.insert({ username: 'test', name: 'Hans Mola' });
|
||||
await expect(async () =>
|
||||
|
4
src/test/fixtures/fake-user-store.ts
vendored
4
src/test/fixtures/fake-user-store.ts
vendored
@ -27,6 +27,10 @@ class UserStoreMock extends UserStore {
|
||||
return user;
|
||||
}
|
||||
|
||||
async count(): Promise<number> {
|
||||
return Promise.resolve(this.data.length);
|
||||
}
|
||||
|
||||
async insert(user: User): Promise<User> {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
user.id = this.idSeq;
|
||||
|
Loading…
Reference in New Issue
Block a user