2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-03-26 15:34:00 +09:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { UsersRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { SignupService } from '@/core/SignupService.js';
|
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2023-09-20 11:33:36 +09:00
|
|
|
import { localUsernameSchema, passwordSchema } from '@/models/User.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
|
tags: ['admin'],
|
|
|
|
|
|
2021-03-06 22:34:11 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 22:34:11 +09:00
|
|
|
ref: 'User',
|
|
|
|
|
properties: {
|
|
|
|
|
token: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'string',
|
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
2022-09-18 03:27:08 +09:00
|
|
|
username: localUsernameSchema,
|
|
|
|
|
password: passwordSchema,
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
|
required: ['username', 'password'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
|
private signupService: SignupService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, _me) => {
|
|
|
|
|
const me = _me ? await this.usersRepository.findOneByOrFail({ id: _me.id }) : null;
|
|
|
|
|
const noUsers = (await this.usersRepository.countBy({
|
|
|
|
|
host: IsNull(),
|
|
|
|
|
})) === 0;
|
2023-01-12 21:02:26 +09:00
|
|
|
if (!noUsers && !me?.isRoot) throw new Error('access denied');
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
|
const { account, secret } = await this.signupService.signup({
|
|
|
|
|
username: ps.username,
|
|
|
|
|
password: ps.password,
|
2023-04-29 17:03:14 +09:00
|
|
|
ignorePreservedUsernames: true,
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await this.userEntityService.pack(account, account, {
|
|
|
|
|
detail: true,
|
|
|
|
|
includeSecrets: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
(res as any).token = secret;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|