Skip to content

Commit cf4ca6b

Browse files
Dev to Develop merge (#80)
* Added the feature of setting access restriction type and email/domain whitelist blaklist * Added the feature of smtp configuration * Updated messages and docs * resolved the delete command bug if one domain exist * Updated content and all other changes * resolved all conflicts * resolved all the bugs in cli --------- Co-authored-by: muskansoninmh <muskansoninmh1@gmail.com>
1 parent edfbaf0 commit cf4ca6b

32 files changed

Lines changed: 1395 additions & 75 deletions

File tree

api/auth.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func SitesBasic(tokens *SitesToken) error {
131131
if err != nil {
132132
return err
133133
}
134-
_, err = GetAppsInfo()
134+
_,_, err = GetAppsInfo()
135135
if err != nil {
136136
return err
137137
}
@@ -140,19 +140,20 @@ func SitesBasic(tokens *SitesToken) error {
140140

141141
}
142142

143-
func GetAppsInfo() (map[int64]SitesReponse, error) {
143+
func GetAppsInfo() (map[int64]SitesReponse,map[int64]SharedSitesReponse, error) {
144144
var Apps CoreAppData
145145

146146
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
147147
data, err := request.Rest(http.MethodGet, coreAppData, nil, "")
148148
if err != nil {
149-
return nil, err
149+
return nil,nil, err
150150
}
151151
err = json.Unmarshal(data, &Apps)
152152
if err != nil {
153-
return nil, err
153+
return nil,nil, err
154154
}
155-
return storeSiteInfo(Apps), nil
155+
apps, sharedApps := storeSiteInfo(Apps)
156+
return apps,sharedApps, nil
156157
}
157158

158159
func CurrentID() (int64, error) {
@@ -203,12 +204,17 @@ func IsPasswordLessEnabled(features FeatureSchema) bool {
203204
return false
204205
}
205206

206-
func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
207+
func storeSiteInfo(data CoreAppData) (map[int64]SitesReponse, map[int64]SharedSitesReponse) {
207208
siteInfo := make(map[int64]SitesReponse, len(data.Apps.Data))
209+
sharedsiteInfo := make(map[int64]SharedSitesReponse, len(data.Apps.Data))
208210
for _, app := range data.Apps.Data {
209211
siteInfo[app.Appid] = app
210212
}
211213
obj, _ := json.Marshal(siteInfo)
214+
for _, app := range data.SharedApps.Data {
215+
sharedsiteInfo[app.Appid] = app
216+
}
217+
obj, _ = json.Marshal(sharedsiteInfo)
212218
cmdutil.WriteFile("siteInfo.json", obj)
213219
currentId, err := CurrentID()
214220
if err == nil {
@@ -218,7 +224,7 @@ func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
218224
cmdutil.WriteFile("currentSite.json", obj)
219225
}
220226
}
221-
return siteInfo
227+
return siteInfo,sharedsiteInfo
222228
}
223229

224230
func UpdatePhoneLogin(feature string, status bool) (*FeatureSchema, error) {

api/deployment.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ type SitesReponse struct {
4949
} `json:"ProductPlan"`
5050
}
5151

52+
type SharedSitesReponse struct {
53+
Appname string `json:"AppName"`
54+
Domain string `json:"Domain"`
55+
Appid int64 `json:"AppId"`
56+
AppKey string `json:"ApiKey"`
57+
AppSecret string `json:"ApiSecret"`
58+
Role []string `json:"Role"`
59+
AdditionalPermissions []string `json:"AdditionalPermissions"`
60+
Recurlyaccountcode *string `json:"RecurlyAccountCode"`
61+
Userlimit int `json:"UserLimit"`
62+
Domainlimit int `json:"DomainLimit"`
63+
Apiversion string `json:"ApiVersion"`
64+
Israasenabled bool `json:"IsRaasEnabled"`
65+
Ownerid string `json:"OwnerId"`
66+
Productplan *struct {
67+
Name string `json:"Name"`
68+
Expirytime time.Time `json:"ExpiryTime"`
69+
Billingcycle interface{} `json:"BillingCycle"`
70+
Fromdate interface{} `json:"FromDate"`
71+
} `json:"ProductPlan"`
72+
}
73+
5274
type HostedPageResponse struct {
5375
Pages []struct {
5476
Pagetype string `json:"PageType"`
@@ -81,6 +103,34 @@ type CoreAppData struct {
81103
Apps struct {
82104
Data []SitesReponse `json:"Data"`
83105
} `json:"apps"`
106+
SharedApps struct {
107+
Data []SharedSitesReponse `json:"Data"`
108+
} `json:"sharedApps"`
109+
}
110+
111+
type SmtpConfigSchema struct {
112+
FromEmailId string `json:"FromEmailId"`
113+
FromName string `json:"FromName"`
114+
IsSsl bool `json:"IsSsl"`
115+
Key string `json:"Key"`
116+
Password string `json:"Password"`
117+
Provider string `json:"provider"`
118+
Secret string `json:"Secret"`
119+
SmtpHost string `json:"SmtpHost"`
120+
SmtpPort int `json:"SmtpPort"`
121+
UserName string `json:"UserName"`
122+
}
123+
124+
type VerifySmtpConfigSchema struct {
125+
SmtpConfigSchema
126+
EmailId string `json:"emailId"`
127+
Message string `json:"message"`
128+
Subject string `json:"subject"`
129+
}
130+
131+
type VerifySmtpConfigError struct {
132+
Description string `json:"description"`
133+
Message string `json:"message"`
84134
}
85135

86136
func GetSites() (*SitesReponse, error) {
@@ -228,3 +278,61 @@ func CardPay() (bool, error) {
228278
}
229279
return true, nil
230280
}
281+
282+
func GetSMTPConfiguration() (*SmtpConfigSchema, error) {
283+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/config?"
284+
285+
var resultResp SmtpConfigSchema
286+
resp, err := request.Rest(http.MethodGet, url, nil, "")
287+
if err != nil {
288+
return nil, err
289+
}
290+
291+
err = json.Unmarshal(resp, &resultResp)
292+
if err != nil {
293+
return nil, err
294+
}
295+
return &resultResp, nil
296+
}
297+
298+
func AddSMTPConfiguration(data SmtpConfigSchema) (*SmtpConfigSchema, error) {
299+
var url string
300+
if strings.ToLower(data.Provider) == "mailazy"{
301+
url = conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/smtpprovider?"
302+
} else {
303+
url = conf.AdminConsoleAPIDomain + "/deployment/smtp-settings?"
304+
}
305+
body, _ := json.Marshal(data)
306+
var resultResp SmtpConfigSchema
307+
resp, err := request.Rest(http.MethodPost, url, nil, string(body))
308+
if err != nil {
309+
return nil, err
310+
}
311+
err = json.Unmarshal(resp, &resultResp)
312+
if err != nil {
313+
return nil, err
314+
}
315+
return &resultResp, nil
316+
}
317+
318+
func VerifySMTPConfiguration(data VerifySmtpConfigSchema) error {
319+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/verifysmtpsettings?"
320+
body, _ := json.Marshal(data)
321+
322+
_, err := request.Rest(http.MethodPost, url, nil, string(body))
323+
324+
if err != nil {
325+
return err
326+
}
327+
return nil
328+
}
329+
330+
func DeleteSMTPConfiguration() error {
331+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/reset?"
332+
333+
_, err := request.Rest(http.MethodPost, url, nil, "")
334+
if err != nil {
335+
return err
336+
}
337+
return nil
338+
}

api/platformConfiguration.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ func GetRegistrationFields() (map[string]Schema, error) {
208208
provMap := make(map[string]Schema, len(resultResp.Data))
209209

210210
for _ ,value := range resultResp.Data {
211-
provMap[strings.ToLower(value.Name)] = value
211+
if !strings.Contains(value.Name, "cf") {
212+
provMap[strings.ToLower(value.Name)] = value
213+
} else {
214+
provMap[value.Name] = value
215+
}
212216
}
213217
return provMap, nil
214218
}

api/securityConfiguration.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ type ResetResponse struct {
1414
XToken string `json:"xtoken"`
1515
}
1616

17+
type EmailResponse struct {
18+
Domains []string `json:"Domains"`
19+
ListType string `json:"ListType"`
20+
RegistrationType string `json:"RegistrationType"`
21+
}
22+
23+
type RegistrationRestrictionTypeSchema struct {
24+
SelectedRestrictionType string `json:"selectedRestrictionType"`
25+
}
26+
27+
type EmailWhiteBLackListSchema struct {
28+
Domains []string `json:"Domains"`
29+
}
30+
1731
func ResetSecret() error {
1832

1933
// Restting the Secret
@@ -59,3 +73,40 @@ func ResetSecret() error {
5973
return nil
6074

6175
}
76+
77+
func GetEmailWhiteListBlackList() (*EmailResponse, error) {
78+
79+
url := conf.AdminConsoleAPIDomain + "/security-configuration/restriction/config?"
80+
resp, err := request.Rest(http.MethodGet, url, nil, "")
81+
if err != nil {
82+
return nil, err
83+
}
84+
var resObj EmailResponse
85+
err = json.Unmarshal(resp, &resObj)
86+
if err != nil {
87+
return nil, err
88+
}
89+
return &resObj, nil
90+
91+
}
92+
93+
func AddEmailWhitelistBlacklist(restrictionType RegistrationRestrictionTypeSchema, data EmailWhiteBLackListSchema) error {
94+
typeUrl := conf.AdminConsoleAPIDomain + "/security-configuration/restriction/type?"
95+
body, _ := json.Marshal(restrictionType)
96+
_, err := request.Rest(http.MethodPut, typeUrl, nil, string(body))
97+
98+
if err != nil {
99+
return err
100+
}
101+
102+
if restrictionType.SelectedRestrictionType != "none" {
103+
domainUrl := conf.AdminConsoleAPIDomain + "/security-configuration/restriction/config?"
104+
body, _ := json.Marshal(data)
105+
_, err := request.Rest(http.MethodPost, domainUrl, nil, string(body))
106+
107+
if err != nil {
108+
return err
109+
}
110+
}
111+
return nil
112+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package accessRestriction
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/AlecAivazis/survey/v2"
10+
"github.com/loginradius/lr-cli/api"
11+
12+
"github.com/loginradius/lr-cli/cmdutil"
13+
"github.com/loginradius/lr-cli/prompt"
14+
15+
16+
"github.com/spf13/cobra"
17+
)
18+
19+
type domain struct {
20+
Domain string `json:"domain"`
21+
}
22+
23+
func NewaccessRestrictionCmd() *cobra.Command {
24+
25+
cmd := &cobra.Command{
26+
Use: "access-restriction",
27+
Short: "Whitelist/Blacklist a Domain/Email",
28+
Long: `Use this command to Whitelist/Blacklist Domain/Email or to disable access restriction.`,
29+
Example: heredoc.Doc(`
30+
$ lr add access-restriction
31+
? Select the Restriction Type: Whitelist
32+
? Enter Domain/Email: <Domain>
33+
? Adding Domain/Email to Whitelist will result in deletion of all Blacklist Domains/Emails. Are you sure you want to proceed?(Y/N):Yes
34+
35+
36+
Whitelist Domain/email have been updated successfully
37+
`),
38+
RunE: func(cmd *cobra.Command, args []string) error {
39+
return addAccessRestriction()
40+
},
41+
}
42+
43+
return cmd
44+
}
45+
46+
func addAccessRestriction() error {
47+
var restrictionType = []string {"None","WhiteList", "BlackList"}
48+
var num int
49+
var shouldAdd bool
50+
err := prompt.SurveyAskOne(&survey.Select{
51+
Message: "Select the Restriction Type:",
52+
Options: restrictionType,
53+
}, &num)
54+
if err != nil {
55+
return err
56+
}
57+
var restrictType api.RegistrationRestrictionTypeSchema
58+
restrictType.SelectedRestrictionType = strings.ToLower(restrictionType[num])
59+
var AddEmail api.EmailWhiteBLackListSchema
60+
if restrictionType[num] != "None" {
61+
resp, err := api.GetEmailWhiteListBlackList()
62+
if err != nil {
63+
if err.Error() != "No records found" {
64+
return err
65+
}
66+
} else {
67+
if strings.ToLower(resp.ListType) == strings.ToLower(restrictType.SelectedRestrictionType) {
68+
AddEmail.Domains = resp.Domains
69+
}
70+
}
71+
var email string
72+
prompt.SurveyAskOne(&survey.Input{
73+
Message: "Enter Domain/Email:",
74+
}, &email, survey.WithValidator(survey.Required))
75+
if !cmdutil.AccessRestrictionDomain.MatchString(email) {
76+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email field is invalid")}
77+
}
78+
for _, val := range AddEmail.Domains {
79+
if val == email {
80+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email is already added")}
81+
}
82+
}
83+
if resp != nil && restrictionType[num] != resp.ListType {
84+
85+
if err := prompt.Confirm("Adding Domain/Email to " + restrictionType[num] + " will result in the deletion of all " + resp.ListType + " Domains/Emails. Are you sure you want to proceed?",
86+
&shouldAdd); err != nil {
87+
return err
88+
}
89+
} else {
90+
shouldAdd = true
91+
}
92+
93+
AddEmail.Domains = append(AddEmail.Domains, email)
94+
95+
} else {
96+
if err := prompt.Confirm("Disabling access restriction will result in the deletion of all Whitelist/Blacklist Domains/Emails. Are you sure you want to proceed?",
97+
&shouldAdd); err != nil {
98+
return err
99+
}
100+
}
101+
102+
if shouldAdd {
103+
err = api.AddEmailWhitelistBlacklist(restrictType, AddEmail);
104+
if err != nil {
105+
return err
106+
}
107+
if restrictType.SelectedRestrictionType == "none" {
108+
fmt.Println("Access restrictions have been disabled" )
109+
} else {
110+
fmt.Println(restrictionType[num] + " Domains/Emails have been updated successfully" )
111+
}
112+
}
113+
114+
return nil
115+
116+
}

0 commit comments

Comments
 (0)