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.
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE FunctionalDependencies #-}
18 {-# LANGUAGE Arrows #-}
20 module Gargantext.Database.Query.Table.User
27 , getUsersWithHyperdata
30 , selectUsersLightWith
36 , module Gargantext.Database.Schema.User
40 import Control.Arrow (returnA)
41 import Control.Lens ((^.))
42 import Data.List (find)
43 import Data.Text (Text)
44 import Data.Time (UTCTime)
45 import Gargantext.Core.Types.Individu
46 import qualified Gargantext.Prelude.Crypto.Auth as Auth
47 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataUser)
48 import Gargantext.Database.Prelude
49 import Gargantext.Database.Schema.Node (node_hyperdata, node_id, queryNodeTable)
50 import Gargantext.Database.Schema.User
51 import Gargantext.Prelude
54 ------------------------------------------------------------------------
55 -- TODO: on conflict, nice message
56 insertUsers :: [UserWrite] -> Cmd err Int64
57 insertUsers us = mkCmd $ \c -> runInsert_ c insert
59 insert = Insert userTable us rCount Nothing
61 deleteUsers :: [Username] -> Cmd err Int64
62 deleteUsers us = mkCmd $ \c -> runDelete_ c
64 (\user -> in_ (map sqlStrictText us) (user_username user))
67 -- Updates email or password only (for now)
68 updateUserDB :: UserWrite -> Cmd err Int64
69 updateUserDB us = mkCmd $ \c -> runUpdate_ c (updateUserQuery us)
71 updateUserQuery :: UserWrite -> Update Int64
72 updateUserQuery us' = Update
74 , uUpdateWith = updateEasy (\ (UserDB _id _p ll su un fn ln _em is ia dj)
75 -> UserDB _id p' ll su un fn ln em' is ia dj
77 , uWhere = (\row -> user_username row .== un')
81 UserDB _ p' _ _ un' _ _ em' _ _ _ = us'
83 -----------------------------------------------------------------------
84 toUserWrite :: NewUser HashPassword -> UserWrite
85 toUserWrite (NewUser u m (Auth.PasswordHash p)) =
86 UserDB (Nothing) (sqlStrictText p)
87 (Nothing) (sqlBool True) (sqlStrictText u)
88 (sqlStrictText "first_name")
89 (sqlStrictText "last_name")
92 (sqlBool True) Nothing
94 ------------------------------------------------------------------
95 getUsersWith :: Username -> Cmd err [UserLight]
96 getUsersWith u = map toUserLight <$> runOpaQuery (selectUsersLightWith u)
98 selectUsersLightWith :: Username -> Select UserRead
99 selectUsersLightWith u = proc () -> do
100 row <- queryUserTable -< ()
101 restrict -< user_username row .== sqlStrictText u
104 ----------------------------------------------------------
105 getUsersWithId :: Int -> Cmd err [UserLight]
106 getUsersWithId i = map toUserLight <$> runOpaQuery (selectUsersLightWithId i)
108 selectUsersLightWithId :: Int -> Select UserRead
109 selectUsersLightWithId i' = proc () -> do
110 row <- queryUserTable -< ()
111 restrict -< user_id row .== sqlInt4 i'
115 queryUserTable :: Select UserRead
116 queryUserTable = selectTable userTable
118 ----------------------------------------------------------------------
119 getUserHyperdata :: Int -> Cmd err [HyperdataUser]
120 getUserHyperdata i = do
121 runOpaQuery (selectUserHyperdataWithId i)
123 selectUserHyperdataWithId :: Int -> Select (Column SqlJsonb)
124 selectUserHyperdataWithId i' = proc () -> do
125 row <- queryNodeTable -< ()
126 restrict -< row^.node_id .== (sqlInt4 i')
127 returnA -< row^.node_hyperdata
129 getUsersWithHyperdata :: Int -> Cmd err [(UserLight, HyperdataUser)]
130 getUsersWithHyperdata i = do
131 u <- getUsersWithId i
132 h <- getUserHyperdata i
134 ------------------------------------------------------------------
135 -- | Select User with some parameters
136 -- Not optimized version
137 userWith :: (Eq a1, Foldable t) => (a -> a1) -> a1 -> t a -> Maybe a
138 userWith f t xs = find (\x -> f x == t) xs
140 -- | Select User with Username
141 userWithUsername :: Text -> [UserDB] -> Maybe UserDB
142 userWithUsername t xs = userWith user_username t xs
144 userWithId :: Int -> [UserDB] -> Maybe UserDB
145 userWithId t xs = userWith user_id t xs
147 userLightWithUsername :: Text -> [UserLight] -> Maybe UserLight
148 userLightWithUsername t xs = userWith userLight_username t xs
150 userLightWithId :: Int -> [UserLight] -> Maybe UserLight
151 userLightWithId t xs = userWith userLight_id t xs
152 ----------------------------------------------------------------------
153 users :: Cmd err [UserDB]
154 users = runOpaQuery queryUserTable
156 usersLight :: Cmd err [UserLight]
157 usersLight = map toUserLight <$> users
159 getUser :: Username -> Cmd err (Maybe UserLight)
160 getUser u = userLightWithUsername u <$> usersLight
162 ----------------------------------------------------------------------
163 insertNewUsers :: [NewUser GargPassword] -> Cmd err Int64
164 insertNewUsers newUsers = do
165 users' <- liftBase $ mapM toUserHash newUsers
166 insertUsers $ map toUserWrite users'
168 ----------------------------------------------------------------------
169 instance DefaultFromField SqlTimestamptz (Maybe UTCTime) where
170 defaultFromField = fromPGSFromField