]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/User/New.hs
[MERGE]
[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 pure r
66 ------------------------------------------------------------------------
67
68 updateUser :: HasNodeError err
69 => SendEmail -> Text -> NewUser GargPassword -> Cmd err Int64
70 updateUser (SendEmail send) server u = do
71 u' <- liftBase $ toUserHash u
72 n <- updateUserDB $ toUserWrite u'
73 _ <- case send of
74 True -> liftBase $ mail server (PassUpdate u)
75 False -> pure ()
76 pure n
77
78 ------------------------------------------------------------------------
79 rmUser :: HasNodeError err => User -> Cmd err Int64
80 rmUser (UserName un) = deleteUsers [un]
81 rmUser _ = nodeError NotImplYet
82
83 -- TODO
84 rmUsers :: HasNodeError err => [User] -> Cmd err Int64
85 rmUsers [] = pure 0
86 rmUsers _ = undefined