Skip to content

Commit 4b541fa

Browse files
committed
admin users
1 parent a27e272 commit 4b541fa

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
.idea

admin_users.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

gogs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func Version() string {
17-
return "0.3.0"
17+
return "0.4.0"
1818
}
1919

2020
// Client represents a Gogs API client.

utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
func Bool(v bool) *bool {
8+
return &v
9+
}

0 commit comments

Comments
 (0)