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.Core.Types.Individu (GargPassword, toGargPassword)
29 import Gargantext.Database.Prelude (fromField')
30 import Gargantext.Prelude
31 import GHC.Generics (Generic)
32 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
33 import Data.Aeson.TH (deriveJSON)
35 -- FIXME PLZ : the import below leads to an error, why ?
36 -- import Gargantext.Database.Schema.Prelude hiding (makeLensesWith, abbreviatedFields, makeAdaptorAndInstance)
38 -- When FIXED : Imports to remove:
39 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
40 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
41 import Opaleye hiding (FromField)
42 import Opaleye.Internal.Table (Table(..))
43 ------------------------------------------------------------------------
44 data UserLight = UserLight { userLight_id :: !Int
45 , userLight_username :: !Text
46 , userLight_email :: !Text
47 , userLight_password :: !GargPassword
48 , userLight_forgot_password_uuid :: !(Maybe Text)
49 } deriving (Show, Generic)
50 instance GQLType UserLight where
51 typeOptions _ = GAGU.unPrefix "userLight_"
53 toUserLight :: UserDB -> UserLight
54 toUserLight (UserDB { user_id
57 , user_email }) = UserLight { userLight_id = user_id
58 , userLight_username = user_username
59 , userLight_email = user_email
60 , userLight_password = toGargPassword user_password
61 , userLight_forgot_password_uuid = Nothing }
64 data UserPoly id pass llogin suser
66 mail staff active djoined
68 UserDB { user_id :: !id
69 , user_password :: !pass
70 , user_lastLogin :: !llogin
71 , user_isSuperUser :: !suser
73 , user_username :: !uname
74 , user_firstName :: !fname
75 , user_lastName :: !lname
78 , user_isStaff :: !staff
79 , user_isActive :: !active
80 , user_dateJoined :: !djoined
82 , user_forgot_password_uuid :: !fpuuid
83 } deriving (Show, Generic)
86 type UserWrite = UserPoly (Maybe (Column SqlInt4)) (Column SqlText)
87 (Maybe (Column SqlTimestamptz)) (Column SqlBool)
88 (Column SqlText) (Column SqlText)
89 (Column SqlText) (Column SqlText)
90 (Column SqlBool) (Column SqlBool)
91 (Maybe (Column SqlTimestamptz))
92 (Maybe (Column SqlText))
94 type UserRead = UserPoly (Column SqlInt4) (Column SqlText)
95 (Column SqlTimestamptz) (Column SqlBool)
96 (Column SqlText) (Column SqlText)
97 (Column SqlText) (Column SqlText)
98 (Column SqlBool) (Column SqlBool)
99 (Column SqlTimestamptz)
102 type UserReadNull = UserPoly (Column (Nullable SqlInt4)) (Column (Nullable SqlText))
103 (Column (Nullable SqlTimestamptz)) (Column (Nullable SqlBool))
104 (Column (Nullable SqlText)) (Column (Nullable SqlText))
105 (Column (Nullable SqlText)) (Column (Nullable SqlText))
106 (Column (Nullable SqlBool)) (Column (Nullable SqlBool))
107 (Column (Nullable SqlTimestamptz))
108 (Column (Nullable SqlText))
110 type UserDB = UserPoly Int Text (Maybe UTCTime) Bool Text Text Text Text Bool Bool UTCTime (Maybe Text)
112 $(makeAdaptorAndInstance "pUserDB" ''UserPoly)
113 $(makeLensesWith abbreviatedFields ''UserPoly)
115 userTable :: Table UserWrite UserRead
116 userTable = Table "auth_user"
117 (pUserDB UserDB { user_id = optionalTableField "id"
118 , user_password = requiredTableField "password"
119 , user_lastLogin = optionalTableField "last_login"
120 , user_isSuperUser = requiredTableField "is_superuser"
121 , user_username = requiredTableField "username"
122 , user_firstName = requiredTableField "first_name"
123 , user_lastName = requiredTableField "last_name"
124 , user_email = requiredTableField "email"
125 , user_isStaff = requiredTableField "is_staff"
126 , user_isActive = requiredTableField "is_active"
127 , user_dateJoined = optionalTableField "date_joined"
128 , user_forgot_password_uuid = optionalTableField "forgot_password_uuid"
132 instance FromField UserLight where
133 fromField = fromField'
135 instance FromField UserDB where
136 fromField = fromField'
138 $(deriveJSON (unPrefix "userLight_") ''UserLight)
139 $(deriveJSON (unPrefix "user_") ''UserPoly)