|
| 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(`$ lr add access-restriction |
| 30 | + ? Select the Restriction Type: <Type> |
| 31 | + ? Enter Domain/Email: <domain> |
| 32 | + (if whitelist domain are added and you want to add blacklist domains vice/versa) |
| 33 | + ? Are you Sure you want to add Domain/Email to BlackList Domains/Emails as all the whitelist Domains/Emails will be deleted ? Yes |
| 34 | + <Type> domains/emails have been updated |
| 35 | + `), |
| 36 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 37 | + return addAccessRestriction() |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func addAccessRestriction() error { |
| 45 | + var restrictionType = []string {"None","WhiteList", "BlackList"} |
| 46 | + var num int |
| 47 | + var shouldAdd bool |
| 48 | + err := prompt.SurveyAskOne(&survey.Select{ |
| 49 | + Message: "Select the Restriction Type:", |
| 50 | + Options: restrictionType, |
| 51 | + }, &num) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + var restrictType api.RegistrationRestrictionTypeSchema |
| 56 | + restrictType.SelectedRestrictionType = strings.ToLower(restrictionType[num]) |
| 57 | + var AddEmail api.EmailWhiteBLackListSchema |
| 58 | + if restrictionType[num] != "None" { |
| 59 | + resp, err := api.GetEmailWhiteListBlackList() |
| 60 | + if err != nil { |
| 61 | + if err.Error() != "No records found" { |
| 62 | + return err |
| 63 | + } |
| 64 | + } else { |
| 65 | + if strings.ToLower(resp.ListType) == strings.ToLower(restrictType.SelectedRestrictionType) { |
| 66 | + AddEmail.Domains = resp.Domains |
| 67 | + } |
| 68 | + } |
| 69 | + var email string |
| 70 | + prompt.SurveyAskOne(&survey.Input{ |
| 71 | + Message: "Enter Domain/Email:", |
| 72 | + }, &email, survey.WithValidator(survey.Required)) |
| 73 | + if !cmdutil.AccessRestrictionDomain.MatchString(email) { |
| 74 | + return &cmdutil.FlagError{Err: errors.New("Domain/Email field is invalid")} |
| 75 | + } |
| 76 | + for _, val := range AddEmail.Domains { |
| 77 | + if val == email { |
| 78 | + return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email is already added")} |
| 79 | + } |
| 80 | + } |
| 81 | + if resp != nil && restrictionType[num] != resp.ListType { |
| 82 | + |
| 83 | + if err := prompt.Confirm("Are you Sure you want to add Domain/Email to " + restrictionType[num] + " Domains as all the " + resp.ListType + " Domains/Emails will be deleted ?", |
| 84 | + &shouldAdd); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + } else { |
| 88 | + shouldAdd = true |
| 89 | + } |
| 90 | + |
| 91 | + AddEmail.Domains = append(AddEmail.Domains, email) |
| 92 | + |
| 93 | + } else { |
| 94 | + if err := prompt.Confirm("Are you Sure you want to disable access restrictions as all the WhiteList/Blacklist Domains/Emails will be deleted ?", |
| 95 | + &shouldAdd); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if shouldAdd { |
| 101 | + err = api.AddEmailWhitelistBlacklist(restrictType, AddEmail); |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + if restrictType.SelectedRestrictionType == "none" { |
| 106 | + fmt.Println("Access restrictions have been successfully disabled " ) |
| 107 | + } else { |
| 108 | + fmt.Println(restrictionType[num] + " domains/emails have been updated" ) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | + |
| 114 | +} |
0 commit comments