github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 10 ) 11 12 // User represents a CLI user. 13 // This means that v7action.User has the same fields as a ccv3.User 14 type User ccv3.User 15 16 // CreateUser creates a new user in UAA and registers it with cloud controller. 17 func (actor Actor) CreateUser(username string, password string, origin string) (User, Warnings, error) { 18 uaaUser, err := actor.UAAClient.CreateUser(username, password, origin) 19 if err != nil { 20 return User{}, nil, err 21 } 22 23 ccUser, ccWarnings, err := actor.CloudControllerClient.CreateUser(uaaUser.ID) 24 25 return User(ccUser), Warnings(ccWarnings), err 26 } 27 28 // GetUser gets a user in UAA with the given username and (if provided) origin. 29 // It returns an error if no matching user is found. 30 // It returns an error if multiple matching users are found. 31 // NOTE: The UAA /Users endpoint used here requires admin scopes. 32 func (actor Actor) GetUser(username, origin string) (User, error) { 33 uaaUsers, err := actor.UAAClient.ListUsers(username, origin) 34 if err != nil { 35 return User{}, err 36 } 37 38 if len(uaaUsers) == 0 { 39 return User{}, actionerror.UserNotFoundError{Username: username, Origin: origin} 40 } 41 42 if len(uaaUsers) > 1 { 43 var origins []string 44 for _, user := range uaaUsers { 45 origins = append(origins, user.Origin) 46 } 47 return User{}, actionerror.MultipleUAAUsersFoundError{Username: username, Origins: origins} 48 } 49 50 uaaUser := uaaUsers[0] 51 52 v7actionUser := User{ 53 GUID: uaaUser.ID, 54 Origin: uaaUser.Origin, 55 } 56 return v7actionUser, nil 57 } 58 59 // DeleteUser 60 func (actor Actor) DeleteUser(userGuid string) (Warnings, error) { 61 var allWarnings Warnings 62 jobURL, ccWarningsDelete, err := actor.CloudControllerClient.DeleteUser(userGuid) 63 allWarnings = Warnings(ccWarningsDelete) 64 65 // If there is an error that is not a ResourceNotFoundError 66 if _, ok := err.(ccerror.ResourceNotFoundError); !ok && err != nil { 67 return allWarnings, err 68 } 69 70 ccWarningsPoll, err := actor.CloudControllerClient.PollJob(jobURL) 71 allWarnings = append(allWarnings, Warnings(ccWarningsPoll)...) 72 if err != nil { 73 return allWarnings, err 74 } 75 76 _, err = actor.UAAClient.DeleteUser(userGuid) 77 78 return allWarnings, err 79 } 80 81 func SortUsers(users []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 User) string { 101 if user.Origin == "" { 102 return "client" 103 } 104 105 return user.Origin 106 }