condensing code for profile settings

This commit is contained in:
nek0 2014-12-28 00:01:40 +01:00
parent 4b81a54503
commit aaaedd4f15
3 changed files with 91 additions and 121 deletions

View file

@ -2,6 +2,7 @@ module Handler.Commons where
import Import import Import
import Yesod import Yesod
import Helper
import Data.String import Data.String
loginIsAdmin :: IsString t => Handler (Either (t, Route App) ()) loginIsAdmin :: IsString t => Handler (Either (t, Route App) ())
@ -18,3 +19,22 @@ loginIsAdmin = do
return $ Left ("You have no admin rights", HomeR) return $ Left ("You have no admin rights", HomeR)
Nothing -> Nothing ->
return $ Left ("You are not logged in", LoginR) return $ Left ("You are not logged in", LoginR)
profileCheck :: IsString t => UserId -> Handler (Either (t, Route App) User)
profileCheck userId = do
tempUser <- runDB $ get userId
case tempUser of
Just user -> do
msu <- lookupSession "userId"
case msu of
Just tempLoginId -> do
loginId <- return $ getUserIdFromText tempLoginId
case loginId == userId of
True ->
return $ Right user
False ->
return $ Left ("You can only change your own profile settings", UserR $ userName user)
Nothing ->
return $ Left ("You nedd to be logged in to change settings", LoginR)
Nothing ->
return $ Left ("This user does not exist", HomeR)

View file

@ -1,6 +1,7 @@
module Handler.ProfileDelete where module Handler.ProfileDelete where
import Import import Import
import Handler.Commons
import qualified Data.Text as T import qualified Data.Text as T
import Data.Maybe import Data.Maybe
import qualified Data.List as L import qualified Data.List as L
@ -9,41 +10,21 @@ import System.FilePath
getProfileDeleteR :: UserId -> Handler Html getProfileDeleteR :: UserId -> Handler Html
getProfileDeleteR userId = do getProfileDeleteR userId = do
tempUser <- runDB $ get userId checkRes <- profileCheck userId
case tempUser of case checkRes of
Just user -> do Right user -> do
username <- return $ userName user
msu <- lookupSession "userId"
case msu of
Just tempLoginId -> do
loginId <- return $ getUserIdFromText tempLoginId
case loginId == userId of
True -> do
defaultLayout $ do defaultLayout $ do
setTitle "Eidolon :: Delete user profile" setTitle "Eidolon :: Delete user profile"
$(widgetFile "profileDelete") $(widgetFile "profileDelete")
False -> do Left (errorMsg, route) -> do
setMessage "You can only delete your own profile" setMessage errorMsg
redirect $ UserR username redirect $ route
Nothing -> do
setMessage "You must be logged in to delete profiles"
redirect $ LoginR
Nothing -> do
setMessage "This user does not exist"
redirect $ HomeR
postProfileDeleteR :: UserId -> Handler Html postProfileDeleteR :: UserId -> Handler Html
postProfileDeleteR userId = do postProfileDeleteR userId = do
tempUser <- runDB $ get userId checkRes <- profileCheck userId
case tempUser of case checkRes of
Just user -> do Right user -> do
username <- return $ userName user
msu <- lookupSession "userId"
case msu of
Just tempLoginId -> do
loginId <- return $ getUserIdFromText tempLoginId
case loginId == userId of
True -> do
confirm <- lookupPostParam "confirm" confirm <- lookupPostParam "confirm"
case confirm of case confirm of
Just "confirm" -> do Just "confirm" -> do
@ -69,12 +50,6 @@ postProfileDeleteR userId = do
_ -> do _ -> do
setMessage "You must confirm the deletion" setMessage "You must confirm the deletion"
redirect $ ProfileSettingsR userId redirect $ ProfileSettingsR userId
False -> do Left (errorMsg, route) -> do
setMessage "You can only delete your own profile" setMessage errorMsg
redirect $ UserR username redirect $ route
Nothing -> do
setMessage "You must be logged in to delete profiles"
redirect $ LoginR
Nothing -> do
setMessage "This user does not exist"
redirect $ HomeR

View file

@ -1,45 +1,26 @@
module Handler.ProfileSettings where module Handler.ProfileSettings where
import Import import Import
import Handler.Commons
getProfileSettingsR :: UserId -> Handler Html getProfileSettingsR :: UserId -> Handler Html
getProfileSettingsR userId = do getProfileSettingsR userId = do
tempUser <- runDB $ get userId checkRes <- profileCheck userId
case tempUser of case checkRes of
Just user -> do Right user -> do
username <- return $ userName user
msu <- lookupSession "userId"
case msu of
Just tempLoginId -> do
loginId <- return $ getUserIdFromText tempLoginId
case loginId == userId of
True -> do
(profileSettingsWidget, enctype) <- generateFormPost $ profileSettingsForm user (profileSettingsWidget, enctype) <- generateFormPost $ profileSettingsForm user
defaultLayout $ do defaultLayout $ do
setTitle "Eidolon :: Profile settings" setTitle "Eidolon :: Profile settings"
$(widgetFile "profileSettings") $(widgetFile "profileSettings")
False -> do Left (errorMsg, route) -> do
setMessage "You can only change your own profile settings" setMessage errorMsg
redirect $ UserR username redirect $ route
Nothing -> do
setMessage "You need to be logged in to change settings"
redirect $ LoginR
Nothing -> do
setMessage "This user does not exist"
redirect $ HomeR
postProfileSettingsR :: UserId -> Handler Html postProfileSettingsR :: UserId -> Handler Html
postProfileSettingsR userId = do postProfileSettingsR userId = do
tempUser <- runDB $ get userId checkRes <- profileCheck userId
case tempUser of case checkRes of
Just user -> do Right user -> do
username <- return $ userName user
msu <- lookupSession "userId"
case msu of
Just tempLoginId -> do
loginId <- return $ getUserIdFromText tempLoginId
case loginId == userId of
True -> do
((result, profileSettingsWidget), enctype) <- runFormPost $ profileSettingsForm user ((result, profileSettingsWidget), enctype) <- runFormPost $ profileSettingsForm user
case result of case result of
FormSuccess temp -> do FormSuccess temp -> do
@ -49,19 +30,13 @@ postProfileSettingsR userId = do
, UserEmail =. (userEmail temp) , UserEmail =. (userEmail temp)
] ]
setMessage "Profile settings changed successfully" setMessage "Profile settings changed successfully"
redirect $ UserR username redirect $ UserR $ userName user
_ -> do _ -> do
setMessage "There was an error changing your settings" setMessage "There was an error changing your settings"
redirect $ ProfileSettingsR userId redirect $ ProfileSettingsR userId
False -> do Left (errorMsg, route) -> do
setMessage "You can only change your own profile settings" setMessage errorMsg
redirect $ UserR username redirect $ route
Nothing -> do
setMessage "You need to be logged in to change settings"
redirect $ LoginR
Nothing -> do
setMessage "This user does not exist"
redirect $ HomeR
profileSettingsForm :: User -> Form User profileSettingsForm :: User -> Form User