github.com/vmware/govmomi@v0.43.0/govc/sso/user/update.go (about) 1 /* 2 Copyright (c) 2018 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package user 18 19 import ( 20 "context" 21 "flag" 22 23 "github.com/vmware/govmomi/govc/cli" 24 "github.com/vmware/govmomi/govc/sso" 25 "github.com/vmware/govmomi/ssoadmin" 26 "github.com/vmware/govmomi/ssoadmin/types" 27 ) 28 29 type update struct { 30 userDetails 31 } 32 33 func init() { 34 cli.Register("sso.user.update", &update{}) 35 } 36 37 func (cmd *update) Description() string { 38 return `Update SSO users. 39 40 Examples: 41 govc sso.user.update -C "$(cat cert.pem)" NAME 42 govc sso.user.update -p password NAME` 43 } 44 45 // merge uses the current value of a field if the corresponding flag was not set. 46 // Otherwise the API call would set the current fields to empty strings. 47 func merge(src *string, current string) { 48 if *src == "" { 49 *src = current 50 } 51 } 52 53 func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error { 54 if f.NArg() != 1 { 55 return flag.ErrHelp 56 } 57 id := f.Arg(0) 58 59 return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error { 60 user, err := c.FindUser(ctx, id) 61 if err != nil { 62 return err 63 } 64 65 if user.Kind == "person" { 66 current, cerr := c.FindPersonUser(ctx, id) 67 if cerr != nil { 68 return cerr 69 } 70 71 merge(&cmd.AdminPersonDetails.Description, current.Details.Description) 72 merge(&cmd.FirstName, current.Details.FirstName) 73 merge(&cmd.LastName, current.Details.LastName) 74 merge(&cmd.EmailAddress, current.Details.EmailAddress) 75 if err := c.UpdatePersonUser(ctx, id, cmd.AdminPersonDetails); err != nil { 76 return err 77 } 78 79 if cmd.password != "" { 80 return c.ResetPersonPassword(ctx, id, cmd.password) 81 } 82 83 return nil 84 } 85 86 if cmd.actas != nil { 87 action := c.GrantWSTrustRole 88 if !*cmd.actas { 89 action = c.RevokeWSTrustRole 90 } 91 if _, err := action(ctx, user.Id, types.RoleActAsUser); err != nil { 92 return err 93 } 94 } 95 96 if cmd.role != "" { 97 if _, err := c.SetRole(ctx, user.Id, cmd.role); err != nil { 98 return err 99 } 100 } 101 102 cmd.solution.Certificate = cmd.Certificate() 103 cmd.solution.Description = cmd.AdminPersonDetails.Description 104 105 current, cerr := c.FindSolutionUser(ctx, id) 106 if cerr != nil { 107 return cerr 108 } 109 110 merge(&cmd.solution.Certificate, current.Details.Certificate) 111 merge(&cmd.solution.Description, current.Details.Description) 112 return c.UpdateSolutionUser(ctx, id, cmd.solution) 113 }) 114 }