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.Field.Role
|
2016-06-01 16:35:22 +09:00
|
|
|
( newRepoRoleIdentField
|
|
|
|
, newRepoOpField
|
2016-06-01 17:52:14 +09:00
|
|
|
, newProjectRoleIdentField
|
|
|
|
, newProjectOpField
|
2016-05-29 22:17:55 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
-- import Control.Monad (void)
|
|
|
|
-- import Control.Monad.Trans.Maybe
|
|
|
|
-- import Data.Char (isDigit)
|
|
|
|
-- import Data.Maybe (isNothing, isJust)
|
|
|
|
import Data.Text (Text)
|
|
|
|
import Database.Esqueleto
|
|
|
|
import Yesod.Form.Fields (textField, selectField, optionsEnum)
|
|
|
|
import Yesod.Form.Functions (checkM, convertField)
|
|
|
|
import Yesod.Form.Types (Field)
|
|
|
|
import Yesod.Persist.Core (runDB)
|
|
|
|
|
|
|
|
-- import qualified Data.Text as T (null, all, find, split)
|
|
|
|
|
|
|
|
-- import Data.Char.Local (isAsciiLetter)
|
|
|
|
import Vervis.Foundation (Handler, AppDB)
|
|
|
|
import Vervis.Model
|
|
|
|
import Vervis.Model.Ident (RlIdent, rl2text, text2rl)
|
|
|
|
import Vervis.Model.Role
|
|
|
|
|
2016-06-01 17:52:14 +09:00
|
|
|
roleIdentField :: Field Handler RlIdent
|
|
|
|
roleIdentField = convertField text2rl rl2text textField
|
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
newRepoRoleIdentField :: PersonId -> Field Handler RlIdent
|
|
|
|
newRepoRoleIdentField pid = checkUniqueCI pid roleIdentField
|
|
|
|
where
|
|
|
|
checkUniqueCI :: PersonId -> Field Handler RlIdent -> Field Handler RlIdent
|
|
|
|
checkUniqueCI pid = checkM $ \ rl -> do
|
|
|
|
sames <- runDB $ select $ from $ \ role -> do
|
|
|
|
where_ $
|
|
|
|
role ^. RepoRolePerson ==. val pid &&.
|
|
|
|
lower_ (role ^. RepoRoleIdent) ==. lower_ (val rl)
|
|
|
|
limit 1
|
|
|
|
return ()
|
|
|
|
return $ if null sames
|
|
|
|
then Right rl
|
|
|
|
else Left ("This role name is already in use" :: Text)
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
newRepoOpField :: AppDB RepoRoleId -> Field Handler RepoOperation
|
|
|
|
newRepoOpField getrid = checkOpNew getrid opField
|
|
|
|
where
|
|
|
|
opField :: Field Handler RepoOperation
|
|
|
|
opField = selectField optionsEnum
|
2016-05-29 22:17:55 +09:00
|
|
|
|
2016-06-01 16:35:22 +09:00
|
|
|
checkOpNew
|
|
|
|
:: AppDB RepoRoleId
|
|
|
|
-> Field Handler RepoOperation
|
|
|
|
-> Field Handler RepoOperation
|
|
|
|
checkOpNew getrid = checkM $ \ op -> do
|
|
|
|
ma <- runDB $ do
|
|
|
|
rid <- getrid
|
|
|
|
getBy $ UniqueRepoAccess rid op
|
|
|
|
return $ case ma of
|
|
|
|
Nothing -> Right op
|
|
|
|
Just _ -> Left ("Role already has this operation" :: Text)
|
2016-06-01 17:52:14 +09:00
|
|
|
|
|
|
|
newProjectRoleIdentField :: PersonId -> Field Handler RlIdent
|
|
|
|
newProjectRoleIdentField pid = checkUniqueCI pid roleIdentField
|
|
|
|
where
|
|
|
|
checkUniqueCI :: PersonId -> Field Handler RlIdent -> Field Handler RlIdent
|
|
|
|
checkUniqueCI pid = checkM $ \ rl -> do
|
|
|
|
sames <- runDB $ select $ from $ \ role -> do
|
|
|
|
where_ $
|
|
|
|
role ^. ProjectRolePerson ==. val pid &&.
|
|
|
|
lower_ (role ^. ProjectRoleIdent) ==. lower_ (val rl)
|
|
|
|
limit 1
|
|
|
|
return ()
|
|
|
|
return $ if null sames
|
|
|
|
then Right rl
|
|
|
|
else Left ("This role name is already in use" :: Text)
|
|
|
|
|
|
|
|
newProjectOpField :: AppDB ProjectRoleId -> Field Handler ProjectOperation
|
|
|
|
newProjectOpField getrid = checkOpNew getrid opField
|
|
|
|
where
|
|
|
|
opField :: Field Handler ProjectOperation
|
|
|
|
opField = selectField optionsEnum
|
|
|
|
|
|
|
|
checkOpNew
|
|
|
|
:: AppDB ProjectRoleId
|
|
|
|
-> Field Handler ProjectOperation
|
|
|
|
-> Field Handler ProjectOperation
|
|
|
|
checkOpNew getrid = checkM $ \ op -> do
|
|
|
|
ma <- runDB $ do
|
|
|
|
rid <- getrid
|
|
|
|
getBy $ UniqueProjectAccess rid op
|
|
|
|
return $ case ma of
|
|
|
|
Nothing -> Right op
|
|
|
|
Just _ -> Left ("Role already has this operation" :: Text)
|