Skip to content

Commit 9dcc403

Browse files
Merge pull request #2 from muskansoninmh/ft_CLI_permission_pca_10325
Ft cli permission pca 10325
2 parents 1afc142 + 640f994 commit 9dcc403

52 files changed

Lines changed: 440 additions & 57 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/account.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ type SitesToken struct {
1818
XToken string `json:"xtoken"`
1919
}
2020

21-
func SetSites(appid int64) (*SitesToken, error) {
22-
switchapp := conf.AdminConsoleAPIDomain + "/account/switchapp?appid=" + strconv.FormatInt(appid, 10)
21+
func SetSites(appid int64, owned bool) (*SitesToken, error) {
22+
var api string
23+
if (owned){
24+
api = "/account/switchapp?appid="
25+
} else {
26+
api = "/account/switchsharedsite?appid="
27+
}
28+
switchapp := conf.AdminConsoleAPIDomain + api + strconv.FormatInt(appid, 10)
2329
switchResp, err := request.Rest(http.MethodGet, switchapp, nil, "")
2430
var switchRespObj SitesToken
2531
err = json.Unmarshal(switchResp, &switchRespObj)

api/auth.go

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"net/http"
88
"time"
9+
"reflect"
910

1011
"github.com/loginradius/lr-cli/cmdutil"
1112
"github.com/loginradius/lr-cli/config"
@@ -20,7 +21,7 @@ type LoginResponse struct {
2021
AppName string `json:"app_name"`
2122
Authenticated bool `json:"authenticated"`
2223
NoOfLogins int32 `json:"no_of_logins"`
23-
PlanDetails interface{} `json"plan_detail"`
24+
PlanDetails interface{} `json:"plan_detail"`
2425
XSign string `json:"xsign"`
2526
XToken string `json:"xtoken"`
2627
}
@@ -44,6 +45,28 @@ type Feature struct {
4445
Status bool `json:"status"`
4546
}
4647

48+
type PermissionResponse struct {
49+
Permissions Permission `json:"permissions"`
50+
}
51+
52+
type Permission struct {
53+
API_AdminConfiguration bool `json:"API_AdminConfiguration"`
54+
API_EditConfiguration bool `json:"API_EditConfiguration"`
55+
API_EditCredentials bool `json:"API_EditCredentials"`
56+
API_EditThirdPartyCredentials bool `json:"API_EditThirdPartyCredentials"`
57+
API_ViewCredentials bool `json:"API_ViewCredentials"`
58+
UserManagement_Admin bool `json:"UserManagement_Admin"`
59+
API_ViewConfiguration bool `json:"API_ViewConfiguration"`
60+
API_ViewThirdPartyCredentials bool `json:"API_ViewThirdPartyCredentials"`
61+
ThirdPartyIntegration_View bool `json:"ThirdPartyIntegration_View"`
62+
UserManagement_View bool `json:"UserManagement_View"`
63+
SecurityPolicy_View bool `json:"SecurityPolicy_View"`
64+
API_AdminThirdPartyCredentials bool `json:"API_AdminThirdPartyCredentials"`
65+
ThirdPartyIntegration_Admin bool `json:"ThirdPartyIntegration_Admin"`
66+
SecurityPolicy_Admin bool `json:"SecurityPolicy_Admin"`
67+
SecurityPolicy_Edit bool `json:"SecurityPolicy_Edit"`
68+
}
69+
4770
func AuthLogin(params LoginOpts) (*LoginResponse, error) {
4871

4972
var resObj LoginResponse
@@ -228,9 +251,15 @@ func storeSiteInfo(data CoreAppData) (map[int64]SitesReponse, map[int64]SharedSi
228251
currentId, err := CurrentID()
229252
if err == nil {
230253
site, ok := siteInfo[currentId]
254+
sharedsite, sharedok := sharedsiteInfo[currentId]
231255
if ok {
232-
obj, _ := json.Marshal(site)
233-
cmdutil.WriteFile("currentSite.json", obj)
256+
257+
obj, _ := json.Marshal(site)
258+
cmdutil.WriteFile("currentSite.json", obj)
259+
260+
} else if sharedok {
261+
obj, _ := json.Marshal(sharedsite)
262+
cmdutil.WriteFile("currentSite.json", obj)
234263
}
235264
}
236265
return siteInfo,sharedsiteInfo
@@ -259,3 +288,58 @@ func UpdatePhoneLogin(feature string, status bool) (*FeatureSchema, error) {
259288
}
260289
return &resultResp, err
261290
}
291+
292+
293+
func GetPermissionsfromAPI() ( error) {
294+
295+
coreAppData := conf.AdminConsoleAPIDomain + "/auth/permissions?"
296+
data, err := request.Rest(http.MethodGet, coreAppData, nil, "")
297+
var permissionsResp PermissionResponse
298+
var permission Permission
299+
if err != nil {
300+
return err
301+
}
302+
err = json.Unmarshal(data, &permissionsResp)
303+
permission = permissionsResp.Permissions
304+
err = storePermissionData(permission)
305+
if err != nil {
306+
return err
307+
}
308+
return nil
309+
}
310+
311+
312+
func storePermissionData(data Permission) (error) {
313+
var permissionobj = cmdutil.PermissionCommands
314+
permission := make(map[string]bool, len(permissionobj))
315+
for key, val := range permissionobj {
316+
v := reflect.ValueOf(&data).Elem().FieldByName(val)
317+
if v.IsValid() {
318+
permission[key] = v.Bool()
319+
}
320+
}
321+
obj, err := json.Marshal(permission)
322+
if err != nil {
323+
return nil
324+
}
325+
cmdutil.WriteFile("permission.json", obj)
326+
return nil
327+
}
328+
329+
func GetPermission(str string) (bool, error) {
330+
data, err := cmdutil.ReadFile("permission.json")
331+
if err != nil {
332+
return false, err
333+
}
334+
permission := make(map[string]bool)
335+
err = json.Unmarshal(data, &permission)
336+
if err != nil {
337+
return false, err
338+
}
339+
if permission[str] == false {
340+
fmt.Println("You don't have access to proceed, request access from the site owner. If you've already been granted access, log out and log back in. If the issue persists, contact LoginRadius support at ")
341+
fmt.Println( conf.DashboardDomain + "/support/tickets")
342+
return false , nil
343+
}
344+
return true , nil
345+
}

api/deployment.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ type VerifySmtpConfigSchema struct {
131131
type VerifySmtpConfigError struct {
132132
Description string `json:"description"`
133133
Message string `json:"message"`
134-
135134
}
136135

137136
func GetSites() (*SitesReponse, error) {

cmd/add/accessRestriction/accessRestriction.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func NewaccessRestrictionCmd() *cobra.Command {
4646
4747
`),
4848
RunE: func(cmd *cobra.Command, args []string) error {
49+
isPermission, err := api.GetPermission("lr_add_access-restriction")
50+
if(!isPermission || err != nil) {
51+
return nil
52+
}
4953
return addAccessRestriction(opts)
5054
},
5155
}
@@ -236,4 +240,4 @@ func addIpOrIpRange(allowedip string, deniedip string) error {
236240

237241

238242
return nil
239-
}
243+
}

cmd/add/account/account.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/loginradius/lr-cli/cmdutil"
1212
"github.com/loginradius/lr-cli/request"
13+
"github.com/loginradius/lr-cli/api"
1314

1415
"github.com/spf13/cobra"
1516
)
@@ -51,6 +52,11 @@ func NewaccountCmd() *cobra.Command {
5152
ID: <id>
5253
`),
5354
RunE: func(cmd *cobra.Command, args []string) error {
55+
isPermission, errr := api.GetPermission("lr_add_account")
56+
if(!isPermission || errr != nil) {
57+
return nil
58+
}
59+
5460
if opts.Email[0].Value == "" {
5561
return &cmdutil.FlagError{Err: errors.New("`email` required argument")}
5662
}

cmd/add/customField/customFields.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func NewAddCFCmd() *cobra.Command {
2727
You can now add the custom field in your registration schema using "lr set schema" command
2828
`),
2929
RunE: func(cmd *cobra.Command, args []string) error {
30+
isPermission, errr := api.GetPermission("lr_add_custom-field")
31+
if(!isPermission || errr != nil) {
32+
return nil
33+
}
3034
if fieldName == "" {
3135
return &cmdutil.FlagError{Err: errors.New("`fieldName` is required argument")}
3236
}

cmd/add/domain/domain.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func NewdomainCmd() *cobra.Command {
3232
Your Domain <newDomain> is now whitelisted
3333
`),
3434
RunE: func(cmd *cobra.Command, args []string) error {
35+
isPermission, errr := api.GetPermission("lr_add_domain")
36+
if(!isPermission || errr != nil) {
37+
return nil
38+
}
3539
if opts.Domain == "" {
3640
return &cmdutil.FlagError{Err: errors.New("`domain` is required argument")}
3741
}

cmd/add/hooks/hooks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func addHooks() error {
6868
// }
6969
// }
7070

71+
isPermission, errr := api.GetPermission("lr_add_hooks")
72+
if(!isPermission || errr != nil) {
73+
return nil
74+
}
75+
7176
checkInput,err := input()
7277
if !checkInput {
7378
return err

cmd/add/loginMethod/loginMethod.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func NewloginMethodCmd() *cobra.Command {
3434
}
3535

3636
func addloginMethod() error {
37+
isPermission, errr := api.GetPermission("lr_add_login-method")
38+
if(!isPermission || errr != nil) {
39+
return nil
40+
}
41+
3742
err := api.CheckLoginMethod()
3843
if err != nil {
3944
return err

cmd/add/site/site.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func add() error {
109109
if err != nil {
110110
return err
111111
}
112-
switchRespObj, err := api.SetSites(App.AppId)
112+
switchRespObj, err := api.SetSites(App.AppId, true)
113113
if err != nil {
114114
return err
115115
}

0 commit comments

Comments
 (0)