refactor for multiple public keys

This commit is contained in:
Filip Gralinski 2015-11-11 22:37:25 +01:00
parent d69e953c35
commit 5bca45faf3
3 changed files with 15 additions and 5 deletions

View File

@ -175,7 +175,6 @@ instance YesodAuth App where
, userName = Nothing
, userIsAdmin = False
, userLocalId = Nothing
, userSshPubKey = Nothing
}
-- You can add other plugins like BrowserID, email or OAuth here

View File

@ -13,7 +13,9 @@ getYourAccountR :: Handler Html
getYourAccountR = do
userId <- requireAuthId
user <- runDB $ get404 userId
(formWidget, formEnctype) <- generateFormPost (yourAccountForm (userName user) (userLocalId user) (userSshPubKey user))
keyS <- runDB $ selectFirst [PublicKeyUser ==. userId] []
let key = publicKeyPubkey <$> entityVal <$> keyS
(formWidget, formEnctype) <- generateFormPost (yourAccountForm (userName user) (userLocalId user) key)
let submission = Nothing :: Maybe (Import.FileInfo, Text)
handlerName = "getYourAccountR" :: Text
defaultLayout $ do
@ -75,8 +77,15 @@ updateLocalIdAndPubKey userId (Just localId) maybeSshPubKey = do
case userLocalId user of
Just prevLocalId -> do
unless (prevLocalId == localId) $ setMessage $ toHtml ("only the administrator can change your ID" :: Text)
Nothing -> do
runDB $ update userId [UserLocalId =. (Just localId), UserSshPubKey =. maybeSshPubKey]
Nothing -> return ()
runDB $ deleteWhere [PublicKeyUser ==. userId]
case maybeSshPubKey of
Just key -> do
runDB $ insert $ PublicKey {
publicKeyUser=userId,
publicKeyPubkey=key }
return ()
Nothing -> return ()
else
setMessage $ toHtml ("unexpected ID (use only lower-case letters, digits and hyphens, start with a letter)" :: Text)

View File

@ -5,8 +5,10 @@ User
name Text Maybe
isAdmin Bool default=True
localId Text Maybe
sshPubKey Text Maybe
deriving Typeable
PublicKey
user UserId
pubkey Text
Email
email Text
user UserId Maybe