fix: Add 10 domain limit per team (#3733)
* fix: Validate team domains are FQDN's Add 10 domain limit per team fix: Deletion of domains not happening within request lifecycle * tests * docs
This commit is contained in:
@@ -6,11 +6,16 @@ import {
|
||||
ForeignKey,
|
||||
NotEmpty,
|
||||
NotIn,
|
||||
BeforeValidate,
|
||||
BeforeCreate,
|
||||
} from "sequelize-typescript";
|
||||
import { MAX_TEAM_DOMAINS } from "@shared/constants";
|
||||
import { ValidationError } from "@server/errors";
|
||||
import Team from "./Team";
|
||||
import User from "./User";
|
||||
import IdModel from "./base/IdModel";
|
||||
import Fix from "./decorators/Fix";
|
||||
import IsFQDN from "./validators/IsFQDN";
|
||||
import Length from "./validators/Length";
|
||||
|
||||
@Table({ tableName: "team_domains", modelName: "team_domain" })
|
||||
@@ -22,6 +27,7 @@ class TeamDomain extends IdModel {
|
||||
})
|
||||
@NotEmpty
|
||||
@Length({ min: 0, max: 255, msg: "Must be less than 255 characters" })
|
||||
@IsFQDN
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
@@ -40,6 +46,25 @@ class TeamDomain extends IdModel {
|
||||
@ForeignKey(() => User)
|
||||
@Column
|
||||
createdById: string;
|
||||
|
||||
// hooks
|
||||
|
||||
@BeforeValidate
|
||||
static async cleanupDomain(model: TeamDomain) {
|
||||
model.name = model.name.toLowerCase().trim();
|
||||
}
|
||||
|
||||
@BeforeCreate
|
||||
static async checkLimit(model: TeamDomain) {
|
||||
const count = await this.count({
|
||||
where: { teamId: model.teamId },
|
||||
});
|
||||
if (count >= MAX_TEAM_DOMAINS) {
|
||||
throw ValidationError(
|
||||
`You have reached the limit of ${MAX_TEAM_DOMAINS} domains`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default TeamDomain;
|
||||
|
||||
Reference in New Issue
Block a user