2014-08-12 12:42:25 +00:00
|
|
|
module Handler.Activate where
|
|
|
|
|
2014-08-24 19:42:42 +00:00
|
|
|
import Import as I hiding (returnJson)
|
2014-08-14 00:24:28 +00:00
|
|
|
import Data.Text
|
2014-08-24 19:42:42 +00:00
|
|
|
import Data.Text.Encoding
|
|
|
|
import Data.Maybe
|
2014-08-14 00:24:28 +00:00
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
2014-08-12 12:42:25 +00:00
|
|
|
|
2014-08-12 21:45:33 +00:00
|
|
|
getActivateR :: Text -> Handler Html
|
2014-08-12 12:42:25 +00:00
|
|
|
getActivateR token = do
|
2014-08-24 19:42:42 +00:00
|
|
|
t <- runDB $ selectFirst [ActivatorToken ==. token] []
|
2014-08-12 12:42:25 +00:00
|
|
|
case t of
|
2014-08-13 16:16:31 +00:00
|
|
|
Nothing -> do
|
2014-08-15 13:47:16 +00:00
|
|
|
setMessage "Invalid Token!"
|
2014-08-12 12:42:25 +00:00
|
|
|
redirect $ HomeR
|
2014-08-24 19:42:42 +00:00
|
|
|
Just (Entity activatorKey activator) -> do
|
|
|
|
uSalt <- return $ userSalt $ activatorUser activator
|
|
|
|
mToken <- runDB $ selectFirst [TokenToken ==. (encodeUtf8 token), TokenKind ==. "activate"] []
|
|
|
|
case mToken of
|
|
|
|
Just (Entity uTokenId uToken) -> do
|
|
|
|
hexSalt <- return $ toHex uSalt
|
|
|
|
defaultLayout $ do
|
|
|
|
$(widgetFile "activate")
|
|
|
|
_ -> do
|
|
|
|
returnJsonError "Invalid token!"
|
|
|
|
redirect $ ActivateR token
|
|
|
|
|
|
|
|
postActivateR :: Text -> Handler RepJson
|
|
|
|
postActivateR token = do
|
|
|
|
msalted <- fromJust <$> lookupPostParam "salted"
|
|
|
|
salted <- return $ fromHex' $ unpack msalted
|
|
|
|
mToken <- runDB $ selectFirst [TokenToken ==. (encodeUtf8 token), TokenKind ==. "activate"] []
|
|
|
|
case mToken of
|
|
|
|
Just (Entity uTokenId uToken) -> do
|
|
|
|
newUser <- runDB $ selectFirst [ActivatorToken ==. token] []
|
|
|
|
case newUser of
|
|
|
|
Just (Entity aId activ) -> do
|
|
|
|
-- putting user in active state
|
|
|
|
uId <- runDB $ insert $ activatorUser activ
|
|
|
|
runDB $ update uId [UserSalted =. (Just salted)]
|
|
|
|
-- create user directory
|
|
|
|
liftIO $ createDirectoryIfMissing True $ "static" </> "data" </> (unpack $ extractKey uId)
|
|
|
|
-- cleanup
|
|
|
|
runDB $ delete aId
|
|
|
|
runDB $ delete uTokenId
|
|
|
|
-- login and redirect
|
|
|
|
setSession "userId" (extractKey uId)
|
|
|
|
welcomeLink <- ($ ProfileR uId) <$> getUrlRender
|
|
|
|
returnJson ["welcome" .= welcomeLink]
|
|
|
|
-- redirect $ HomeR
|
|
|
|
_ -> do
|
|
|
|
returnJsonError "Invalid activation token!"
|
|
|
|
|
|
|
|
returnJson :: (Monad m, ToJSON a, a ~ Value) =>
|
|
|
|
[(Text, a)] -> m RepJson
|
|
|
|
returnJson = return . repJson . object
|
|
|
|
|
|
|
|
returnJsonError :: Text -> Handler RepJson
|
|
|
|
returnJsonError = returnJson . (:[]) . ("error" .=)
|