Skip to content

Commit bc7ad18

Browse files
authored
Merge pull request #78 from muskansoninmh/ft_smtp_configuration_using_cli
Ft smtp configuration using cli
2 parents baa6f40 + a04b111 commit bc7ad18

11 files changed

Lines changed: 759 additions & 8 deletions

File tree

api/deployment.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ type CoreAppData struct {
8383
} `json:"apps"`
8484
}
8585

86+
type SmtpConfigSchema struct {
87+
FromEmailId string `json:"FromEmailId"`
88+
FromName string `json:"FromName"`
89+
IsSsl bool `json:"IsSsl"`
90+
Key string `json:"Key"`
91+
Password string `json:"Password"`
92+
Provider string `json:"provider"`
93+
Secret string `json:"Secret"`
94+
SmtpHost string `json:"SmtpHost"`
95+
SmtpPort int `json:"SmtpPort"`
96+
UserName string `json:"UserName"`
97+
}
98+
99+
type VerifySmtpConfigSchema struct {
100+
SmtpConfigSchema
101+
EmailId string `json:"emailId"`
102+
Message string `json:"message"`
103+
Subject string `json:"subject"`
104+
}
105+
106+
type VerifySmtpConfigError struct {
107+
Description string `json:"description"`
108+
Message string `json:"message"`
109+
}
110+
86111
func GetSites() (*SitesReponse, error) {
87112
var url string
88113
url = conf.AdminConsoleAPIDomain + "/deployment/sites?ownerUid=&"
@@ -228,3 +253,61 @@ func CardPay() (bool, error) {
228253
}
229254
return true, nil
230255
}
256+
257+
func GetSMTPConfiguration() (*SmtpConfigSchema, error) {
258+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/config?"
259+
260+
var resultResp SmtpConfigSchema
261+
resp, err := request.Rest(http.MethodGet, url, nil, "")
262+
if err != nil {
263+
return nil, err
264+
}
265+
266+
err = json.Unmarshal(resp, &resultResp)
267+
if err != nil {
268+
return nil, err
269+
}
270+
return &resultResp, nil
271+
}
272+
273+
func AddSMTPConfiguration(data SmtpConfigSchema) (*SmtpConfigSchema, error) {
274+
var url string
275+
if strings.ToLower(data.Provider) == "mailazy"{
276+
url = conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/smtpprovider?"
277+
} else {
278+
url = conf.AdminConsoleAPIDomain + "/deployment/smtp-settings?"
279+
}
280+
body, _ := json.Marshal(data)
281+
var resultResp SmtpConfigSchema
282+
resp, err := request.Rest(http.MethodPost, url, nil, string(body))
283+
if err != nil {
284+
return nil, err
285+
}
286+
err = json.Unmarshal(resp, &resultResp)
287+
if err != nil {
288+
return nil, err
289+
}
290+
return &resultResp, nil
291+
}
292+
293+
func VerifySMTPConfiguration(data VerifySmtpConfigSchema) error {
294+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/verifysmtpsettings?"
295+
body, _ := json.Marshal(data)
296+
297+
_, err := request.Rest(http.MethodPost, url, nil, string(body))
298+
299+
if err != nil {
300+
return err
301+
}
302+
return nil
303+
}
304+
305+
func DeleteSMTPConfiguration() error {
306+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/reset?"
307+
308+
_, err := request.Rest(http.MethodPost, url, nil, "")
309+
if err != nil {
310+
return err
311+
}
312+
return nil
313+
}

cmd/add/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/loginradius/lr-cli/cmd/add/hooks"
1010
"github.com/loginradius/lr-cli/cmd/add/social"
1111
"github.com/loginradius/lr-cli/cmd/add/accessRestriction"
12+
"github.com/loginradius/lr-cli/cmd/add/smtpConfiguration"
1213

