2 Module : Gargantext.Database.Action.User.New
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 module Gargantext.Database.Action.User.New
17 import Control.Lens (view)
18 import Control.Monad.Random
19 import Data.Text (Text, unlines, splitOn)
20 import Gargantext.Core.Types.Individu
21 import Gargantext.Database.Action.Flow (getOrMkRoot)
22 import Gargantext.Database.Prelude
23 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..), nodeError, NodeError(..))
24 import Gargantext.Database.Query.Table.User
25 import Gargantext.Prelude
26 import Gargantext.Prelude.Config
27 import Gargantext.Prelude.Crypto.Pass.User (gargPass)
28 import Gargantext.Prelude.Mail (gargMail, GargMail(..))
31 ------------------------------------------------------------------------
32 type EmailAddress = Text
33 ------------------------------------------------------------------------
34 newUsers :: (CmdM env err m, MonadRandom m, HasNodeError err)
35 => [EmailAddress] -> m Int64
37 us' <- mapM newUserQuick us
38 conf <- view hasConfig
39 newUsers' (_gc_url conf) us'
40 ------------------------------------------------------------------------
41 newUserQuick :: (MonadRandom m)
42 => Text -> m (NewUser GargPassword)
45 let (u,_m) = guessUserName n
46 pure (NewUser u n (GargPassword pass))
48 guessUserName :: Text -> (Text,Text)
49 guessUserName n = case splitOn "@" n of
50 [u',m'] -> if m' /= "" then (u',m')
51 else panic "Email Invalid"
52 _ -> panic "Email invalid"
53 ------------------------------------------------------------------------
54 newUser' :: HasNodeError err
55 => Text -> NewUser GargPassword -> Cmd err Int64
56 newUser' address u = newUsers' address [u]
58 newUsers' :: HasNodeError err
59 => Text -> [NewUser GargPassword] -> Cmd err Int64
60 newUsers' address us = do
61 us' <- liftBase $ mapM toUserHash us
62 r <- insertUsers $ map toUserWrite us'
63 _ <- mapM getOrMkRoot $ map (\u -> UserName (_nu_username u)) us
64 _ <- liftBase $ mapM (mail Invitation address) us
66 ------------------------------------------------------------------------
67 updateUser :: HasNodeError err
68 => Text -> NewUser GargPassword -> Cmd err Int64
69 updateUser address u = do
70 u' <- liftBase $ toUserHash u
71 n <- updateUserDB $ toUserWrite u'
72 _ <- liftBase $ mail Update address u
75 ------------------------------------------------------------------------
76 data Mail = Invitation
80 -- TODO gargantext.ini config
81 mail :: Mail -> Text -> NewUser GargPassword -> IO ()
82 mail mtype address nu@(NewUser u m _) = gargMail (GargMail m (Just u) subject body)
84 subject = "[Your Garg Account]"
85 body = bodyWith mtype address nu
87 bodyWith :: Mail -> Text -> NewUser GargPassword -> Text
88 bodyWith Invitation add nu = logInstructions add nu
89 bodyWith Update add nu = updateInstructions add nu
92 -- TODO put this in a configurable file (path in gargantext.ini)
93 logInstructions :: Text -> NewUser GargPassword -> Text
94 logInstructions address (NewUser u _ (GargPassword p)) =
96 , "You have been invited to test the new GarganText platform!"
98 , "You can log in to: " <> address
99 , "Your username is: " <> u
100 , "Your password is: " <> p
102 , "Please read the full terms of use on:"
103 , "https://gitlab.iscpif.fr/humanities/tofu/tree/master"
105 , "Your feedback will be valuable for further development"
106 , "of the platform, do not hesitate to contact us and"
107 , "to contribute on our forum:"
108 , " https://discourse.iscpif.fr/c/gargantext"
110 , "With our best regards,"
112 , "The Gargantext Team (CNRS)"
115 updateInstructions :: Text -> NewUser GargPassword -> Text
116 updateInstructions address (NewUser u _ (GargPassword p)) =
118 , "Your account have been updated on the GarganText platform!"
120 , "You can log in to: " <> address
121 , "Your username is: " <> u
122 , "Your password is: " <> p
124 , "As reminder, please read the full terms of use on:"
125 , "https://gitlab.iscpif.fr/humanities/tofu/tree/master"
127 , "Your feedback is always valuable for further development"
128 , "of the platform, do not hesitate to contact us and"
129 , "to contribute on our forum:"
130 , " https://discourse.iscpif.fr/c/gargantext"
132 , "With our best regards,"
134 , "The Gargantext Team (CNRS)"
138 ------------------------------------------------------------------------
139 rmUser :: HasNodeError err => User -> Cmd err Int64
140 rmUser (UserName un) = deleteUsers [un]
141 rmUser _ = nodeError NotImplYet
144 rmUsers :: HasNodeError err => [User] -> Cmd err Int64
146 rmUsers _ = undefined