-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgewis.ts
149 lines (136 loc) · 5.03 KB
/
gewis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of GEWIS.
*
* @module GEWIS
* @mergeTarget
*/
import User, { UserType } from '../entity/user/user';
import GewisUser from './entity/gewis-user';
import AuthenticationService from '../service/authentication-service';
import { asNumber } from '../helpers/validators';
import { bindUser, LDAPUser } from '../helpers/ad';
import GewiswebToken from './gewisweb-token';
import { parseRawUserToResponse, RawUser } from '../helpers/revision-to-response';
import Bindings from '../helpers/bindings';
import { GewisUserResponse } from './controller/response/gewis-user-response';
import { AppDataSource } from '../database/database';
import WithManager from '../database/with-manager';
export interface RawGewisUser extends RawUser {
gewisId: number
}
/**
* The GEWIS-specific module with definitions and helper functions.
*/
export default class Gewis extends WithManager {
/**
* This function creates a new user if needed and binds it to a GEWIS number and AD account.
* @param ADUser
*/
public async findOrCreateGEWISUserAndBind(ADUser: LDAPUser): Promise<User> {
// The employeeNumber is the leading truth for m-number.
if (!ADUser.mNumber) return undefined;
let gewisUser;
try {
const gewisId = asNumber(ADUser.mNumber);
// Check if GEWIS User already exists.
gewisUser = await GewisUser.findOne({ where: { gewisId }, relations: ['user'] });
if (gewisUser) {
// If user exists we only have to bind the AD user
await bindUser(this.manager, ADUser, gewisUser.user);
} else {
// If m-account does not exist we create an account and bind it.
const u = await new AuthenticationService(this.manager).createUserAndBind(ADUser);
gewisUser = await this.createGEWISUser(u, gewisId);
}
} catch (error) {
return undefined;
}
return gewisUser.user;
}
/**
* Function that creates a SudoSOS user based on the payload provided by the GEWIS Web token.
* @param token
*/
public async createUserFromWeb(token: GewiswebToken): Promise<GewisUser> {
const user = Object.assign(new User(), {
firstName: token.given_name,
lastName: (token.middle_name.length > 0 ? `${token.middle_name} ` : '') + token.family_name,
type: UserType.MEMBER,
active: true,
email: token.email,
ofAge: token.is_18_plus,
canGoIntoDebt: true,
} as User) as User;
const u = await this.manager.save(user);
return this.createGEWISUser(u, token.lidnr);
}
/**
* Parses a raw User DB object to a UserResponse
* @param user - User to parse
* @param timestamps - Boolean if createdAt and UpdatedAt should be included
*/
public static parseRawUserToGewisResponse(user: RawGewisUser, timestamps = false)
: GewisUserResponse {
if (!user) return undefined;
return {
...parseRawUserToResponse(user, timestamps),
gewisId: user.gewisId,
};
}
public static getUserBuilder() {
return AppDataSource.createQueryBuilder()
.from(User, 'user')
.leftJoin(GewisUser, 'gewis_user', 'userId = id')
.orderBy('userId', 'ASC');
}
/**
* Function that turns a local User into a GEWIS User.
* @param user - The local user
* @param gewisId - GEWIS member ID of the user
*/
public async createGEWISUser(user: User, gewisId: number): Promise<GewisUser> {
const gewisUser = Object.assign(new GewisUser(), {
user,
gewisId,
});
await this.manager.save(gewisUser);
// 09-08-2022 (Roy): code block below (temporarily) disabled, because the huge amount of queries
// in this chain makes the request too slow for the test suite
//
// // This would be the place to make a PIN Code and mail it to the user.
// // This is not meant for production code
// await AuthenticationService
// .setUserAuthenticationHash<PinAuthenticator>(user, gewisId.toString(), PinAuthenticator);
return gewisUser;
}
// eslint-disable-next-line class-methods-use-this
static overwriteBindings() {
Bindings.ldapUserCreation = () => {
const service = new Gewis();
return service.findOrCreateGEWISUserAndBind.bind(service);
};
Bindings.Users = {
parseToResponse: Gewis.parseRawUserToGewisResponse,
getBuilder: Gewis.getUserBuilder,
};
}
}