{- This file is part of Vervis. - - Written in 2016 by fr33domlover . - - ♡ Copying is an act of love. Please copy, reuse and share. - - The author(s) have dedicated all copyright and related and neighboring - rights to this software to the public domain worldwide. This software is - distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along - with this software. If not, see - . -} module Vervis.Handler.Key ( getKeysR , postKeysR , getKeyNewR , getKeyR ) where import Prelude import Data.ByteString.Base64 (encode) import Data.Monoid ((<>)) import Data.Text (Text, intercalate) import Data.Text.Encoding (decodeUtf8With) import Data.Text.Encoding.Error (lenientDecode) import Database.Persist import Text.Blaze.Html (Html, toHtml) import Yesod.Core (defaultLayout) import Yesod.Core.Handler (setMessage, redirectUltDest) import Yesod.Core.Widget (setTitle) import Yesod.Form.Functions (runFormPost) import Yesod.Form.Types (FormResult (..)) import Yesod.Persist.Core (runDB, getBy404) import Vervis.Form.Key import Vervis.Foundation import Vervis.Model import Vervis.Settings getKeysR :: Text -> Handler Html getKeysR user = do keys <- runDB $ do Entity sid _sharer <- getBy404 $ UniqueSharerIdent user Entity pid _person <- getBy404 $ UniquePersonIdent sid ks <- selectList [SshKeyPerson ==. pid] [Asc SshKeyName] return $ map (\ (Entity _ k) -> sshKeyName k) ks defaultLayout $ do setTitle $ toHtml $ intercalate " > " ["Vervis", "People", user, "Keys"] $(widgetFile "keys") postKeysR :: Text -> Handler Html postKeysR user = do pid <- runDB $ do Entity s _sharer <- getBy404 $ UniqueSharerIdent user Entity p _person <- getBy404 $ UniquePersonIdent s return p ((result, widget), enctype) <- runFormPost $ newKeyForm pid case result of FormSuccess key -> do runDB $ insert_ key setMessage "Key added." redirectUltDest HomeR FormMissing -> do setMessage "Field(s) missing" defaultLayout $(widgetFile "key-new") FormFailure _l -> do setMessage "Invalid input, see below" defaultLayout $(widgetFile "key-new") getKeyNewR :: Text -> Handler Html getKeyNewR user = do pid <- runDB $ do Entity s _sharer <- getBy404 $ UniqueSharerIdent user Entity p _person <- getBy404 $ UniquePersonIdent s return p ((_result, widget), enctype) <- runFormPost $ newKeyForm pid defaultLayout $ do setTitle $ toHtml $ "Vervis > People > " <> user <> " > New Key" $(widgetFile "key-new") getKeyR :: Text -> Text -> Handler Html getKeyR user tag = do Entity _kid key <- runDB $ do Entity sid _sharer <- getBy404 $ UniqueSharerIdent user Entity pid _person <- getBy404 $ UniquePersonIdent sid getBy404 $ UniqueSshKey pid tag let toText = decodeUtf8With lenientDecode content = toText $ encode $ sshKeyContent key defaultLayout $ do setTitle $ toHtml $ intercalate " > " ["Vervis", "People", user, "Keys", tag] $(widgetFile "key")