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(..))
29 import qualified Data.List as List
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 url <- view $ config . gc_url
40 ------------------------------------------------------------------------
41 newUserQuick :: (MonadRandom m)
42 => Text -> m (NewUser GargPassword)
45 let u = case guessUserName n of
47 Nothing -> panic "[G.D.A.U.N.newUserQuick]: Email invalid"
48 pure (NewUser u n (GargPassword pass))
50 ------------------------------------------------------------------------
51 isEmail :: Text -> Bool
52 isEmail = ((==) 2) . List.length . (splitOn "@")
54 guessUserName :: Text -> Maybe (Text,Text)
55 guessUserName n = case splitOn "@" n of
56 [u',m'] -> if m' /= "" then Just (u',m')
59 ------------------------------------------------------------------------
60 newUser' :: HasNodeError err
61 => Text -> NewUser GargPassword -> Cmd err Int64
62 newUser' address u = newUsers' address [u]
64 newUsers' :: HasNodeError err
65 => Text -> [NewUser GargPassword] -> Cmd err Int64
66 newUsers' address us = do
67 us' <- liftBase $ mapM toUserHash us
68 r <- insertUsers $ map toUserWrite us'
69 _ <- mapM getOrMkRoot $ map (\u -> UserName (_nu_username u)) us
70 _ <- liftBase $ mapM (mail Invitation address) us
72 ------------------------------------------------------------------------
74 data SendEmail = SendEmail Bool
76 updateUser :: HasNodeError err
77 => SendEmail -> Text -> NewUser GargPassword -> Cmd err Int64
78 updateUser (SendEmail send) address u = do
79 u' <- liftBase $ toUserHash u
80 n <- updateUserDB $ toUserWrite u'
82 True -> liftBase $ mail Update address u
86 ------------------------------------------------------------------------
87 data Mail = Invitation
91 -- TODO gargantext.ini config
92 mail :: Mail -> Text -> NewUser GargPassword -> IO ()
93 mail mtype address nu@(NewUser u m _) = gargMail (GargMail m (Just u) subject body)
95 subject = "[Your Garg Account]"
96 body = bodyWith mtype address nu
98 bodyWith :: Mail -> Text -> NewUser GargPassword -> Text
99 bodyWith Invitation add nu = logInstructions add nu
100 bodyWith Update add nu = updateInstructions add nu
103 -- TODO put this in a configurable file (path in gargantext.ini)
104 logInstructions :: Text -> NewUser GargPassword -> Text
105 logInstructions address (NewUser u _ (GargPassword p)) =
107 , "You have been invited to test the new GarganText platform!"
109 , "You can log in to: " <> address
110 , "Your username is: " <> u
111 , "Your password is: " <> p
113 , "Please read the full terms of use on:"
114 , "https://gitlab.iscpif.fr/humanities/tofu/tree/master"
116 , "Your feedback will be valuable for further development"
117 , "of the platform, do not hesitate to contact us and"
118 , "to contribute on our forum:"
119 , " https://discourse.iscpif.fr/c/gargantext"
121 , "With our best regards,"
123 , "The Gargantext Team (CNRS)"
126 updateInstructions :: Text -> NewUser GargPassword -> Text
127 updateInstructions address (NewUser u _ (GargPassword p)) =
129 , "Your account have been updated on the GarganText platform!"
131 , "You can log in to: " <> address
132 , "Your username is: " <> u
133 , "Your password is: " <> p
135 , "As reminder, please read the full terms of use on:"
136 , "https://gitlab.iscpif.fr/humanities/tofu/tree/master"
138 , "Your feedback is always valuable for further development"
139 , "of the platform, do not hesitate to contact us and"
140 , "to contribute on our forum:"
141 , " https://discourse.iscpif.fr/c/gargantext"
143 , "With our best regards,"
145 , "The Gargantext Team (CNRS)"
149 ------------------------------------------------------------------------
150 rmUser :: HasNodeError err => User -> Cmd err Int64
151 rmUser (UserName un) = deleteUsers [un]
152 rmUser _ = nodeError NotImplYet
155 rmUsers :: HasNodeError err => [User] -> Cmd err Int64
157 rmUsers _ = undefined