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 FlexibleInstances #-}
18 {-# LANGUAGE MultiParamTypeClasses #-}
19 {-# LANGUAGE FunctionalDependencies #-}
20 {-# LANGUAGE Arrows #-}
21 {-# LANGUAGE NoImplicitPrelude #-}
23 module Gargantext.Database.User where
25 import Control.Arrow (returnA)
26 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
27 import Data.Eq(Eq(..))
28 import Data.List (find)
29 import Data.Maybe (Maybe)
30 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
31 import Data.Text (Text)
32 import Data.Time (UTCTime)
33 import GHC.Show(Show(..))
35 import qualified Database.PostgreSQL.Simple as PGS
37 import Gargantext.Prelude
38 import Gargantext.Database.Node (Cmd(..), mkCmd, runCmd)
41 data UserLight = UserLight { userLight_id :: Int
42 , userLight_username :: Text
43 , userLight_email :: Text
46 toUserLight :: User -> UserLight
47 toUserLight (User id _ _ _ u _ _ e _ _ _ ) = UserLight id u e
49 data UserPoly id pass llogin suser
51 mail staff active djoined = User { user_id :: id
52 , user_password :: pass
53 , user_lastLogin :: llogin
54 , user_isSuperUser :: suser
56 , user_username :: uname
57 , user_firstName :: fname
58 , user_lastName :: lname
61 , user_isStaff :: staff
62 , user_isActive :: active
63 , user_dateJoined :: djoined
66 type UserWrite = UserPoly (Maybe (Column PGInt4)) (Column PGText)
67 (Maybe (Column PGTimestamptz)) (Column PGBool)
68 (Column PGText) (Column PGText)
69 (Column PGText) (Column PGText)
70 (Column PGBool) (Column PGBool)
71 (Column PGTimestamptz)
73 type UserRead = UserPoly (Column PGInt4) (Column PGText)
74 (Column PGTimestamptz) (Column PGBool)
75 (Column PGText) (Column PGText)
76 (Column PGText) (Column PGText)
77 (Column PGBool) (Column PGBool)
78 (Column PGTimestamptz)
80 type User = UserPoly Int Text (Maybe UTCTime) Bool Text Text Text Text Bool Bool UTCTime
82 $(makeAdaptorAndInstance "pUser" ''UserPoly)
83 $(makeLensesWith abbreviatedFields ''UserPoly)
86 userTable :: Table UserWrite UserRead
87 userTable = Table "auth_user" (pUser User { user_id = optional "id"
88 , user_password = required "password"
89 , user_lastLogin = optional "last_login"
90 , user_isSuperUser = required "is_superuser"
91 , user_username = required "username"
92 , user_firstName = required "first_name"
93 , user_lastName = required "last_name"
94 , user_email = required "email"
95 , user_isStaff = required "is_staff"
96 , user_isActive = required "is_active"
97 , user_dateJoined = required "date_joined"
101 ------------------------------------------------------------------
102 queryUserTable :: Query UserRead
103 queryUserTable = queryTable userTable
105 selectUsersLight :: Query UserRead
106 selectUsersLight = proc () -> do
107 row@(User i _p _ll _is _un _fn _ln _m _iff _ive _dj) <- queryUserTable -< ()
109 --returnA -< User i p ll is un fn ln m iff ive dj
111 ------------------------------------------------------------------
112 -- | Select User with some parameters
113 -- Not optimized version
114 userWith :: (Eq a1, Foldable t) => (a -> a1) -> a1 -> t a -> Maybe a
115 userWith f t xs = find (\x -> f x == t) xs
117 -- | Select User with Username
118 userWithUsername :: Text -> [User] -> Maybe User
119 userWithUsername t xs = userWith user_username t xs
121 userWithId :: Int -> [User] -> Maybe User
122 userWithId t xs = userWith user_id t xs
124 userLightWithUsername :: Text -> [UserLight] -> Maybe UserLight
125 userLightWithUsername t xs = userWith userLight_username t xs
127 userLightWithId :: Int -> [UserLight] -> Maybe UserLight
128 userLightWithId t xs = userWith userLight_id t xs
131 instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
132 queryRunnerColumnDefault = fieldQueryRunnerColumn
136 users = mkCmd $ \conn -> runQuery conn queryUserTable
138 usersLight :: Cmd [UserLight]
139 usersLight = mkCmd $ \conn -> map toUserLight <$> runQuery conn queryUserTable
143 getUser :: Username -> Cmd (Maybe UserLight)
144 getUser u = mkCmd $ \c -> userLightWithUsername u <$> runCmd c usersLight