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-orphans #-}
13 module Gargantext.Database.Action.User.New
16 import Control.Lens (view)
17 import Control.Monad.Random
18 import Data.Text (Text, splitOn)
19 import qualified Data.Text as Text
20 import Gargantext.Core.Mail
21 import Gargantext.Core.Mail.Types (HasMail, mailSettings)
22 import Gargantext.Core.Types.Individu
23 import Gargantext.Database.Action.Flow (getOrMkRoot)
24 import Gargantext.Database.Prelude
25 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..), nodeError, NodeError(..))
26 import Gargantext.Database.Query.Table.User
27 import Gargantext.Prelude
28 import Gargantext.Prelude.Crypto.Pass.User (gargPass)
29 import Gargantext.Prelude.Mail.Types (MailConfig)
31 ------------------------------------------------------------------------
32 newUsers :: (CmdM env err m, MonadRandom m, HasNodeError err, HasMail env)
33 => [EmailAddress] -> m Int64
35 us' <- mapM newUserQuick us
36 config <- view $ mailSettings
39 ------------------------------------------------------------------------
41 updateUsersPassword :: (CmdM env err m, MonadRandom m, HasNodeError err, HasMail env)
42 => [EmailAddress] -> m Int64
43 updateUsersPassword us = do
44 us' <- mapM newUserQuick us
45 config <- view $ mailSettings
46 _ <- mapM (\u -> updateUser (SendEmail True) config u) us'
49 ------------------------------------------------------------------------
50 ------------------------------------------------------------------------
51 newUserQuick :: (MonadRandom m)
52 => Text -> m (NewUser GargPassword)
55 let u = case guessUserName n of
57 Nothing -> panic "[G.D.A.U.N.newUserQuick]: Email invalid"
58 pure (NewUser u n (GargPassword pass))
59 ------------------------------------------------------------------------
61 ------------------------------------------------------------------------
63 -- guess username and normalize it (Text.toLower)
64 guessUserName :: Text -> Maybe (Text,Text)
65 guessUserName n = case splitOn "@" n of
66 [u',m'] -> if m' /= "" then Just (Text.toLower u',m')
69 ------------------------------------------------------------------------
70 newUser' :: HasNodeError err
71 => MailConfig -> NewUser GargPassword -> Cmd err Int64
72 newUser' cfg u = newUsers' cfg [u]
74 newUsers' :: HasNodeError err
75 => MailConfig -> [NewUser GargPassword] -> Cmd err Int64
77 us' <- liftBase $ mapM toUserHash us
78 r <- insertUsers $ map toUserWrite us'
79 _ <- mapM getOrMkRoot $ map (\u -> UserName (_nu_username u)) us
80 _ <- mapM (\u -> mail cfg (Invitation u)) us
81 printDebug "newUsers'" us
83 ------------------------------------------------------------------------
84 updateUser :: HasNodeError err
85 => SendEmail -> MailConfig -> NewUser GargPassword -> Cmd err Int64
86 updateUser (SendEmail send) cfg u = do
87 u' <- liftBase $ toUserHash u
88 n <- updateUserDB $ toUserWrite u'
90 True -> mail cfg (PassUpdate u)
94 ------------------------------------------------------------------------
95 rmUser :: HasNodeError err => User -> Cmd err Int64
96 rmUser (UserName un) = deleteUsers [un]
97 rmUser _ = nodeError NotImplYet
100 rmUsers :: HasNodeError err => [User] -> Cmd err Int64
102 rmUsers _ = undefined