diff options
Diffstat (limited to 'app/Models/User.ts')
-rw-r--r-- | app/Models/User.ts | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/app/Models/User.ts b/app/Models/User.ts new file mode 100644 index 0000000..0ed4627 --- /dev/null +++ b/app/Models/User.ts | |||
@@ -0,0 +1,98 @@ | |||
1 | import { DateTime } from 'luxon'; | ||
2 | import { | ||
3 | BaseModel, | ||
4 | beforeSave, | ||
5 | column, | ||
6 | HasMany, | ||
7 | hasMany, | ||
8 | } from '@ioc:Adonis/Lucid/Orm'; | ||
9 | import Hash from '@ioc:Adonis/Core/Hash'; | ||
10 | import Event from '@ioc:Adonis/Core/Event'; | ||
11 | import moment from 'moment'; | ||
12 | import Encryption from '@ioc:Adonis/Core/Encryption'; | ||
13 | import randtoken from 'rand-token'; | ||
14 | import Token from './Token'; | ||
15 | import Workspace from './Workspace'; | ||
16 | import Service from './Service'; | ||
17 | |||
18 | export default class User extends BaseModel { | ||
19 | @column({ isPrimary: true }) | ||
20 | public id: number; | ||
21 | |||
22 | @column() | ||
23 | public email: string; | ||
24 | |||
25 | @column() | ||
26 | public username: string; | ||
27 | |||
28 | @column() | ||
29 | public password: string; | ||
30 | |||
31 | @column() | ||
32 | public lastname: string; | ||
33 | |||
34 | // TODO: Type the settings object. | ||
35 | @column() | ||
36 | public settings: object; | ||
37 | |||
38 | @column.dateTime({ autoCreate: true }) | ||
39 | public created_at: DateTime; | ||
40 | |||
41 | @column.dateTime({ autoCreate: true, autoUpdate: true }) | ||
42 | public updated_at: DateTime; | ||
43 | |||
44 | @beforeSave() | ||
45 | public static async hashPassword(user: User) { | ||
46 | if (user.$dirty.password) { | ||
47 | user.password = await Hash.make(user.password); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | @hasMany(() => Token, { | ||
52 | foreignKey: 'user_id', | ||
53 | }) | ||
54 | public tokens: HasMany<typeof Token>; | ||
55 | |||
56 | @hasMany(() => Service, { | ||
57 | foreignKey: 'userId', | ||
58 | }) | ||
59 | public services: HasMany<typeof Service>; | ||
60 | |||
61 | @hasMany(() => Workspace, { | ||
62 | foreignKey: 'userId', | ||
63 | }) | ||
64 | public workspaces: HasMany<typeof Workspace>; | ||
65 | |||
66 | public async forgotPassword(): Promise<void> { | ||
67 | const token = await this.generateToken(this, 'forgot_password'); | ||
68 | |||
69 | await Event.emit('forgot:password', { | ||
70 | user: this, | ||
71 | token, | ||
72 | }); | ||
73 | } | ||
74 | |||
75 | private async generateToken(user: User, type: string): Promise<string> { | ||
76 | const query = user | ||
77 | .related('tokens') | ||
78 | .query() | ||
79 | .where('type', type) | ||
80 | .where('is_revoked', false) | ||
81 | .where( | ||
82 | 'updated_at', | ||
83 | '>=', | ||
84 | moment().subtract(24, 'hours').format('YYYY-MM-DD HH:mm:ss'), | ||
85 | ); | ||
86 | |||
87 | const row = await query.first(); | ||
88 | if (row) { | ||
89 | return row.token; | ||
90 | } | ||
91 | |||
92 | const token = Encryption.encrypt(randtoken.generate(16)); | ||
93 | |||
94 | await user.related('tokens').create({ type, token }); | ||
95 | |||
96 | return token; | ||
97 | } | ||
98 | } | ||