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.
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE DeriveAnyClass #-}
17 {-# LANGUAGE FunctionalDependencies #-}
18 {-# LANGUAGE Arrows #-}
19 {-# LANGUAGE TemplateHaskell #-}
21 module Gargantext.Database.Schema.User where
23 import Data.Morpheus.Types (GQLType(typeOptions))
24 import Data.Text (Text)
25 import Data.Time (UTCTime)
26 import qualified Gargantext.API.GraphQL.Utils as GAGU
27 import Gargantext.Core.Utils.Prefix (unPrefix)
28 import Gargantext.Database.Prelude (fromField')
29 import Gargantext.Prelude
30 import GHC.Generics (Generic)
31 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
32 import Data.Aeson.TH (deriveJSON)
34 -- FIXME PLZ : the import below leads to an error, why ?
35 -- import Gargantext.Database.Schema.Prelude hiding (makeLensesWith, abbreviatedFields, makeAdaptorAndInstance)
37 -- When FIXED : Imports to remove:
38 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
39 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
40 import Opaleye hiding (FromField)
41 import Opaleye.Internal.Table (Table(..))
42 ------------------------------------------------------------------------
43 data UserLight = UserLight { userLight_id :: !Int
44 , userLight_username :: !Text
45 , userLight_email :: !Text
46 , userLight_password :: !Text
47 } deriving (Show, Generic)
49 instance GQLType UserLight where
50 typeOptions _ = GAGU.unPrefix "userLight_"
52 toUserLight :: UserDB -> UserLight
53 toUserLight (UserDB id p _ _ u _ _ e _ _ _ ) = UserLight id u e p
56 data UserPoly id pass llogin suser
58 mail staff active djoined =
59 UserDB { user_id :: !id
60 , user_password :: !pass
61 , user_lastLogin :: !llogin
62 , user_isSuperUser :: !suser
64 , user_username :: !uname
65 , user_firstName :: !fname
66 , user_lastName :: !lname
69 , user_isStaff :: !staff
70 , user_isActive :: !active
71 , user_dateJoined :: !djoined
72 } deriving (Show, Generic)
75 type UserWrite = UserPoly (Maybe (Column SqlInt4)) (Column SqlText)
76 (Maybe (Column SqlTimestamptz)) (Column SqlBool)
77 (Column SqlText) (Column SqlText)
78 (Column SqlText) (Column SqlText)
79 (Column SqlBool) (Column SqlBool)
80 (Maybe (Column SqlTimestamptz))
82 type UserRead = UserPoly (Column SqlInt4) (Column SqlText)
83 (Column SqlTimestamptz) (Column SqlBool)
84 (Column SqlText) (Column SqlText)
85 (Column SqlText) (Column SqlText)
86 (Column SqlBool) (Column SqlBool)
87 (Column SqlTimestamptz)
89 type UserReadNull = UserPoly (Column (Nullable SqlInt4)) (Column (Nullable SqlText))
90 (Column (Nullable SqlTimestamptz)) (Column (Nullable SqlBool))
91 (Column (Nullable SqlText)) (Column (Nullable SqlText))
92 (Column (Nullable SqlText)) (Column (Nullable SqlText))
93 (Column (Nullable SqlBool)) (Column (Nullable SqlBool))
94 (Column (Nullable SqlTimestamptz))
96 type UserDB = UserPoly Int Text (Maybe UTCTime) Bool Text Text Text Text Bool Bool UTCTime
98 $(makeAdaptorAndInstance "pUserDB" ''UserPoly)
99 $(makeLensesWith abbreviatedFields ''UserPoly)
101 userTable :: Table UserWrite UserRead
102 userTable = Table "auth_user"
103 (pUserDB UserDB { user_id = optionalTableField "id"
104 , user_password = requiredTableField "password"
105 , user_lastLogin = optionalTableField "last_login"
106 , user_isSuperUser = requiredTableField "is_superuser"
107 , user_username = requiredTableField "username"
108 , user_firstName = requiredTableField "first_name"
109 , user_lastName = requiredTableField "last_name"
110 , user_email = requiredTableField "email"
111 , user_isStaff = requiredTableField "is_staff"
112 , user_isActive = requiredTableField "is_active"
113 , user_dateJoined = optionalTableField "date_joined"
117 instance FromField UserLight where
118 fromField = fromField'
120 instance FromField UserDB where
121 fromField = fromField'
123 $(deriveJSON (unPrefix "userLight_") ''UserLight)
124 $(deriveJSON (unPrefix "user_") ''UserPoly)