eidolon/Handler/Signup.hs

77 lines
2.4 KiB
Haskell
Raw Normal View History

2014-08-12 12:42:25 +00:00
{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Handler.Signup where
import Import as I
import Data.Text as T
2014-08-24 19:42:42 +00:00
import Data.Text.Encoding
import Data.Maybe
2014-08-12 12:42:25 +00:00
getSignupR :: Handler Html
getSignupR = do
master <- getYesod
block <- return $ appSignupBlocked $ appSettings master
2014-12-08 14:47:19 +00:00
case block of
False -> do
defaultLayout $ do
setTitle "Eidolon :: Signup"
$(widgetFile "signup")
True -> do
setMessage "User signup has been disabled"
redirect $ HomeR
2014-08-12 12:42:25 +00:00
postSignupR :: Handler Html
postSignupR = do
master <- getYesod
block <- return $ appSignupBlocked $ appSettings master
2014-12-08 14:47:19 +00:00
case block of
2014-08-24 19:42:42 +00:00
False -> do
2014-12-08 14:47:19 +00:00
mUserName <- lookupPostParam "username"
newUserName <- case validateLen (fromJust mUserName) of
True -> return $ fromJust $ mUserName
False -> do
setMessage "Invalid username"
redirect $ SignupR
mEmail <- lookupPostParam "email"
mTos1 <- lookupPostParam "tos-1"
mTos2 <- lookupPostParam "tos-2"
case (mTos1, mTos2) of
(Just "tos-1", Just "tos-2") ->
return ()
_ -> do
setMessage "You need to agree to our terms."
redirect $ SignupR
-- create user
namesakes <- runDB $ selectList [UserName ==. newUserName] []
case namesakes of
[] -> do
salt <- liftIO generateSalt
newUser <- return $ User newUserName
newUserName
(fromJust mEmail)
salt
""
[]
False
activatorText <- liftIO generateString
2014-12-28 06:12:25 +00:00
_ <- runDB $ insert $ Activator activatorText newUser
_ <- runDB $ insert $ Token (encodeUtf8 activatorText) "activate" Nothing
2014-12-08 14:47:19 +00:00
activateLink <- ($ ActivateR activatorText) <$> getUrlRender
sendMail (userEmail newUser) "Please activate your account!" $
[shamlet|
<h1>Hello #{userSlug newUser} and Welcome to Eidolon!
2014-12-19 21:00:37 +00:00
To complete your signup please activate your account by visiting the following link:
2014-12-08 14:47:19 +00:00
<a href="#{activateLink}">#{activateLink}
|]
setMessage "User pending activation"
redirect $ HomeR
_ -> do
setMessage "This user already exists"
redirect $ SignupR
True -> do
setMessage "User signup is disabled"
2014-08-24 19:42:42 +00:00
redirect $ HomeR
2014-08-12 12:42:25 +00:00
validateLen :: Text -> Bool
validateLen a =
(T.length a) > 3