From 6f89c0bcb9f4981b26cd2dd01425a8aea0051dae Mon Sep 17 00:00:00 2001 From: nek0 Date: Thu, 4 Sep 2014 03:57:01 +0200 Subject: [PATCH] password change succesful --- Application.hs | 1 + Handler/Activate.hs | 49 ++++++++++++++++++++++------------ Handler/Reactivate.hs | 52 +++++++++++++++++++++++++++++++++++++ config/routes | 1 + eidolon.cabal | 1 + templates/reactivate.hamlet | 8 ++++++ 6 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 Handler/Reactivate.hs create mode 100644 templates/reactivate.hamlet diff --git a/Application.hs b/Application.hs index 300a238..6385ec3 100644 --- a/Application.hs +++ b/Application.hs @@ -38,6 +38,7 @@ import Handler.Album import Handler.Medium import Handler.AlbumSettings import Handler.MediumSettings +import Handler.Reactivate -- This line actually creates our YesodDispatch instance. It is the second half -- of the call to mkYesodData which occurs in Foundation.hs. Please see the diff --git a/Handler/Activate.hs b/Handler/Activate.hs index f343bc6..5f31037 100644 --- a/Handler/Activate.hs +++ b/Handler/Activate.hs @@ -12,8 +12,16 @@ getActivateR token = do t <- runDB $ selectFirst [ActivatorToken ==. token] [] case t of Nothing -> do - setMessage "Invalid Token!" - redirect $ HomeR + mToken <- runDB $ selectFirst [TokenToken ==. (encodeUtf8 token), TokenKind ==. "activate"] [] + case mToken of + Just (Entity uTokenId uToken) -> do + user <- runDB $ getJust (fromJust $ tokenUser uToken) + hexSalt <- return $ toHex $ userSalt user + defaultLayout $ do + $(widgetFile "activate") + _ -> do + setMessage "Invalid token!" + redirect $ HomeR Just (Entity activatorKey activator) -> do uSalt <- return $ userSalt $ activatorUser activator mToken <- runDB $ selectFirst [TokenToken ==. (encodeUtf8 token), TokenKind ==. "activate"] [] @@ -33,23 +41,32 @@ postActivateR token = do 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 =. salted] - -- create user directory - liftIO $ createDirectoryIfMissing True $ "static" "data" (unpack $ extractKey uId) + case tokenUser uToken == Nothing of + True -> 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 =. 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] + Nothing -> do + returnJsonError "Invalid token" + False -> do + runDB $ update (fromJust $ tokenUser uToken) [UserSalted =. salted] -- cleanup - runDB $ delete aId runDB $ delete uTokenId - -- login and redirect - setSession "userId" (extractKey uId) - welcomeLink <- ($ ProfileR uId) <$> getUrlRender + setSession "userId" (extractKey $ fromJust $ tokenUser uToken) + welcomeLink <- ($ ProfileR (fromJust $ tokenUser uToken)) <$> getUrlRender returnJson ["welcome" .= welcomeLink] - Nothing -> do - returnJsonError "Invalid token" _ -> do returnJsonError "Invalid activation token!" diff --git a/Handler/Reactivate.hs b/Handler/Reactivate.hs new file mode 100644 index 0000000..8ddd7ab --- /dev/null +++ b/Handler/Reactivate.hs @@ -0,0 +1,52 @@ +module Handler.Reactivate where + +import Import +import Control.Monad +import Data.Text.Encoding + +getReactivateR :: Handler Html +getReactivateR = do + (reactivateWidget, enctype) <- generateFormPost reactivateForm + defaultLayout $ do + $(widgetFile "reactivate") + +postReactivateR :: Handler Html +postReactivateR = do + ((result, reactivateWidget), enctype) <- runFormPost reactivateForm + case result of + FormSuccess temp -> do + tempUsers <- runDB $ selectList [UserEmail ==. temp] [] + case tempUsers of + users -> do + userTokens <- foldM (\userTokens (Entity userId user) -> do + token <- liftIO $ generateString +-- aId <- runDB $ insert $ Activator token user +-- runDB $ delete userId + tId <- runDB $ insert $ Token (encodeUtf8 token) "activate" (Just userId) + return $ (user, token) : userTokens + ) [] users + sent <- foldM (\sent (user, token) -> + case sent of + False -> + return False + True -> do + activateLink <- ($ ActivateR token) <$> getUrlRender + sendMail (userEmail user) "Reset your password" $ + [shamlet| +

Welcome again to Eidolon + To reset your password visit the following link: + #{activateLink} + + See you soon! + |] + return True + ) True userTokens + setMessage "Your new passwort will arrive in your e-mail" + redirect $ HomeR + [] -> do + setMessage "No user mith this Email" + redirect $ LoginR + +reactivateForm :: Form Text +reactivateForm = renderDivs $ (\a -> a) + <$> areq emailField "Email" Nothing diff --git a/config/routes b/config/routes index 3b42dde..cb93b4c 100644 --- a/config/routes +++ b/config/routes @@ -19,3 +19,4 @@ /album/#AlbumId/delete AlbumDeleteR GET POST /medium/#MediumId/settings MediumSettingsR GET POST /medium/#MediumId/delete MediumDeleteR GET POST +/reactivate ReactivateR GET POST diff --git a/eidolon.cabal b/eidolon.cabal index 2e9cff9..61c8aaa 100644 --- a/eidolon.cabal +++ b/eidolon.cabal @@ -31,6 +31,7 @@ library Handler.Medium Handler.AlbumSettings Handler.MediumSettings + Handler.Reactivate if flag(dev) || flag(library-only) cpp-options: -DDEVELOPMENT diff --git a/templates/reactivate.hamlet b/templates/reactivate.hamlet new file mode 100644 index 0000000..39402ae --- /dev/null +++ b/templates/reactivate.hamlet @@ -0,0 +1,8 @@ +$newline always + +

Reset Password + +
+ ^{reactivateWidget} +
+