2 Module : Gargantext.Database.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 FlexibleContexts #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE MultiParamTypeClasses #-}
20 {-# LANGUAGE FunctionalDependencies #-}
21 {-# LANGUAGE Arrows #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE OverloadedStrings #-}
24 {-# LANGUAGE RankNTypes #-}
26 module Gargantext.Database.Action.Query.User
29 import Control.Arrow (returnA)
30 import Data.Eq(Eq(..))
31 import Data.List (find)
32 import Data.Maybe (Maybe)
33 import Data.Text (Text)
34 import Data.Time (UTCTime)
35 import Gargantext.Core.Types.Individu
36 import Gargantext.Database.Schema.User
37 import Gargantext.Database.Admin.Utils
38 import Gargantext.Prelude
41 ------------------------------------------------------------------------
42 -- TODO: on conflict, nice message
43 insertUsers :: [UserWrite] -> Cmd err Int64
44 insertUsers us = mkCmd $ \c -> runInsert_ c insert
46 insert = Insert userTable us rCount Nothing
49 gargantextUser :: Username -> UserWrite
50 gargantextUser u = UserDB (Nothing) (pgStrictText "password")
51 (Nothing) (pgBool True) (pgStrictText u)
52 (pgStrictText "first_name")
53 (pgStrictText "last_name")
54 (pgStrictText "e@mail")
55 (pgBool True) (pgBool True) (Nothing)
57 insertUsersDemo :: Cmd err Int64
58 insertUsersDemo = insertUsers $ map (\u -> gargantextUser u) arbitraryUsername
60 ------------------------------------------------------------------
61 queryUserTable :: Query UserRead
62 queryUserTable = queryTable userTable
64 selectUsersLight :: Query UserRead
65 selectUsersLight = proc () -> do
66 row@(UserDB i _p _ll _is _un _fn _ln _m _iff _ive _dj) <- queryUserTable -< ()
68 --returnA -< User i p ll is un fn ln m iff ive dj
70 ------------------------------------------------------------------
71 -- | Select User with some parameters
72 -- Not optimized version
73 userWith :: (Eq a1, Foldable t) => (a -> a1) -> a1 -> t a -> Maybe a
74 userWith f t xs = find (\x -> f x == t) xs
76 -- | Select User with Username
77 userWithUsername :: Text -> [UserDB] -> Maybe UserDB
78 userWithUsername t xs = userWith user_username t xs
80 userWithId :: Int -> [UserDB] -> Maybe UserDB
81 userWithId t xs = userWith user_id t xs
83 userLightWithUsername :: Text -> [UserLight] -> Maybe UserLight
84 userLightWithUsername t xs = userWith userLight_username t xs
86 userLightWithId :: Int -> [UserLight] -> Maybe UserLight
87 userLightWithId t xs = userWith userLight_id t xs
90 instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
91 queryRunnerColumnDefault = fieldQueryRunnerColumn
94 users :: Cmd err [UserDB]
95 users = runOpaQuery queryUserTable
97 usersLight :: Cmd err [UserLight]
98 usersLight = map toUserLight <$> users
100 getUser :: Username -> Cmd err (Maybe UserLight)
101 getUser u = userLightWithUsername u <$> usersLight