eidolon/Handler/AdminProfileSettings.hs

174 lines
6 KiB
Haskell
Raw Normal View History

2015-01-18 19:44:41 +00:00
-- eidolon -- A simple gallery in Haskell and Yesod
-- Copyright (C) 2015 Amedeo Molnár
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
2015-01-21 09:00:18 +00:00
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-01-18 19:44:41 +00:00
module Handler.AdminProfileSettings where
import Import
2014-12-28 00:08:35 +00:00
import Handler.Commons
import qualified Data.Text as T
import qualified Data.List as L
2014-12-27 22:09:51 +00:00
import Data.Maybe
import System.Directory
import System.FilePath
2014-09-07 04:56:34 +00:00
getAdminProfilesR :: Handler Html
getAdminProfilesR = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
profiles <- runDB $ selectList [] [Desc UserName]
defaultLayout $ do
setTitle "Administration: Profiles"
$(widgetFile "adminProfiles")
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route
2014-09-08 01:57:17 +00:00
getAdminUserAlbumsR :: UserId -> Handler Html
getAdminUserAlbumsR ownerId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempOwner <- runDB $ get ownerId
case tempOwner of
Just owner -> do
albums <- runDB $ selectList [AlbumOwner ==. ownerId] [Desc AlbumTitle]
defaultLayout $ do
setTitle "Administration: User albums"
$(widgetFile "adminUserAlbums")
Nothing -> do
setMessage "This user does not exist"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route
2014-09-09 02:25:47 +00:00
getAdminUserMediaR :: UserId -> Handler Html
getAdminUserMediaR ownerId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempOwner <- runDB $ get ownerId
case tempOwner of
Just owner -> do
media <- runDB $ selectList [MediumOwner ==. ownerId] [Desc MediumTitle]
defaultLayout $ do
setTitle "Administration: User media"
$(widgetFile "adminUserMedia")
Nothing -> do
setMessage "This user does not exist"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route
getAdminProfileSettingsR :: UserId -> Handler Html
getAdminProfileSettingsR ownerId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempOwner <- runDB $ get ownerId
case tempOwner of
Just owner -> do
tempUserId <- lookupSession "userId"
2015-09-14 16:54:46 +00:00
let userId = getUserIdFromText $ fromJust tempUserId
2014-12-27 22:09:51 +00:00
(adminProfileSetWidget, enctype) <- generateFormPost $ adminProfileForm owner
formLayout $ do
2014-12-27 22:09:51 +00:00
setTitle "Administration: Profile settings"
$(widgetFile "adminProfileSettings")
Nothing -> do
setMessage "This user does not exist"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route
postAdminProfileSettingsR :: UserId -> Handler Html
postAdminProfileSettingsR ownerId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempOwner <- runDB $ get ownerId
case tempOwner of
Just owner -> do
2014-12-28 06:12:25 +00:00
((result, _), _) <- runFormPost $ adminProfileForm owner
2014-12-27 22:09:51 +00:00
case result of
FormSuccess temp -> do
runDB $ update ownerId
2015-09-14 16:54:46 +00:00
[ UserName =. userName temp
, UserSlug =. userSlug temp
, UserEmail =. userEmail temp
, UserAdmin =. userAdmin temp
2014-12-27 22:09:51 +00:00
]
setMessage "User data updated successfully"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
_ -> do
setMessage "There was an error"
redirect $ AdminProfileSettingsR ownerId
Nothing -> do
setMessage "This user does not exist"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route
adminProfileForm :: User -> Form User
adminProfileForm owner = renderDivs $ User
<$> areq textField "Username" (Just $ userName owner)
<*> areq textField "Userslug" (Just $ userSlug owner)
<*> areq emailField "Email" (Just $ userEmail owner)
<*> pure (userSalt owner)
<*> pure (userSalted owner)
<*> pure (userAlbums owner)
<*> areq boolField "Admin" (Just $ userAdmin owner)
getAdminProfileDeleteR :: UserId -> Handler Html
getAdminProfileDeleteR ownerId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempOwner <- runDB $ get ownerId
case tempOwner of
Just owner -> do
2015-09-14 16:54:46 +00:00
let albumList = userAlbums owner
2014-12-28 06:12:25 +00:00
_ <- mapM (\albumId -> do
2014-12-27 22:09:51 +00:00
album <- runDB $ getJust albumId
2015-09-14 16:54:46 +00:00
let mediaList = albumContent album
2014-12-28 06:12:25 +00:00
_ <- mapM (\med -> do
2014-12-27 22:09:51 +00:00
-- delete comments
commEnts <- runDB $ selectList [CommentOrigin ==. med] []
2015-09-14 16:54:46 +00:00
_ <- mapM (runDB . delete . entityKey) commEnts
2014-12-27 22:09:51 +00:00
-- delete media files
medium <- runDB $ getJust med
liftIO $ removeFile (normalise $ L.tail $ mediumPath medium)
liftIO $ removeFile (normalise $ L.tail $ mediumThumb medium)
-- delete medium database entry
runDB $ delete med
) mediaList
runDB $ delete albumId
) albumList
runDB $ delete ownerId
2015-09-14 16:54:46 +00:00
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> T.unpack (extractKey ownerId)
2014-12-27 22:09:51 +00:00
setMessage "User successfully deleted"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Nothing -> do
setMessage "This user does not exist"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route