Commit 93e1c3ab authored by Alexandre Delanoë's avatar Alexandre Delanoë

[Clean] New Users fun

parent 34efdd82
...@@ -30,6 +30,7 @@ data User = UserDBId UserId | UserName Text | RootId NodeId | UserPublic ...@@ -30,6 +30,7 @@ data User = UserDBId UserId | UserName Text | RootId NodeId | UserPublic
type Username = Text type Username = Text
type HashPassword = Auth.PasswordHash Auth.Argon2
newtype GargPassword = GargPassword Text newtype GargPassword = GargPassword Text
deriving (Generic) deriving (Generic)
...@@ -40,12 +41,14 @@ instance ToJSON GargPassword ...@@ -40,12 +41,14 @@ instance ToJSON GargPassword
instance FromJSON GargPassword instance FromJSON GargPassword
instance ToSchema GargPassword instance ToSchema GargPassword
type Email = Text
type Email = Text
type UsernameMaster = Username type UsernameMaster = Username
type UsernameSimple = Username type UsernameSimple = Username
data NewUser a = NewUser { _nu_username :: Username
, _nu_email :: Email
, _nu_password :: a
}
arbitraryUsername :: [Username] arbitraryUsername :: [Username]
arbitraryUsername = ["gargantua"] <> users arbitraryUsername = ["gargantua"] <> users
...@@ -57,19 +60,19 @@ arbitraryPassword :: [GargPassword] ...@@ -57,19 +60,19 @@ arbitraryPassword :: [GargPassword]
arbitraryPassword = map (\u -> GargPassword (reverse u)) arbitraryUsername arbitraryPassword = map (\u -> GargPassword (reverse u)) arbitraryUsername
----------------------------------------------------------- -----------------------------------------------------------
userHash :: MonadIO m
=> NewUser GargPassword
-> m (NewUser HashPassword)
userHash (NewUser u m (GargPassword p)) = do
h <- Auth.createPasswordHash p
pure $ NewUser u m h
arbitraryUsersHash :: MonadIO m arbitraryUsersHash :: MonadIO m
=> m [(Username, Email, Auth.PasswordHash Auth.Argon2)] => m [NewUser HashPassword]
arbitraryUsersHash = mapM userHash arbitraryUsers arbitraryUsersHash = mapM userHash arbitraryUsers
userHash :: MonadIO m arbitraryUsers :: [NewUser GargPassword]
=> (Username, Email, GargPassword) arbitraryUsers = map (\u -> NewUser u (u <> "@gargantext.org") (GargPassword $ reverse u))
-> m (Username, Email, Auth.PasswordHash Auth.Argon2) arbitraryUsername
userHash (u,m,GargPassword p) = do
h <- Auth.createPasswordHash p
pure (u, m, h)
arbitraryUsers :: [(Username, Email, GargPassword)]
arbitraryUsers = map (\u -> (u, u <> "@gargantext.org", GargPassword $ reverse u)) arbitraryUsername
...@@ -97,6 +97,3 @@ queryInsertNgrams = [sql| ...@@ -97,6 +97,3 @@ queryInsertNgrams = [sql|
JOIN ngrams c USING (terms); -- columns of unique index JOIN ngrams c USING (terms); -- columns of unique index
|] |]
...@@ -21,7 +21,6 @@ module Gargantext.Database.Query.Table.User ...@@ -21,7 +21,6 @@ module Gargantext.Database.Query.Table.User
( insertUsers ( insertUsers
, queryUserTable , queryUserTable
, getUser , getUser
, gargUserWith
, insertUsersDemo , insertUsersDemo
, selectUsersLightWith , selectUsersLightWith
, userWithUsername , userWithUsername
...@@ -46,8 +45,6 @@ import Gargantext.Prelude ...@@ -46,8 +45,6 @@ import Gargantext.Prelude
import Opaleye import Opaleye
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- TODO: on conflict, nice message -- TODO: on conflict, nice message
insertUsers :: [UserWrite] -> Cmd err Int64 insertUsers :: [UserWrite] -> Cmd err Int64
insertUsers us = mkCmd $ \c -> runInsert_ c insert insertUsers us = mkCmd $ \c -> runInsert_ c insert
...@@ -57,17 +54,18 @@ insertUsers us = mkCmd $ \c -> runInsert_ c insert ...@@ -57,17 +54,18 @@ insertUsers us = mkCmd $ \c -> runInsert_ c insert
insertUsersDemo :: Cmd err Int64 insertUsersDemo :: Cmd err Int64
insertUsersDemo = do insertUsersDemo = do
users <- liftBase arbitraryUsersHash users <- liftBase arbitraryUsersHash
insertUsers $ map (\(u,m,h) -> gargUserWith u m h) users insertUsers $ map toUserWrite users
----------------------------------------------------------------------- -----------------------------------------------------------------------
gargUserWith :: Username -> Email -> Auth.PasswordHash Auth.Argon2 -> UserWrite toUserWrite :: NewUser HashPassword -> UserWrite
gargUserWith u m (Auth.PasswordHash p) = UserDB (Nothing) (pgStrictText p) toUserWrite (NewUser u m (Auth.PasswordHash p)) =
(Nothing) (pgBool True) (pgStrictText u) UserDB (Nothing) (pgStrictText p)
(pgStrictText "first_name") (Nothing) (pgBool True) (pgStrictText u)
(pgStrictText "last_name") (pgStrictText "first_name")
(pgStrictText m) (pgStrictText "last_name")
(pgBool True) (pgStrictText m)
(pgBool True) Nothing (pgBool True)
(pgBool True) Nothing
------------------------------------------------------------------ ------------------------------------------------------------------
getUsersWith :: Username -> Cmd err [UserLight] getUsersWith :: Username -> Cmd err [UserLight]
...@@ -101,11 +99,7 @@ userLightWithUsername t xs = userWith userLight_username t xs ...@@ -101,11 +99,7 @@ userLightWithUsername t xs = userWith userLight_username t xs
userLightWithId :: Int -> [UserLight] -> Maybe UserLight userLightWithId :: Int -> [UserLight] -> Maybe UserLight
userLightWithId t xs = userWith userLight_id t xs userLightWithId t xs = userWith userLight_id t xs
----------------------------------------------------------------------
instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
queryRunnerColumnDefault = fieldQueryRunnerColumn
users :: Cmd err [UserDB] users :: Cmd err [UserDB]
users = runOpaQuery queryUserTable users = runOpaQuery queryUserTable
...@@ -115,3 +109,6 @@ usersLight = map toUserLight <$> users ...@@ -115,3 +109,6 @@ usersLight = map toUserLight <$> users
getUser :: Username -> Cmd err (Maybe UserLight) getUser :: Username -> Cmd err (Maybe UserLight)
getUser u = userLightWithUsername u <$> usersLight getUser u = userLightWithUsername u <$> usersLight
----------------------------------------------------------------------
instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
queryRunnerColumnDefault = fieldQueryRunnerColumn
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment