@@ -12,11 +12,27 @@ import { UserEntityService } from '@/core/entities/UserEntityService.js';
12
12
import { InstanceActorService } from '@/core/InstanceActorService.js' ;
13
13
import { localUsernameSchema , passwordSchema } from '@/models/User.js' ;
14
14
import { DI } from '@/di-symbols.js' ;
15
+ import type { Config } from '@/config.js' ;
16
+ import { ApiError } from '@/server/api/error.js' ;
15
17
import { Packed } from '@/misc/json-schema.js' ;
16
18
17
19
export const meta = {
18
20
tags : [ 'admin' ] ,
19
21
22
+ errors : {
23
+ accessDenied : {
24
+ message : 'Access denied.' ,
25
+ code : 'ACCESS_DENIED' ,
26
+ id : '1fb7cb09-d46a-4fff-b8df-057708cce513' ,
27
+ } ,
28
+
29
+ wrongInitialPassword : {
30
+ message : 'Initial password is incorrect.' ,
31
+ code : 'INCORRECT_INITIAL_PASSWORD' ,
32
+ id : '97147c55-1ae1-4f6f-91d6-e1c3e0e76d62' ,
33
+ } ,
34
+ } ,
35
+
20
36
res : {
21
37
type : 'object' ,
22
38
optional : false , nullable : false ,
@@ -35,13 +51,17 @@ export const paramDef = {
35
51
properties : {
36
52
username : localUsernameSchema ,
37
53
password : passwordSchema ,
54
+ initialPassword : { type : 'string' , nullable : true } ,
38
55
} ,
39
56
required : [ 'username' , 'password' ] ,
40
57
} as const ;
41
58
42
59
@Injectable ( )
43
60
export default class extends Endpoint < typeof meta , typeof paramDef > { // eslint-disable-line import/no-default-export
44
61
constructor (
62
+ @Inject ( DI . config )
63
+ private config : Config ,
64
+
45
65
@Inject ( DI . usersRepository )
46
66
private usersRepository : UsersRepository ,
47
67
@@ -52,7 +72,23 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
52
72
super ( meta , paramDef , async ( ps , _me , token ) => {
53
73
const me = _me ? await this . usersRepository . findOneByOrFail ( { id : _me . id } ) : null ;
54
74
const realUsers = await this . instanceActorService . realLocalUsersPresent ( ) ;
55
- if ( ( realUsers && ! me ?. isRoot ) || token !== null ) throw new Error ( 'access denied' ) ;
75
+
76
+ if ( ! realUsers && me == null && token == null ) {
77
+ // 初回セットアップの場合
78
+ if ( this . config . initialPassword != null ) {
79
+ // 初期パスワードが設定されている場合
80
+ if ( ps . initialPassword !== this . config . initialPassword ) {
81
+ // 初期パスワードが違う場合
82
+ throw new ApiError ( meta . errors . wrongInitialPassword ) ;
83
+ }
84
+ } else if ( ps . initialPassword != null && ps . initialPassword . trim ( ) !== '' ) {
85
+ // 初期パスワードが設定されていないのに初期パスワードが入力された場合
86
+ throw new ApiError ( meta . errors . wrongInitialPassword ) ;
87
+ }
88
+ } else if ( ( realUsers && ! me ?. isRoot ) || token !== null ) {
89
+ // 初回セットアップではなく、管理者でない場合 or 外部トークンを使用している場合
90
+ throw new ApiError ( meta . errors . accessDenied ) ;
91
+ }
56
92
57
93
const { account, secret } = await this . signupService . signup ( {
58
94
username : ps . username ,
0 commit comments