@stamhoofd/models 2.137.5 → 2.138.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/Handlebars.d.ts.map +1 -1
- package/dist/helpers/Handlebars.js +1 -1
- package/dist/helpers/Handlebars.js.map +1 -1
- package/dist/helpers/errorToSimpleErrors.d.ts +3 -0
- package/dist/helpers/errorToSimpleErrors.d.ts.map +1 -0
- package/dist/helpers/errorToSimpleErrors.js +17 -0
- package/dist/helpers/errorToSimpleErrors.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/migrations/1782902314-tokens-authenticated-at.sql +2 -0
- package/dist/migrations/1782902315-create-mfa-totp-table.sql +13 -0
- package/dist/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
- package/dist/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
- package/dist/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
- package/dist/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
- package/dist/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
- package/dist/migrations/1784122354-webauthn-credential-provider.sql +3 -0
- package/dist/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
- package/dist/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
- package/dist/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
- package/dist/migrations/1785354093-platform-uri.sql +1 -0
- package/dist/migrations/1785354094-platform-domain.sql +1 -0
- package/dist/migrations/1785354095-platform-uri-unique.sql +1 -0
- package/dist/migrations/1785354096-platform-domain-unique.sql +1 -0
- package/dist/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.d.ts +4 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.d.ts.map +1 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.js +10 -0
- package/dist/migrations/1785354098-platform-tenant-backfill.js.map +1 -0
- package/dist/migrations/1785417301-users-last-active-at.sql +2 -0
- package/dist/models/Email.d.ts +0 -6
- package/dist/models/Email.d.ts.map +1 -1
- package/dist/models/Email.js +4 -625
- package/dist/models/Email.js.map +1 -1
- package/dist/models/MFARecoveryCode.d.ts +17 -0
- package/dist/models/MFARecoveryCode.d.ts.map +1 -0
- package/dist/models/MFARecoveryCode.js +55 -0
- package/dist/models/MFARecoveryCode.js.map +1 -0
- package/dist/models/MFATOTP.d.ts +44 -0
- package/dist/models/MFATOTP.d.ts.map +1 -0
- package/dist/models/MFATOTP.js +116 -0
- package/dist/models/MFATOTP.js.map +1 -0
- package/dist/models/MFAToken.d.ts +49 -0
- package/dist/models/MFAToken.d.ts.map +1 -0
- package/dist/models/MFAToken.js +137 -0
- package/dist/models/MFAToken.js.map +1 -0
- package/dist/models/Organization.d.ts +1 -38
- package/dist/models/Organization.d.ts.map +1 -1
- package/dist/models/Organization.js +1 -132
- package/dist/models/Organization.js.map +1 -1
- package/dist/models/Platform.d.ts +28 -4
- package/dist/models/Platform.d.ts.map +1 -1
- package/dist/models/Platform.js +109 -53
- package/dist/models/Platform.js.map +1 -1
- package/dist/models/Token.d.ts +40 -1
- package/dist/models/Token.d.ts.map +1 -1
- package/dist/models/Token.js +68 -2
- package/dist/models/Token.js.map +1 -1
- package/dist/models/User.d.ts +25 -0
- package/dist/models/User.d.ts.map +1 -1
- package/dist/models/User.js +36 -0
- package/dist/models/User.js.map +1 -1
- package/dist/models/WebauthnChallenge.d.ts +27 -0
- package/dist/models/WebauthnChallenge.d.ts.map +1 -0
- package/dist/models/WebauthnChallenge.js +81 -0
- package/dist/models/WebauthnChallenge.js.map +1 -0
- package/dist/models/WebauthnCredential.d.ts +58 -0
- package/dist/models/WebauthnCredential.d.ts.map +1 -0
- package/dist/models/WebauthnCredential.js +144 -0
- package/dist/models/WebauthnCredential.js.map +1 -0
- package/dist/models/index.d.ts +5 -0
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/index.js +5 -0
- package/dist/models/index.js.map +1 -1
- package/package.json +3 -3
- package/src/helpers/Handlebars.ts +5 -9
- package/src/helpers/errorToSimpleErrors.ts +17 -0
- package/src/index.ts +0 -1
- package/src/migrations/1782902314-tokens-authenticated-at.sql +2 -0
- package/src/migrations/1782902315-create-mfa-totp-table.sql +13 -0
- package/src/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
- package/src/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
- package/src/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
- package/src/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
- package/src/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
- package/src/migrations/1784122354-webauthn-credential-provider.sql +3 -0
- package/src/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
- package/src/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
- package/src/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
- package/src/migrations/1785354093-platform-uri.sql +1 -0
- package/src/migrations/1785354094-platform-domain.sql +1 -0
- package/src/migrations/1785354095-platform-uri-unique.sql +1 -0
- package/src/migrations/1785354096-platform-domain-unique.sql +1 -0
- package/src/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
- package/src/migrations/1785354098-platform-tenant-backfill.ts +14 -0
- package/src/migrations/1785417301-users-last-active-at.sql +2 -0
- package/src/models/Email.ts +4 -713
- package/src/models/MFARecoveryCode.ts +52 -0
- package/src/models/MFATOTP.ts +112 -0
- package/src/models/MFAToken.ts +138 -0
- package/src/models/Organization.test.ts +0 -46
- package/src/models/Organization.ts +0 -154
- package/src/models/Platform.test.ts +167 -3
- package/src/models/Platform.ts +121 -53
- package/src/models/Token.ts +73 -2
- package/src/models/User.ts +37 -0
- package/src/models/WebauthnChallenge.ts +80 -0
- package/src/models/WebauthnCredential.ts +130 -0
- package/src/models/index.ts +6 -0
- package/dist/helpers/EmailBuilder.d.ts +0 -121
- package/dist/helpers/EmailBuilder.d.ts.map +0 -1
- package/dist/helpers/EmailBuilder.js +0 -659
- package/dist/helpers/EmailBuilder.js.map +0 -1
- package/src/helpers/EmailBuilder.test.ts +0 -291
- package/src/helpers/EmailBuilder.ts +0 -818
- package/src/models/Email.test.ts +0 -1222
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel } from '@stamhoofd/sql';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A single-use recovery (backup) code. Only the Argon2 hash is stored. A user has one
|
|
7
|
+
* active batch; regenerating deletes the old batch.
|
|
8
|
+
*/
|
|
9
|
+
export class MFARecoveryCode extends QueryableModel {
|
|
10
|
+
static table = 'mfa_recovery_codes';
|
|
11
|
+
|
|
12
|
+
@column({
|
|
13
|
+
primary: true, type: 'string', beforeSave(value) {
|
|
14
|
+
return value ?? uuidv4();
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
id!: string;
|
|
18
|
+
|
|
19
|
+
@column({ type: 'string' })
|
|
20
|
+
userId: string;
|
|
21
|
+
|
|
22
|
+
@column({ type: 'string' })
|
|
23
|
+
codeHash: string;
|
|
24
|
+
|
|
25
|
+
@column({ type: 'datetime', nullable: true })
|
|
26
|
+
usedAt: Date | null = null;
|
|
27
|
+
|
|
28
|
+
@column({
|
|
29
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
30
|
+
if (old !== undefined) {
|
|
31
|
+
return old;
|
|
32
|
+
}
|
|
33
|
+
const date = new Date();
|
|
34
|
+
date.setMilliseconds(0);
|
|
35
|
+
return date;
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
createdAt: Date;
|
|
39
|
+
|
|
40
|
+
static async getForUser(userId: string): Promise<MFARecoveryCode[]> {
|
|
41
|
+
return await this.select().where('userId', userId).fetch();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static async getUnusedForUser(userId: string): Promise<MFARecoveryCode[]> {
|
|
45
|
+
const all = await this.getForUser(userId);
|
|
46
|
+
return all.filter(c => c.usedAt === null);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static async deleteForUser(userId: string): Promise<void> {
|
|
50
|
+
await this.delete().where('userId', userId);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel, SQL, SQLNull, SQLScalar, SQLWhereEqual, SQLWhereOr, SQLWhereSign } from '@stamhoofd/sql';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A single TOTP authenticator enrolled by a user. A user may have multiple.
|
|
7
|
+
* The `secret` is stored encrypted at rest (see EncryptionHelper).
|
|
8
|
+
*/
|
|
9
|
+
export class MFATOTP extends QueryableModel {
|
|
10
|
+
static table = 'mfa_totp';
|
|
11
|
+
|
|
12
|
+
@column({
|
|
13
|
+
primary: true, type: 'string', beforeSave(value) {
|
|
14
|
+
return value ?? uuidv4();
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
id!: string;
|
|
18
|
+
|
|
19
|
+
@column({ type: 'string' })
|
|
20
|
+
userId: string;
|
|
21
|
+
|
|
22
|
+
@column({ type: 'string' })
|
|
23
|
+
name = '';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The encrypted base32 TOTP secret.
|
|
27
|
+
*/
|
|
28
|
+
@column({ type: 'string' })
|
|
29
|
+
secret: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Null until the user has verified their first code. Unconfirmed rows do not count
|
|
33
|
+
* as an enrolled factor.
|
|
34
|
+
*/
|
|
35
|
+
@column({ type: 'datetime', nullable: true })
|
|
36
|
+
confirmedAt: Date | null = null;
|
|
37
|
+
|
|
38
|
+
@column({ type: 'datetime', nullable: true })
|
|
39
|
+
lastUsedAt: Date | null = null;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The last accepted TOTP step counter. A code may only be accepted if its counter is
|
|
43
|
+
* strictly greater than this, so a still-valid code cannot be replayed (RFC 6238 §5.2).
|
|
44
|
+
*/
|
|
45
|
+
@column({ type: 'integer', nullable: true })
|
|
46
|
+
lastUsedCounter: number | null = null;
|
|
47
|
+
|
|
48
|
+
@column({
|
|
49
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
50
|
+
if (old !== undefined) {
|
|
51
|
+
return old;
|
|
52
|
+
}
|
|
53
|
+
const date = new Date();
|
|
54
|
+
date.setMilliseconds(0);
|
|
55
|
+
return date;
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
createdAt: Date;
|
|
59
|
+
|
|
60
|
+
@column({
|
|
61
|
+
type: 'datetime', beforeSave() {
|
|
62
|
+
const date = new Date();
|
|
63
|
+
date.setMilliseconds(0);
|
|
64
|
+
return date;
|
|
65
|
+
},
|
|
66
|
+
skipUpdate: true,
|
|
67
|
+
})
|
|
68
|
+
updatedAt: Date;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Record `counter` as the last accepted step, but only when no other request claimed
|
|
72
|
+
* the same or a newer one first. Returns false when it was already claimed: two logins
|
|
73
|
+
* that submit the same still-valid code at the same time must not both succeed
|
|
74
|
+
* (RFC 6238 §5.2), so the claim has to happen in a single conditional UPDATE instead of
|
|
75
|
+
* a read followed by a write.
|
|
76
|
+
*/
|
|
77
|
+
async claimCounter(counter: number): Promise<boolean> {
|
|
78
|
+
const { changedRows } = await MFATOTP.update()
|
|
79
|
+
.set('lastUsedCounter', counter)
|
|
80
|
+
.set('lastUsedAt', new Date())
|
|
81
|
+
.where('id', this.id)
|
|
82
|
+
.where(new SQLWhereOr([
|
|
83
|
+
new SQLWhereEqual(SQL.column('lastUsedCounter'), SQLWhereSign.Equal, new SQLNull()),
|
|
84
|
+
new SQLWhereEqual(SQL.column('lastUsedCounter'), SQLWhereSign.Less, new SQLScalar(counter)),
|
|
85
|
+
]))
|
|
86
|
+
.update();
|
|
87
|
+
|
|
88
|
+
if (changedRows !== 1) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.lastUsedCounter = counter;
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static async getForUser(userId: string): Promise<MFATOTP[]> {
|
|
97
|
+
return await this.select().where('userId', userId).fetch();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static async getConfirmedForUser(userId: string): Promise<MFATOTP[]> {
|
|
101
|
+
const all = await this.getForUser(userId);
|
|
102
|
+
return all.filter(t => t.confirmedAt !== null);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Remove abandoned, never-confirmed authenticators (e.g. from earlier setup attempts
|
|
107
|
+
* that were never completed) so they don't accumulate as orphaned rows.
|
|
108
|
+
*/
|
|
109
|
+
static async deleteUnconfirmedForUser(userId: string): Promise<void> {
|
|
110
|
+
await this.delete().where('userId', userId).where('confirmedAt', null);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel, SQLCalculation, SQLColumnExpression, SQLScalar, SQLWhereSign } from '@stamhoofd/sql';
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
|
|
5
|
+
export type MFATokenPurpose = 'login' | 'setup';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A short-lived, single-use session token issued during a login that requires a second
|
|
9
|
+
* factor ('login') or forced 2FA enrollment ('setup'). Mirrors EmailVerificationCode.
|
|
10
|
+
*/
|
|
11
|
+
export class MFAToken extends QueryableModel {
|
|
12
|
+
static table = 'mfa_tokens';
|
|
13
|
+
static MAX_TRIES = 5;
|
|
14
|
+
static EXPIRY_MS = 15 * 60 * 1000;
|
|
15
|
+
|
|
16
|
+
@column({ primary: true, type: 'string' })
|
|
17
|
+
token: string;
|
|
18
|
+
|
|
19
|
+
@column({ type: 'string' })
|
|
20
|
+
userId: string;
|
|
21
|
+
|
|
22
|
+
@column({ type: 'string' })
|
|
23
|
+
purpose: MFATokenPurpose = 'login';
|
|
24
|
+
|
|
25
|
+
@column({ type: 'integer' })
|
|
26
|
+
tries = 0;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The pending WebAuthn authentication challenge for passkey login (if any).
|
|
30
|
+
*/
|
|
31
|
+
@column({ type: 'string', nullable: true })
|
|
32
|
+
webauthnChallenge: string | null = null;
|
|
33
|
+
|
|
34
|
+
@column({ type: 'datetime' })
|
|
35
|
+
expiresAt: Date;
|
|
36
|
+
|
|
37
|
+
@column({
|
|
38
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
39
|
+
if (old !== undefined) {
|
|
40
|
+
return old;
|
|
41
|
+
}
|
|
42
|
+
const date = new Date();
|
|
43
|
+
date.setMilliseconds(0);
|
|
44
|
+
return date;
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
createdAt: Date;
|
|
48
|
+
|
|
49
|
+
@column({
|
|
50
|
+
type: 'datetime', beforeSave() {
|
|
51
|
+
const date = new Date();
|
|
52
|
+
date.setMilliseconds(0);
|
|
53
|
+
return date;
|
|
54
|
+
},
|
|
55
|
+
skipUpdate: true,
|
|
56
|
+
})
|
|
57
|
+
updatedAt: Date;
|
|
58
|
+
|
|
59
|
+
static async createFor(userId: string, purpose: MFATokenPurpose, webauthnChallenge: string | null = null): Promise<MFAToken> {
|
|
60
|
+
// A new attempt supersedes the previous one. Without this, every single login
|
|
61
|
+
// attempt of a user with 2FA leaves a row behind (anyone who knows the password
|
|
62
|
+
// can create them at will), and the abandoned tokens stay usable until they
|
|
63
|
+
// expire. Only one challenge per purpose can be outstanding at a time.
|
|
64
|
+
await this.delete().where('userId', userId).where('purpose', purpose).delete();
|
|
65
|
+
|
|
66
|
+
const model = new MFAToken();
|
|
67
|
+
model.token = crypto.randomBytes(48).toString('base64url');
|
|
68
|
+
model.userId = userId;
|
|
69
|
+
model.purpose = purpose;
|
|
70
|
+
model.webauthnChallenge = webauthnChallenge;
|
|
71
|
+
model.expiresAt = new Date(Date.now() + MFAToken.EXPIRY_MS);
|
|
72
|
+
await model.save();
|
|
73
|
+
return model;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Remove every expired token. Tokens are also dropped when they are looked up after
|
|
78
|
+
* expiring, but a token that is never used again would stay behind forever.
|
|
79
|
+
*/
|
|
80
|
+
static async deleteExpired(): Promise<number> {
|
|
81
|
+
const { affectedRows } = await this.delete().where('expiresAt', SQLWhereSign.Less, new Date()).delete();
|
|
82
|
+
return affectedRows;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
isExpired(): boolean {
|
|
86
|
+
return this.expiresAt < new Date();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Look up a valid, non-expired, non-locked token for the given purpose. Deletes it if
|
|
91
|
+
* expired. Does not consume it (call consume() after a successful verification).
|
|
92
|
+
*/
|
|
93
|
+
static async getValid(token: string, purpose: MFATokenPurpose): Promise<MFAToken | undefined> {
|
|
94
|
+
if (!token) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const model = await this.select().where('token', token).first(false);
|
|
98
|
+
if (!model || model.purpose !== purpose) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
if (model.isExpired()) {
|
|
102
|
+
await model.delete();
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
if (model.tries >= MFAToken.MAX_TRIES) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
return model;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Register a failed attempt. Returns true if the token is now locked out.
|
|
113
|
+
*
|
|
114
|
+
* The counter is incremented in the database rather than read-modify-written, so that
|
|
115
|
+
* guesses sent in parallel each cost a try. Reading the value first would let an
|
|
116
|
+
* attacker fire MAX_TRIES requests at once and have them all write back `1`.
|
|
117
|
+
*/
|
|
118
|
+
async registerFailedAttempt(): Promise<boolean> {
|
|
119
|
+
await MFAToken.update()
|
|
120
|
+
.set('tries', new SQLCalculation(new SQLColumnExpression('tries')).add(new SQLScalar(1)))
|
|
121
|
+
.where('token', this.token)
|
|
122
|
+
.update();
|
|
123
|
+
|
|
124
|
+
const updated = await MFAToken.select().where('token', this.token).first(false);
|
|
125
|
+
this.tries = updated?.tries ?? this.tries + 1;
|
|
126
|
+
return this.tries >= MFAToken.MAX_TRIES;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Claim this token. Returns false when another request already consumed it: a single
|
|
131
|
+
* challenge may only ever hand out one session, even when two requests verify the same
|
|
132
|
+
* second factor at the same time.
|
|
133
|
+
*/
|
|
134
|
+
async consume(): Promise<boolean> {
|
|
135
|
+
const { affectedRows } = await MFAToken.delete().where('token', this.token).delete();
|
|
136
|
+
return affectedRows === 1;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Database } from '@simonbackx/simple-database';
|
|
2
|
-
import { AccessRight, PermissionLevel, PermissionRoleDetailed, Permissions } from '@stamhoofd/structures';
|
|
3
2
|
import { EventFactory } from '../factories/EventFactory.js';
|
|
4
3
|
import { OrganizationFactory } from '../factories/OrganizationFactory.js';
|
|
5
|
-
import { UserFactory } from '../factories/UserFactory.js';
|
|
6
4
|
import { Organization } from './Organization.js';
|
|
7
5
|
import { TestUtils } from '@stamhoofd/test-utils';
|
|
8
6
|
|
|
@@ -119,47 +117,3 @@ describe('Organization.updateFutureEventsForOrganizations', () => {
|
|
|
119
117
|
expect(await getHasFutureEvents(organization)).toBe(true);
|
|
120
118
|
});
|
|
121
119
|
});
|
|
122
|
-
|
|
123
|
-
describe('Organization admin recipients', () => {
|
|
124
|
-
async function createOrganizationWithAdmins() {
|
|
125
|
-
const financeRole = PermissionRoleDetailed.create({
|
|
126
|
-
name: 'financial director',
|
|
127
|
-
accessRights: [AccessRight.OrganizationFinanceDirector],
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const organization = await new OrganizationFactory({ roles: [financeRole] }).create();
|
|
131
|
-
|
|
132
|
-
const fullAdmin = await new UserFactory({
|
|
133
|
-
organization,
|
|
134
|
-
email: 'full-admin@example.com',
|
|
135
|
-
permissions: Permissions.create({ level: PermissionLevel.Full }),
|
|
136
|
-
}).create();
|
|
137
|
-
|
|
138
|
-
const financeAdmin = await new UserFactory({
|
|
139
|
-
organization,
|
|
140
|
-
email: 'finance-admin@example.com',
|
|
141
|
-
permissions: Permissions.create({
|
|
142
|
-
level: PermissionLevel.None,
|
|
143
|
-
roles: [financeRole],
|
|
144
|
-
}),
|
|
145
|
-
}).create();
|
|
146
|
-
|
|
147
|
-
return { organization, fullAdmin, financeAdmin };
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
test('The finance admin recipients include a financial director without full access', async () => {
|
|
151
|
-
const { organization, fullAdmin, financeAdmin } = await createOrganizationWithAdmins();
|
|
152
|
-
|
|
153
|
-
const emails = (await organization.getFinanceAdminRecipients()).map(r => r.email);
|
|
154
|
-
|
|
155
|
-
expect(emails).toIncludeSameMembers([fullAdmin.email, financeAdmin.email]);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test('The regular admin recipients only include full admins', async () => {
|
|
159
|
-
const { organization, fullAdmin } = await createOrganizationWithAdmins();
|
|
160
|
-
|
|
161
|
-
const emails = (await organization.getAdminRecipients()).map(r => r.email);
|
|
162
|
-
|
|
163
|
-
expect(emails).toIncludeSameMembers([fullAdmin.email]);
|
|
164
|
-
});
|
|
165
|
-
});
|
|
@@ -352,160 +352,6 @@ export class Organization extends QueryableModel {
|
|
|
352
352
|
});
|
|
353
353
|
}
|
|
354
354
|
|
|
355
|
-
/**
|
|
356
|
-
* E-mail address when we receive replies for organization@stamhoofd.email.
|
|
357
|
-
* Note that this sould be private because it can contain personal e-mail addresses if the organization is not configured correctly
|
|
358
|
-
*/
|
|
359
|
-
async getReplyEmails(): Promise<EmailInterfaceRecipient[]> {
|
|
360
|
-
const sender: OrganizationEmail | undefined = this.privateMeta.emails.find(e => e.default) ?? this.privateMeta.emails[0];
|
|
361
|
-
|
|
362
|
-
if (sender) {
|
|
363
|
-
return [
|
|
364
|
-
{
|
|
365
|
-
name: sender.name,
|
|
366
|
-
email: sender.email,
|
|
367
|
-
},
|
|
368
|
-
];
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
return await this.getAdminToEmails();
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
async getAdmins() {
|
|
375
|
-
// Circular reference fix
|
|
376
|
-
const User = (await import('./User.js')).User;
|
|
377
|
-
return await User.getAdmins(this.id, { verified: true });
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* These email addresess are private
|
|
382
|
-
*/
|
|
383
|
-
async getFullAdmins() {
|
|
384
|
-
const admins = await this.getAdmins();
|
|
385
|
-
const platform = await Platform.getSharedStruct();
|
|
386
|
-
|
|
387
|
-
// Only full access
|
|
388
|
-
return admins.filter(a => a.permissions && a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasFullAccess());
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* These email addresess are private
|
|
393
|
-
*/
|
|
394
|
-
async getFinanceAdmins() {
|
|
395
|
-
const admins = await this.getAdmins();
|
|
396
|
-
const platform = await Platform.getSharedStruct();
|
|
397
|
-
const filtered = admins.filter(a => a.permissions && (a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasFullAccess() || a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasAccessRight(AccessRight.OrganizationFinanceDirector)));
|
|
398
|
-
|
|
399
|
-
// Only full access
|
|
400
|
-
return filtered;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* These email addresess are private
|
|
405
|
-
*/
|
|
406
|
-
async getAdminToEmails(): Promise<EmailInterfaceRecipient[]> {
|
|
407
|
-
const filtered = await this.getFullAdmins();
|
|
408
|
-
|
|
409
|
-
if (STAMHOOFD.environment === 'production') {
|
|
410
|
-
if (filtered.length > 1) {
|
|
411
|
-
// remove stamhoofd email addresses
|
|
412
|
-
const f = filtered.flatMap(f => f.getEmailTo()).filter(e => !e.email.endsWith('@stamhoofd.be') && !e.email.endsWith('@stamhoofd.nl'));
|
|
413
|
-
if (f.length > 0) {
|
|
414
|
-
return f;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
return filtered.flatMap(f => f.getEmailTo());
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
adminsToRecipients(admins: User[]) {
|
|
423
|
-
return admins.flatMap((f) => {
|
|
424
|
-
return Recipient.create({
|
|
425
|
-
firstName: f.firstName,
|
|
426
|
-
lastName: f.lastName,
|
|
427
|
-
email: f.email,
|
|
428
|
-
replacements: [],
|
|
429
|
-
});
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* These email addresess are private
|
|
435
|
-
*/
|
|
436
|
-
async getAdminRecipients(): Promise<Recipient[]> {
|
|
437
|
-
const filtered = await this.getFullAdmins();
|
|
438
|
-
return this.adminsToRecipients(filtered);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* These email addresess are private
|
|
443
|
-
*/
|
|
444
|
-
async getFinanceAdminRecipients(): Promise<Recipient[]> {
|
|
445
|
-
const filtered = await this.getFinanceAdmins();
|
|
446
|
-
return this.adminsToRecipients(filtered);
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
/**
|
|
450
|
-
* These email addresess are private
|
|
451
|
-
*/
|
|
452
|
-
async getInvoicingToEmails() {
|
|
453
|
-
// Circular reference fix
|
|
454
|
-
const filtered = await this.getFinanceAdmins();
|
|
455
|
-
|
|
456
|
-
if (filtered.length > 0) {
|
|
457
|
-
return filtered.flatMap(f => f.getEmailTo()).map((recipient) => {
|
|
458
|
-
if (!recipient.name) {
|
|
459
|
-
return recipient.email;
|
|
460
|
-
}
|
|
461
|
-
const cleanedName = Formatter.emailSenderName(recipient.name);
|
|
462
|
-
if (cleanedName.length < 2) {
|
|
463
|
-
return recipient.email;
|
|
464
|
-
}
|
|
465
|
-
return '"' + cleanedName + '" <' + recipient.email + '>';
|
|
466
|
-
}).join(', ');
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
return undefined;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* Returns one email for invoices. since in ubl we can only add one address.
|
|
474
|
-
* We choose the oldest user that was active in the last 3 months (otherwise the oldest user if noone was active)
|
|
475
|
-
*/
|
|
476
|
-
async getInvoicingToEmail(): Promise<string | undefined> {
|
|
477
|
-
// Circular reference fix
|
|
478
|
-
const admins = await this.getAdmins();
|
|
479
|
-
|
|
480
|
-
const tokens = await Token.select().where('userId', admins.map(a => a.id)).fetch();
|
|
481
|
-
|
|
482
|
-
// Sort by admins that were active in the last 3 months, then creation date
|
|
483
|
-
const cutoffDate = new Date(Date.now() - 1000 * 60 * 60 * 24 * 31 * 3);
|
|
484
|
-
admins.sort((a, b) => {
|
|
485
|
-
const aTokens = tokens.filter(t => t.userId === a.id);
|
|
486
|
-
const bTokens = tokens.filter(t => t.userId === b.id);
|
|
487
|
-
const aActive = !!aTokens.find(t => t.updatedAt > cutoffDate);
|
|
488
|
-
const bActive = !!bTokens.find(t => t.updatedAt > cutoffDate);
|
|
489
|
-
return Sorter.stack(
|
|
490
|
-
Sorter.byBooleanValue(aActive, bActive),
|
|
491
|
-
Sorter.byDateValue(b.createdAt, a.createdAt),
|
|
492
|
-
);
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
const platform = await Platform.getSharedStruct();
|
|
496
|
-
const filtered = admins.filter(a => a.verified && a.permissions && !a.email.endsWith('@stamhoofd.be') && (a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasFullAccess() || a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasAccessRight(AccessRight.OrganizationFinanceDirector)));
|
|
497
|
-
|
|
498
|
-
if (filtered.length > 0) {
|
|
499
|
-
return filtered.map(f => f.email)[0];
|
|
500
|
-
}
|
|
501
|
-
const filtered2 = admins.filter(a => a.verified && a.permissions && (a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasFullAccess() || a.permissions.forOrganization(this, platform, { inheritFromPlatform: false })?.hasAccessRight(AccessRight.OrganizationFinanceDirector)));
|
|
502
|
-
|
|
503
|
-
if (filtered2.length > 0) {
|
|
504
|
-
return filtered2.map(f => f.email)[0];
|
|
505
|
-
}
|
|
506
|
-
return undefined;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
355
|
/**
|
|
510
356
|
* Return default e-mail address if no email addresses are set.
|
|
511
357
|
*/
|