1
0
Fork 0
mirror of https://code.naskya.net/repos/ndqEd synced 2025-01-10 21:56:46 +09:00

New YAML setting: Optional user limit

This commit is contained in:
fr33domlover 2016-07-27 21:46:48 +00:00
parent e642914d2a
commit ddd4393825
3 changed files with 47 additions and 30 deletions

View file

@ -42,3 +42,4 @@ repo-dir: repos
ssh-port: 5022 ssh-port: 5022
ssh-key-file: config/ssh-host-key ssh-key-file: config/ssh-host-key
registration: false registration: false
max-accounts: 3

View file

@ -24,7 +24,7 @@ where
import Vervis.Import hiding ((==.)) import Vervis.Import hiding ((==.))
--import Prelude --import Prelude
import Database.Esqueleto hiding (isNothing) import Database.Esqueleto hiding (isNothing, count)
import Vervis.Form.Person import Vervis.Form.Person
--import Model --import Model
import Text.Blaze.Html (toHtml) import Text.Blaze.Html (toHtml)
@ -45,8 +45,15 @@ getPeopleR = do
-- | Create new user -- | Create new user
postPeopleR :: Handler Html postPeopleR :: Handler Html
postPeopleR = do postPeopleR = do
regEnabled <- getsYesod $ appRegister . appSettings settings <- getsYesod appSettings
if regEnabled if appRegister settings
then do
room <- case appAccounts settings of
Nothing -> return True
Just cap -> do
current <- runDB $ count ([] :: [Filter Person])
return $ current < cap
if room
then do then do
((result, widget), enctype) <- runFormPost newPersonForm ((result, widget), enctype) <- runFormPost newPersonForm
case result of case result of
@ -72,9 +79,15 @@ postPeopleR = do
setMessage "Field(s) missing" setMessage "Field(s) missing"
defaultLayout $(widgetFile "person-new") defaultLayout $(widgetFile "person-new")
FormFailure _l -> do FormFailure _l -> do
setMessage "User registration failed, see errors below" setMessage
"User registration failed, see errors below"
defaultLayout $(widgetFile "person-new") defaultLayout $(widgetFile "person-new")
else notFound else do
setMessage "Maximal number of registered users reached"
redirect PeopleR
else do
setMessage "User registration disabled"
redirect PeopleR
getPersonNewR :: Handler Html getPersonNewR :: Handler Html
getPersonNewR = do getPersonNewR = do

View file

@ -77,6 +77,8 @@ data AppSettings = AppSettings
, appSshKeyFile :: FilePath , appSshKeyFile :: FilePath
-- | Whether new user accounts can be created. -- | Whether new user accounts can be created.
, appRegister :: Bool , appRegister :: Bool
-- | The maximal number of user accounts that can be registered.
, appAccounts :: Maybe Int
} }
instance FromJSON AppSettings where instance FromJSON AppSettings where
@ -104,6 +106,7 @@ instance FromJSON AppSettings where
appSshPort <- o .: "ssh-port" appSshPort <- o .: "ssh-port"
appSshKeyFile <- o .: "ssh-key-file" appSshKeyFile <- o .: "ssh-key-file"
appRegister <- o .: "registration" appRegister <- o .: "registration"
appAccounts <- o .: "max-accounts"
return AppSettings {..} return AppSettings {..}