package Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . controller Ċ Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . domain . User Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . dto . User Dto Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . generic . Invalid Credentials Exception Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . generic . ResponseError Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . generic . User NotFoundException Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . generic . Utils . Companion . INVALID _ CRED ENTIAL S Ċ import Ġnl . orange fl aming o . vo orn amen in lied jes backend admin . repository . UserRepository Ċ import Ġorg . slf 4 j . LoggerFactory Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . security . access . pre post . Pre Authorize Ċ import Ġorg . springframework . security . crypto . password . Password Encoder Ċ import Ġorg . springframework . web . bind . annotation .* Ċ import Ġjava . util .* Ċ Ċ @ RestController Ċ @ RequestMapping ("/ api ") Ċ class ĠUser Controller Ġ{ ĊĠĠĠ Ġprivate Ġval Ġlog Ġ= ĠLoggerFactory . getLogger ( UserController :: class . java ) ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġuser Repository : ĠUser Repository ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġpassword Encoder : ĠPassword Encoder ĊĊĠĠĠ Ġ@ Pre Authorize (" has Role (' ROLE _ OWNER ') ") ĊĠĠĠ Ġ@ GetMapping ("/ users ") ĊĠĠĠ Ġ@ Cross Origin ( orig ins Ġ= Ġ[" http :// localhost : 3 0 0 0 ", Ġ" https :// vo orn amen in lied jes . nl ", Ġ"* "]) ĊĠĠĠ Ġfun Ġget Users (): ĠList < User Dto > Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . info (" Request ing Ġall Ġusers ") ĊĠĠĠĠĠĠĠ Ġreturn Ġuser Repository . findAll (). map Ġ{ ĠconvertTo Dto ( it ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ PostMapping ("/ authenticate ") ĊĠĠĠ Ġ@ Cross Origin ( orig ins Ġ= Ġ[" http :// localhost : 3 0 0 0 ", Ġ" https :// vo orn amen in lied jes . nl ", Ġ"* "]) ĊĠĠĠ Ġfun Ġauthenticate (@ RequestBody Ġuser : ĠUser Dto ): ĠResponseEntity < User Dto > Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . info (" Auth entic ating Ġuser Ġ${ user . username }") ĊĠĠĠĠĠĠĠ Ġval Ġdb User Ġ= Ġuser Repository . findBy Username ( username Ġ= Ġuser . username ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠInvalid Credentials Exception ( INVALID _ CRED ENTIAL S ) ĊĠĠĠĠĠĠĠ Ġif Ġ(! password Encoder . matches ( user . password , Ġdb User . password )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠInvalid Credentials Exception ( INVALID _ CRED ENTIAL S ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . of ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠOptional . of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠUser Dto ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġid Ġ= Ġdb User . id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġusername Ġ= Ġdb User . username , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġpassword Ġ= Ġuser . password , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġroles Ġ= Ġdb User . roles ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ExceptionHandler ( Invalid Credentials Exception :: class ) ĊĠĠĠ Ġfun Ġhandle Invalid Credentials Exception ( exception : ĠInvalid Credentials Exception ): ĠResponseEntity < ResponseError > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( ResponseError ( exception . message Ġ?: Ġ" On be ken de Ġfout "), ĠHttpStatus . UN AUTHORIZ ED ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ExceptionHandler ( User NotFoundException :: class ) ĊĠĠĠ Ġfun Ġhandle User NotFoundException ( exception : ĠUser NotFoundException ): ĠResponseEntity < ResponseError > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( ResponseError ( exception . message Ġ?: Ġ" On be ken de Ġfout "), ĠHttpStatus . BAD _ REQUEST ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Pre Authorize (" has Role (' ROLE _ OWNER ') ") ĊĠĠĠ Ġ@ GetMapping ("/ users /{ id }") ĊĠĠĠ Ġ@ Cross Origin ( orig ins Ġ= Ġ[" http :// localhost : 3 0 0 0 ", Ġ" https :// vo orn amen in lied jes . nl ", Ġ"* "]) ĊĠĠĠ Ġfun ĠgetUser ById (@ PathVariable (" id ") Ġid : ĠString ): ĠUser Dto Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . info (" Request ing Ġuser Ġwith Ġid Ġ$ id ") ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= Ġuser Repository . findById ( id ). orElse Throw Ġ{ ĠUser NotFoundException (" User Ġwith Ġ$ id Ġnot Ġfound ") Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠconvertTo Dto ( user ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Pre Authorize (" has Role (' ROLE _ OWNER ') ") ĊĠĠĠ Ġ@ PostMapping ("/ users ") ĊĠĠĠ Ġ@ Cross Origin ( orig ins Ġ= Ġ[" http :// localhost : 3 0 0 0 ", Ġ" https :// vo orn amen in lied jes . nl ", Ġ"* "]) ĊĠĠĠ Ġfun Ġnew User (@ RequestBody Ġnew User : ĠUser Dto ): ĠUser Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . info (" Request ing Ġcreation Ġof Ġnew Ġuser Ġ${ new User . username }") ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= Ġconvert ( new User ) ĊĠĠĠĠĠĠĠ Ġreturn Ġuser Repository . save ( user ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Pre Authorize (" has Role (' ROLE _ OWNER ') ") ĊĠĠĠ Ġ@ Delete Mapping ("/ users /{ userId }") ĊĠĠĠ Ġ@ Cross Origin ( orig ins Ġ= Ġ[" http :// localhost : 3 0 0 0 ", Ġ" https :// vo orn amen in lied jes . nl ", Ġ"* "]) ĊĠĠĠ Ġfun Ġdelete User (@ PathVariable ĠuserId : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġlog . info (" Deleting Ġuser Ġwith Ġid Ġ$ userId ") ĊĠĠĠĠĠĠĠ Ġuser Repository . delete ById ( userId ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġconvert ( user Dto : ĠUser Dto ): ĠUser Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠUser ( user Dto . id , Ġuser Dto . username , Ġpassword Encoder . encode ( user Dto . password ), Ġuser Dto . roles ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠconvertTo Dto ( user : ĠUser ): ĠUser Dto Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠUser Dto ( user . id , Ġuser . username , Ġuser . password , Ġuser . roles ) ĊĠĠĠ Ġ} Ċ } Ċ