1- import { InjectRedis } from '@nestjs-modules/ioredis' ;
1+ import { InjectRedis } from '@nestjs-modules/ioredis' ;
22import {
33 BadRequestException ,
44 HttpException ,
@@ -8,30 +8,31 @@ import {
88 NotFoundException ,
99} from '@nestjs/common' ;
1010import * as crypto from 'crypto' ;
11- import { randomInt } from 'crypto' ;
11+ import { randomInt } from 'crypto' ;
1212import Redis from 'ioredis' ;
13- import { AbstractService } from '~/_common/abstracts/abstract.service' ;
14- import { ActionType } from '~/core/backends/_enum/action-type.enum' ;
15- import { BackendsService } from '~/core/backends/backends.service' ;
16- import { Jobs } from '~/core/jobs/_schemas/jobs.schema' ;
17- import { AskTokenDto } from './_dto/ask-token.dto' ;
18- import { ChangePasswordDto } from './_dto/change-password.dto' ;
19- import { ResetPasswordDto } from './_dto/reset-password.dto' ;
20- import { IdentitiesCrudService } from '../identities/identities-crud.service' ;
21- import { get } from 'radash' ;
22- import { Identities } from '../identities/_schemas/identities.schema' ;
23- import { MailerService } from '@nestjs-modules/mailer' ;
24- import { InitAccountDto } from '~/management/passwd/_dto/init-account.dto' ;
25- import { ConfigService } from '@nestjs/config' ;
26- import { ResetByCodeDto } from '~/management/passwd/_dto/reset-by-code.dto' ;
27- import { PasswdadmService } from '~/settings/passwdadm.service' ;
28- import { IdentityState } from '~/management/identities/_enums/states.enum' ;
29- import { InitResetDto } from '~/management/passwd/_dto/init-reset.dto' ;
30- import { SmsadmService } from '~/settings/smsadm.service' ;
31- import { InitManyDto } from '~/management/passwd/_dto/init-many.dto' ;
32- import { InitStatesEnum } from '~/management/identities/_enums/init-state.enum' ;
33- import { MailadmService } from '~/settings/mailadm.service' ;
34- import { DataStatusEnum } from "~/management/identities/_enums/data-status" ;
13+ import { AbstractService } from '~/_common/abstracts/abstract.service' ;
14+ import { ActionType } from '~/core/backends/_enum/action-type.enum' ;
15+ import { BackendsService } from '~/core/backends/backends.service' ;
16+ import { Jobs } from '~/core/jobs/_schemas/jobs.schema' ;
17+ import { AskTokenDto } from './_dto/ask-token.dto' ;
18+ import { ChangePasswordDto } from './_dto/change-password.dto' ;
19+ import { ResetPasswordDto } from './_dto/reset-password.dto' ;
20+ import { IdentitiesCrudService } from '../identities/identities-crud.service' ;
21+ import { get } from 'radash' ;
22+ import { Identities } from '../identities/_schemas/identities.schema' ;
23+ import { MailerService } from '@nestjs-modules/mailer' ;
24+ import { InitAccountDto } from '~/management/passwd/_dto/init-account.dto' ;
25+ import { ConfigService } from '@nestjs/config' ;
26+ import { ResetByCodeDto } from '~/management/passwd/_dto/reset-by-code.dto' ;
27+ import { PasswdadmService } from '~/settings/passwdadm.service' ;
28+ import { IdentityState } from '~/management/identities/_enums/states.enum' ;
29+ import { InitResetDto } from '~/management/passwd/_dto/init-reset.dto' ;
30+ import { SmsadmService } from '~/settings/smsadm.service' ;
31+ import { InitManyDto } from '~/management/passwd/_dto/init-many.dto' ;
32+ import { InitStatesEnum } from '~/management/identities/_enums/init-state.enum' ;
33+ import { MailadmService } from '~/settings/mailadm.service' ;
34+ import { DataStatusEnum } from "~/management/identities/_enums/data-status" ;
35+ import { SentMessageInfo } from 'nodemailer' ;
3536
3637interface TokenData {
3738 k : string ;
@@ -63,14 +64,15 @@ export class PasswdService extends AbstractService {
6364 ) {
6465 super ( ) ;
6566 }
67+
6668 //Initialisation du reset de mot de passe envoie un email ou par sms un code et fourni un token au front.
6769 // Le code est la clé du token
6870 public async initReset ( initDto : InitResetDto ) : Promise < any > {
6971 //envoi du mail
7072 try {
7173 const identity = ( await this . identities . findOne ( { 'inetOrgPerson.uid' : initDto . uid } ) ) as Identities ;
7274 //test si on peu reninitialiser le compte
73- if ( identity . dataStatus === DataStatusEnum . INACTIVE || identity . dataStatus === DataStatusEnum . DELETED ) {
75+ if ( identity . dataStatus === DataStatusEnum . INACTIVE || identity . dataStatus === DataStatusEnum . DELETED ) {
7476 throw new BadRequestException (
7577 'Une erreur est survenue : Tentative de réinitialisation de mot de passe impossible' ,
7678 ) ;
@@ -143,67 +145,70 @@ export class PasswdService extends AbstractService {
143145 return falseToken ;
144146 }
145147 }
148+
146149 //Initialisation du compte. Envoi d' un mail avec un token pour l'init du compte
147- public async initAccount ( initDto : InitAccountDto ) : Promise < any > {
148- //recherche de l'identity
150+ public async initAccount ( initDto : InitAccountDto ) : Promise < SentMessageInfo > {
151+ const identity = ( await this . identities . findOne ( { 'inetOrgPerson.uid' : initDto . uid } ) ) as Identities ;
152+ //test si on peu reninitialiser le compte
153+ if ( identity . dataStatus === DataStatusEnum . INACTIVE || identity . dataStatus === DataStatusEnum . DELETED ) {
154+ throw new BadRequestException (
155+ 'Une erreur est survenue : Tentative de réinitialisation de mot de passe impossible' ,
156+ ) ;
157+ }
158+ //envoi du mail
159+ const params = await this . passwdadmService . getPolicies ( ) ;
160+ const mailAttribute = params . emailAttribute ;
161+ this . logger . log ( 'mailer.identityMailAttribute : ' + mailAttribute ) ;
162+
163+ if ( ! mailAttribute ) {
164+ this . logger . error ( 'Error while initAccount identityMailAttribute Empty' ) ;
165+ throw new BadRequestException ( {
166+ message : "Une erreur est survenue : l'attribut de l'adresse mail n'est pas défini" ,
167+ error : 'Bad Request' ,
168+ statusCode : 400 ,
169+ } ) ;
170+ }
171+
172+ const mail = < string > get ( identity . toObject ( ) , mailAttribute ) ;
173+ if ( ! mail ) {
174+ this . logger . error ( 'Error while initAccount identityMailAttribute not defined' ) ;
175+ throw new BadRequestException ( {
176+ message : "Une erreur est survenue : L'identité <" + ( identity . inetOrgPerson ?. cn || identity . _id ) + "> n'a pas d'adresse mail" ,
177+ error : 'Bad Request' ,
178+ statusCode : 400 ,
179+ } ) ;
180+ }
181+
182+ const smtpParams = await this . mailadmService . getParams ( ) ;
183+ //demande du token
184+ const k = crypto . randomBytes ( PasswdService . RANDOM_BYTES_K ) . toString ( 'hex' ) ;
185+ const token = await this . askToken ( { mail : mail , uid : initDto . uid } , k , params . initTokenTTL ) ;
186+ //envoi du token
187+
149188 try {
150- const identity = ( await this . identities . findOne ( { 'inetOrgPerson.uid' : initDto . uid } ) ) as Identities ;
151- //test si on peu reninitialiser le compte
152- if ( identity . dataStatus === DataStatusEnum . INACTIVE || identity . dataStatus === DataStatusEnum . DELETED ) {
153- throw new BadRequestException (
154- 'Une erreur est survenue : Tentative de réinitialisation de mot de passe impossible' ,
155- ) ;
156- }
157- //envoi du mail
158- const params = await this . passwdadmService . getPolicies ( ) ;
159- const mailAttribute = params . emailAttribute ;
160- this . logger . log ( 'mailer.identityMailAttribute : ' + mailAttribute ) ;
161- if ( mailAttribute !== '' ) {
162- const mail = < string > get ( identity . toObject ( ) , mailAttribute ) ;
163- if ( mail ) {
164- const smtpParams = await this . mailadmService . getParams ( ) ;
165- //demande du token
166- const k = crypto . randomBytes ( PasswdService . RANDOM_BYTES_K ) . toString ( 'hex' ) ;
167- const token = await this . askToken ( { mail : mail , uid : initDto . uid } , k , params . initTokenTTL ) ;
168- //envoi du token
169- this . mailer
170- . sendMail ( {
171- from : smtpParams . sender ,
172- to : mail ,
173- subject : 'Activation de votre compte' ,
174- template : 'initaccount' ,
175- context : {
176- displayName : identity . inetOrgPerson . displayName ,
177- uid : initDto . uid ,
178- url : this . config . get ( 'frontPwd.url' ) + '/initaccount/' + token ,
179- mail : identity . inetOrgPerson . mail
180- } ,
181- } )
182- . then ( ( ) => {
183- this . logger . log ( 'Init compte envoyé pour uid' + initDto . uid + ' à ' + mail ) ;
184- this . setInitState ( identity , InitStatesEnum . SENT ) ;
185- } )
186- . catch ( ( e ) => {
187- this . logger . error ( 'Erreur serveur lors de l envoi du mail' + e ) ;
188- throw new BadRequestException ( {
189- message : 'Erreur serveur lors de l envoi du mail' + e ,
190- error : 'Bad Request' ,
191- statusCode : 400 ,
192- } ) ;
193- } ) ;
189+ const send = await this . mailer . sendMail ( {
190+ from : smtpParams . sender ,
191+ to : mail ,
192+ subject : 'Activation de votre compte' ,
193+ template : 'initaccount' ,
194+ context : {
195+ displayName : identity . inetOrgPerson . displayName ,
196+ uid : initDto . uid ,
197+ url : this . config . get ( 'frontPwd.url' ) + '/initaccount/' + token ,
198+ mail : identity . inetOrgPerson . mail
199+ } ,
200+ } )
201+ this . logger . log ( 'Init compte envoyé pour uid ' + initDto . uid + ' à ' + mail ) ;
202+ this . setInitState ( identity , InitStatesEnum . SENT ) ;
194203
195- return true ;
196- } else {
197- this . logger . error ( 'Error while initAccount identityMailAttribute Empty' ) ;
198- return false ;
199- }
200- } else {
201- this . logger . error ( 'Error while initAccount identityMailAttribute not defined' ) ;
202- return false ;
203- }
204+ return send ;
204205 } catch ( e ) {
205- this . logger . error ( 'Error while initialize password. ' + e + ` (uid=${ initDto ?. uid } )` ) ;
206- return false ;
206+ this . logger . error ( 'Error while sending init account email: ' + e ) ;
207+ throw new BadRequestException ( {
208+ message : 'Erreur serveur lors de l envoi du mail' ,
209+ error : 'Bad Request' ,
210+ statusCode : 400 ,
211+ } ) ;
207212 }
208213 }
209214
@@ -214,7 +219,7 @@ export class PasswdService extends AbstractService {
214219 'inetOrgPerson.uid' : passwdDto . uid ,
215220 state : IdentityState . SYNCED ,
216221 } ) ) as Identities ;
217- if ( identity . dataStatus === DataStatusEnum . INACTIVE || identity . dataStatus === DataStatusEnum . DELETED ) {
222+ if ( identity . dataStatus === DataStatusEnum . INACTIVE || identity . dataStatus === DataStatusEnum . DELETED ) {
218223 throw new BadRequestException (
219224 'Une erreur est survenue : Tentative de réinitialisation de mot de passe impossible' ,
220225 ) ;
@@ -246,7 +251,7 @@ export class PasswdService extends AbstractService {
246251 } ,
247252 ) ;
248253 // on met actif l'identité
249- await this . identities . model . updateOne ( { _id :identity . _id } , { dataStatus : DataStatusEnum . ACTIVE } )
254+ await this . identities . model . updateOne ( { _id : identity . _id } , { dataStatus : DataStatusEnum . ACTIVE } )
250255 return result ;
251256 } catch ( e ) {
252257 let job = undefined ;
@@ -446,12 +451,19 @@ export class PasswdService extends AbstractService {
446451 if ( identities . length === 0 ) {
447452 throw new HttpException ( 'Aucune identité trouvée.' , 404 ) ;
448453 }
454+
449455 const updated = await Promise . all (
450- identities . map ( ( identity ) => {
456+ identities . map ( async ( identity ) => {
451457 this . logger . verbose ( 'send To :' + identity . get ( 'inetOrgPerson.uid' ) ) ;
452- return this . initAccount ( { uid : identity . get ( 'inetOrgPerson.uid' ) } ) ;
458+ try {
459+ return await this . initAccount ( { uid : identity . get ( 'inetOrgPerson.uid' ) } ) ;
460+ } catch ( e ) {
461+ this . logger . error ( 'Error while init account for ' + identity . get ( 'inetOrgPerson.uid' ) + ': ' + e ) ;
462+ return null ;
463+ }
453464 } ) ,
454465 ) ;
466+
455467 return updated as any ;
456468 }
457469
0 commit comments