1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2025-01-10 03:26:47 +09:00
vervis/src/Vervis/Handler/Key.hs

102 lines
2.9 KiB
Haskell
Raw Normal View History

2016-03-07 09:42:06 +09:00
{- This file is part of Vervis.
-
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- 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
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Vervis.Handler.Key
( getKeysR
, postKeysR
, getKeyNewR
, getKeyR
2016-05-14 06:41:46 +09:00
, deleteKeyR
, postKeyR
2016-03-07 09:42:06 +09:00
)
where
import Prelude
import Data.ByteString.Base64 (encode)
2016-03-08 11:52:46 +09:00
import Data.Monoid ((<>))
2016-03-07 09:42:06 +09:00
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.Auth (requireAuthId)
2016-03-07 09:42:06 +09:00
import Yesod.Core (defaultLayout)
2016-05-14 06:41:46 +09:00
import Yesod.Core.Handler
2016-03-07 09:42:06 +09:00
import Yesod.Core.Widget (setTitle)
2016-03-08 11:52:46 +09:00
import Yesod.Form.Functions (runFormPost)
import Yesod.Form.Types (FormResult (..))
2016-03-07 09:42:06 +09:00
import Yesod.Persist.Core (runDB, getBy404)
2016-03-08 11:52:46 +09:00
import Vervis.Form.Key
2016-03-07 09:42:06 +09:00
import Vervis.Foundation
import Vervis.Model
import Vervis.Model.Ident
2016-03-07 09:42:06 +09:00
import Vervis.Settings
getKeysR :: Handler Html
getKeysR = do
pid <- requireAuthId
2016-03-07 09:42:06 +09:00
keys <- runDB $ do
ks <- selectList [SshKeyPerson ==. pid] [Asc SshKeyIdent]
return $ map (\ (Entity _ k) -> sshKeyIdent k) ks
defaultLayout $(widgetFile "key/list")
2016-03-07 09:42:06 +09:00
postKeysR :: Handler Html
postKeysR = do
pid <- requireAuthId
2016-03-08 11:52:46 +09:00
((result, widget), enctype) <- runFormPost $ newKeyForm pid
case result of
FormSuccess key -> do
runDB $ insert_ key
setMessage "Key added."
redirect KeysR
2016-03-08 11:52:46 +09:00
FormMissing -> do
setMessage "Field(s) missing"
defaultLayout $(widgetFile "key/new")
2016-03-08 11:52:46 +09:00
FormFailure _l -> do
setMessage "Invalid input, see below"
defaultLayout $(widgetFile "key/new")
2016-03-07 09:42:06 +09:00
getKeyNewR :: Handler Html
getKeyNewR = do
pid <- requireAuthId
2016-03-08 11:52:46 +09:00
((_result, widget), enctype) <- runFormPost $ newKeyForm pid
defaultLayout $(widgetFile "key/new")
2016-03-07 09:42:06 +09:00
getKeyR :: KyIdent -> Handler Html
getKeyR tag = do
pid <- requireAuthId
Entity _kid key <- runDB $ getBy404 $ UniqueSshKey pid tag
2016-03-07 09:42:06 +09:00
let toText = decodeUtf8With lenientDecode
content = toText $ encode $ sshKeyContent key
defaultLayout $(widgetFile "key/one")
2016-05-14 06:41:46 +09:00
deleteKeyR :: KyIdent -> Handler Html
deleteKeyR tag = do
pid <- requireAuthId
2016-05-14 06:41:46 +09:00
runDB $ do
Entity kid _k <- getBy404 $ UniqueSshKey pid tag
delete kid
setMessage "Key deleted."
redirect KeysR
2016-05-14 06:41:46 +09:00
postKeyR :: KyIdent -> Handler Html
postKeyR tag = do
2016-05-14 06:41:46 +09:00
mmethod <- lookupPostParam "_method"
case mmethod of
Just "DELETE" -> deleteKeyR tag
2016-05-14 06:41:46 +09:00
_ -> notFound