|
| 1 | +package box |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/tarantool/go-tarantool/v2" |
| 8 | + "github.com/vmihailenco/msgpack/v5" |
| 9 | +) |
| 10 | + |
| 11 | +// SchemaUser provides methods to interact with schema-related user operations in Tarantool. |
| 12 | +type SchemaUser struct { |
| 13 | + conn tarantool.Doer // Connection interface for interacting with Tarantool. |
| 14 | +} |
| 15 | + |
| 16 | +func NewSchemaUser(conn tarantool.Doer) *SchemaUser { |
| 17 | + return &SchemaUser{conn: conn} |
| 18 | +} |
| 19 | + |
| 20 | +// UserExistsRequest represents a request to check if a user exists in Tarantool. |
| 21 | +type UserExistsRequest struct { |
| 22 | + *tarantool.CallRequest // Underlying Tarantool call request. |
| 23 | +} |
| 24 | + |
| 25 | +// UserExistsResponse represents the response to a user existence check. |
| 26 | +type UserExistsResponse struct { |
| 27 | + Exists bool // True if the user exists, false otherwise. |
| 28 | +} |
| 29 | + |
| 30 | +// DecodeMsgpack decodes the response from a Msgpack-encoded byte slice. |
| 31 | +func (uer *UserExistsResponse) DecodeMsgpack(d *msgpack.Decoder) error { |
| 32 | + arrayLen, err := d.DecodeArrayLen() |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + // Ensure that the response array contains exactly 1 element (the "Exists" field). |
| 38 | + if arrayLen != 1 { |
| 39 | + return fmt.Errorf("protocol violation; expected 1 array entry, got %d", arrayLen) |
| 40 | + } |
| 41 | + |
| 42 | + // Decode the boolean value indicating whether the user exists. |
| 43 | + uer.Exists, err = d.DecodeBool() |
| 44 | + |
| 45 | + return err |
| 46 | +} |
| 47 | + |
| 48 | +// NewUserExistsRequest creates a new request to check if a user exists. |
| 49 | +func NewUserExistsRequest(username string) UserExistsRequest { |
| 50 | + callReq := tarantool.NewCallRequest("box.schema.user.exists").Args([]interface{}{username}) |
| 51 | + |
| 52 | + return UserExistsRequest{ |
| 53 | + callReq, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Exists checks if the specified user exists in Tarantool. |
| 58 | +func (u *SchemaUser) Exists(ctx context.Context, username string) (bool, error) { |
| 59 | + // Create a request and send it to Tarantool. |
| 60 | + req := NewUserExistsRequest(username).Context(ctx) |
| 61 | + resp := &UserExistsResponse{} |
| 62 | + |
| 63 | + // Execute the request and parse the response. |
| 64 | + err := u.conn.Do(req).GetTyped(resp) |
| 65 | + |
| 66 | + return resp.Exists, err |
| 67 | +} |
| 68 | + |
| 69 | +// UserCreateOptions represents options for creating a user in Tarantool. |
| 70 | +type UserCreateOptions struct { |
| 71 | + // IfNotExists - if true, prevents an error if the user already exists. |
| 72 | + IfNotExists bool `msgpack:"if_not_exists"` |
| 73 | + // Password for the new user. |
| 74 | + Password string `msgpack:"password"` |
| 75 | +} |
| 76 | + |
| 77 | +// UserCreateRequest represents a request to create a new user in Tarantool. |
| 78 | +type UserCreateRequest struct { |
| 79 | + *tarantool.CallRequest // Underlying Tarantool call request. |
| 80 | +} |
| 81 | + |
| 82 | +// NewUserCreateRequest creates a new request to create a user with specified options. |
| 83 | +func NewUserCreateRequest(username string, options UserCreateOptions) UserCreateRequest { |
| 84 | + callReq := tarantool.NewCallRequest("box.schema.user.create"). |
| 85 | + Args([]interface{}{username, options}) |
| 86 | + |
| 87 | + return UserCreateRequest{ |
| 88 | + callReq, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// UserCreateResponse represents the response to a user creation request. |
| 93 | +type UserCreateResponse struct { |
| 94 | +} |
| 95 | + |
| 96 | +// DecodeMsgpack decodes the response for a user creation request. |
| 97 | +// In this case, the response does not contain any data. |
| 98 | +func (uer *UserCreateResponse) DecodeMsgpack(_ *msgpack.Decoder) error { |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// Create creates a new user in Tarantool with the given username and options. |
| 103 | +func (u *SchemaUser) Create(ctx context.Context, username string, options UserCreateOptions) error { |
| 104 | + // Create a request and send it to Tarantool. |
| 105 | + req := NewUserCreateRequest(username, options).Context(ctx) |
| 106 | + resp := &UserCreateResponse{} |
| 107 | + |
| 108 | + // Execute the request and handle the response. |
| 109 | + fut := u.conn.Do(req) |
| 110 | + |
| 111 | + err := fut.GetTyped(resp) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +// UserDropOptions represents options for dropping a user in Tarantool. |
| 120 | +type UserDropOptions struct { |
| 121 | + IfExists bool `msgpack:"if_exists"` // If true, prevents an error if the user does not exist. |
| 122 | +} |
| 123 | + |
| 124 | +// UserDropRequest represents a request to drop a user from Tarantool. |
| 125 | +type UserDropRequest struct { |
| 126 | + *tarantool.CallRequest // Underlying Tarantool call request. |
| 127 | +} |
| 128 | + |
| 129 | +// NewUserDropRequest creates a new request to drop a user with specified options. |
| 130 | +func NewUserDropRequest(username string, options UserDropOptions) UserDropRequest { |
| 131 | + callReq := tarantool.NewCallRequest("box.schema.user.drop"). |
| 132 | + Args([]interface{}{username, options}) |
| 133 | + |
| 134 | + return UserDropRequest{ |
| 135 | + callReq, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// UserDropResponse represents the response to a user drop request. |
| 140 | +type UserDropResponse struct{} |
| 141 | + |
| 142 | +// Drop drops the specified user from Tarantool, with optional conditions. |
| 143 | +func (u *SchemaUser) Drop(ctx context.Context, username string, options UserDropOptions) error { |
| 144 | + // Create a request and send it to Tarantool. |
| 145 | + req := NewUserDropRequest(username, options).Context(ctx) |
| 146 | + resp := &UserCreateResponse{} |
| 147 | + |
| 148 | + // Execute the request and handle the response. |
| 149 | + fut := u.conn.Do(req) |
| 150 | + |
| 151 | + err := fut.GetTyped(resp) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +// UserPasswordRequest represents a request to retrieve a user's password from Tarantool. |
| 160 | +type UserPasswordRequest struct { |
| 161 | + *tarantool.CallRequest // Underlying Tarantool call request. |
| 162 | +} |
| 163 | + |
| 164 | +// NewUserPasswordRequest creates a new request to fetch the user's password. |
| 165 | +// It takes the username and constructs the request to Tarantool. |
| 166 | +func NewUserPasswordRequest(username string) UserPasswordRequest { |
| 167 | + // Create a request to get the user's password. |
| 168 | + callReq := tarantool.NewCallRequest("box.schema.user.password").Args([]interface{}{username}) |
| 169 | + |
| 170 | + return UserPasswordRequest{ |
| 171 | + callReq, |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// UserPasswordResponse represents the response to the user password request. |
| 176 | +// It contains the password hash. |
| 177 | +type UserPasswordResponse struct { |
| 178 | + Hash string // The password hash of the user. |
| 179 | +} |
| 180 | + |
| 181 | +// DecodeMsgpack decodes the response from Tarantool in Msgpack format. |
| 182 | +// It expects the response to be an array of length 1, containing the password hash string. |
| 183 | +func (upr *UserPasswordResponse) DecodeMsgpack(d *msgpack.Decoder) error { |
| 184 | + // Decode the array length. |
| 185 | + arrayLen, err := d.DecodeArrayLen() |
| 186 | + if err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + // Ensure the array contains exactly 1 element (the password hash). |
| 191 | + if arrayLen != 1 { |
| 192 | + return fmt.Errorf("protocol violation; expected 1 array entry, got %d", arrayLen) |
| 193 | + } |
| 194 | + |
| 195 | + // Decode the string containing the password hash. |
| 196 | + upr.Hash, err = d.DecodeString() |
| 197 | + |
| 198 | + return err |
| 199 | +} |
| 200 | + |
| 201 | +// Password sends a request to retrieve the user's password from Tarantool. |
| 202 | +// It returns the password hash as a string or an error if the request fails. |
| 203 | +func (u *SchemaUser) Password(ctx context.Context, username string) (string, error) { |
| 204 | + // Create the request and send it to Tarantool. |
| 205 | + req := NewUserPasswordRequest(username).Context(ctx) |
| 206 | + resp := &UserPasswordResponse{} |
| 207 | + |
| 208 | + // Execute the request and handle the response. |
| 209 | + fut := u.conn.Do(req) |
| 210 | + |
| 211 | + // Get the decoded response. |
| 212 | + err := fut.GetTyped(resp) |
| 213 | + if err != nil { |
| 214 | + return "", err |
| 215 | + } |
| 216 | + |
| 217 | + // Return the password hash. |
| 218 | + return resp.Hash, nil |
| 219 | +} |
0 commit comments