2024-09-29 13:56:38 +02:00
|
|
|
import { Reception } from './classes.reception.js';
|
|
|
|
|
import { User } from './classes.user.js';
|
2024-10-01 13:49:18 +02:00
|
|
|
import * as plugins from '../plugins.js';
|
2024-09-29 13:56:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* a user manager
|
|
|
|
|
*/
|
|
|
|
|
export class UserManager {
|
|
|
|
|
// refs
|
|
|
|
|
public receptionRef: Reception;
|
|
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
|
|
|
|
public get db() {
|
|
|
|
|
return this.receptionRef.db.smartdataDb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// classes
|
|
|
|
|
public CUser = plugins.smartdata.setDefaultManagerForDoc(this, User);
|
|
|
|
|
|
|
|
|
|
constructor(receptionRefArg: Reception) {
|
|
|
|
|
this.receptionRef = receptionRefArg;
|
|
|
|
|
this.receptionRef.typedrouter.addTypedRouter(this.typedrouter);
|
|
|
|
|
this.typedrouter.addTypedHandler<plugins.lointReception.request.IReq_GetRolesAndOrganizationsForUserId>(
|
|
|
|
|
new plugins.typedrequest.TypedHandler('getRolesAndOrganizationsForUserId', async reqArg => {
|
2024-10-06 23:56:03 +02:00
|
|
|
console.log('user manager: getting roles and orgs');
|
2024-09-29 13:56:38 +02:00
|
|
|
const user = await this.getUserByJwtValidation(reqArg.jwt);
|
|
|
|
|
const organizations = await this.receptionRef.organizationmanager.getAllOrganizationsForUser(
|
|
|
|
|
user
|
|
|
|
|
);
|
|
|
|
|
const roles = await this.receptionRef.roleManager.getAllRolesForUser(user);
|
|
|
|
|
return {
|
|
|
|
|
organizations,
|
|
|
|
|
roles
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gets the user by validating a JWT
|
|
|
|
|
*/
|
|
|
|
|
public async getUserByJwt(jwtString: string) {
|
|
|
|
|
const jwtInstance = await this.receptionRef.jwtManager.verifyJWTAndGetData(jwtString);
|
|
|
|
|
const user = await this.CUser.getInstance({
|
|
|
|
|
id: jwtInstance.data.userId
|
|
|
|
|
});
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* just validate jwt
|
|
|
|
|
* faster than the "getUserByJwt"
|
|
|
|
|
*/
|
|
|
|
|
public async getUserByJwtValidation(jwtStringArg: string) {
|
|
|
|
|
const jwtDataArg: plugins.lointReception.data.IJwt = await this.receptionRef.jwtManager.smartjwtInstance.verifyJWTAndGetData(jwtStringArg);
|
|
|
|
|
const resultingUser = await this.CUser.getInstance({
|
|
|
|
|
id: jwtDataArg.data.userId
|
|
|
|
|
});
|
|
|
|
|
return resultingUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|