1314
"github.com/spf13/cobra"
1415
)
@@ -48,5 +49,8 @@ func NewaddCmd() *cobra.Command {
4849
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
4950
cmd.AddCommand(accessRestrictionCmd)
5051

52+
smtpConfigurationCmd := smtpConfiguration.NewsmtpConfigurationCmd()
53+
cmd.AddCommand(smtpConfigurationCmd)
54+
5155
return cmd
5256
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package smtpConfiguration
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
"reflect"
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/AlecAivazis/survey/v2"
11+
"github.com/loginradius/lr-cli/api"
12+
"strconv"
13+
"github.com/loginradius/lr-cli/cmdutil"
14+
"github.com/loginradius/lr-cli/prompt"
15+
"github.com/loginradius/lr-cli/config"
16+
17+
18+
"github.com/spf13/cobra"
19+
)
20+
21+
22+
var conf = config.GetInstance()
23+
24+
25+
func NewsmtpConfigurationCmd() *cobra.Command {
26+
27+
cmd := &cobra.Command{
28+
Use: "smtp-configuration",
29+
Short: "Add a SMTP configuration",
30+
Long: `Configure your SMTP email settings to allow LoginRadius to send email from your email server automatically.`,
31+
Example: heredoc.Doc(`
32+
$ lr add smtp-configuration
33+
SMTP Providers: Mailazy
34+
If you don't have a mailazy account. Please Create a mailazy account via https://app.mailazy.com/signup
35+
? Key: <Key>
36+
? Secret: <Secret>
37+
? From Name: <Name>
38+
? From Email Id: <Email ID>
39+
40+
SMTP settings are saved
41+
42+
? Send an email to verify your configuration settings are correct (Y/N): Yes
43+
? To Email: <Email ID for Verification>
44+
SMTP settings are verified
45+
`),
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
return addAccessRestriction()
48+
},
49+
}
50+
51+
return cmd
52+
}
53+
54+
func addAccessRestriction() error {
55+
var smtpSchema api.SmtpConfigSchema
56+
var smtpLabels = [] string {"Provider","Key","Secret", "SmtpHost", "SmtpPort",
57+
"FromName","FromEmailId","UserName","Password", "IsSsl"}
58+
var smtpProviders []string
59+
var num int
60+
var isSsl bool
61+
var isMailazy bool
62+
num = len(cmdutil.SmtpProviders)
63+
for i := 0; i < num; i++ {
64+
smtpProviders = append(smtpProviders, cmdutil.SmtpProviders[i].Name)
65+
}
66+
for _,val := range smtpLabels {
67+
68+
if val == "Provider" {
69+
err := prompt.SurveyAskOne(&survey.Select{
70+
Message: cmdutil.SmtpOptionNames[val] + " :",
71+
Options: smtpProviders,
72+
}, &num)
73+
if err != nil {
74+
return err
75+
}
76+
} else if val == "IsSsl" && num != 0 {
77+
isSsl = cmdutil.SmtpProviders[num].EnableSSL
78+
if err := prompt.Confirm(cmdutil.SmtpOptionNames[val] + ":",
79+
&isSsl); err != nil {
80+
return err
81+
}
82+
} else {
83+
if num == 0 {
84+
isMailazy = ( val != "Password" && val != "IsSsl" && val != "UserName" && val != "SmtpHost" && val != "SmtpPort")
85+
} else if num == 9 {
86+
isMailazy = (val != "Key" && val != "Secret" )
87+
} else if num != 0 {
88+
isMailazy = (val != "Key" && val != "Secret" && val != "SmtpHost" && val != "SmtpPort")
89+
}
90+
if isMailazy == true {
91+
configObj := reflect.ValueOf(&smtpSchema).Elem()
92+
field := configObj.FieldByName(val)
93+
var promptRes string
94+
95+
if val == "Key" {
96+
fmt.Println("(If you don't have a mailazy account. Please Create a mailazy account via https://app.mailazy.com/signup)")
97+
}
98+
prompt.SurveyAskOne(&survey.Input{
99+
Message: cmdutil.SmtpOptionNames[val] + ":",
100+
}, &promptRes, survey.WithValidator(survey.Required))
101+
102+
if strings.TrimSpace(promptRes) == "" {
103+
return errors.New(cmdutil.SmtpOptionNames[val] + " is required")
104+
}
105+
if val == "FromEmailId" && !cmdutil.ValidateEmail.MatchString(promptRes) {
106+
return &cmdutil.FlagError{Err: errors.New("Invalid email format")}
107+
}
108+
if val == "SmtpPort" {
109+
smtpPort, err := strconv.ParseInt(promptRes, 10, 64)
110+
if err != nil {
111+
return &cmdutil.FlagError{Err: errors.New("Please enter the valid SMTP Port")}
112+
}
113+
field.SetInt(smtpPort)
114+
} else {
115+
field.SetString(strings.TrimSpace(promptRes))
116+
}
117+
}
118+
}
119+
}
120+
if num != 9 {
121+
smtpSchema.Provider = smtpProviders[num]
122+
}
123+
smtpSchema.IsSsl = isSsl
124+
125+
if num == 0 {
126+
if strings.Contains(conf.HubPageDomain ,"devhub.") {
127+
smtpSchema.SmtpHost = "devsmtp.mailazy.com";
128+
smtpSchema.SmtpPort = 588;
129+
} else {
130+
smtpSchema.SmtpHost = "smtp.mailazy.com"
131+
smtpSchema.SmtpPort = 587;
132+
}
133+
smtpSchema.UserName = smtpSchema.Key
134+
smtpSchema.Password = smtpSchema.Secret
135+
smtpSchema.IsSsl = cmdutil.SmtpProviders[num].EnableSSL
136+
137+
} else if num != 9 {
138+
var err error
139+
smtpSchema.SmtpHost = cmdutil.SmtpProviders[num].SmtpHost
140+
smtpSchema.SmtpPort,err = strconv.Atoi(cmdutil.SmtpProviders[num].SmtpPort)
141+
if err != nil {
142+
return err
143+
}
144+
}
145+
146+
var _, err = api.AddSMTPConfiguration(smtpSchema)
147+
if err != nil {
148+
return err
149+
}
150+
fmt.Println("SMTP settings are saved")
151+
var verify bool
152+
153+
if err := prompt.Confirm("Send an email to verify your configuration settings are correct?",
154+
&verify); err != nil {
155+
return err
156+
}
157+
if !verify {
158+
return nil
159+
}
160+
var emailid string
161+
var verifySchema api.VerifySmtpConfigSchema
162+
163+
prompt.SurveyAskOne(&survey.Input{
164+
Message: "To Email :",
165+
}, &emailid, survey.WithValidator(survey.Required))
166+
167+
if !cmdutil.ValidateEmail.MatchString(emailid) {
168+
return &cmdutil.FlagError{Err: errors.New("Invalid email format")}
169+
}
170+
171+
var respMap map[string]string
172+
data, _ := json.Marshal(smtpSchema)
173+
json.Unmarshal(data, &respMap)
174+
175+
for _, val := range smtpLabels {
176+
if val != "IsSsl" && val != "SmtpPort" {
177+
configObj := reflect.ValueOf(&verifySchema).Elem()
178+
field := configObj.FieldByName(val)
179+
field.SetString(respMap[val])
180+
}
181+
}
182+
183+
verifySchema.EmailId = emailid
184+
verifySchema.Message = "This is the test email to validate your SMTP credentials for LoginRadius' User Registration feature on your website. <br><br>The SMTP server credentials are verified.<br><br>Thank you,<br>LoginRadius Team"
185+
verifySchema.Subject = "Test Email - LoginRadius"
186+
verifySchema.SmtpPort = smtpSchema.SmtpPort
187+
verifySchema.IsSsl = smtpSchema.IsSsl
188+
189+
err = api.VerifySMTPConfiguration(verifySchema)
190+
if err != nil {
191+
fmt.Println("Error: " + strings.Replace(err.Error(), "Learn more at", "", 1))
192+
return nil
193+
}
194+
fmt.Println("SMTP settings are verified")
195+
196+
197+
return nil
198+
199+
}

