forked from FoundKeyGang/FoundKey
Johann150
7b8333a21f
Instead of throwing an IdentifiableError which then just always gets converted into an ApiError, the getter can just throw the same ApiError directly. This makes it more convenient to use and thus more endpoints have been refactored to use it to reduce code repetition.
56 lines
1.1 KiB
TypeScript
56 lines
1.1 KiB
TypeScript
import { hashPassword } from '@/misc/password.js';
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
|
import { Users, UserProfiles } from '@/models/index.js';
|
|
import { ApiError } from '@/server/api/error.js';
|
|
import define from '@/server/api/define.js';
|
|
import { getLocalUser } from '@/server/api/common/getters.js';
|
|
|
|
export const meta = {
|
|
tags: ['admin'],
|
|
|
|
requireCredential: true,
|
|
requireModerator: true,
|
|
|
|
res: {
|
|
type: 'object',
|
|
optional: false, nullable: false,
|
|
properties: {
|
|
password: {
|
|
type: 'string',
|
|
optional: false, nullable: false,
|
|
},
|
|
},
|
|
},
|
|
|
|
errors: ['NO_SUCH_USER', 'IS_ADMIN'],
|
|
} as const;
|
|
|
|
export const paramDef = {
|
|
type: 'object',
|
|
properties: {
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
},
|
|
required: ['userId'],
|
|
} as const;
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
export default define(meta, paramDef, async (ps) => {
|
|
const user = await getLocalUser(ps.userId);
|
|
|
|
if (user.isAdmin) {
|
|
throw new ApiError('IS_ADMIN');
|
|
}
|
|
|
|
const password = secureRndstr(8, true);
|
|
|
|
await UserProfiles.update({
|
|
userId: user.id,
|
|
}, {
|
|
password: await hashPassword(password),
|
|
});
|
|
|
|
return {
|
|
password,
|
|
};
|
|
});
|