2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* 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';
|
2024-02-04 11:46:28 +00:00
|
|
|
import { InstanceActorService } from '@/core/InstanceActorService.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';
|
2024-10-03 18:18:00 +09:00
|
|
|
import type { Config } from '@/config.js';
|
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2024-01-31 15:45:35 +09:00
|
|
|
import { Packed } from '@/misc/json-schema.js';
|
2024-11-14 02:50:44 -06:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['admin'],
|
|
|
|
|
2024-10-03 18:18:00 +09:00
|
|
|
errors: {
|
|
|
|
accessDenied: {
|
2024-11-14 02:50:44 -06:00
|
|
|
httpStatusCode: 403,
|
2024-10-03 18:18:00 +09:00
|
|
|
message: 'Access denied.',
|
|
|
|
code: 'ACCESS_DENIED',
|
|
|
|
id: '1fb7cb09-d46a-4fff-b8df-057708cce513',
|
|
|
|
},
|
|
|
|
|
|
|
|
wrongInitialPassword: {
|
2024-11-14 02:50:44 -06:00
|
|
|
httpStatusCode: 401,
|
2024-10-03 18:18:00 +09:00
|
|
|
message: 'Initial password is incorrect.',
|
|
|
|
code: 'INCORRECT_INITIAL_PASSWORD',
|
|
|
|
id: '97147c55-1ae1-4f6f-91d6-e1c3e0e76d62',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2021-03-06 22:34:11 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2024-01-31 15:45:35 +09:00
|
|
|
ref: 'MeDetailed',
|
2021-03-06 22:34:11 +09:00
|
|
|
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,
|
2024-10-03 20:40:39 +09:00
|
|
|
setupPassword: { type: 'string', nullable: true },
|
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(
|
2024-10-03 18:18:00 +09:00
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
2024-11-14 02:50:44 -06:00
|
|
|
private roleService: RoleService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private signupService: SignupService,
|
2024-02-04 11:46:28 +00:00
|
|
|
private instanceActorService: InstanceActorService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
2023-12-27 17:36:38 +09:00
|
|
|
super(meta, paramDef, async (ps, _me, token) => {
|
2022-09-18 03:27:08 +09:00
|
|
|
const me = _me ? await this.usersRepository.findOneByOrFail({ id: _me.id }) : null;
|
2024-02-04 11:46:28 +00:00
|
|
|
const realUsers = await this.instanceActorService.realLocalUsersPresent();
|
2024-10-03 18:18:00 +09:00
|
|
|
|
|
|
|
if (!realUsers && me == null && token == null) {
|
|
|
|
// 初回セットアップの場合
|
2024-10-03 20:40:39 +09:00
|
|
|
if (this.config.setupPassword != null) {
|
2024-10-03 18:18:00 +09:00
|
|
|
// 初期パスワードが設定されている場合
|
2024-10-03 20:40:39 +09:00
|
|
|
if (ps.setupPassword !== this.config.setupPassword) {
|
2024-10-03 18:18:00 +09:00
|
|
|
// 初期パスワードが違う場合
|
|
|
|
throw new ApiError(meta.errors.wrongInitialPassword);
|
|
|
|
}
|
2024-10-03 20:40:39 +09:00
|
|
|
} else if (ps.setupPassword != null && ps.setupPassword.trim() !== '') {
|
2024-10-03 18:18:00 +09:00
|
|
|
// 初期パスワードが設定されていないのに初期パスワードが入力された場合
|
|
|
|
throw new ApiError(meta.errors.wrongInitialPassword);
|
|
|
|
}
|
2024-11-14 02:50:44 -06:00
|
|
|
} else if (!(me?.isRoot) && !await this.roleService.isAdministrator(me)) {
|
|
|
|
// 管理者でない場合
|
2024-10-03 18:18:00 +09:00
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
}
|
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, {
|
2024-01-31 15:45:35 +09:00
|
|
|
schema: 'MeDetailed',
|
2022-09-18 03:27:08 +09:00
|
|
|
includeSecrets: true,
|
2024-01-31 15:45:35 +09:00
|
|
|
}) as Packed<'MeDetailed'> & { token: string };
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2024-01-31 15:45:35 +09:00
|
|
|
res.token = secret;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|