2020-07-27 16:02:09 +02:00
|
|
|
import Model from "wms-core/db/Model";
|
|
|
|
import User from "wms-core/auth/models/User";
|
|
|
|
import {ManyModelRelation, OneModelRelation} from "wms-core/db/ModelRelation";
|
|
|
|
import ModelFactory from "wms-core/db/ModelFactory";
|
|
|
|
import MailIdentity from "./MailIdentity";
|
|
|
|
|
|
|
|
export default class MailDomain extends Model {
|
|
|
|
public id?: number = undefined;
|
|
|
|
public name?: string = undefined;
|
|
|
|
public user_id?: number = undefined;
|
|
|
|
|
|
|
|
public readonly owner: OneModelRelation<MailDomain, User> = new OneModelRelation(this, ModelFactory.get(User), {
|
|
|
|
localKey: 'user_id',
|
|
|
|
foreignKey: 'id',
|
|
|
|
});
|
|
|
|
|
|
|
|
public readonly identities: ManyModelRelation<MailDomain, MailIdentity> = new ManyModelRelation(this, ModelFactory.get(MailIdentity), {
|
|
|
|
localKey: 'id',
|
|
|
|
foreignKey: 'mail_domain_id',
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
public updateWithData(data: any) {
|
|
|
|
super.updateWithData(data);
|
|
|
|
if (typeof this.user_id !== 'undefined' && this.user_id <= 0) {
|
|
|
|
this.user_id = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected init(): void {
|
2020-07-30 09:49:22 +02:00
|
|
|
this.setValidation('name').defined().maxLength(252)
|
|
|
|
.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();
|
|
|
|
}
|
|
|
|
}
|