github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/set_org_role_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     6  	"code.cloudfoundry.org/cli/cf/errors"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  )
    10  
    11  type SetOrgRoleCommand struct {
    12  	BaseCommand
    13  
    14  	Args            flag.OrgRoleArgs `positional-args:"yes"`
    15  	IsClient        bool             `long:"client" description:"Assign an org role to a client-id of a (non-user) service account"`
    16  	Origin          string           `long:"origin" description:"Indicates the identity provider to be used for authentication"`
    17  	usage           interface{}      `usage:"CF_NAME set-org-role USERNAME ORG ROLE\n   CF_NAME set-org-role USERNAME ORG ROLE [--client]\n   CF_NAME set-org-role USERNAME ORG ROLE [--origin ORIGIN]\n\nROLES:\n   OrgManager - Invite and manage users, select and change plans, and set spending limits\n   BillingManager - Create and manage the billing account and payment info\n   OrgAuditor - Read-only access to org info and reports"`
    18  	relatedCommands interface{}      `related_commands:"org-users, set-space-role"`
    19  }
    20  
    21  func (cmd *SetOrgRoleCommand) Execute(args []string) error {
    22  	err := cmd.validateFlags()
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	err = cmd.SharedActor.CheckTarget(false, false)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	currentUser, err := cmd.Config.CurrentUser()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	cmd.UI.DisplayTextWithFlavor("Assigning role {{.RoleType}} to user {{.TargetUserName}} in org {{.OrgName}} as {{.CurrentUserName}}...", map[string]interface{}{
    38  		"RoleType":        cmd.Args.Role.Role,
    39  		"TargetUserName":  cmd.Args.Username,
    40  		"OrgName":         cmd.Args.Organization,
    41  		"CurrentUserName": currentUser.Name,
    42  	})
    43  
    44  	roleType, err := convertRoleType(cmd.Args.Role)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	org, warnings, err := cmd.Actor.GetOrganizationByName(cmd.Args.Organization)
    50  	cmd.UI.DisplayWarnings(warnings)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	warnings, err = cmd.Actor.CreateOrgRole(roleType, org.GUID, cmd.Args.Username, cmd.Origin, cmd.IsClient)
    56  	cmd.UI.DisplayWarnings(warnings)
    57  	if err != nil {
    58  		if _, ok := err.(ccerror.RoleAlreadyExistsError); ok {
    59  			cmd.UI.DisplayWarning("User '{{.TargetUserName}}' already has role '{{.RoleType}}' in org '{{.OrgName}}'.", map[string]interface{}{
    60  				"RoleType":       cmd.Args.Role.Role,
    61  				"TargetUserName": cmd.Args.Username,
    62  				"OrgName":        cmd.Args.Organization,
    63  			})
    64  		} else {
    65  			return err
    66  		}
    67  	}
    68  
    69  	cmd.UI.DisplayOK()
    70  
    71  	return nil
    72  }
    73  
    74  func (cmd SetOrgRoleCommand) validateFlags() error {
    75  	if cmd.IsClient && cmd.Origin != "" {
    76  		return translatableerror.ArgumentCombinationError{
    77  			Args: []string{"--client", "--origin"},
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func convertRoleType(givenRole flag.OrgRole) (constant.RoleType, error) {
    85  	switch givenRole.Role {
    86  	case "OrgAuditor":
    87  		return constant.OrgAuditorRole, nil
    88  	case "OrgManager":
    89  		return constant.OrgManagerRole, nil
    90  	case "BillingManager":
    91  		return constant.OrgBillingManagerRole, nil
    92  	default:
    93  		return "", errors.New("Invalid role type.")
    94  	}
    95  }