diff --git a/Handler/Admin.hs b/Handler/Admin.hs index 9b88255..9c4c32f 100644 --- a/Handler/Admin.hs +++ b/Handler/Admin.hs @@ -1,7 +1,7 @@ module Handler.Admin where import Import -import Handler.Commons +import Helper getAdminR :: Handler Html getAdminR = do diff --git a/Handler/AdminAlbumSettings.hs b/Handler/AdminAlbumSettings.hs index 54afd53..3783223 100644 --- a/Handler/AdminAlbumSettings.hs +++ b/Handler/AdminAlbumSettings.hs @@ -1,7 +1,7 @@ module Handler.AdminAlbumSettings where import Import -import Handler.Commons +import Helper import qualified Data.Text as T import qualified Data.List as L import System.FilePath diff --git a/Handler/AdminComments.hs b/Handler/AdminComments.hs index fc61479..2ea31d2 100644 --- a/Handler/AdminComments.hs +++ b/Handler/AdminComments.hs @@ -1,7 +1,7 @@ module Handler.AdminComments where import Import -import Handler.Commons +import Helper import Data.Maybe import Data.Time import System.Locale diff --git a/Handler/AdminMediumSettings.hs b/Handler/AdminMediumSettings.hs index d42da76..0aea192 100644 --- a/Handler/AdminMediumSettings.hs +++ b/Handler/AdminMediumSettings.hs @@ -1,7 +1,7 @@ module Handler.AdminMediumSettings where import Import -import Handler.Commons +import Helper import System.FilePath import System.Directory import Data.List (tail) diff --git a/Handler/AdminProfileSettings.hs b/Handler/AdminProfileSettings.hs index 1100fa4..614697a 100644 --- a/Handler/AdminProfileSettings.hs +++ b/Handler/AdminProfileSettings.hs @@ -1,7 +1,7 @@ module Handler.AdminProfileSettings where import Import -import Handler.Commons +import Helper import qualified Data.Text as T import qualified Data.List as L import Data.Maybe diff --git a/Handler/Commons.hs b/Handler/Commons.hs deleted file mode 100644 index 3540c89..0000000 --- a/Handler/Commons.hs +++ /dev/null @@ -1,40 +0,0 @@ -module Handler.Commons where - -import Import -import Yesod -import Helper -import Data.String - -loginIsAdmin :: IsString t => Handler (Either (t, Route App) ()) -loginIsAdmin = do - msu <- lookupSession "userId" - case msu of - Just tempUserId -> do - userId <- return $ getUserIdFromText tempUserId - user <- runDB $ getJust userId - case userAdmin user of - True -> - return $ Right () - False -> - return $ Left ("You have no admin rights", HomeR) - Nothing -> - 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) diff --git a/Handler/ProfileDelete.hs b/Handler/ProfileDelete.hs index bc3903e..ce6b116 100644 --- a/Handler/ProfileDelete.hs +++ b/Handler/ProfileDelete.hs @@ -1,7 +1,7 @@ module Handler.ProfileDelete where import Import -import Handler.Commons +import Helper import qualified Data.Text as T import Data.Maybe import qualified Data.List as L diff --git a/Handler/ProfileSettings.hs b/Handler/ProfileSettings.hs index db4be5c..72dec0d 100644 --- a/Handler/ProfileSettings.hs +++ b/Handler/ProfileSettings.hs @@ -1,7 +1,7 @@ module Handler.ProfileSettings where import Import -import Handler.Commons +import Helper getProfileSettingsR :: UserId -> Handler Html getProfileSettingsR userId = do diff --git a/Helper.hs b/Helper.hs index 03f0332..8c820c7 100644 --- a/Helper.hs +++ b/Helper.hs @@ -1,24 +1,4 @@ -module Helper - ( getUserIdFromText - , extractKey - , fromHex - , fromHex' - , toHex - , makeRandomToken - , generateSalt - , tagField - , userField - , sendMail - , generateString - , removeItem - , acceptedTypes - , iso8601 - , localTimeToZonedTime - , rfc822 - , mediumStaticImageRoute - , mediumStaticThumbRoute - ) -where +module Helper where import Prelude import Yesod.Static @@ -185,3 +165,37 @@ mediumStaticImageRoute medium = mediumStaticThumbRoute :: Medium -> Route Static mediumStaticThumbRoute medium = StaticRoute (drop 2 $ T.splitOn "/" $ T.pack $ mediumThumb medium) [] + +loginIsAdmin :: IsString t => Handler (Either (t, Route App) ()) +loginIsAdmin = do + msu <- lookupSession "userId" + case msu of + Just tempUserId -> do + userId <- return $ getUserIdFromText tempUserId + user <- runDB $ getJust userId + case userAdmin user of + True -> + return $ Right () + False -> + return $ Left ("You have no admin rights", HomeR) + Nothing -> + 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)