diff --git a/src/Handler/Utils/LMS.hs b/src/Handler/Utils/LMS.hs index e3244e507..425ae2a5b 100644 --- a/src/Handler/Utils/LMS.hs +++ b/src/Handler/Utils/LMS.hs @@ -14,6 +14,7 @@ module Handler.Utils.LMS , csvFilenameLmsResult , lmsUserToDelete, _lmsUserToDelete , lmsUserToDeleteExpr + , randomLMSIdent, randomLMSpw ) where -- general utils for LMS Interface Handlers @@ -22,6 +23,10 @@ import Import import Handler.Utils import qualified Database.Esqueleto.Legacy as E +import qualified Data.Text as T +import qualified Data.UUID as UUID (toText) +import qualified Data.UUID.V4 as UUID (nextRandom) + -- generic Column names csvLmsIdent :: IsString a => a csvLmsIdent = fromString "user" -- "Benutzerkennung" @@ -83,3 +88,24 @@ lmsUserToDelete LmsUser{lmsUserEnded, lmsUserStatus} = isNothing lmsUserEnded && _lmsUserToDelete :: Getter LmsUser Bool _lmsUserToDelete = to lmsUserToDelete + +-- random generation of LmsIdentifiers, maybe this should be in Model.Types.Lms since length specifications are type-y? + +lengthIdent :: Int +lengthIdent = 8 + +lengthPassword :: Int +lengthPassword = 8 + +randomText :: MonadIO m => Int -> m Text +randomText n + | n <= 8 = T.take n . UUID.toText <$> liftIO UUID.nextRandom + | n <= 32 = T.take n . T.filter ('-' ==) . UUID.toText <$> liftIO UUID.nextRandom + | otherwise = (<>) <$> randomText 32 <*> randomText (n - 32) + +randomLMSIdent :: MonadIO m => m LmsIdent +randomLMSIdent = LmsIdent <$> randomText lengthIdent + +randomLMSpw :: MonadIO m => m Text +randomLMSpw = randomText lengthPassword + \ No newline at end of file