github.com/sleungcy/cli@v7.1.0+incompatible/actor/v7action/user.go (about)

     1  package v7action
     2  
     3  import (
     4  	"sort"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/resources"
    10  )
    11  
    12  // CreateUser creates a new user in UAA and registers it with cloud controller.
    13  func (actor Actor) CreateUser(username string, password string, origin string) (resources.User, Warnings, error) {
    14  	uaaUser, err := actor.UAAClient.CreateUser(username, password, origin)
    15  	if err != nil {
    16  		return resources.User{}, nil, err
    17  	}
    18  
    19  	ccUser, ccWarnings, err := actor.CloudControllerClient.CreateUser(uaaUser.ID)
    20  
    21  	return resources.User(ccUser), Warnings(ccWarnings), err
    22  }
    23  
    24  // GetUser gets a user in UAA with the given username and (if provided) origin.
    25  // It returns an error if no matching user is found.
    26  // It returns an error if multiple matching users are found.
    27  // NOTE: The UAA /Users endpoint used here requires admin scopes.
    28  func (actor Actor) GetUser(username, origin string) (resources.User, error) {
    29  	uaaUsers, err := actor.UAAClient.ListUsers(username, origin)
    30  	if err != nil {
    31  		return resources.User{}, err
    32  	}
    33  
    34  	if len(uaaUsers) == 0 {
    35  		return resources.User{}, actionerror.UserNotFoundError{Username: username, Origin: origin}
    36  	}
    37  
    38  	if len(uaaUsers) > 1 {
    39  		var origins []string
    40  		for _, user := range uaaUsers {
    41  			origins = append(origins, user.Origin)
    42  		}
    43  		return resources.User{}, actionerror.MultipleUAAUsersFoundError{Username: username, Origins: origins}
    44  	}
    45  
    46  	uaaUser := uaaUsers[0]
    47  
    48  	v7actionUser := resources.User{
    49  		GUID:   uaaUser.ID,
    50  		Origin: uaaUser.Origin,
    51  	}
    52  	return v7actionUser, nil
    53  }
    54  
    55  // DeleteUser
    56  func (actor Actor) DeleteUser(userGuid string) (Warnings, error) {
    57  	var allWarnings Warnings
    58  	jobURL, ccWarningsDelete, err := actor.CloudControllerClient.DeleteUser(userGuid)
    59  	allWarnings = Warnings(ccWarningsDelete)
    60  
    61  	// If there is an error that is not a ResourceNotFoundError
    62  	if _, ok := err.(ccerror.ResourceNotFoundError); !ok && err != nil {
    63  		return allWarnings, err
    64  	}
    65  
    66  	ccWarningsPoll, err := actor.CloudControllerClient.PollJob(jobURL)
    67  	allWarnings = append(allWarnings, Warnings(ccWarningsPoll)...)
    68  	if err != nil {
    69  		return allWarnings, err
    70  	}
    71  
    72  	_, err = actor.UAAClient.DeleteUser(userGuid)
    73  
    74  	return allWarnings, err
    75  }
    76  
    77  func (actor Actor) UpdateUserPassword(userGUID string, oldPassword string, newPassword string) error {
    78  	return actor.UAAClient.UpdatePassword(userGUID, oldPassword, newPassword)
    79  }
    80  
    81  func SortUsers(users []resources.User) {
    82  	sort.Slice(users, func(i, j int) bool {
    83  		if users[i].PresentationName == users[j].PresentationName {
    84  
    85  			if users[i].Origin == constant.DefaultOriginUaa || users[j].Origin == "" {
    86  				return true
    87  			}
    88  
    89  			if users[j].Origin == constant.DefaultOriginUaa || users[i].Origin == "" {
    90  				return false
    91  			}
    92  
    93  			return users[i].Origin < users[j].Origin
    94  		}
    95  
    96  		return users[i].PresentationName < users[j].PresentationName
    97  	})
    98  }
    99  
   100  func GetHumanReadableOrigin(user resources.User) string {
   101  	if user.Origin == "" {
   102  		return "client"
   103  	}
   104  
   105  	return user.Origin
   106  }