Synchronise matrikelnummer from LDAP

This commit is contained in:
Gregor Kleen 2017-11-22 18:33:24 +01:00
parent 2c188926a6
commit 57cac79d69
4 changed files with 39 additions and 20 deletions

View File

@ -25,6 +25,8 @@ stanzas:
- LDAPPW - LDAPPW
- LDAPBN - LDAPBN
- DUMMY_LOGIN - DUMMY_LOGIN
- DETAILED_LOGGING
- LOG_ALL
# Use the following to automatically copy your bundle upon creation via `yesod # Use the following to automatically copy your bundle upon creation via `yesod
# keter`. Uses `scp` internally, so you can set it to a remote destination # keter`. Uses `scp` internally, so you can set it to a remote destination

View File

@ -13,8 +13,8 @@ approot: "_env:APPROOT:http://localhost:3000"
# Optional values with the following production defaults. # Optional values with the following production defaults.
# In development, they default to the inverse. # In development, they default to the inverse.
# #
# detailed-logging: false detailed-logging: "_env:DETAILED_LOGGING:false"
# should-log-all: false should-log-all: "_env:LOG_ALL:false"
# reload-templates: false # reload-templates: false
# mutable-static: false # mutable-static: false
# skip-combining: false # skip-combining: false

2
models
View File

@ -1,7 +1,7 @@
User User
plugin Text plugin Text
ident Text ident Text
matrikelnummer Text matrikelnummer Text Maybe
UniqueAuthentication plugin ident UniqueAuthentication plugin ident
Term json Term json
name TermIdentifier name TermIdentifier

View File

@ -6,6 +6,7 @@
{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternGuards #-} {-# LANGUAGE PatternGuards #-}
module Foundation where module Foundation where
@ -16,10 +17,12 @@ import Text.Hamlet (hamletFile)
import Text.Jasmine (minifym) import Text.Jasmine (minifym)
-- Used only when in "auth-dummy-login" setting is enabled. -- Used only when in "auth-dummy-login" setting is enabled.
import Yesod.Auth.Message
import Yesod.Auth.Dummy import Yesod.Auth.Dummy
import Yesod.Auth.LDAP import Yesod.Auth.LDAP
import LDAP.Data (LDAPScope(..)) import LDAP.Data (LDAPScope(..))
import LDAP.Search (LDAPEntry(..))
import Yesod.Default.Util (addStaticContentExternal) import Yesod.Default.Util (addStaticContentExternal)
import Yesod.Core.Types (Logger) import Yesod.Core.Types (Logger)
@ -265,21 +268,29 @@ instance YesodAuth UniWorX where
-- Override the above two destinations when a Referer: header is present -- Override the above two destinations when a Referer: header is present
redirectToReferer _ = True redirectToReferer _ = True
authenticate Creds{..} = runDB $ do authenticate creds@(Creds{..}) = runDB $ do
let (plugin, ident) let (userPlugin, userIdent)
| credsPlugin == "dummy" | isDummy
, [dummyPlugin, dummyIdent] <- Text.splitOn ":" credsIdent , [dummyPlugin, dummyIdent] <- Text.splitOn ":" credsIdent
= (dummyPlugin, dummyIdent) = (dummyPlugin, dummyIdent)
| otherwise | otherwise
= (credsPlugin, credsIdent) = (credsPlugin, credsIdent)
x <- getBy $ UniqueAuthentication plugin ident isDummy = credsPlugin == "dummy"
case x of uAuth = UniqueAuthentication userPlugin userIdent
Just (Entity uid _) -> return $ Authenticated uid
Nothing -> Authenticated <$> insert User $logDebugS "auth" $ tshow ((userPlugin, userIdent), creds)
{ userPlugin = plugin
, userIdent = ident case isDummy of
, userMatrikelnummer = "DummyMatrikel" True ->
} maybe (UserError $ IdentifierNotFound credsIdent) (Authenticated . entityKey) <$> getBy uAuth
False -> do
let
userMatrikelnummer = lookup "LMU-Stud-Matrikelnummer" credsExtra
newUser = User{..}
userUpdate = [ UserMatrikelnummer =. userMatrikelnummer
]
Authenticated . entityKey <$> upsertBy uAuth newUser userUpdate
-- 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 = [genericAuthLDAP $ ldapConfig app] ++ extraAuthPlugins authPlugins app = [genericAuthLDAP $ ldapConfig app] ++ extraAuthPlugins
@ -290,14 +301,20 @@ instance YesodAuth UniWorX where
ldapConfig :: UniWorX -> LDAPConfig ldapConfig :: UniWorX -> LDAPConfig
ldapConfig app@(appSettings -> settings) = LDAPConfig ldapConfig app@(appSettings -> settings) = LDAPConfig
{ usernameFilter = ("userPrincipalName=" <>) { usernameFilter = \u -> principalName <> "=" <> u
, identifierModifier = \n _ -> n , identifierModifier
, ldapUri = appLDAPURI settings , ldapUri = appLDAPURI settings
, initDN = appLDAPDN settings , initDN = appLDAPDN settings
, initPass = appLDAPPw settings , initPass = appLDAPPw settings
, baseDN = appLDAPBaseName settings , baseDN = appLDAPBaseName settings
, ldapScope = LdapScopeSubtree , ldapScope = LdapScopeSubtree
} }
where
principalName :: IsString a => a
principalName = "userPrincipalName"
identifierModifier _ entry = case lookup principalName $ leattrs entry of
Just [n] -> Text.pack n
_ -> error "Could not determine user principal name"
-- | Access function to determine if a user is logged in. -- | Access function to determine if a user is logged in.
isAuthenticated :: Handler AuthResult isAuthenticated :: Handler AuthResult