Skip to content

Commit cb5ddc7

Browse files
authored
impr(api): add support of bracket pinning per user and team (#155)
1 parent c676131 commit cb5ddc7

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

api/teams.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type PostTeamsParams struct {
2828
Banned bool `json:"banned"`
2929
Hidden bool `json:"hidden"`
3030
Fields []Field `json:"fields"`
31+
BracketID *string `json:"bracket_id,omitempty"`
3132
}
3233

3334
func (client *Client) PostTeams(params *PostTeamsParams, opts ...Option) (*Team, error) {
@@ -61,6 +62,7 @@ type PatchTeamsParams struct {
6162
Banned *bool `json:"banned,omitempty"`
6263
Hidden *bool `json:"hidden,omitempty"`
6364
Fields []Field `json:"fields"`
65+
BracketID *string `json:"bracket_id,omitempty"`
6466
}
6567

6668
func (client *Client) PatchTeamsMe(params *PatchTeamsParams, opts ...Option) (*Team, error) {

api/users.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type PostUsersParams struct {
3232
Hidden bool `json:"hidden"`
3333
Banned bool `json:"banned"`
3434
Fields []Field `json:"fields"`
35+
BracketID *string `json:"bracket_id,omitempty"`
3536
}
3637

3738
func (client *Client) PostUsers(params *PostUsersParams, opts ...Option) (*User, error) {
@@ -63,6 +64,7 @@ type PatchUsersParams struct {
6364
Hidden *bool `json:"hidden,omitempty"`
6465
Banned *bool `json:"banned,omitempty"`
6566
Fields []Field `json:"fields"`
67+
BracketID *string `json:"bracket_id,omitempty"`
6668
}
6769

6870
func (client *Client) PatchUsersMe(params *PatchUsersParams, opts ...Option) (*User, error) {

examples/bracket/main.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
8+
"github.com/ctfer-io/go-ctfd/api"
9+
)
10+
11+
func main() {
12+
url := "http://localhost:8000"
13+
apiKey := "ctfd_xxx"
14+
15+
// Connecting to existing CTFd
16+
fmt.Println("[+] Getting initial nonce and session values")
17+
nonce, session, err := api.GetNonceAndSession(url)
18+
if err != nil {
19+
log.Fatalf("Getting nonce and session: %s", err)
20+
}
21+
cli := api.NewClient(url, nonce, session, apiKey)
22+
23+
// Create a user bracket
24+
fmt.Println("[+] Creating user bracket")
25+
bu, err := cli.PostBrackets(&api.PostBracketsParams{
26+
Name: "Juniors",
27+
Description: "Brackets for 14-25 years old players.",
28+
Type: "users",
29+
})
30+
if err != nil {
31+
log.Fatalf("Creating bracket: %s", err)
32+
}
33+
34+
// Create a user
35+
fmt.Println("[+] Creating user")
36+
user, err := cli.PostUsers(&api.PostUsersParams{
37+
Name: "player1",
38+
39+
Password: "password",
40+
BracketID: ptr(strconv.Itoa(bu.ID)),
41+
Type: "user",
42+
Fields: []api.Field{},
43+
})
44+
if err != nil {
45+
log.Fatalf("Creating user: %s", err)
46+
}
47+
fmt.Printf("Created user %d\n", user.ID)
48+
49+
// Create a team bracket
50+
fmt.Println("[+] Creating team bracket")
51+
bt, err := cli.PostBrackets(&api.PostBracketsParams{
52+
Name: "Students",
53+
Description: "Brackets for students",
54+
Type: "teams",
55+
})
56+
if err != nil {
57+
log.Fatalf("Creating bracket: %s", err)
58+
}
59+
60+
// Create a team
61+
team, err := cli.PostTeams(&api.PostTeamsParams{
62+
Name: "students",
63+
64+
Password: "password",
65+
BracketID: ptr(strconv.Itoa(bt.ID)),
66+
Fields: []api.Field{},
67+
})
68+
if err != nil {
69+
log.Fatalf("Creating team: %s", err)
70+
}
71+
fmt.Printf("Created team %d\n", team.ID)
72+
73+
if _, err := cli.PostTeamMembers(team.ID, &api.PostTeamsMembersParams{
74+
UserID: user.ID,
75+
}); err != nil {
76+
log.Fatalf("Adding user %d in team %d: %s", user.ID, team.ID, err)
77+
}
78+
fmt.Printf("Added user %d in team %d\n", user.ID, team.ID)
79+
}
80+
81+
func ptr[T any](t T) *T {
82+
return &t
83+
}

0 commit comments

Comments
 (0)