2016-02-18 01:43:23 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
|
|
|
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
|
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
2016-02-23 17:45:03 +09:00
|
|
|
module Vervis.Handler.Person
|
2016-02-18 01:43:23 +09:00
|
|
|
( getPeopleR
|
2016-02-23 11:27:01 +09:00
|
|
|
, postPeopleR
|
2016-02-19 13:10:42 +09:00
|
|
|
, getPersonNewR
|
2016-02-18 01:43:23 +09:00
|
|
|
, getPersonR
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2016-02-23 17:45:03 +09:00
|
|
|
import Vervis.Import hiding ((==.))
|
2016-02-18 01:43:23 +09:00
|
|
|
--import Prelude
|
|
|
|
|
2016-02-23 11:27:01 +09:00
|
|
|
import Database.Esqueleto hiding (isNothing)
|
2016-02-25 12:10:30 +09:00
|
|
|
import Vervis.Form.Person
|
2016-02-18 01:43:23 +09:00
|
|
|
--import Model
|
2016-03-07 09:39:07 +09:00
|
|
|
import Text.Blaze.Html (toHtml)
|
2016-02-23 11:27:01 +09:00
|
|
|
import Yesod.Auth.HashDB (setPassword)
|
2016-02-18 01:43:23 +09:00
|
|
|
|
2016-05-24 05:46:54 +09:00
|
|
|
import Vervis.Model.Ident
|
|
|
|
|
2016-02-19 13:10:42 +09:00
|
|
|
-- | Get list of users
|
2016-02-18 01:43:23 +09:00
|
|
|
getPeopleR :: Handler Html
|
|
|
|
getPeopleR = do
|
|
|
|
people <- runDB $ select $ from $ \ (sharer, person) -> do
|
|
|
|
where_ $ sharer ^. SharerId ==. person ^. PersonIdent
|
|
|
|
orderBy [asc $ sharer ^. SharerIdent]
|
|
|
|
return $ sharer ^. SharerIdent
|
2016-05-24 05:46:54 +09:00
|
|
|
defaultLayout $(widgetFile "people")
|
2016-02-18 01:43:23 +09:00
|
|
|
|
2016-02-19 13:10:42 +09:00
|
|
|
-- | Create new user
|
2016-02-23 11:27:01 +09:00
|
|
|
postPeopleR :: Handler Html
|
|
|
|
postPeopleR = do
|
2016-05-24 05:46:54 +09:00
|
|
|
regEnabled <- getsYesod $ appRegister . appSettings
|
2016-04-20 01:03:27 +09:00
|
|
|
if regEnabled
|
|
|
|
then do
|
|
|
|
((result, widget), enctype) <- runFormPost formPersonNew
|
|
|
|
case result of
|
|
|
|
FormSuccess pn -> do
|
2016-05-24 17:34:40 +09:00
|
|
|
now <- liftIO getCurrentTime
|
2016-04-20 01:03:27 +09:00
|
|
|
runDB $ do
|
|
|
|
let sharer = Sharer
|
2016-05-24 17:34:40 +09:00
|
|
|
{ sharerIdent = text2shr $ uLogin pn
|
|
|
|
, sharerName = uName pn
|
|
|
|
, sharerCreated = now
|
2016-04-20 01:03:27 +09:00
|
|
|
}
|
|
|
|
sid <- insert sharer
|
|
|
|
let person = Person
|
|
|
|
{ personIdent = sid
|
|
|
|
, personLogin = uLogin pn
|
|
|
|
, personHash = Nothing
|
|
|
|
, personEmail = uEmail pn
|
|
|
|
}
|
|
|
|
person' <- setPassword (uPass pn) person
|
|
|
|
insert_ person'
|
|
|
|
redirectUltDest HomeR
|
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
|
|
|
defaultLayout $(widgetFile "person-new")
|
2016-05-24 05:46:54 +09:00
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "User registration failed, see errors below"
|
2016-04-20 01:03:27 +09:00
|
|
|
defaultLayout $(widgetFile "person-new")
|
|
|
|
else notFound
|
2016-02-19 13:10:42 +09:00
|
|
|
|
|
|
|
getPersonNewR :: Handler Html
|
|
|
|
getPersonNewR = do
|
|
|
|
mpid <- maybeAuthId
|
|
|
|
if isJust mpid
|
|
|
|
then redirect HomeR
|
|
|
|
else do
|
2016-04-20 01:03:27 +09:00
|
|
|
regEnabled <- appRegister . appSettings <$> getYesod
|
|
|
|
if regEnabled
|
|
|
|
then do
|
|
|
|
((_result, widget), enctype) <- runFormPost formPersonNew
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle "Vervis > People > New"
|
|
|
|
$(widgetFile "person-new")
|
|
|
|
else notFound
|
2016-02-19 13:10:42 +09:00
|
|
|
|
2016-05-23 21:24:14 +09:00
|
|
|
getPersonR :: ShrIdent -> Handler Html
|
2016-02-18 01:43:23 +09:00
|
|
|
getPersonR ident = do
|
2016-03-03 17:35:29 +09:00
|
|
|
person <- runDB $ do
|
2016-05-24 05:46:54 +09:00
|
|
|
Entity sid _s <- getBy404 $ UniqueSharer ident
|
2016-03-03 17:35:29 +09:00
|
|
|
Entity _pid p <- getBy404 $ UniquePersonIdent sid
|
|
|
|
return p
|
2016-05-23 21:24:14 +09:00
|
|
|
defaultLayout $(widgetFile "person")
|