helper cannot handle commons.

This commit is contained in:
nek0 2014-12-28 01:08:35 +01:00
parent 1d8adf6ebc
commit fef737f957
10 changed files with 70 additions and 42 deletions

View file

@ -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

View file

@ -1,7 +1,7 @@
module Handler.Admin where
import Import
import Helper
import Handler.Commons
getAdminR :: Handler Html
getAdminR = do

View file

@ -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

View file

@ -1,7 +1,7 @@
module Handler.AdminComments where
import Import
import Helper
import Handler.Commons
import Data.Maybe
import Data.Time
import System.Locale

View file

@ -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)

View file

@ -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

63
Handler/Commons.hs Normal file
View file

@ -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)

View file

@ -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

View file

@ -1,7 +1,7 @@
module Handler.ProfileSettings where
import Import
import Helper
import Handler.Commons
getProfileSettingsR :: UserId -> Handler Html
getProfileSettingsR userId = do

View file

@ -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)