2020-07-21 13:20:51 +00:00
|
|
|
import {Column, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne,
|
|
|
|
PrimaryGeneratedColumn, RelationId} from "typeorm";
|
2020-08-04 15:52:56 +00:00
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
import {Role} from "app/common/roles";
|
|
|
|
import {OrganizationProperties, organizationPropertyKeys} from "app/common/UserAPI";
|
|
|
|
import {AclRuleOrg} from "./AclRule";
|
|
|
|
import {BillingAccount} from "./BillingAccount";
|
2020-08-04 15:52:56 +00:00
|
|
|
import {Pref} from "./Pref";
|
2020-07-21 13:20:51 +00:00
|
|
|
import {Resource} from "./Resource";
|
|
|
|
import {User} from "./User";
|
|
|
|
import {Workspace} from "./Workspace";
|
|
|
|
|
|
|
|
// Information about how an organization may be accessed.
|
|
|
|
export interface AccessOption {
|
|
|
|
id: number; // a user id
|
|
|
|
email: string; // a user email
|
|
|
|
name: string; // a user name
|
|
|
|
perms: number; // permissions the user would have on organization
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AccessOptionWithRole extends AccessOption {
|
|
|
|
access: Role; // summary of permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
@Entity({name: 'orgs'})
|
|
|
|
export class Organization extends Resource {
|
|
|
|
|
|
|
|
@PrimaryGeneratedColumn()
|
|
|
|
public id: number;
|
|
|
|
|
|
|
|
@Column({
|
|
|
|
nullable: true
|
|
|
|
})
|
|
|
|
public domain: string;
|
|
|
|
|
|
|
|
@OneToOne(type => User, user => user.personalOrg)
|
|
|
|
@JoinColumn({name: 'owner_id'})
|
|
|
|
public owner: User;
|
|
|
|
|
|
|
|
@RelationId((org: Organization) => org.owner)
|
|
|
|
public ownerId: number;
|
|
|
|
|
|
|
|
@OneToMany(type => Workspace, workspace => workspace.org)
|
|
|
|
public workspaces: Workspace[];
|
|
|
|
|
|
|
|
@OneToMany(type => AclRuleOrg, aclRule => aclRule.organization)
|
|
|
|
public aclRules: AclRuleOrg[];
|
|
|
|
|
|
|
|
@Column({name: 'billing_account_id'})
|
|
|
|
public billingAccountId: number;
|
|
|
|
|
|
|
|
@ManyToOne(type => BillingAccount)
|
|
|
|
@JoinColumn({name: 'billing_account_id'})
|
|
|
|
public billingAccount: BillingAccount;
|
|
|
|
|
|
|
|
// Property that may be returned when the org is fetched to indicate the access the
|
|
|
|
// fetching user has on the org, i.e. 'owners', 'editors', 'viewers'
|
|
|
|
public access: string;
|
|
|
|
|
|
|
|
// Property that may be used internally to track multiple ways an org can be accessed
|
|
|
|
public accessOptions?: AccessOptionWithRole[];
|
|
|
|
|
|
|
|
// a computed column with permissions.
|
|
|
|
// {insert: false} makes sure typeorm doesn't try to put values into such
|
|
|
|
// a column when creating organizations.
|
|
|
|
@Column({name: 'permissions', type: 'text', select: false, insert: false})
|
|
|
|
public permissions?: any;
|
|
|
|
|
|
|
|
// For custom domains, this is the preferred host associated with this org/team.
|
|
|
|
@Column({name: 'host', type: 'text', nullable: true})
|
|
|
|
public host: string|null;
|
|
|
|
|
2020-08-04 15:52:56 +00:00
|
|
|
// Any prefs relevant to the org and user. This relation is marked to not result
|
|
|
|
// in saves, since OneToMany saves in TypeORM are not reliable - see e.g. later
|
|
|
|
// parts of this issue:
|
|
|
|
// https://github.com/typeorm/typeorm/issues/3095
|
|
|
|
@OneToMany(type => Pref, pref => pref.org, {persistence: false})
|
|
|
|
public prefs?: Pref[];
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
public checkProperties(props: any): props is Partial<OrganizationProperties> {
|
|
|
|
return super.checkProperties(props, organizationPropertyKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateFromProperties(props: Partial<OrganizationProperties>) {
|
|
|
|
super.updateFromProperties(props);
|
|
|
|
if (props.domain) { this.domain = props.domain; }
|
|
|
|
}
|
|
|
|
}
|