2 Module : Gargantext.Database.Query.Table.User
3 Description : User Database management tools
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Functions to deal with users, database side.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE FunctionalDependencies #-}
18 {-# LANGUAGE Arrows #-}
20 module Gargantext.Database.Query.Table.User
27 , selectUsersLightWith
32 , module Gargantext.Database.Schema.User
36 import Control.Arrow (returnA)
37 import Data.Eq(Eq(..))
38 import Data.List (find)
39 import Data.Maybe (Maybe)
40 import Data.Text (Text)
41 import Data.Time (UTCTime)
42 import Gargantext.Core.Types.Individu
43 import qualified Gargantext.Prelude.Crypto.Auth as Auth
44 import Gargantext.Database.Schema.User
45 import Gargantext.Database.Prelude
46 import Gargantext.Prelude
49 ------------------------------------------------------------------------
50 -- TODO: on conflict, nice message
51 insertUsers :: [UserWrite] -> Cmd err Int64
52 insertUsers us = mkCmd $ \c -> runInsert_ c insert
54 insert = Insert userTable us rCount Nothing
56 deleteUsers :: [Username] -> Cmd err Int64
57 deleteUsers us = mkCmd $ \c -> runDelete c userTable
58 (\user -> in_ (map pgStrictText us) (user_username user))
60 -----------------------------------------------------------------------
61 toUserWrite :: NewUser HashPassword -> UserWrite
62 toUserWrite (NewUser u m (Auth.PasswordHash p)) =
63 UserDB (Nothing) (pgStrictText p)
64 (Nothing) (pgBool True) (pgStrictText u)
65 (pgStrictText "first_name")
66 (pgStrictText "last_name")
71 ------------------------------------------------------------------
72 getUsersWith :: Username -> Cmd err [UserLight]
73 getUsersWith u = map toUserLight <$> runOpaQuery (selectUsersLightWith u)
75 selectUsersLightWith :: Username -> Query UserRead
76 selectUsersLightWith u = proc () -> do
77 row <- queryUserTable -< ()
78 restrict -< user_username row .== pgStrictText u
81 queryUserTable :: Query UserRead
82 queryUserTable = queryTable userTable
84 ------------------------------------------------------------------
85 -- | Select User with some parameters
86 -- Not optimized version
87 userWith :: (Eq a1, Foldable t) => (a -> a1) -> a1 -> t a -> Maybe a
88 userWith f t xs = find (\x -> f x == t) xs
90 -- | Select User with Username
91 userWithUsername :: Text -> [UserDB] -> Maybe UserDB
92 userWithUsername t xs = userWith user_username t xs
94 userWithId :: Int -> [UserDB] -> Maybe UserDB
95 userWithId t xs = userWith user_id t xs
97 userLightWithUsername :: Text -> [UserLight] -> Maybe UserLight
98 userLightWithUsername t xs = userWith userLight_username t xs
100 userLightWithId :: Int -> [UserLight] -> Maybe UserLight
101 userLightWithId t xs = userWith userLight_id t xs
103 ----------------------------------------------------------------------
104 users :: Cmd err [UserDB]
105 users = runOpaQuery queryUserTable
107 usersLight :: Cmd err [UserLight]
108 usersLight = map toUserLight <$> users
110 getUser :: Username -> Cmd err (Maybe UserLight)
111 getUser u = userLightWithUsername u <$> usersLight
114 ----------------------------------------------------------------------
115 insertUsersDemo :: Cmd err Int64
117 users <- liftBase arbitraryUsersHash
118 insertUsers $ map toUserWrite users
120 ----------------------------------------------------------------------
121 instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
122 queryRunnerColumnDefault = fieldQueryRunnerColumn