@solidstarters/solid-core 1.2.53 → 1.2.54

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solidstarters/solid-core",
3
- "version": "1.2.53",
3
+ "version": "1.2.54",
4
4
  "description": "This module is a NestJS module containing all the required core providers required by a Solid application",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -39,6 +39,7 @@ import {
39
39
  RegistrationValidationSource,
40
40
  TransactionalRegistrationValidationSource
41
41
  } from "../constants";
42
+ import { CreateUserDto } from 'src/dtos/create-user.dto';
42
43
 
43
44
  enum LoginProvider {
44
45
  LOCAL = 'local',
@@ -112,55 +113,15 @@ export class AuthenticationService {
112
113
  }
113
114
 
114
115
  try {
115
- const user = new User();
116
- user.username = signUpDto.username;
117
- user.email = signUpDto.email;
118
- user.fullName = signUpDto.fullName;
119
- if (signUpDto.mobile) {
120
- user.mobile = signUpDto.mobile;
121
- }
122
- // If password has been specified by the user, then we simply create & activate the user based on the configuration parameter "activateUserOnRegistration".
123
- let pwd = '';
124
- let autoGeneratedPwd = '';
125
- if (signUpDto.password) {
126
- pwd = await this.hashingService.hash(signUpDto.password);
127
- }
128
- else {
129
- // TODO: If password is not specified then auto-generate a random password, trigger this password over an email to the user.
130
- // TODO: Also track if the user has to force reset / change their password, and then activate the user.
131
- autoGeneratedPwd = this.generatePassword();
132
- pwd = await this.hashingService.hash(autoGeneratedPwd);
133
- user.forcePasswordChange = true;
134
- }
135
- user.password = pwd;
136
- user.active = this.iamConfiguration.activateUserOnRegistration;
116
+ var { user, pwd, autoGeneratedPwd } = await this.populateForSignup(new User(), signUpDto, this.iamConfiguration.activateUserOnRegistration);
137
117
  const savedUser = await this.userRepository.save(user);
138
118
 
139
119
  // Also assign a default role to the newly created user.
140
- let roles = [];
120
+ const userRoles = signUpDto.roles ?? [];
141
121
  if (this.iamConfiguration.defaultRole) {
142
- roles.push(this.iamConfiguration.defaultRole);
143
- }
144
- // Default Internal user role assigned
145
- roles.push("Internal User");
146
- if (signUpDto.roles) {
147
- roles = [...roles, ...signUpDto.roles];
148
- }
149
- roles = Array.from(new Set([...roles]));
150
- if (roles.length > 0) {
151
- this.userService.addRolesToUser(user.username, roles);
152
- }
153
- // Tanay: Adding user password to history table
154
- const userPasswordHistory = new UserPasswordHistory();
155
- userPasswordHistory.passwordHash = pwd;
156
- userPasswordHistory.user = savedUser;
157
- await this.userPasswordHistoryRepository.save(userPasswordHistory);
158
-
159
- // if forcePasswordChange is true, then we trigger an email to the user to change the password, this needs to be done using a queue.
160
- // Create a new method like notifyUserOnForcePasswordChange, create a new email template we can call it on-force-password-change this template to include the random password
161
- if (user.forcePasswordChange && autoGeneratedPwd) {
162
- this.notifyUserOnForcePasswordChange(user, autoGeneratedPwd);
122
+ userRoles.push(this.iamConfiguration.defaultRole);
163
123
  }
124
+ await this.handlePostSignup(savedUser, userRoles, pwd, autoGeneratedPwd);
164
125
 
165
126
  // TODO: make provision to trigger a welcome email also.
166
127
 
@@ -174,6 +135,79 @@ export class AuthenticationService {
174
135
  }
175
136
  }
176
137
 
138
+ async signupForExtensionUser<T extends User, U extends CreateUserDto>(signUpDto: SignUpDto, extensionUserDto: U, extensionUserRepo: Repository<T>): Promise<T> {
139
+ try {
140
+ // Merge the extended signUpDto attributes into the user entity
141
+ //@ts-ignore
142
+ const extensionUser = extensionUserRepo.merge(extensionUserRepo.create() as T, extensionUserDto);
143
+ var { user, pwd, autoGeneratedPwd } = await this.populateForSignup<T>(extensionUser, signUpDto);
144
+ const savedUser = await extensionUserRepo.save(user);
145
+
146
+ await this.handlePostSignup(savedUser, signUpDto.roles, pwd, autoGeneratedPwd);
147
+
148
+ return savedUser;
149
+ }
150
+ catch (err) {
151
+ const pgUniqueViolationErrorCode = '23505';
152
+ if (err.code === pgUniqueViolationErrorCode) {
153
+ throw new ConflictException();
154
+ }
155
+ throw err;
156
+ }
157
+ }
158
+
159
+
160
+ private async populateForSignup<T extends User>(user: T, signUpDto: SignUpDto, isUserActive: boolean = true) {
161
+ // const user = new User();
162
+ user.username = signUpDto.username;
163
+ user.email = signUpDto.email;
164
+ user.fullName = signUpDto.fullName;
165
+ if (signUpDto.mobile) {
166
+ user.mobile = signUpDto.mobile;
167
+ }
168
+ // If password has been specified by the user, then we simply create & activate the user based on the configuration parameter "activateUserOnRegistration".
169
+ let pwd = '';
170
+ let autoGeneratedPwd = '';
171
+ if (signUpDto.password) {
172
+ pwd = await this.hashingService.hash(signUpDto.password);
173
+ }
174
+ else {
175
+ // TODO: If password is not specified then auto-generate a random password, trigger this password over an email to the user.
176
+ // TODO: Also track if the user has to force reset / change their password, and then activate the user.
177
+ autoGeneratedPwd = this.generatePassword();
178
+ pwd = await this.hashingService.hash(autoGeneratedPwd);
179
+ user.forcePasswordChange = true;
180
+ }
181
+ user.password = pwd;
182
+ // user.active = this.iamConfiguration.activateUserOnRegistration;
183
+ user.active = isUserActive;
184
+ return { user, pwd, autoGeneratedPwd };
185
+ }
186
+
187
+ private async handlePostSignup(user: User, roles: string[]=[], pwd: string, autoGeneratedPwd: string) {
188
+ let userRoles = [];
189
+ // Default Internal user role assigned
190
+ userRoles.push("Internal User");
191
+ if (roles) {
192
+ userRoles = [...userRoles, ...roles];
193
+ }
194
+ userRoles = Array.from(new Set([...userRoles]));
195
+ if (userRoles.length > 0) {
196
+ this.userService.addRolesToUser(user.username, userRoles);
197
+ }
198
+ // Tanay: Adding user password to history table
199
+ const userPasswordHistory = new UserPasswordHistory();
200
+ userPasswordHistory.passwordHash = pwd;
201
+ userPasswordHistory.user = user;
202
+ await this.userPasswordHistoryRepository.save(userPasswordHistory);
203
+
204
+ // if forcePasswordChange is true, then we trigger an email to the user to change the password, this needs to be done using a queue.
205
+ // Create a new method like notifyUserOnForcePasswordChange, create a new email template we can call it on-force-password-change this template to include the random password
206
+ if (user.forcePasswordChange && autoGeneratedPwd) {
207
+ this.notifyUserOnForcePasswordChange(user, autoGeneratedPwd);
208
+ }
209
+ }
210
+
177
211
  generatePassword(length: number = 8): string {
178
212
  const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
179
213
  const lowerCase = "abcdefghijklmnopqrstuvwxyz";