feat(lms): random ident pw generation without db

This commit is contained in:
Steffen Jost 2022-04-05 16:16:02 +02:00
parent efcc9526ac
commit 21b74a5d7f

View File

@ -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