Enable simple username+password login - ugly but serviceable

This commit is contained in:
Danielle McLean 2017-10-06 10:38:10 +11:00
parent e141fb4666
commit f934632484
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 20 additions and 31 deletions

View file

@ -1,9 +1,10 @@
User User
username Text maxlen=190 username Text maxlen=190
password Text maxlen=79
fullName Text maxlen=500 fullName Text maxlen=500
email Text maxlen=190 email Text maxlen=190
note Text sqltype=mediumtext note Text sqltype=mediumtext
UniqueUsername username UniqueUser username
UniqueEmail email UniqueEmail email
deriving Typeable deriving Typeable

View file

@ -46,9 +46,11 @@ dependencies:
- time - time
- case-insensitive - case-insensitive
- wai - wai
- conduit-combinators >= 1.1 && < 1.2 - conduit-combinators >= 1.1 && < 1.2
- libravatar >=0.4 && <0.5 - libravatar >=0.4 && <0.5
- split >=0.2 && <0.3 - split >=0.2 && <0.3
- yesod-auth-hashdb >=1.6.2 && <1.7
- yesod-sitemap >=1.4 && <1.5 - yesod-sitemap >=1.4 && <1.5
# The library contains all of our application code. The executable # The library contains all of our application code. The executable

View file

@ -12,10 +12,9 @@ import Database.Persist.Sql (ConnectionPool, runSqlPool)
import Text.Hamlet (hamletFile) import Text.Hamlet (hamletFile)
import Text.Jasmine (minifym) import Text.Jasmine (minifym)
-- Used only when in "auth-dummy-login" setting is enabled. import Yesod.Auth.HashDB (authHashDB)
import Yesod.Auth.Dummy import qualified Yesod.Auth.Message as AuthMsg
import Yesod.Auth.OpenId (authOpenId, IdentifierType (Claimed))
import Yesod.Default.Util (addStaticContentExternal) import Yesod.Default.Util (addStaticContentExternal)
import Yesod.Core.Types (Logger) import Yesod.Core.Types (Logger)
import qualified Yesod.Core.Unsafe as Unsafe import qualified Yesod.Core.Unsafe as Unsafe
@ -125,13 +124,7 @@ instance Yesod App where
authRoute _ = Just $ AuthR LoginR authRoute _ = Just $ AuthR LoginR
-- Routes not requiring authentication. -- Routes not requiring authentication.
isAuthorized (AuthR _) _ = return Authorized isAuthorized _ _ = return Authorized
isAuthorized HomeR _ = return Authorized
isAuthorized FaviconR _ = return Authorized
isAuthorized KeybaseR _ = return Authorized
isAuthorized RobotsR _ = return Authorized
isAuthorized SitemapR _ = return Authorized
isAuthorized (StaticR _) _ = return Authorized
-- This function creates static content files in the static folder -- This function creates static content files in the static folder
-- and names them based on a hash of their content. This allows -- and names them based on a hash of their content. This allows
@ -163,9 +156,8 @@ instance Yesod App where
-- Define breadcrumbs. -- Define breadcrumbs.
instance YesodBreadcrumbs App where instance YesodBreadcrumbs App where
breadcrumb HomeR = return ("Home", Nothing) breadcrumb (AuthR _) = return ("login", Just HomeR)
breadcrumb (AuthR _) = return ("Login", Just HomeR) breadcrumb _ = return ("home", Nothing)
breadcrumb _ = return ("home", Nothing)
-- How to run database actions. -- How to run database actions.
instance YesodPersist App where instance YesodPersist App where
@ -183,25 +175,14 @@ instance YesodAuth App where
loginDest _ = HomeR loginDest _ = HomeR
-- Where to send a user after logout -- Where to send a user after logout
logoutDest _ = HomeR logoutDest _ = HomeR
-- Override the above two destinations when a Referer: header is present
redirectToReferer _ = True
authenticate creds = runDB $ do authenticate = fmap toResult . lookupCreds
x <- getBy . UniqueUsername $ credsIdent creds where
case x of lookupCreds = runDB . getBy . UniqueUser . credsIdent
Just (Entity uid _) -> return $ Authenticated uid toResult = UserError AuthMsg.InvalidLogin `maybe` (Authenticated . entityKey)
Nothing -> Authenticated <$> insert User
{ userUsername = credsIdent creds
, userFullName = ""
, userEmail = ""
, userNote = ""
}
-- You can add other plugins like Google Email, email or OAuth here -- You can add other plugins like Google Email, email or OAuth here
authPlugins app = [authOpenId Claimed []] ++ extraAuthPlugins authPlugins _ = [authHashDB (Just . UniqueUser)]
-- Enable authDummy login if enabled.
where extraAuthPlugins = [authDummy | appAuthDummyLogin $ appSettings app]
authHttpManager = getHttpManager authHttpManager = getHttpManager
-- | Access function to determine if a user is logged in. -- | Access function to determine if a user is logged in.

View file

@ -10,7 +10,7 @@ import Widget.Hcard (hcard)
getHomeR :: Handler Html getHomeR :: Handler Html
getHomeR = do getHomeR = do
user <- runDB . getBy404 $ UniqueUsername "dani" user <- runDB . getBy404 $ UniqueUser "dani"
let name = userFullName . entityVal $ user let name = userFullName . entityVal $ user
maybeTitle <- asks $ appTitle . appSettings maybeTitle <- asks $ appTitle . appSettings
defaultLayout $ do defaultLayout $ do

View file

@ -11,6 +11,7 @@ module Model where
import ClassyPrelude.Yesod import ClassyPrelude.Yesod
import Database.Persist.Quasi import Database.Persist.Quasi
import Yesod.Auth.HashDB ( HashDBUser(..) )
-- You can define all of your database entities in the entities file. -- You can define all of your database entities in the entities file.
-- You can find more information on persistent and how to declare entities -- You can find more information on persistent and how to declare entities
@ -18,3 +19,7 @@ import Database.Persist.Quasi
-- http://www.yesodweb.com/book/persistent/ -- http://www.yesodweb.com/book/persistent/
share [mkPersist sqlSettings, mkMigrate "migrateAll"] share [mkPersist sqlSettings, mkMigrate "migrateAll"]
$(persistFileWith lowerCaseSettings "config/models") $(persistFileWith lowerCaseSettings "config/models")
instance HashDBUser User where
userPasswordHash = Just . userPassword
setPasswordHash pw u = u { userPassword = pw }