2021-01-25 18:04:11 +01:00
|
|
|
import Model from "swaf/db/Model";
|
|
|
|
import User from "swaf/auth/models/User";
|
|
|
|
import {ManyModelRelation, OneModelRelation} from "swaf/db/ModelRelation";
|
2020-07-27 16:02:09 +02:00
|
|
|
import MailIdentity from "./MailIdentity";
|
|
|
|
|
|
|
|
export default class MailDomain extends Model {
|
|
|
|
public id?: number = undefined;
|
|
|
|
public name?: string = undefined;
|
2020-11-03 11:21:32 +01:00
|
|
|
public user_id?: number | null = undefined;
|
2020-07-27 16:02:09 +02:00
|
|
|
|
2020-11-02 18:59:35 +01:00
|
|
|
public readonly owner: OneModelRelation<MailDomain, User> = new OneModelRelation(this, User, {
|
2020-07-27 16:02:09 +02:00
|
|
|
localKey: 'user_id',
|
|
|
|
foreignKey: 'id',
|
|
|
|
});
|
|
|
|
|
2020-11-02 19:27:18 +01:00
|
|
|
public readonly identities: ManyModelRelation<MailDomain, MailIdentity> = new ManyModelRelation(this, MailIdentity,
|
|
|
|
{
|
|
|
|
localKey: 'id',
|
|
|
|
foreignKey: 'mail_domain_id',
|
|
|
|
});
|
2020-07-27 16:02:09 +02:00
|
|
|
|
|
|
|
|
2020-11-02 19:27:18 +01:00
|
|
|
public updateWithData(data: Pick<this, keyof this> | Record<string, unknown>): void {
|
2020-07-27 16:02:09 +02:00
|
|
|
super.updateWithData(data);
|
2020-11-03 11:21:32 +01:00
|
|
|
if (this.user_id !== null && this.user_id !== undefined && this.user_id <= 0) this.user_id = null;
|
2020-07-27 16:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected init(): void {
|
2020-07-30 11:14:35 +02:00
|
|
|
this.setValidation('name').acceptUndefined()
|
|
|
|
.maxLength(252)
|
2020-07-30 09:49:22 +02:00
|
|
|
.regexp(/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/)
|
|
|
|
.unique(this);
|
2020-07-27 16:02:09 +02:00
|
|
|
this.setValidation('user_id').acceptUndefined().exists(User, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public isPublic(): boolean {
|
|
|
|
return !this.user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public canCreateAddresses(user: User): boolean {
|
|
|
|
return this.user_id === user.id || this.isPublic();
|
|
|
|
}
|
2020-11-02 18:59:35 +01:00
|
|
|
}
|