]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/User/New.hs
[DEBUG]
[gargantext.git] / src / Gargantext / Database / Action / User / New.hs
1 {-|
2 Module : Gargantext.Database.Action.User.New
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9 -}
10
11 {-# OPTIONS_GHC -fno-warn-orphans #-}
12
13 module Gargantext.Database.Action.User.New
14 where
15
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)
28
29 ------------------------------------------------------------------------
30 ------------------------------------------------------------------------
31 newUsers :: (CmdM env err m, MonadRandom m, HasNodeError err)
32 => [EmailAddress] -> m Int64
33 newUsers us = do
34 us' <- mapM newUserQuick us
35 url <- view $ hasConfig . gc_url
36 newUsers' url us'
37 ------------------------------------------------------------------------
38 newUserQuick :: (MonadRandom m)
39 => Text -> m (NewUser GargPassword)
40 newUserQuick n = do
41 pass <- gargPass
42 let u = case guessUserName n of
43 Just (u', _m) -> u'
44 Nothing -> panic "[G.D.A.U.N.newUserQuick]: Email invalid"
45 pure (NewUser u n (GargPassword pass))
46
47 ------------------------------------------------------------------------
48 guessUserName :: Text -> Maybe (Text,Text)
49 guessUserName n = case splitOn "@" n of
50 [u',m'] -> if m' /= "" then Just (u',m')
51 else Nothing
52 _ -> Nothing
53 ------------------------------------------------------------------------
54 newUser' :: HasNodeError err
55 => ServerAdress -> NewUser GargPassword -> Cmd err Int64
56 newUser' address u = newUsers' address [u]
57
58 newUsers' :: HasNodeError err
59 => ServerAdress -> [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
66 pure r
67 ------------------------------------------------------------------------
68
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'
74 _ <- case send of
75 True -> liftBase $ mail server (PassUpdate u)
76 False -> pure ()
77 pure n
78
79 ------------------------------------------------------------------------
80 rmUser :: HasNodeError err => User -> Cmd err Int64
81 rmUser (UserName un) = deleteUsers [un]
82 rmUser _ = nodeError NotImplYet
83
84 -- TODO
85 rmUsers :: HasNodeError err => [User] -> Cmd err Int64
86 rmUsers [] = pure 0
87 rmUsers _ = undefined