11{-# LANGUAGE  DeriveDataTypeable, TypeFamilies, TemplateHaskell, RankNTypes,
2-     NamedFieldPuns, RecordWildCards, RecursiveDo, BangPatterns #-}
2+     NamedFieldPuns, RecordWildCards, RecursiveDo, BangPatterns, OverloadedStrings  #-}
33module  Distribution.Server.Features.UserDetails  (
44    initUserDetailsFeature ,
55    UserDetailsFeature (.. ),
@@ -11,11 +11,14 @@ module Distribution.Server.Features.UserDetails (
1111import  Distribution.Server.Framework 
1212import  Distribution.Server.Framework.BackupDump 
1313import  Distribution.Server.Framework.BackupRestore 
14+ import  Distribution.Server.Framework.Templating 
1415
1516import  Distribution.Server.Features.Users 
17+ import  Distribution.Server.Features.Upload 
1618import  Distribution.Server.Features.Core 
1719
1820import  Distribution.Server.Users.Types 
21+ import  Distribution.Server.Util.Validators  (guardValidLookingEmail , guardValidLookingName )
1922
2023import  Data.SafeCopy  (base , deriveSafeCopy )
2124
@@ -250,23 +253,31 @@ userDetailsToCSV backuptype (UserDetailsTable tbl)
250253initUserDetailsFeature  ::  ServerEnv 
251254                       ->  IO UserFeature 
252255                           ->  CoreFeature 
256+                            ->  UploadFeature 
253257                           ->  IO UserDetailsFeature )
254- initUserDetailsFeature ServerEnv {serverStateDir} =  do 
258+ initUserDetailsFeature ServerEnv {serverStateDir, serverTemplatesDir, serverTemplatesMode } =  do 
255259    --  Canonical state
256260    usersDetailsState <-  userDetailsStateComponent serverStateDir
257261
258262    -- TODO: link up to user feature to delete
259263
260-     return  $  \ users core ->  do 
261-       let  feature =  userDetailsFeature usersDetailsState users core
264+     templates <- 
265+       loadTemplates serverTemplatesMode
266+       [serverTemplatesDir, serverTemplatesDir </>  " UserDetails" 
267+       [ " user-details-form.html" 
268+ 
269+     return  $  \ users core upload ->  do 
270+       let  feature =  userDetailsFeature templates usersDetailsState users core upload
262271      return  feature
263272
264273
265- userDetailsFeature  ::  StateComponent  AcidState  UserDetailsTable 
274+ userDetailsFeature  ::  Templates 
275+                    ->  StateComponent  AcidState  UserDetailsTable 
266276                   ->  UserFeature 
267277                   ->  CoreFeature 
278+                    ->  UploadFeature 
268279                   ->  UserDetailsFeature 
269- userDetailsFeature userDetailsState UserFeature {.. } CoreFeature {.. }
280+ userDetailsFeature templates  userDetailsState UserFeature {.. } CoreFeature {.. }  UploadFeature {uploadersGroup }
270281  =  UserDetailsFeature  {.. }
271282
272283  where 
@@ -286,7 +297,9 @@ userDetailsFeature userDetailsState UserFeature{..} CoreFeature{..}
286297                         , (PUT ,    " set the name and contact details of a user account" 
287298                         , (DELETE , " delete the name and contact details of a user account" 
288299                         ]
289-       , resourceGet    =  [ (" json" 
300+       , resourceGet    =  [ (" json" 
301+                          , (" html" 
302+                          ]
290303      , resourcePut    =  [ (" json" 
291304      , resourceDelete =  [ (" " 
292305      }
@@ -314,6 +327,30 @@ userDetailsFeature userDetailsState UserFeature{..} CoreFeature{..}
314327
315328    --  Request handlers
316329    -- 
330+     handlerGetUserNameContactHtml  ::  DynamicPath  ->  ServerPartE  Response 
331+     handlerGetUserNameContactHtml dpath =  do 
332+       (uid, uinfo) <-  lookupUserNameFull =<<  userNameInPath dpath
333+       template <-  getTemplate templates " user-details-form.html" 
334+       udetails <-  queryUserDetails uid
335+       showConfirmationOfSave <-  not  .  null  <$>  queryString (lookBSs " showConfirmationOfSave" 
336+       let 
337+         emailTxt =  maybe  " " 
338+         nameTxt  =  maybe  " " 
339+       cacheControl
340+         [Private ]
341+         (etagFromHash
342+           ( emailTxt
343+           , nameTxt
344+           , showConfirmationOfSave
345+           )
346+         )
347+       ok .  toResponse $ 
348+         template
349+           [ " username" $=  display (userName uinfo)
350+           , " contactEmailAddress" $=  emailTxt
351+           , " name" $=  nameTxt
352+           , " showConfirmationOfSave" $=  showConfirmationOfSave
353+           ]
317354
318355    handlerGetUserNameContact  ::  DynamicPath  ->  ServerPartE  Response 
319356    handlerGetUserNameContact dpath =  do 
@@ -333,7 +370,10 @@ userDetailsFeature userDetailsState UserFeature{..} CoreFeature{..}
333370    handlerPutUserNameContact dpath =  do 
334371        uid <-  lookupUserName =<<  userNameInPath dpath
335372        guardAuthorised_ [IsUserId  uid, InGroup  adminGroup]
373+         void $  guardAuthorisedWhenInAnyGroup [uploadersGroup, adminGroup]
336374        NameAndContact  name email <-  expectAesonContent
375+         guardValidLookingName name
376+         guardValidLookingEmail email
337377        updateState userDetailsState (SetUserNameContact  uid name email)
338378        noContent $  toResponse () 
339379
0 commit comments