fix some foreign keys

This commit is contained in:
Evert Prants 2022-09-14 21:02:48 +03:00
parent 7e56e0eec4
commit eb5f66e4cd
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
6 changed files with 108 additions and 14 deletions

View File

@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class deletableclient1663177839075 implements MigrationInterface {
name = 'deletableclient1663177839075';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_url\` DROP FOREIGN KEY \`FK_aca59c7bdd65987487eea98d00f\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_url\` ADD CONSTRAINT \`FK_aca59c7bdd65987487eea98d00f\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_url\` DROP FOREIGN KEY \`FK_aca59c7bdd65987487eea98d00f\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_url\` ADD CONSTRAINT \`FK_aca59c7bdd65987487eea98d00f\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}
}

View File

@ -0,0 +1,51 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class cascades1663178177421 implements MigrationInterface {
name = 'cascades1663178177421';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP FOREIGN KEY \`FK_3ecb760b321ef9bbab635f05b45\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD CONSTRAINT \`FK_3ecb760b321ef9bbab635f05b45\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP FOREIGN KEY \`FK_81ffb9b8d672cf3af1af9e789f3\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD CONSTRAINT \`FK_81ffb9b8d672cf3af1af9e789f3\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` DROP FOREIGN KEY \`FK_d37db50eecdf9b8ce4eedd2f918\``,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` ADD CONSTRAINT \`FK_d37db50eecdf9b8ce4eedd2f918\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`user_token\` DROP FOREIGN KEY \`FK_d37db50eecdf9b8ce4eedd2f918\``,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` ADD CONSTRAINT \`FK_d37db50eecdf9b8ce4eedd2f918\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP FOREIGN KEY \`FK_3ecb760b321ef9bbab635f05b45\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD CONSTRAINT \`FK_3ecb760b321ef9bbab635f05b45\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP FOREIGN KEY \`FK_81ffb9b8d672cf3af1af9e789f3\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD CONSTRAINT \`FK_81ffb9b8d672cf3af1af9e789f3\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}
}

View File

@ -232,7 +232,7 @@ export class OAuth2AdminController {
@Body() setter: Partial<OAuth2Client>,
@CurrentUser() user: User,
) {
const client = await this._oaClient.getById(parseInt(id, 10), []);
const client = await this._oaClient.getById(parseInt(id, 10), ['owner']);
const reducedPermissions = !this._service.userHasPrivilege(
user,
'admin:oauth2',
@ -301,7 +301,7 @@ export class OAuth2AdminController {
@Scopes('management')
@Privileges(['admin', 'admin:oauth2'], 'self:oauth2')
async deleteOauth2Client(@Param('id') id: string, @CurrentUser() user: User) {
const client = await this._oaClient.getById(parseInt(id, 10), []);
const client = await this._oaClient.getById(parseInt(id, 10), ['owner']);
if (!client) {
throw new NotFoundException('Client not found');
}
@ -326,7 +326,7 @@ export class OAuth2AdminController {
@Scopes('management')
@Privileges(['admin', 'admin:oauth2'], 'self:oauth2')
async createNewSecret(@Param('id') id: string, @CurrentUser() user: User) {
const client = await this._oaClient.getById(parseInt(id, 10), []);
const client = await this._oaClient.getById(parseInt(id, 10), ['owner']);
if (!client) {
throw new NotFoundException('Client not found');
}
@ -354,7 +354,7 @@ export class OAuth2AdminController {
@Param('id') id: string,
@CurrentUser() user: User,
) {
const client = await this._oaClient.getById(parseInt(id, 10), []);
const client = await this._oaClient.getById(parseInt(id, 10), ['owner']);
if (!client) {
throw new NotFoundException('Client not found');
}
@ -374,7 +374,10 @@ export class OAuth2AdminController {
@Scopes('management')
@Privileges(['admin', 'admin:oauth2'], 'self:oauth2')
async oauth2ClientURLs(@Param('id') id: string, @CurrentUser() user: User) {
const client = await this._oaClient.getById(parseInt(id, 10), ['urls']);
const client = await this._oaClient.getById(parseInt(id, 10), [
'urls',
'owner',
]);
if (!client) {
throw new NotFoundException('Client not found');
}
@ -396,7 +399,10 @@ export class OAuth2AdminController {
@Param('url') urlId: string,
@CurrentUser() user: User,
) {
const client = await this._oaClient.getById(parseInt(id, 10), ['urls']);
const client = await this._oaClient.getById(parseInt(id, 10), [
'urls',
'owner',
]);
const parsedURLId = parseInt(urlId, 10);
if (!client) {
@ -432,7 +438,10 @@ export class OAuth2AdminController {
@Body() setter: { url: string; type: string },
@CurrentUser() user: User,
) {
const client = await this._oaClient.getById(parseInt(id, 10), ['urls']);
const client = await this._oaClient.getById(parseInt(id, 10), [
'urls',
'owner',
]);
const plucked = this._form.pluckObject(setter, ['url', 'type']);
const parsedURLId = parseInt(urlId, 10);
@ -472,7 +481,10 @@ export class OAuth2AdminController {
@Body() setter: { url: string; type: string },
@CurrentUser() user: User,
) {
const client = await this._oaClient.getById(parseInt(id, 10), ['urls']);
const client = await this._oaClient.getById(parseInt(id, 10), [
'urls',
'owner',
]);
if (!client) {
throw new NotFoundException('Client not found');
}
@ -506,7 +518,10 @@ export class OAuth2AdminController {
@Param('id') id: string,
@UploadedFile() file: Express.Multer.File,
) {
const client = await this._oaClient.getById(parseInt(id, 10), ['picture']);
const client = await this._oaClient.getById(parseInt(id, 10), [
'picture',
'owner',
]);
try {
if (!client) {
@ -551,7 +566,10 @@ export class OAuth2AdminController {
@Param('id') id: string,
@CurrentUser() user: User,
) {
const client = await this._oaClient.getById(parseInt(id, 10), ['picture']);
const client = await this._oaClient.getById(parseInt(id, 10), [
'picture',
'owner',
]);
if (!client) {
throw new NotFoundException('Client not found');
}

View File

@ -39,6 +39,8 @@ export class OAuth2ClientURL {
})
public updated_at: Date;
@ManyToOne(() => OAuth2Client, (client) => client.urls)
@ManyToOne(() => OAuth2Client, (client) => client.urls, {
onDelete: 'CASCADE',
})
public client: OAuth2Client;
}

View File

@ -32,10 +32,10 @@ export class OAuth2Token {
@Column({ type: 'text', nullable: true })
scope: string;
@ManyToOne(() => User, { nullable: true })
@ManyToOne(() => User, { nullable: true, onDelete: 'CASCADE' })
user: User;
@ManyToOne(() => OAuth2Client)
@ManyToOne(() => OAuth2Client, { onDelete: 'CASCADE' })
client: OAuth2Client;
@Column({ type: 'timestamp' })

View File

@ -39,6 +39,6 @@ export class UserToken {
@CreateDateColumn()
public created_at: Date;
@ManyToOne(() => User)
@ManyToOne(() => User, { onDelete: 'CASCADE' })
user: User;
}