...

Source file src/github.com/google/go-github/v45/github/admin_users.go

Documentation: github.com/google/go-github/v45/github

     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 is a subset of User and is used internally
    14  // by CreateUser to pass only the known fields for the endpoint.
    15  type createUserRequest struct {
    16  	Login *string `json:"login,omitempty"`
    17  	Email *string `json:"email,omitempty"`
    18  }
    19  
    20  // CreateUser creates a new user in GitHub Enterprise.
    21  //
    22  // GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/users/#create-a-new-user
    23  func (s *AdminService) CreateUser(ctx context.Context, login, email string) (*User, *Response, error) {
    24  	u := "admin/users"
    25  
    26  	userReq := &createUserRequest{
    27  		Login: &login,
    28  		Email: &email,
    29  	}
    30  
    31  	req, err := s.client.NewRequest("POST", u, userReq)
    32  	if err != nil {
    33  		return nil, nil, err
    34  	}
    35  
    36  	var user User
    37  	resp, err := s.client.Do(ctx, req, &user)
    38  	if err != nil {
    39  		return nil, resp, err
    40  	}
    41  
    42  	return &user, resp, nil
    43  }
    44  
    45  // DeleteUser deletes a user in GitHub Enterprise.
    46  //
    47  // GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/users/#delete-a-user
    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 Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/users/#create-an-impersonation-oauth-token
    99  func (s *AdminService) CreateUserImpersonation(ctx context.Context, username string, opts *ImpersonateUserOptions) (*UserAuthorization, *Response, error) {
   100  	u := fmt.Sprintf("admin/users/%s/authorizations", username)
   101  
   102  	req, err := s.client.NewRequest("POST", u, opts)
   103  	if err != nil {
   104  		return nil, nil, err
   105  	}
   106  
   107  	a := new(UserAuthorization)
   108  	resp, err := s.client.Do(ctx, req, a)
   109  	if err != nil {
   110  		return nil, resp, err
   111  	}
   112  
   113  	return a, resp, nil
   114  }
   115  
   116  // DeleteUserImpersonation deletes an impersonation OAuth token.
   117  //
   118  // GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/users/#delete-an-impersonation-oauth-token
   119  func (s *AdminService) DeleteUserImpersonation(ctx context.Context, username string) (*Response, error) {
   120  	u := fmt.Sprintf("admin/users/%s/authorizations", username)
   121  
   122  	req, err := s.client.NewRequest("DELETE", u, nil)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	resp, err := s.client.Do(ctx, req, nil)
   128  	if err != nil {
   129  		return resp, err
   130  	}
   131  
   132  	return resp, nil
   133  }
   134  

View as plain text