rainbox.email/src/models/MailDomain.ts

46 lines
1.5 KiB
TypeScript
Raw Normal View History

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, User, {
2020-07-27 16:02:09 +02:00
localKey: 'user_id',
foreignKey: 'id',
});
public readonly identities: ManyModelRelation<MailDomain, MailIdentity> = new ManyModelRelation(this, MailIdentity, {
2020-07-27 16:02:09 +02:00
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 {
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();
}
}