| 1 |
// Copyright 2019 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
// CreateUserRequest represents the fields sent to the `CreateUser` endpoint. |
| 14 |
// Note that `Login` is a required field. |
| 15 |
type CreateUserRequest struct { |
| 16 |
Login string `json:"login"` |
| 17 |
Email *string `json:"email,omitempty"` |
| 18 |
Suspended *bool `json:"suspended,omitempty"` |
| 19 |
} |
| 20 |
|
| 21 |
// CreateUser creates a new user in GitHub Enterprise. |
| 22 |
// |
| 23 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/users#create-a-user |
| 24 |
// |
| 25 |
//meta:operation POST /admin/users |
| 26 |
func (s *AdminService) CreateUser(ctx context.Context, userReq CreateUserRequest) (*User, *Response, error) { |
| 27 |
u := "admin/users" |
| 28 |
|
| 29 |
req, err := s.client.NewRequest("POST", u, userReq) |
| 30 |
if err != nil { |
| 31 |
return nil, nil, err |
| 32 |
} |
| 33 |
|
| 34 |
var user User |
| 35 |
resp, err := s.client.Do(ctx, req, &user) |
| 36 |
if err != nil { |
| 37 |
return nil, resp, err |
| 38 |
} |
| 39 |
|
| 40 |
return &user, resp, nil |
| 41 |
} |
| 42 |
|
| 43 |
// DeleteUser deletes a user in GitHub Enterprise. |
| 44 |
// |
| 45 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/users#delete-a-user |
| 46 |
// |
| 47 |
//meta:operation DELETE /admin/users/{username} |
| 48 |
func (s *AdminService) DeleteUser(ctx context.Context, username string) (*Response, error) { |
| 49 |
u := "admin/users/" + username |
| 50 |
|
| 51 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
|
| 56 |
resp, err := s.client.Do(ctx, req, nil) |
| 57 |
if err != nil { |
| 58 |
return resp, err |
| 59 |
} |
| 60 |
|
| 61 |
return resp, nil |
| 62 |
} |
| 63 |
|
| 64 |
// ImpersonateUserOptions represents the scoping for the OAuth token. |
| 65 |
type ImpersonateUserOptions struct { |
| 66 |
Scopes []string `json:"scopes,omitempty"` |
| 67 |
} |
| 68 |
|
| 69 |
// OAuthAPP represents the GitHub Site Administrator OAuth app. |
| 70 |
type OAuthAPP struct { |
| 71 |
URL *string `json:"url,omitempty"` |
| 72 |
Name *string `json:"name,omitempty"` |
| 73 |
ClientID *string `json:"client_id,omitempty"` |
| 74 |
} |
| 75 |
|
| 76 |
func (s OAuthAPP) String() string { |
| 77 |
return Stringify(s) |
| 78 |
} |
| 79 |
|
| 80 |
// UserAuthorization represents the impersonation response. |
| 81 |
type UserAuthorization struct { |
| 82 |
ID *int64 `json:"id,omitempty"` |
| 83 |
URL *string `json:"url,omitempty"` |
| 84 |
Scopes []string `json:"scopes,omitempty"` |
| 85 |
Token *string `json:"token,omitempty"` |
| 86 |
TokenLastEight *string `json:"token_last_eight,omitempty"` |
| 87 |
HashedToken *string `json:"hashed_token,omitempty"` |
| 88 |
App *OAuthAPP `json:"app,omitempty"` |
| 89 |
Note *string `json:"note,omitempty"` |
| 90 |
NoteURL *string `json:"note_url,omitempty"` |
| 91 |
UpdatedAt *Timestamp `json:"updated_at,omitempty"` |
| 92 |
CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 93 |
Fingerprint *string `json:"fingerprint,omitempty"` |
| 94 |
} |
| 95 |
|
| 96 |
// CreateUserImpersonation creates an impersonation OAuth token. |
| 97 |
// |
| 98 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/users#create-an-impersonation-oauth-token |
| 99 |
// |
| 100 |
//meta:operation POST /admin/users/{username}/authorizations |
| 101 |
func (s *AdminService) CreateUserImpersonation(ctx context.Context, username string, opts *ImpersonateUserOptions) (*UserAuthorization, *Response, error) { |
| 102 |
u := fmt.Sprintf("admin/users/%s/authorizations", username) |
| 103 |
|
| 104 |
req, err := s.client.NewRequest("POST", u, opts) |
| 105 |
if err != nil { |
| 106 |
return nil, nil, err |
| 107 |
} |
| 108 |
|
| 109 |
a := new(UserAuthorization) |
| 110 |
resp, err := s.client.Do(ctx, req, a) |
| 111 |
if err != nil { |
| 112 |
return nil, resp, err |
| 113 |
} |
| 114 |
|
| 115 |
return a, resp, nil |
| 116 |
} |
| 117 |
|
| 118 |
// DeleteUserImpersonation deletes an impersonation OAuth token. |
| 119 |
// |
| 120 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/users#delete-an-impersonation-oauth-token |
| 121 |
// |
| 122 |
//meta:operation DELETE /admin/users/{username}/authorizations |
| 123 |
func (s *AdminService) DeleteUserImpersonation(ctx context.Context, username string) (*Response, error) { |
| 124 |
u := fmt.Sprintf("admin/users/%s/authorizations", username) |
| 125 |
|
| 126 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 127 |
if err != nil { |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
|
| 131 |
resp, err := s.client.Do(ctx, req, nil) |
| 132 |
if err != nil { |
| 133 |
return resp, err |
| 134 |
} |
| 135 |
|
| 136 |
return resp, nil |
| 137 |
} |