11package user
22
33import (
4+ "bytes"
45 "strconv"
56
7+ "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
8+ "github.com/softlayer/softlayer-go/datatypes"
69 "github.com/spf13/cobra"
710 "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
811 . "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
@@ -22,10 +25,14 @@ func NewPermissionsCommand(sl *metadata.SoftlayerCommand) (cmd *PermissionsComma
2225 SoftlayerCommand : sl ,
2326 UserManager : managers .NewUserManager (sl .Session ),
2427 }
25-
28+ subs := map [string ]interface {}{
29+ "HelpUrl" : "https://cloud.ibm.com/docs/account?topic=account-migrated_permissions" ,
30+ }
2631 cobraCmd := & cobra.Command {
2732 Use : "permissions " + T ("USER_ID" ),
2833 Short : T ("View user permissions" ),
34+ Long : T (`Some permissions here may also be managed by the IBM IAM service.
35+ See {{.HelpUrl}} for more details.` , subs ),
2936 Args : metadata .OneArgs ,
3037 RunE : func (cmd * cobra.Command , args []string ) error {
3138 return thisCmd .Run (args )
@@ -36,7 +43,19 @@ func NewPermissionsCommand(sl *metadata.SoftlayerCommand) (cmd *PermissionsComma
3643 return thisCmd
3744}
3845
46+ type PermissionCollection struct {
47+ Department string
48+ Permissions []Permission
49+ }
50+
51+ type Permission struct {
52+ KeyName string
53+ Assigned string
54+ Description string
55+ }
56+
3957func (cmd * PermissionsCommand ) Run (args []string ) error {
58+ outputFormat := cmd .GetOutputFlag ()
4059 id , err := strconv .Atoi (args [0 ])
4160 if err != nil {
4261 return errors .NewInvalidUsageError (T ("User ID should be a number." ))
@@ -48,17 +67,41 @@ func (cmd *PermissionsCommand) Run(args []string) error {
4867 return errors .NewAPIError (T ("Failed to get user." ), err .Error (), 2 )
4968 }
5069
51- allPermission , err := cmd .UserManager .GetAllPermission ()
70+ allPermission , err := cmd .UserManager .GetAllPermissionDepartments ()
5271 if err != nil {
5372 return errors .NewAPIError (T ("Failed to get permissions." ), err .Error (), 2 )
5473 }
5574
75+ userPermissions := []PermissionCollection {}
76+
5677 isMasterUser := false
5778 if user .IsMasterUserFlag != nil && * user .IsMasterUserFlag {
58- cmd .UI .Print (T ("This account is the Master User and has all permissions enabled" ))
79+ if outputFormat != "JSON" {
80+ cmd .UI .Print (T ("This account is the Master User and has all permissions enabled" ))
81+ }
5982 isMasterUser = true
6083 }
6184
85+
86+ for _ , department := range allPermission {
87+ depPerm := PermissionCollection {Department : * department .KeyName }
88+ for _ , perm := range department .Permissions {
89+ assignedPerm := UserHasPermission (user .Permissions , * perm .KeyName ) || isMasterUser
90+ thisPerm := Permission {
91+ KeyName : * perm .KeyName ,
92+ Description : * perm .Description ,
93+ Assigned : strconv .FormatBool (assignedPerm ),
94+ }
95+ depPerm .Permissions = append (depPerm .Permissions , thisPerm )
96+ }
97+ userPermissions = append (userPermissions , depPerm )
98+ }
99+
100+ if outputFormat == "JSON" {
101+ return utils .PrintPrettyJSON (cmd .UI , userPermissions )
102+ }
103+
104+
62105 table := cmd .UI .Table ([]string {T ("ID" ), T ("Role Name" ), T ("Description" )})
63106
64107 for _ , role := range user .Roles {
@@ -71,31 +114,28 @@ func (cmd *PermissionsCommand) Run(args []string) error {
71114 table .Add ("" , "" , "" )
72115 table .Print ()
73116
74- tablePermission := cmd .UI .Table ([]string {T ("Description" ), T ("KeyName" ), T ("Assigned" )})
75- for _ , perm := range allPermission {
76- var assigned bool
77- // Display master user as having all permissions, even though they have none, technically.
78- if isMasterUser {
79- assigned = true
80- }
81- for _ , userPerm := range user .Permissions {
82- if perm .KeyName != nil && userPerm .KeyName != nil && * perm .KeyName == * userPerm .KeyName {
83- assigned = true
84- }
85-
86- }
87- flag := true
88- arr := []string {"ACCOUNT_SUMMARY_VIEW" , "REQUEST_COMPLIANCE_REPORT" , "COMPANY_EDIT" , "ONE_TIME_PAYMENTS" , "UPDATE_PAYMENT_DETAILS" ,
89- "EU_LIMITED_PROCESSING_MANAGE" , "TICKET_ADD" , "TICKET_EDIT" , "TICKET_SEARCH" , "TICKET_VIEW" , "TICKET_VIEW_ALL" }
90- for i := 0 ; i < len (arr ); i ++ {
91- if * perm .KeyName == arr [i ] {
92- flag = false
93- }
94- }
95- if flag == true {
96- tablePermission .Add (utils .FormatStringPointer (perm .Name ), utils .FormatStringPointer (perm .KeyName ), strconv .FormatBool (assigned ))
117+ tablePermission := cmd .UI .Table ([]string {T ("Department" ), T ("Permissions" )})
118+ for _ , department := range userPermissions {
119+ buf := new (bytes.Buffer )
120+ headers := []string {T ("KeyName" ), T ("Assigned" ), T ("Description" )}
121+ subTable := terminal .NewTable (buf , headers )
122+ for _ , perm := range department .Permissions {
123+ subTable .Add (perm .KeyName , perm .Assigned , perm .Description )
97124 }
125+ subTable .Print ()
126+ tablePermission .Add (department .Department , buf .String ())
98127 }
99128 tablePermission .Print ()
100129 return nil
101130}
131+
132+
133+ func UserHasPermission (userPermissions []datatypes.User_Customer_CustomerPermission_Permission , keyName string ) bool {
134+ assigned := false
135+ for _ , userPerm := range userPermissions {
136+ if * userPerm .KeyName == keyName {
137+ assigned = true
138+ }
139+ }
140+ return assigned
141+ }
0 commit comments