git-annex/Assistant/WebApp/Configurators/WebDAV.hs

101 lines
3.2 KiB
Haskell
Raw Normal View History

2012-11-17 19:30:11 +00:00
{- git-annex assistant webapp configurators for WebDAV remotes
-
- Copyright 2012, 2013 Joey Hess <id@joeyh.name>
2012-11-17 19:30:11 +00:00
-
- Licensed under the GNU AGPL version 3 or higher.
-}
{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
2012-11-17 19:30:11 +00:00
module Assistant.WebApp.Configurators.WebDAV where
2012-11-25 04:26:46 +00:00
import Assistant.WebApp.Common
2013-04-20 23:25:50 +00:00
import Creds
2012-11-17 19:30:11 +00:00
import qualified Remote.WebDAV as WebDAV
2013-10-28 15:33:14 +00:00
import Assistant.WebApp.MakeRemote
2012-11-17 19:30:11 +00:00
import qualified Remote
import Types.Remote (RemoteConfig, config)
2012-11-17 19:30:11 +00:00
import Types.StandardGroups
import Logs.Remote
import Git.Types (RemoteName)
import Assistant.Gpg
import Types.GitConfig
import Annex.SpecialRemote.Config
import Types.ProposedAccepted
import Utility.Url.Parse
2012-11-17 19:30:11 +00:00
import qualified Data.Map as M
2013-04-20 23:25:50 +00:00
import qualified Data.Text as T
import Network.URI
2012-11-17 19:30:11 +00:00
webDAVConfigurator :: Widget -> Handler Html
webDAVConfigurator = page "Add a WebDAV repository" (Just Configuration)
2012-11-17 19:30:11 +00:00
data WebDAVInput = WebDAVInput
{ user :: Text
, password :: Text
, embedCreds :: Bool
2012-11-17 19:30:11 +00:00
, directory :: Text
, enableEncryption :: EnableEncryption
2012-11-17 19:30:11 +00:00
}
toCredPair :: WebDAVInput -> CredPair
toCredPair input = (T.unpack $ user input, T.unpack $ password input)
2013-06-03 20:33:05 +00:00
webDAVCredsAForm :: Maybe CredPair -> MkAForm WebDAVInput
webDAVCredsAForm defcreds = WebDAVInput
2014-04-18 00:07:09 +00:00
<$> areq textField (bfs "Username or Email") (T.pack . fst <$> defcreds)
<*> areq passwordField (bfs "Password") (T.pack . snd <$> defcreds)
<*> pure False
<*> pure T.empty
<*> pure NoEncryption -- not used!
getEnableWebDAVR :: UUID -> Handler Html
2013-03-16 22:48:23 +00:00
getEnableWebDAVR = postEnableWebDAVR
postEnableWebDAVR :: UUID -> Handler Html
2013-03-16 22:48:23 +00:00
postEnableWebDAVR uuid = do
m <- liftAnnex remoteConfigMap
let c = fromJust $ M.lookup uuid m
let name = fromJust $ lookupName c
let url = fromProposedAccepted $ fromJust $ M.lookup (Accepted "url") c
mcreds <- liftAnnex $ do
dummycfg <- liftIO dummyRemoteGitConfig
pc <- parsedRemoteConfig WebDAV.remote c
2020-01-15 17:47:31 +00:00
getRemoteCredPairFor "webdav" pc dummycfg (WebDAV.davCreds uuid)
case mcreds of
Just creds -> webDAVConfigurator $ liftH $
2013-09-27 04:15:50 +00:00
makeWebDavRemote enableSpecialRemote name creds M.empty
Nothing -> webDAVConfigurator $ showform name url
where
showform name url = do
defcreds <- liftAnnex $
maybe (pure Nothing) previouslyUsedWebDAVCreds $
urlHost url
((result, form), enctype) <- liftH $
2014-04-18 00:07:09 +00:00
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $
webDAVCredsAForm defcreds
case result of
FormSuccess input -> liftH $
2013-09-27 04:15:50 +00:00
makeWebDavRemote enableSpecialRemote name (toCredPair input) M.empty
_ -> do
description <- liftAnnex $
T.pack <$> Remote.prettyUUID uuid
$(widgetFile "configurators/enablewebdav")
2013-09-27 04:15:50 +00:00
makeWebDavRemote :: SpecialRemoteMaker -> RemoteName -> CredPair -> RemoteConfig -> Handler ()
makeWebDavRemote maker name creds c =
setupCloudRemote TransferGroup Nothing $
maker name WebDAV.remote (Just creds) c
{- Only returns creds previously used for the same hostname. -}
previouslyUsedWebDAVCreds :: String -> Annex (Maybe CredPair)
previouslyUsedWebDAVCreds hostname =
previouslyUsedCredPair WebDAV.davCreds WebDAV.remote samehost
where
samehost r = case urlHost =<< WebDAV.configUrl (config r) of
Nothing -> False
Just h -> h == hostname
urlHost :: String -> Maybe String
urlHost url = uriRegName <$> (uriAuthority =<< parseURIPortable url)