Debian Codegraph @

client.go

raw symbols SCIP
1 package g8
2
3 // Client is a struct containing both a Token and a slice of extra Permissions that said token has.
4 type Client struct {
5 // Token is the value used to authenticate with the API.
6 Token string
7
8 // Permissions is a slice of extra permissions that may be used for more granular access control.
9 //
10 // If you only wish to use Gate.Protect and Gate.ProtectFunc, you do not have to worry about this,
11 // since they're only used by Gate.ProtectWithPermissions and Gate.ProtectFuncWithPermissions
12 Permissions []string
13
14 // Data is a field that can be used to store any data you want to associate with the client.
15 Data any
16 }
17
18 // NewClient creates a Client with a given token
19 func NewClient(token string) *Client {
20 return &Client{
21 Token: token,
22 }
23 }
24
25 // NewClientWithPermissions creates a Client with a slice of permissions
26 // Equivalent to using NewClient and WithPermissions
27 func NewClientWithPermissions(token string, permissions []string) *Client {
28 return NewClient(token).WithPermissions(permissions)
29 }
30
31 // NewClientWithData creates a Client with some data
32 // Equivalent to using NewClient and WithData
33 func NewClientWithData(token string, data any) *Client {
34 return NewClient(token).WithData(data)
35 }
36
37 // NewClientWithPermissionsAndData creates a Client with a slice of permissions and some data
38 // Equivalent to using NewClient, WithPermissions and WithData
39 func NewClientWithPermissionsAndData(token string, permissions []string, data any) *Client {
40 return NewClient(token).WithPermissions(permissions).WithData(data)
41 }
42
43 // WithPermissions adds a slice of permissions to a client
44 func (client *Client) WithPermissions(permissions []string) *Client {
45 client.Permissions = append(client.Permissions, permissions...)
46 return client
47 }
48
49 // WithPermission adds a permission to a client
50 func (client *Client) WithPermission(permission string) *Client {
51 client.Permissions = append(client.Permissions, permission)
52 return client
53 }
54
55 // WithData attaches data to a client
56 func (client *Client) WithData(data any) *Client {
57 client.Data = data
58 return client
59 }
60
61 // HasPermission checks whether a client has a given permission
62 func (client *Client) HasPermission(permissionRequired string) bool {
63 for _, permission := range client.Permissions {
64 if permissionRequired == permission {
65 return true
66 }
67 }
68 return false
69 }
70
71 // HasPermissions checks whether a client has the all permissions passed
72 func (client *Client) HasPermissions(permissionsRequired []string) bool {
73 for _, permissionRequired := range permissionsRequired {
74 if !client.HasPermission(permissionRequired) {
75 return false
76 }
77 }
78 return true
79 }