|
| 1 | +// Copyright 2015 The Gogs Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gogs |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | +) |
| 13 | + |
| 14 | +type CreateUserOption struct { |
| 15 | + SourceID int64 `json:"source_id"` |
| 16 | + LoginName string `json:"login_name"` |
| 17 | + Username string `json:"username" binding:"Required;AlphaDashDot;MaxSize(35)"` |
| 18 | + Email string `json:"email" binding:"Required;Email;MaxSize(254)"` |
| 19 | + Password string `json:"password" binding:"MaxSize(255)"` |
| 20 | + SendNotify bool `json:"send_notify"` |
| 21 | +} |
| 22 | + |
| 23 | +func (c *Client) CreateUser(opt CreateUserOption) (*User, error) { |
| 24 | + body, err := json.Marshal(&opt) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + user := new(User) |
| 29 | + return user, c.getParsedResponse("POST", "/admin/users", |
| 30 | + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), user) |
| 31 | +} |
| 32 | + |
| 33 | +type EditUserOption struct { |
| 34 | + SourceID int64 `json:"source_id"` |
| 35 | + LoginName string `json:"login_name"` |
| 36 | + FullName string `json:"full_name" binding:"MaxSize(100)"` |
| 37 | + Email string `json:"email" binding:"Required;Email;MaxSize(254)"` |
| 38 | + Password string `json:"password" binding:"MaxSize(255)"` |
| 39 | + Website string `json:"website" binding:"MaxSize(50)"` |
| 40 | + Location string `json:"location" binding:"MaxSize(50)"` |
| 41 | + Active *bool `json:"active"` |
| 42 | + Admin *bool `json:"admin"` |
| 43 | + AllowGitHook *bool `json:"allow_git_hook"` |
| 44 | + AllowImportLocal *bool `json:"allow_import_local"` |
| 45 | +} |
| 46 | + |
| 47 | +func (c *Client) EditUser(user string, opt EditUserOption) error { |
| 48 | + body, err := json.Marshal(&opt) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + _, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), |
| 53 | + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) |
| 54 | + return err |
| 55 | +} |
| 56 | + |
| 57 | +func (c *Client) DeleteUser(user string) error { |
| 58 | + _, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil) |
| 59 | + return err |
| 60 | +} |
| 61 | + |
| 62 | +func (c *Client) CreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) { |
| 63 | + body, err := json.Marshal(&opt) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + key := new(PublicKey) |
| 68 | + return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), |
| 69 | + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) |
| 70 | +} |
0 commit comments