2016-05-29 22:17:55 +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.Role
|
2016-06-01 16:35:22 +09:00
|
|
|
( getRepoRolesR
|
|
|
|
, postRepoRolesR
|
|
|
|
, getRepoRoleNewR
|
|
|
|
, getRepoRoleR
|
|
|
|
, deleteRepoRoleR
|
|
|
|
, postRepoRoleR
|
|
|
|
, getRepoRoleOpsR
|
|
|
|
, postRepoRoleOpsR
|
|
|
|
, getRepoRoleOpNewR
|
2016-05-29 22:17:55 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Database.Persist
|
|
|
|
import Text.Blaze.Html (Html)
|
|
|
|
import Yesod.Auth (requireAuthId)
|
|
|
|
import Yesod.Core (defaultLayout, setMessage)
|
|
|
|
import Yesod.Core.Handler (lookupPostParam, notFound, redirect)
|
|
|
|
import Yesod.Form.Functions (runFormPost)
|
|
|
|
import Yesod.Form.Types (FormResult (..))
|
|
|
|
import Yesod.Persist.Core (runDB, getBy404)
|
|
|
|
|
|
|
|
import Vervis.Form.Role
|
|
|
|
import Vervis.Foundation
|
|
|
|
import Vervis.Model
|
|
|
|
import Vervis.Model.Ident (RlIdent, rl2text)
|
|
|
|
import Vervis.Settings (widgetFile)
|
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
getRepoRolesR :: Handler Html
|
|
|
|
getRepoRolesR = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-01 16:35:22 +09:00
|
|
|
roles <- runDB $ selectList [RepoRolePerson ==. pid] []
|
|
|
|
defaultLayout $(widgetFile "repo/role/list")
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
postRepoRolesR :: Handler Html
|
|
|
|
postRepoRolesR = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-01 16:35:22 +09:00
|
|
|
((result, widget), enctype) <- runFormPost $ newRepoRoleForm pid
|
2016-05-29 22:17:55 +09:00
|
|
|
case result of
|
2016-06-01 16:35:22 +09:00
|
|
|
FormSuccess nrr -> do
|
2016-05-29 22:17:55 +09:00
|
|
|
runDB $ do
|
2016-06-01 16:35:22 +09:00
|
|
|
let role = RepoRole
|
|
|
|
{ repoRoleIdent = nrrIdent nrr
|
|
|
|
, repoRolePerson = pid
|
|
|
|
, repoRoleDesc = nrrDesc nrr
|
2016-05-29 22:17:55 +09:00
|
|
|
}
|
|
|
|
insert_ role
|
2016-06-01 16:35:22 +09:00
|
|
|
redirect $ RepoRolesR
|
2016-05-29 22:17:55 +09:00
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
2016-06-01 16:35:22 +09:00
|
|
|
defaultLayout $(widgetFile "repo/role/new")
|
2016-05-29 22:17:55 +09:00
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Invalid input, see errors below"
|
2016-06-01 16:35:22 +09:00
|
|
|
defaultLayout $(widgetFile "repo/role/new")
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
getRepoRoleNewR :: Handler Html
|
|
|
|
getRepoRoleNewR = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-01 16:35:22 +09:00
|
|
|
((_result, widget), enctype) <- runFormPost $ newRepoRoleForm pid
|
|
|
|
defaultLayout $(widgetFile "repo/role/new")
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
getRepoRoleR :: RlIdent -> Handler Html
|
|
|
|
getRepoRoleR rl = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-01 16:35:22 +09:00
|
|
|
Entity _rid role <- runDB $ getBy404 $ UniqueRepoRole pid rl
|
|
|
|
defaultLayout $(widgetFile "repo/role/one")
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
deleteRepoRoleR :: RlIdent -> Handler Html
|
|
|
|
deleteRepoRoleR rl = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
|
|
|
runDB $ do
|
2016-06-01 16:35:22 +09:00
|
|
|
Entity rid _r <- getBy404 $ UniqueRepoRole pid rl
|
2016-05-29 22:17:55 +09:00
|
|
|
delete rid
|
|
|
|
setMessage "Role deleted."
|
2016-06-01 16:35:22 +09:00
|
|
|
redirect RepoRolesR
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
postRepoRoleR :: RlIdent -> Handler Html
|
|
|
|
postRepoRoleR rl = do
|
2016-05-29 22:17:55 +09:00
|
|
|
mmethod <- lookupPostParam "_method"
|
|
|
|
case mmethod of
|
2016-06-01 16:35:22 +09:00
|
|
|
Just "DELETE" -> deleteRepoRoleR rl
|
2016-05-29 22:17:55 +09:00
|
|
|
_ -> notFound
|
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
getRepoRoleOpsR :: RlIdent -> Handler Html
|
|
|
|
getRepoRoleOpsR rl = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
|
|
|
ops <- runDB $ do
|
2016-06-01 16:35:22 +09:00
|
|
|
Entity rid _r <- getBy404 $ UniqueRepoRole pid rl
|
|
|
|
as <- selectList [RepoAccessRole ==. rid] []
|
|
|
|
return $ map (repoAccessOp . entityVal) as
|
|
|
|
defaultLayout $(widgetFile "repo/role/op/list")
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
postRepoRoleOpsR :: RlIdent -> Handler Html
|
|
|
|
postRepoRoleOpsR rl = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-01 16:35:22 +09:00
|
|
|
let getrid = fmap entityKey $ getBy404 $ UniqueRepoRole pid rl
|
|
|
|
((result, widget), enctype) <- runFormPost $ newRepoRoleOpForm getrid
|
2016-05-29 22:17:55 +09:00
|
|
|
case result of
|
|
|
|
FormSuccess op -> do
|
|
|
|
runDB $ do
|
|
|
|
rid <- getrid
|
2016-06-01 16:35:22 +09:00
|
|
|
let access = RepoAccess
|
|
|
|
{ repoAccessRole = rid
|
|
|
|
, repoAccessOp = op
|
2016-05-29 22:17:55 +09:00
|
|
|
}
|
|
|
|
insert_ access
|
2016-06-01 16:35:22 +09:00
|
|
|
redirect $ RepoRoleOpsR rl
|
2016-05-29 22:17:55 +09:00
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
2016-06-01 16:35:22 +09:00
|
|
|
defaultLayout $(widgetFile "repo/role/op/new")
|
2016-05-29 22:17:55 +09:00
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Invalid input, see errors below"
|
2016-06-01 16:35:22 +09:00
|
|
|
defaultLayout $(widgetFile "repo/role/op/new")
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
getRepoRoleOpNewR :: RlIdent -> Handler Html
|
|
|
|
getRepoRoleOpNewR rl = do
|
2016-05-29 22:17:55 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-01 16:35:22 +09:00
|
|
|
let getrid = fmap entityKey $ getBy404 $ UniqueRepoRole pid rl
|
|
|
|
((_result, widget), enctype) <- runFormPost $ newRepoRoleOpForm getrid
|
|
|
|
defaultLayout $(widgetFile "repo/role/op/new")
|