Skip to content

Commit 2d8d3e4

Browse files
RevanthTanneerumohammed786
authored andcommitted
Added advanced configuration and code refactoring (#23)
Implement registration schema commands in LoginRadius CLI Implement registration schema commands in LoginRadius CLI Implement registration schema commands in LoginRadius CLI Implementing schema configuration commands in LR CLI (#25)
1 parent 6036326 commit 2d8d3e4

7 files changed

Lines changed: 206 additions & 134 deletions

File tree

api/platformConfiguration.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/loginradius/lr-cli/config"
8+
"github.com/loginradius/lr-cli/request"
9+
)
10+
11+
type FieldTypeConfig struct {
12+
Name string
13+
ShouldDisplayValidaitonRuleInput bool
14+
ShouldShowOption bool
15+
}
16+
17+
var TypeMap = map[int]FieldTypeConfig{
18+
1: FieldTypeConfig{
19+
Name: "String",
20+
ShouldDisplayValidaitonRuleInput: true,
21+
ShouldShowOption: false,
22+
},
23+
2: FieldTypeConfig{
24+
Name: "CheckBox",
25+
ShouldDisplayValidaitonRuleInput: false,
26+
ShouldShowOption: false,
27+
},
28+
3: FieldTypeConfig{
29+
Name: "Option",
30+
ShouldDisplayValidaitonRuleInput: false,
31+
ShouldShowOption: true,
32+
},
33+
4: FieldTypeConfig{
34+
Name: "Password",
35+
ShouldDisplayValidaitonRuleInput: true,
36+
ShouldShowOption: false,
37+
},
38+
5: FieldTypeConfig{
39+
Name: "Hidden",
40+
ShouldDisplayValidaitonRuleInput: true,
41+
ShouldShowOption: false,
42+
},
43+
6: FieldTypeConfig{
44+
Name: "Email",
45+
ShouldDisplayValidaitonRuleInput: true,
46+
ShouldShowOption: false,
47+
},
48+
7: FieldTypeConfig{
49+
Name: "Text",
50+
ShouldDisplayValidaitonRuleInput: true,
51+
ShouldShowOption: false,
52+
},
53+
}
54+
55+
type Schema struct {
56+
Display string `json:"Display"`
57+
Enabled bool `json:"Enabled"`
58+
IsMandatory bool `json:"IsMandatory"`
59+
Parent string `json:"Parent"`
60+
ParentDataSource string `json:"ParentDataSource"`
61+
Permission string `json:"Permission"`
62+
Name string `json:"name"`
63+
Options []Array `json:"options"`
64+
Rules string `json:"rules"`
65+
Status string `json:"status"`
66+
Type string `json:"type"`
67+
}
68+
type Array struct {
69+
Value string `json:"value"`
70+
Text string `json:"text"`
71+
}
72+
73+
var Url string
74+
75+
type ResultResp struct {
76+
Data []Schema `json:"Data"`
77+
}
78+
79+
func GetFields(tem string) (*ResultResp, error) {
80+
conf := config.GetInstance()
81+
if tem == "active" {
82+
Url = conf.AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?"
83+
}
84+
if tem == "all" {
85+
Url = conf.AdminConsoleAPIDomain + "/platform-configuration/platform-registration-fields?"
86+
}
87+
88+
var resultResp ResultResp
89+
resp, err := request.Rest(http.MethodGet, Url, nil, "")
90+
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
err = json.Unmarshal(resp, &resultResp)
96+
if err != nil {
97+
return nil, err
98+
}
99+
return &resultResp, nil
100+
}

cmd/add/add.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package add
33
import (
44
"github.com/loginradius/lr-cli/cmd/add/account"
55
"github.com/loginradius/lr-cli/cmd/add/domain"
6+
7+
"github.com/loginradius/lr-cli/cmd/add/hooks"
68
"github.com/loginradius/lr-cli/cmd/add/schema"
79
"github.com/loginradius/lr-cli/cmd/add/site"
810
"github.com/loginradius/lr-cli/cmd/add/social"

cmd/add/schema/schema.go

Lines changed: 78 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"log"
89
"net/http"
910
"os"
1011

@@ -19,23 +20,6 @@ import (
1920

2021
var temp int
2122

22-
type Schema struct {
23-
Display string `json:"Display"`
24-
Enabled bool `json:"Enabled"`
25-
IsMandatory bool `json:"IsMandatory"`
26-
Parent string `json:"Parent"`
27-
ParentDataSource string `json:"ParentDataSource"`
28-
Permission string `json:"Permission"`
29-
Name string `json:"name"`
30-
Rules string `json:"rules"`
31-
Status string `json:"status"`
32-
Type string `json:"type"`
33-
}
34-
type schemaStr struct {
35-
Data []Schema `json:"Data"`
36-
}
37-
38-
var urlall string
3923
var url1 string
4024

4125
func NewschemaCmd() *cobra.Command {
@@ -59,7 +43,7 @@ func NewschemaCmd() *cobra.Command {
5943
}
6044

6145
func add(temp int) error {
62-
46+
//checking if it is devoloper plan
6347
res, err := api.GetSites()
6448
if err != nil {
6549
return err
@@ -70,48 +54,71 @@ func add(temp int) error {
7054
return nil
7155
}
7256

57+
//changing enabled to true based on field number entered
7358
conf := config.GetInstance()
74-
urlall = conf.AdminConsoleAPIDomain + "/platform-configuration/platform-registration-fields?"
75-
76-
var resultResp schemaStr
77-
resp, err := request.Rest(http.MethodGet, urlall, nil, "")
78-
79-
if err != nil {
80-
return err
81-
}
82-
83-
err = json.Unmarshal(resp, &resultResp)
59+
resultResp, err := api.GetFields("all")
8460
if err != nil {
85-
return err
61+
return nil
8662
}
8763
var temp1 []int
8864
for i := 0; i < len(resultResp.Data); i++ {
8965
if resultResp.Data[i].Parent == "" {
9066
temp1 = append(temp1, i)
9167
}
9268
}
93-
url1 = conf.AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?"
94-
var resultResp1 schemaStr
95-
resp, err = request.Rest(http.MethodGet, url1, nil, "")
69+
resultResp1, err := api.GetFields("active")
70+
if err != nil {
71+
return err
72+
}
73+
if temp > len(temp1) || 0 > temp {
74+
fmt.Println("please run 'lr get schema -all' first. Please enter the field number accordingly")
75+
return nil
76+
}
77+
resultResp.Data[temp1[temp-1]].Enabled = true
78+
79+
//Changing the display name
80+
ChangeDisplay(resultResp, temp1, temp)
81+
82+
//Required or not
83+
IsRequired(resultResp, temp1, temp)
84+
85+
//Advance configuration setup
86+
AdvancedConfig(resultResp, temp1, temp)
87+
88+
//Adding the field to the configuration
89+
resultResp1.Data = append(resultResp1.Data, resultResp.Data[temp1[temp-1]])
90+
body, _ := json.Marshal(resultResp1)
91+
url1 = conf.AdminConsoleAPIDomain + "/platform-configuration/default-fields?"
92+
var resultResp2 api.ResultResp
93+
resp, err := request.Rest(http.MethodPost, url1, nil, string(body))
9694

9795
if err != nil {
9896
return err
9997
}
10098

101-
err = json.Unmarshal(resp, &resultResp1)
99+
err = json.Unmarshal(resp, &resultResp2)
102100
if err != nil {
103101
return err
104102
}
105-
resultResp.Data[temp1[temp-1]].Enabled = true
103+
fmt.Println("Your field has been sucessfully added")
104+
105+
return nil
106+
}
107+
108+
func ChangeDisplay(resultResp *api.ResultResp, temp1 []int, temp int) {
106109
var DisplayName string
107-
var req string
108110
fmt.Print("Enter the Display Name (" + resultResp.Data[temp1[temp-1]].Display + ") :")
109111
scanner := bufio.NewScanner(os.Stdin)
110112
scanner.Scan()
111113
DisplayName = scanner.Text()
112114
if DisplayName == "" {
113115
DisplayName = resultResp.Data[temp1[temp-1]].Display
114116
}
117+
resultResp.Data[temp1[temp-1]].Display = DisplayName
118+
}
119+
120+
func IsRequired(resultResp *api.ResultResp, temp1 []int, temp int) {
121+
var req string
115122
fmt.Print("Is Required (y/n):")
116123
fmt.Scanln(&req)
117124
for req != "Y" && req != "y" && req != "N" && req != "n" {
@@ -123,23 +130,44 @@ func add(temp int) error {
123130
} else if req == "N" || req == "n" {
124131
resultResp.Data[temp1[temp-1]].IsMandatory = false
125132
}
126-
resultResp.Data[temp1[temp-1]].Display = DisplayName
127-
128-
resultResp1.Data = append(resultResp1.Data, resultResp.Data[temp1[temp-1]])
129-
body, _ := json.Marshal(resultResp1)
130-
url1 = conf.AdminConsoleAPIDomain + "/platform-configuration/default-fields?"
131-
var resultResp2 schemaStr
132-
resp, err = request.Rest(http.MethodPost, url1, nil, string(body))
133+
}
133134

134-
if err != nil {
135-
return err
135+
func AdvancedConfig(resultResp *api.ResultResp, temp1 []int, temp int) {
136+
var req string
137+
var ind int
138+
var valStr string
139+
fmt.Print("Do you want to setup advance configuration (y/n):")
140+
fmt.Scanln(&req)
141+
for req != "Y" && req != "y" && req != "N" && req != "n" {
142+
fmt.Print("Please enter (y/n):")
143+
fmt.Scanln(&req)
136144
}
145+
if req == "Y" || req == "y" {
146+
fmt.Println("select feild type")
147+
for i := 0; i < len(api.TypeMap); i++ {
148+
fmt.Print(i + 1)
149+
fmt.Println(": " + api.TypeMap[i+1].Name)
150+
}
137151

138-
err = json.Unmarshal(resp, &resultResp2)
139-
if err != nil {
140-
return err
141-
}
142-
fmt.Println("Your field has been sucessfully added")
152+
fmt.Print("Select a number from 1 to " + fmt.Sprint(len(api.TypeMap)) + ":")
153+
fmt.Scanln(&ind)
154+
resultResp.Data[temp1[temp-1]].Type = api.TypeMap[ind].Name
155+
if api.TypeMap[ind].ShouldDisplayValidaitonRuleInput {
156+
fmt.Print("Enter the validation string(for more info check https://www.loginradius.com/docs/libraries/js-libraries/javascript-hooks/#customvalidationhook15):")
157+
fmt.Scanln(&valStr)
158+
resultResp.Data[temp1[temp-1]].Rules = valStr
159+
}
160+
if api.TypeMap[ind].ShouldShowOption {
161+
fmt.Print("Enter the key value json object(example ")
162+
fmt.Println(`[{"value":"one","text":" hyd"},{"value":"two","text":" vij"},{"value":"three","text":" viz"}])`)
143163

144-
return nil
164+
var record []api.Array
165+
166+
err := json.NewDecoder(os.Stdin).Decode(&record)
167+
if err != nil {
168+
log.Fatal(err)
169+
}
170+
resultResp.Data[temp1[temp-1]].Options = record
171+
}
172+
}
145173
}

0 commit comments

Comments
 (0)