github.com/influxdata/influxdb/v2@v2.7.6/user.go (about) 1 package influxdb 2 3 import ( 4 "context" 5 6 "github.com/influxdata/influxdb/v2/kit/platform" 7 "github.com/influxdata/influxdb/v2/kit/platform/errors" 8 ) 9 10 // UserStatus indicates whether a user is active or inactive 11 type UserStatus string 12 13 // Valid validates user status 14 func (u *UserStatus) Valid() error { 15 if *u != "active" && *u != "inactive" { 16 return &errors.Error{Code: errors.EInvalid, Msg: "Invalid user status"} 17 } 18 19 return nil 20 } 21 22 // User is a user. 🎉 23 type User struct { 24 ID platform.ID `json:"id,omitempty"` 25 Name string `json:"name"` 26 OAuthID string `json:"oauthID,omitempty"` 27 Status Status `json:"status"` 28 } 29 30 // Valid validates user 31 func (u *User) Valid() error { 32 return u.Status.Valid() 33 } 34 35 // Ops for user errors and op log. 36 const ( 37 OpFindUserByID = "FindUserByID" 38 OpFindUser = "FindUser" 39 OpFindUsers = "FindUsers" 40 OpCreateUser = "CreateUser" 41 OpUpdateUser = "UpdateUser" 42 OpDeleteUser = "DeleteUser" 43 ) 44 45 // UserService represents a service for managing user data. 46 type UserService interface { 47 48 // Returns a single user by ID. 49 FindUserByID(ctx context.Context, id platform.ID) (*User, error) 50 51 // Returns the first user that matches filter. 52 FindUser(ctx context.Context, filter UserFilter) (*User, error) 53 54 // Returns a list of users that match filter and the total count of matching users. 55 // Additional options provide pagination & sorting. 56 FindUsers(ctx context.Context, filter UserFilter, opt ...FindOptions) ([]*User, int, error) 57 58 // Creates a new user and sets u.ID with the new identifier. 59 CreateUser(ctx context.Context, u *User) error 60 61 // Updates a single user with changeset. 62 // Returns the new user state after update. 63 UpdateUser(ctx context.Context, id platform.ID, upd UserUpdate) (*User, error) 64 65 // Removes a user by ID. 66 DeleteUser(ctx context.Context, id platform.ID) error 67 68 // FindPermissionForUser 69 FindPermissionForUser(ctx context.Context, UserID platform.ID) (PermissionSet, error) 70 } 71 72 // UserUpdate represents updates to a user. 73 // Only fields which are set are updated. 74 type UserUpdate struct { 75 Name *string `json:"name"` 76 Status *Status `json:"status"` 77 } 78 79 // Valid validates UserUpdate 80 func (uu UserUpdate) Valid() error { 81 if uu.Status == nil { 82 return nil 83 } 84 85 return uu.Status.Valid() 86 } 87 88 // UserFilter represents a set of filter that restrict the returned results. 89 type UserFilter struct { 90 ID *platform.ID 91 Name *string 92 } 93 94 // UserResponse is the response of user 95 type UserResponse struct { 96 Links map[string]string `json:"links"` 97 User 98 }