Skip to content

Commit 45fbf60

Browse files
committed
Fixed User Management Related Bugs (#42)
1 parent 525959d commit 45fbf60

13 files changed

Lines changed: 241 additions & 239 deletions

File tree

api/auth.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io/ioutil"
7-
"log"
87
"net/http"
98
"time"
109

@@ -84,7 +83,7 @@ func SitesBasic(tokens *SitesToken) error {
8483
basic := conf.AdminConsoleAPIDomain + "/auth/basicsettings?"
8584
req, err := http.NewRequest(http.MethodGet, basic, nil)
8685
if err != nil {
87-
log.Printf("Could not make request % -v", err)
86+
return err
8887
}
8988
req.Header.Add("x-is-loginradius--sign", tokens.XSign)
9089
req.Header.Add("x-is-loginradius--token", tokens.XToken)

cmd/add/account/account.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010

11-
"github.com/loginradius/lr-cli/api"
1211
"github.com/loginradius/lr-cli/cmdutil"
13-
"github.com/loginradius/lr-cli/config"
1412
"github.com/loginradius/lr-cli/request"
1513

1614
"github.com/spf13/cobra"
@@ -28,9 +26,10 @@ type account struct {
2826
}
2927

3028
type Result struct {
31-
FirstName string `json:"FirstName"`
32-
Uid string `json:"Uid"`
33-
ID string `json:"ID"`
29+
FirstName *string `json:"FirstName"`
30+
Email []EmailVal `json:"Email"`
31+
Uid string `json:"Uid"`
32+
ID string `json:"ID"`
3433
}
3534

3635
func NewaccountCmd() *cobra.Command {
@@ -45,17 +44,15 @@ func NewaccountCmd() *cobra.Command {
4544
Short: "add account",
4645
Long: `This commmand adds account`,
4746
Example: heredoc.Doc(`$ lr add account --name <name> --email <email>
48-
User Account is successfully created
49-
First name is: <first name>
50-
Uid is: <uid>
51-
ID is: <id>
47+
User Account successfully created
48+
First name: <first name>
49+
Email: <email>
50+
Uid: <uid>
51+
ID: <id>
5252
`),
5353
RunE: func(cmd *cobra.Command, args []string) error {
5454
if opts.Email[0].Value == "" {
55-
return &cmdutil.FlagError{Err: errors.New("`email` is required argument")}
56-
}
57-
if opts.FirstName == "" {
58-
return &cmdutil.FlagError{Err: errors.New("`name` is required argument")}
55+
return &cmdutil.FlagError{Err: errors.New("`email` required argument")}
5956
}
6057
return add(*opts)
6158

@@ -65,20 +62,17 @@ func NewaccountCmd() *cobra.Command {
6562
fl := cmd.Flags()
6663
fl.StringVarP(&opts.Email[0].Value, "email", "e", "", "emailID")
6764
fl.StringVarP(&opts.FirstName, "name", "n", "", "first name")
68-
65+
fl.Lookup("name").NoOptDefVal = ""
6966
return cmd
7067
}
7168

7269
func add(Account account) error {
7370
Account.Password = cmdutil.GeneratePassword()
7471

75-
resObj, err := api.GetSites()
76-
77-
url := config.GetInstance().LoginRadiusAPIDomain + "/identity/v2/manage/account?apikey=" + resObj.Key + "&apisecret=" + resObj.Secret
7872
body, _ := json.Marshal(Account)
7973

8074
var resultResp Result
81-
resp, err := request.Rest(http.MethodPost, url, nil, string(body))
75+
resp, err := request.RestLRAPI(http.MethodPost, "/identity/v2/manage/account", nil, string(body))
8276

8377
if err != nil {
8478
return err
@@ -87,9 +81,21 @@ func add(Account account) error {
8781
if err != nil {
8882
return err
8983
}
90-
fmt.Println("User Account is successfully created")
91-
fmt.Println("First name is:" + resultResp.FirstName)
92-
fmt.Println("Uid is:" + resultResp.Uid)
93-
fmt.Println("ID is:" + resultResp.ID)
84+
85+
body, _ = json.Marshal(map[string]string{
86+
"email": resultResp.Email[0].Value,
87+
})
88+
_, err = request.RestLRAPI(http.MethodPost, "/identity/v2/manage/account/forgot/token?SendEmail=true", nil, string(body))
89+
if err != nil {
90+
return err
91+
}
92+
fmt.Println("User Account successfully created, Please Check email to set the password")
93+
fmt.Println("Please find the user details below:")
94+
if *resultResp.FirstName != "" {
95+
fmt.Println("First name: " + *resultResp.FirstName)
96+
}
97+
fmt.Println("Email: " + resultResp.Email[0].Value)
98+
fmt.Println("Uid: " + resultResp.Uid)
99+
fmt.Println("ID: " + resultResp.ID)
94100
return nil
95101
}

cmd/delete/account/account.go

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010

11-
"github.com/loginradius/lr-cli/api"
1211
"github.com/loginradius/lr-cli/cmdutil"
13-
"github.com/loginradius/lr-cli/config"
1412
"github.com/loginradius/lr-cli/request"
1513

1614
"github.com/spf13/cobra"
@@ -39,9 +37,9 @@ func NewaccountCmd() *cobra.Command {
3937
`),
4038
RunE: func(cmd *cobra.Command, args []string) error {
4139
if inpEmail == "" && inpUID != "" {
42-
return deletebyUID(inpUID)
40+
return delete(inpUID, "uid")
4341
} else if inpUID == "" && inpEmail != "" {
44-
return deletebyEmail(inpEmail)
42+
return delete(inpEmail, "email")
4543
} else {
4644
return &cmdutil.FlagError{Err: errors.New("Please enter exact one flag for this command")}
4745
}
@@ -55,37 +53,15 @@ func NewaccountCmd() *cobra.Command {
5553

5654
return cmd
5755
}
58-
func deletebyEmail(Email string) error {
59-
resObj, err := api.GetSites()
60-
if err != nil {
61-
return err
62-
}
63-
url := config.GetInstance().LoginRadiusAPIDomain + "/identity/v2/manage/account?apikey=" + resObj.Key + "&apisecret=" + resObj.Secret + "&email=" + Email
64-
var resultResp Result
65-
resp, err := request.Rest(http.MethodDelete, url, nil, "")
66-
67-
if err != nil {
68-
return err
69-
}
70-
err = json.Unmarshal(resp, &resultResp)
71-
if err != nil {
72-
return err
73-
}
74-
fmt.Println("User account sucessfully deleted")
75-
fmt.Print("number of records deleted = ")
76-
fmt.Println((resultResp.RecordsDeleted))
77-
78-
return nil
79-
}
80-
81-
func deletebyUID(UID string) error {
82-
resObj, err := api.GetSites()
83-
if err != nil {
84-
return err
56+
func delete(value string, field string) error {
57+
url := ""
58+
if field == "email" {
59+
url = "/identity/v2/manage/account?email=" + value
60+
} else if field == "uid" {
61+
url = "/identity/v2/manage/account/" + value
8562
}
86-
url := config.GetInstance().LoginRadiusAPIDomain + "/identity/v2/manage/account/" + UID + "?apikey=" + resObj.Key + "&apisecret=" + resObj.Secret
8763
var resultResp Result
88-
resp, err := request.Rest(http.MethodDelete, url, nil, "")
64+
resp, err := request.RestLRAPI(http.MethodDelete, url, nil, "")
8965

9066
if err != nil {
9167
return err

cmd/get/account/account.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010

11-
"github.com/loginradius/lr-cli/api"
1211
"github.com/loginradius/lr-cli/cmdutil"
13-
"github.com/loginradius/lr-cli/config"
1412
"github.com/loginradius/lr-cli/request"
1513

1614
"github.com/spf13/cobra"
1715
)
1816

1917
var inpEmail string
2018

19+
type EmailVal struct {
20+
Type string `json:"Type"`
21+
Value string `json:"Value"`
22+
}
23+
2124
type Identities struct {
22-
FirstName string `json:"FirstName"`
23-
Uid string `json:"Uid"`
24-
ID string `json:"ID"`
25+
FirstName string `json:"FirstName"`
26+
Email []EmailVal `json:"Email"`
27+
Uid string `json:"Uid"`
28+
ID string `json:"ID"`
2529
}
2630
type Result struct {
2731
Data []Identities `json:"Data"`
@@ -34,9 +38,10 @@ func NewaccountCmd() *cobra.Command {
3438
Short: "get account",
3539
Long: `This commmand gets account`,
3640
Example: heredoc.Doc(`$ lr get account --email <email>
37-
First name is:<firstname>
38-
Uid is:<uid>
39-
ID is:<id>
41+
First name: <firstname>
42+
Email: <email>
43+
Uid: <uid>
44+
ID: <id>
4045
`),
4146
RunE: func(cmd *cobra.Command, args []string) error {
4247
if inpEmail == "" {
@@ -55,19 +60,20 @@ func NewaccountCmd() *cobra.Command {
5560
}
5661

5762
func get(inpEmail string) error {
58-
resObj, err := api.GetSites()
59-
url := config.GetInstance().LoginRadiusAPIDomain + "/identity/v2/manage/account/identities?apikey=" + resObj.Key + "&apisecret=" + resObj.Secret + "&email=" + inpEmail
6063
var resultResp Result
61-
resp, err := request.Rest(http.MethodGet, url, nil, "")
64+
resp, err := request.RestLRAPI(http.MethodGet, "/identity/v2/manage/account/identities?email="+inpEmail, nil, "")
6265
if err != nil {
6366
return err
6467
}
6568
err = json.Unmarshal(resp, &resultResp)
6669
if err != nil {
6770
return err
6871
}
69-
fmt.Println("First name is:" + resultResp.Data[0].FirstName)
70-
fmt.Println("Uid is:" + resultResp.Data[0].Uid)
71-
fmt.Println("ID is:" + resultResp.Data[0].ID)
72+
if resultResp.Data[0].FirstName != "" {
73+
fmt.Println("First name is:" + resultResp.Data[0].FirstName)
74+
}
75+
fmt.Println("Email: " + resultResp.Data[0].Email[0].Value)
76+
fmt.Println("Uid: " + resultResp.Data[0].Uid)
77+
fmt.Println("ID: " + resultResp.Data[0].ID)
7278
return nil
7379
}

cmd/get/accountPassword/accountPassword.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010

11-
"github.com/loginradius/lr-cli/api"
1211
"github.com/loginradius/lr-cli/cmdutil"
13-
"github.com/loginradius/lr-cli/config"
1412
"github.com/loginradius/lr-cli/request"
1513

1614
"github.com/spf13/cobra"
@@ -25,10 +23,10 @@ type Result struct {
2523
func NewaccountPasswordCmd() *cobra.Command {
2624

2725
cmd := &cobra.Command{
28-
Use: "accountPassword",
29-
Short: "get accountPassword",
30-
Long: `This commmand gets accountPassword`,
31-
Example: heredoc.Doc(`$ lr get accountPassword --uid <uid>
26+
Use: "account-password",
27+
Short: "get account-password",
28+
Long: `This commmand gets account-password`,
29+
Example: heredoc.Doc(`$ lr get account-password --uid <uid>
3230
password hash for UID:<UID> is <hash>
3331
`),
3432
RunE: func(cmd *cobra.Command, args []string) error {
@@ -48,11 +46,8 @@ func NewaccountPasswordCmd() *cobra.Command {
4846
}
4947

5048
func get(UID string) error {
51-
resObj, err := api.GetSites()
52-
53-
url := config.GetInstance().LoginRadiusAPIDomain + "/identity/v2/manage/account/" + UID + "/password?apikey=" + resObj.Key + "&apisecret=" + resObj.Secret
5449
var resultResp Result
55-
resp, err := request.Rest(http.MethodGet, url, nil, "")
50+
resp, err := request.RestLRAPI(http.MethodGet, "/identity/v2/manage/account/"+UID+"/password", nil, "")
5651

5752
if err != nil {
5853
return err

cmd/get/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/loginradius/lr-cli/cmd/get/accountPassword"
66
"github.com/loginradius/lr-cli/cmd/get/config"
77
"github.com/loginradius/lr-cli/cmd/get/hooks"
8-
"github.com/loginradius/lr-cli/cmd/get/profiles"
8+
profiles "github.com/loginradius/lr-cli/cmd/get/profile"
99
"github.com/loginradius/lr-cli/cmd/get/schema"
1010
"github.com/loginradius/lr-cli/cmd/get/site"
1111

cmd/get/profile/profile.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package profiles
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/loginradius/lr-cli/cmdutil"
11+
"github.com/loginradius/lr-cli/request"
12+
13+
"github.com/spf13/cobra"
14+
)
15+
16+
var inpEmail string
17+
var inputUID string
18+
19+
type EmailVal struct {
20+
Type string `json:"Type"`
21+
Value string `json:"Value"`
22+
}
23+
24+
type Result struct {
25+
FirstName string `json:"FirstName"`
26+
Email []EmailVal `json:"Email"`
27+
Uid string `json:"Uid"`
28+
ID string `json:"ID"`
29+
}
30+
31+
func NewprofilesCmd() *cobra.Command {
32+
33+
cmd := &cobra.Command{
34+
Use: "profile",
35+
Short: "get profile",
36+
Long: `This commmand gets profiles`,
37+
Example: heredoc.Doc(`$ lr get profile --email <email> (or) --uid <uid>
38+
First name: <firstname>
39+
Email: <email>
40+
Uid: <uid>
41+
ID: <id>
42+
`),
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
if inputUID != "" {
45+
return getProfile(inputUID, "uid")
46+
} else if inpEmail != "" {
47+
return getProfile(inpEmail, "email")
48+
} else {
49+
return &cmdutil.FlagError{Err: errors.New("atleast one of the flags is necessary")}
50+
}
51+
52+
},
53+
}
54+
55+
fl := cmd.Flags()
56+
fl.StringVarP(&inpEmail, "email", "e", "", "emailID")
57+
fl.StringVarP(&inputUID, "uid", "u", "", "UID")
58+
59+
return cmd
60+
}
61+
62+
func getProfile(value string, field string) error {
63+
url := ""
64+
if field == "email" {
65+
url = "/identity/v2/manage/account?email=" + value
66+
} else if field == "uid" {
67+
url = "/identity/v2/manage/account/" + inputUID
68+
}
69+
var resultResp Result
70+
resp, err := request.RestLRAPI(http.MethodGet, url, nil, "")
71+
72+
if err != nil {
73+
return err
74+
}
75+
err = json.Unmarshal(resp, &resultResp)
76+
if err != nil {
77+
return err
78+
}
79+
if resultResp.FirstName != "" {
80+
fmt.Println("First name: " + resultResp.FirstName)
81+
}
82+
fmt.Println("Email: " + resultResp.Email[0].Value)
83+
fmt.Println("Uid: " + resultResp.Uid)
84+
fmt.Println("ID: " + resultResp.ID)
85+
86+
return nil
87+
}

0 commit comments

Comments
 (0)