helper cannot handle commons.
This commit is contained in:
parent
1d8adf6ebc
commit
fef737f957
10 changed files with 70 additions and 42 deletions
|
@ -60,7 +60,6 @@ import Handler.AdminMediumSettings
|
||||||
import Handler.AdminComments
|
import Handler.AdminComments
|
||||||
import Handler.Tag
|
import Handler.Tag
|
||||||
import Handler.RootFeed
|
import Handler.RootFeed
|
||||||
import Handler.Commons
|
|
||||||
|
|
||||||
-- This line actually creates our YesodDispatch instance. It is the second half
|
-- 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
|
-- of the call to mkYesodData which occurs in Foundation.hs. Please see the
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.Admin where
|
module Handler.Admin where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
import Handler.Commons
|
||||||
|
|
||||||
getAdminR :: Handler Html
|
getAdminR :: Handler Html
|
||||||
getAdminR = do
|
getAdminR = do
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.AdminAlbumSettings where
|
module Handler.AdminAlbumSettings where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
import Handler.Commons
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.List as L
|
import qualified Data.List as L
|
||||||
import System.FilePath
|
import System.FilePath
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.AdminComments where
|
module Handler.AdminComments where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
import Handler.Commons
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.Time
|
import Data.Time
|
||||||
import System.Locale
|
import System.Locale
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.AdminMediumSettings where
|
module Handler.AdminMediumSettings where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
import Handler.Commons
|
||||||
import System.FilePath
|
import System.FilePath
|
||||||
import System.Directory
|
import System.Directory
|
||||||
import Data.List (tail)
|
import Data.List (tail)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.AdminProfileSettings where
|
module Handler.AdminProfileSettings where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
import Handler.Commons
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.List as L
|
import qualified Data.List as L
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
|
63
Handler/Commons.hs
Normal file
63
Handler/Commons.hs
Normal 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)
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.ProfileDelete where
|
module Handler.ProfileDelete where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
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
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Handler.ProfileSettings where
|
module Handler.ProfileSettings where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
import Helper
|
import Handler.Commons
|
||||||
|
|
||||||
getProfileSettingsR :: UserId -> Handler Html
|
getProfileSettingsR :: UserId -> Handler Html
|
||||||
getProfileSettingsR userId = do
|
getProfileSettingsR userId = do
|
||||||
|
|
34
Helper.hs
34
Helper.hs
|
@ -165,37 +165,3 @@ mediumStaticImageRoute medium =
|
||||||
mediumStaticThumbRoute :: Medium -> Route Static
|
mediumStaticThumbRoute :: Medium -> Route Static
|
||||||
mediumStaticThumbRoute medium =
|
mediumStaticThumbRoute medium =
|
||||||
StaticRoute (drop 2 $ T.splitOn "/" $ T.pack $ mediumThumb 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)
|
|
||||||
|
|
Loading…
Reference in a new issue