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.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.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"]
|
2016-04-12 07:31:03 +09:00
|
|
|
$(widgetFile "key/keys")
|
2016-03-07 09:42:06 +09:00
|
|
|
|
|
|
|
postKeysR :: Text -> Handler Html
|
2016-03-08 11:52:46 +09:00
|
|
|
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."
|
2016-05-14 06:41:46 +09:00
|
|
|
redirect $ KeysR user
|
2016-03-08 11:52:46 +09:00
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
2016-04-12 07:31:03 +09:00
|
|
|
defaultLayout $(widgetFile "key/key-new")
|
2016-03-08 11:52:46 +09:00
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Invalid input, see below"
|
2016-04-12 07:31:03 +09:00
|
|
|
defaultLayout $(widgetFile "key/key-new")
|
2016-03-07 09:42:06 +09:00
|
|
|
|
|
|
|
getKeyNewR :: Text -> Handler Html
|
2016-03-08 11:52:46 +09:00
|
|
|
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"
|
2016-04-12 07:31:03 +09:00
|
|
|
$(widgetFile "key/key-new")
|
2016-03-07 09:42:06 +09:00
|
|
|
|
|
|
|
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]
|
2016-04-12 07:31:03 +09:00
|
|
|
$(widgetFile "key/key")
|
2016-05-14 06:41:46 +09:00
|
|
|
|
|
|
|
deleteKeyR :: Text -> Text -> Handler Html
|
|
|
|
deleteKeyR user tag = do
|
|
|
|
runDB $ do
|
|
|
|
Entity sid _s <- getBy404 $ UniqueSharerIdent user
|
|
|
|
Entity pid _p <- getBy404 $ UniquePersonIdent sid
|
|
|
|
Entity kid _k <- getBy404 $ UniqueSshKey pid tag
|
|
|
|
delete kid
|
|
|
|
setMessage "Key deleted."
|
|
|
|
redirect $ KeysR user
|
|
|
|
|
|
|
|
postKeyR :: Text -> Text -> Handler Html
|
|
|
|
postKeyR user tag = do
|
|
|
|
mmethod <- lookupPostParam "_method"
|
|
|
|
case mmethod of
|
|
|
|
Just "DELETE" -> deleteKeyR user tag
|
|
|
|
_ -> notFound
|