cmd/delete/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/loginradius/lr-cli/cmd/delete/social"
1111
"github.com/loginradius/lr-cli/cmd/delete/accessRestriction"
12+
"github.com/loginradius/lr-cli/cmd/delete/smtpConfiguration"
1213

1314
"github.com/spf13/cobra"
1415
)
@@ -48,5 +49,8 @@ func NewdeleteCmd() *cobra.Command {
4849
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
4950
cmd.AddCommand(accessRestrictionCmd)
5051

52+
smtpConfigurationCmd := smtpConfiguration.NewsmtpConfigurationCmd()
53+
cmd.AddCommand(smtpConfigurationCmd)
54+
5155
return cmd
5256
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package smtpConfiguration
2+
3+
import (
4+
5+
"fmt"
6+
7+
"github.com/MakeNowJust/heredoc"
8+
"github.com/loginradius/lr-cli/api"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func NewsmtpConfigurationCmd() *cobra.Command {
14+
15+
16+
cmd := &cobra.Command{
17+
Use: "smtp-configuration",
18+
Short: "Delete/Reset the SMTP Configuration",
19+
Long: `Use this command to remove/reset the configured SMTP email setting.`,
20+
Example: heredoc.Doc(`$ lr delete smtp-configuration
21+
Settings have been reset successfully
22+
`),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
25+
err := api.DeleteSMTPConfiguration()
26+
if err != nil {
27+
return nil
28+
}
29+
fmt.Println("Settings have been reset successfully")
30+
return nil
31+
32+
},
33+
}
34+
35+
return cmd
36+
}

0 commit comments

Comments
 (0)