Skip to content

Commit c2ffcf5

Browse files
committed
resolved conflicts
2 parents 955c991 + baa6f40 commit c2ffcf5

4 files changed

Lines changed: 350 additions & 0 deletions

File tree

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+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package accessRestriction
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/loginradius/lr-cli/api"
10+
"github.com/loginradius/lr-cli/cmdutil"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
16+
type domain struct {
17+
BlacklistDomain string `json:"blacklistdomain"`
18+
WhitelistDomain string `json:"whitelistdomain"`
19+
}
20+
21+
22+
func NewaccessRestrictionCmd() *cobra.Command {
23+
opts := &domain{}
24+
25+
cmd := &cobra.Command{
26+
Use: "access-restriction",
27+
Short: "Deletes Whitelisted/Blacklisted Domains/Emails",
28+
Long: `Use this command to remove the whitelisted/blacklisted Domains/Emails.`,
29+
Example: heredoc.Doc(`
30+
$ lr delete access-restriction --blacklist-domain <domain>
31+
Blacklist Domains/Emails have been updated successfully
32+
`),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
if opts.BlacklistDomain == "" && opts.WhitelistDomain == "" {
35+
return &cmdutil.FlagError{Err: errors.New("`domain` is a required argument")}
36+
}
37+
38+
resp, err := api.GetEmailWhiteListBlackList()
39+
if err != nil {
40+
return err
41+
}
42+
if (resp.ListType == "WhiteList" && opts.BlacklistDomain != "") || (resp.ListType == "BlackList" && opts.WhitelistDomain != "") {
43+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email was not found. As the " + resp.ListType + " restriction type is selected, you can change it by using the command `lr add access-restriction`" )}
44+
45+
}
46+
var domain string
47+
if opts.BlacklistDomain != "" {
48+
domain = opts.BlacklistDomain
49+
} else if opts.WhitelistDomain != "" {
50+
domain = opts.WhitelistDomain
51+
}
52+
53+
_, found := cmdutil.Find(resp.Domains, domain)
54+
if !found {
55+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email not found")}
56+
}
57+
58+
var newDomains []string
59+
newDomains = resp.Domains
60+
for index, url := range resp.Domains {
61+
if url == domain {
62+
newDomains = append(resp.Domains[:index], resp.Domains[index+1:]...)
63+
break
64+
}
65+
}
66+
if len(newDomains) == 0 {
67+
resp.ListType = "none"
68+
}
69+
delete(resp.ListType, newDomains)
70+
return nil
71+
72+
},
73+
}
74+
75+
fl := cmd.Flags()
76+
fl.StringVarP(&opts.BlacklistDomain, "blacklist-domain", "b", "", "Enter Blacklist Domain/Email Value you want to delete")
77+
fl.StringVarP(&opts.WhitelistDomain, "whitelist-domain", "w", "", "Enter Whitelist Domain/Email Value you want to delete")
78+
return cmd
79+
}
80+
81+
func delete(listType string, domain []string) error {
82+
var restrictType api.RegistrationRestrictionTypeSchema
83+
restrictType.SelectedRestrictionType = strings.ToLower(listType)
84+
var AddEmail api.EmailWhiteBLackListSchema
85+
AddEmail.Domains = domain
86+
err := api.AddEmailWhitelistBlacklist(restrictType, AddEmail);
87+
if err != nil {
88+
return err
89+
}
90+
if listType == "none" {
91+
fmt.Println("Access restrictions have been disabled" )
92+
} else {
93+
fmt.Println(listType + " Domains/Emails have been updated successfully" )
94+
}
95+
return nil
96+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package accessRestriction
2+
3+
import (
4+
"fmt"
5+
// "strings"
6+
7+
"github.com/MakeNowJust/heredoc"
8+
"github.com/loginradius/lr-cli/api"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewaccessRestrictionCmd() *cobra.Command {
13+
14+
cmd := &cobra.Command{
15+
Use: "access-restriction",
16+
Short: "Gets whitelisted/blacklisted Domains/Emails",
17+
Long: `Use this command to get the list of the whitelist/blacklist Domains/Emails`,
18+
Example: heredoc.Doc(`
19+
$ lr get access-restriction
20+
WhiteList/Blacklist Domains/Emails
21+
1. http://localhost
22+
...
23+
`),
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
26+
resp, err := api.GetEmailWhiteListBlackList()
27+
if err != nil {
28+
return err
29+
}
30+
fmt.Println(resp.ListType + " Domains/Emails")
31+
for i := 0; i < len(resp.Domains); i++ {
32+
fmt.Print(fmt.Sprint(i+1) + ". ")
33+
fmt.Println(resp.Domains[i])
34+
}
35+
return nil
36+
37+
},
38+
}
39+
40+
return cmd
41+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package accessRestriction
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/loginradius/lr-cli/api"
10+
"github.com/loginradius/lr-cli/cmdutil"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
15+
16+
type domain struct {
17+
BlacklistDomain string `json:"blacklistdomain"`
18+
WhitelistDomain string `json:"whitelistdomain"`
19+
DomainMod string `json:"domainmod"`
20+
}
21+
22+
23+
func NewaccessRestrictionCmd() *cobra.Command {
24+
opts := &domain{}
25+
26+
cmd := &cobra.Command{
27+
Use: "access-restriction",
28+
Short: "Updates whitelisted/blacklisted Domains/Emails",
29+
Long: `Use this command to update the whitelisted/blacklisted Domains/Emails.`,
30+
Example: heredoc.Doc(`
31+
$ lr set access-restriction --blacklist-domain <old-domain> --new-domain <new-domain>
32+
Blacklist Domains/Emails have been updated successfully
33+
`),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
if opts.BlacklistDomain == "" && opts.WhitelistDomain == "" {
36+
return &cmdutil.FlagError{Err: errors.New("`domain` is a required argument ")}
37+
}
38+
39+
if opts.DomainMod == "" {
40+
return &cmdutil.FlagError{Err: errors.New("`new-domain` is a required argument")}
41+
}
42+
43+
resp, err := api.GetEmailWhiteListBlackList()
44+
if err != nil {
45+
return err
46+
}
47+
if (resp.ListType == "WhiteList" && opts.BlacklistDomain != "") || (resp.ListType == "BlackList" && opts.WhitelistDomain != "") {
48+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email was not found. As the " + resp.ListType + " restriction type is selected, you can change it by using the command `lr add access-restriction`" )}
49+
}
50+
var domain string
51+
if opts.BlacklistDomain != "" {
52+
domain = opts.BlacklistDomain
53+
} else if opts.WhitelistDomain != "" {
54+
domain = opts.WhitelistDomain
55+
}
56+
57+
i, found := cmdutil.Find(resp.Domains, domain)
58+
if !found {
59+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email not found")}
60+
}
61+
if !cmdutil.AccessRestrictionDomain.MatchString(opts.DomainMod) {
62+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email field is invalid")}
63+
}
64+
65+
_, found = cmdutil.Find(resp.Domains, opts.DomainMod)
66+
if found {
67+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email has already been added")}
68+
}
69+
var newDomains []string
70+
newDomains = resp.Domains
71+
newDomains[i] = opts.DomainMod
72+
set(resp.ListType, newDomains)
73+
return nil
74+
75+
},
76+
}
77+
78+
fl := cmd.Flags()
79+
fl.StringVarP(&opts.BlacklistDomain, "blacklist-domain", "b", "", "Enter Old Blacklist Domain/Email Value")
80+
fl.StringVarP(&opts.WhitelistDomain, "whitelist-domain", "w", "", "Enter Old Whitelist Domain/Email Value")
81+
fl.StringVarP(&opts.DomainMod, "new-domain", "n", "", "Enter New Domain/Email Value")
82+
83+
return cmd
84+
}
85+
86+
func set(listType string, domain []string) error {
87+
var restrictType api.RegistrationRestrictionTypeSchema
88+
restrictType.SelectedRestrictionType = strings.ToLower(listType)
89+
var AddEmail api.EmailWhiteBLackListSchema
90+
AddEmail.Domains = domain
91+
err := api.AddEmailWhitelistBlacklist(restrictType, AddEmail);
92+
if err != nil {
93+
return err
94+
}
95+
fmt.Println(listType + " Domains/Emails have been updated successfully" )
96+
return nil
97+
}

0 commit comments

Comments
 (0)