From fef737f957a64388167eef0df6ba35121eab7b66 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sun, 28 Dec 2014 01:08:35 +0100 Subject: [PATCH] helper cannot handle commons. --- Application.hs | 1 - Handler/Admin.hs | 2 +- Handler/AdminAlbumSettings.hs | 2 +- Handler/AdminComments.hs | 2 +- Handler/AdminMediumSettings.hs | 2 +- Handler/AdminProfileSettings.hs | 2 +- Handler/Commons.hs | 63 +++++++++++++++++++++++++++++++++ Handler/ProfileDelete.hs | 2 +- Handler/ProfileSettings.hs | 2 +- Helper.hs | 34 ------------------ 10 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 Handler/Commons.hs diff --git a/Application.hs b/Application.hs index 8b707e3..fc5a340 100644 --- a/Application.hs +++ b/Application.hs @@ -60,7 +60,6 @@ import Handler.AdminMediumSettings import Handler.AdminComments import Handler.Tag import Handler.RootFeed -import Handler.Commons -- 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/Admin.hs b/Handler/Admin.hs index 9c4c32f..9b88255 100644 --- a/Handler/Admin.hs +++ b/Handler/Admin.hs @@ -1,7 +1,7 @@ module Handler.Admin where import Import -import Helper +import Handler.Commons getAdminR :: Handler Html getAdminR = do diff --git a/Handler/AdminAlbumSettings.hs b/Handler/AdminAlbumSettings.hs index 3783223..54afd53 100644 --- a/Handler/AdminAlbumSettings.hs +++ b/Handler/AdminAlbumSettings.hs @@ -1,7 +1,7 @@ module Handler.AdminAlbumSettings where import Import -import Helper +import Handler.Commons 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 2ea31d2..fc61479 100644 --- a/Handler/AdminComments.hs +++ b/Handler/AdminComments.hs @@ -1,7 +1,7 @@ module Handler.AdminComments where import Import -import Helper +import Handler.Commons import Data.Maybe import Data.Time import System.Locale diff --git a/Handler/AdminMediumSettings.hs b/Handler/AdminMediumSettings.hs index 0aea192..d42da76 100644 --- a/Handler/AdminMediumSettings.hs +++ b/Handler/AdminMediumSettings.hs @@ -1,7 +1,7 @@ module Handler.AdminMediumSettings where import Import -import Helper +import Handler.Commons import System.FilePath import System.Directory import Data.List (tail) diff --git a/Handler/AdminProfileSettings.hs b/Handler/AdminProfileSettings.hs index 614697a..1100fa4 100644 --- a/Handler/AdminProfileSettings.hs +++ b/Handler/AdminProfileSettings.hs @@ -1,7 +1,7 @@ module Handler.AdminProfileSettings where import Import -import Helper +import Handler.Commons 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 new file mode 100644 index 0000000..ff1f1bf --- /dev/null +++ b/Handler/Commons.hs @@ -0,0 +1,63 @@ +module Handler.Commons where + +import Import +import Yesod +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) + +mediumCheck :: IsString t => MediumId -> Handler (Either (t, Route App) Medium) +mediumCheck mediumId = do + tempMedium <- runDB $ get mediumId + case tempMedium of + Just medium -> do + ownerId <- return $ mediumOwner medium + owner <- runDB $ getJust ownerId + msu <- lookupSession "userId" + case msu of + Just tempUserId -> do + userId <- return $ getUserIdFromText tempUserId + album <- runDB $ getJust $ mediumAlbum medium + presence <- return (userId == ownerId) + albumOwnerPresence <- return (userId == (albumOwner album)) + case presence || albumOwnerPresence of + True -> + return $ Right medium + False -> + return $ Left ("You must own this medium to change its settings", MediumR mediumId) + Nothing -> + return $ Left ("You must be logged in to change settings", LoginR) + Nothing -> + return $ Left ("This medium does not exist", HomeR) diff --git a/Handler/ProfileDelete.hs b/Handler/ProfileDelete.hs index ce6b116..bc3903e 100644 --- a/Handler/ProfileDelete.hs +++ b/Handler/ProfileDelete.hs @@ -1,7 +1,7 @@ module Handler.ProfileDelete where import Import -import Helper +import Handler.Commons 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 72dec0d..db4be5c 100644 --- a/Handler/ProfileSettings.hs +++ b/Handler/ProfileSettings.hs @@ -1,7 +1,7 @@ module Handler.ProfileSettings where import Import -import Helper +import Handler.Commons getProfileSettingsR :: UserId -> Handler Html getProfileSettingsR userId = do diff --git a/Helper.hs b/Helper.hs index 8c820c7..c84c2bc 100644 --- a/Helper.hs +++ b/Helper.hs @@ -165,37 +165,3 @@ 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)