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 Gargantext.Core.Mail
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)
29 ------------------------------------------------------------------------
30 ------------------------------------------------------------------------
31 newUsers :: (CmdM env err m, MonadRandom m, HasNodeError err)
32 => [EmailAddress] -> m Int64
34 us' <- mapM newUserQuick us
35 url <- view $ hasConfig . gc_url
37 ------------------------------------------------------------------------
38 newUserQuick :: (MonadRandom m)
39 => Text -> m (NewUser GargPassword)
42 let u = case guessUserName n of
44 Nothing -> panic "[G.D.A.U.N.newUserQuick]: Email invalid"
45 pure (NewUser u n (GargPassword pass))
47 ------------------------------------------------------------------------
48 guessUserName :: Text -> Maybe (Text,Text)
49 guessUserName n = case splitOn "@" n of
50 [u',m'] -> if m' /= "" then Just (u',m')
53 ------------------------------------------------------------------------
54 newUser' :: HasNodeError err
55 => ServerAddress -> NewUser GargPassword -> Cmd err Int64
56 newUser' address u = newUsers' address [u]
58 newUsers' :: HasNodeError err
59 => ServerAddress -> [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 (\u -> mail address (Invitation u)) us
65 printDebug "newUsers'" us
67 ------------------------------------------------------------------------
69 updateUser :: HasNodeError err
70 => SendEmail -> Text -> NewUser GargPassword -> Cmd err Int64
71 updateUser (SendEmail send) server u = do
72 u' <- liftBase $ toUserHash u
73 n <- updateUserDB $ toUserWrite u'
75 True -> liftBase $ mail server (PassUpdate u)
79 ------------------------------------------------------------------------
80 rmUser :: HasNodeError err => User -> Cmd err Int64
81 rmUser (UserName un) = deleteUsers [un]
82 rmUser _ = nodeError NotImplYet
85 rmUsers :: HasNodeError err => [User] -> Cmd err Int64