|
| 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