-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusers.go
195 lines (168 loc) · 5.55 KB
/
users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Sophrosyne
// Copyright (C) 2024 Mads R. Havmand
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sophrosyne
import (
"context"
"fmt"
"time"
)
type User struct {
ID string
Name string
Email string
Token []byte
IsAdmin bool
DefaultProfile Profile
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
func (u User) EntityType() string {
return "User"
}
func (u User) EntityID() string {
return u.ID
}
type UserService interface {
GetUser(ctx context.Context, id string) (User, error)
GetUserByEmail(ctx context.Context, email string) (User, error)
GetUserByName(ctx context.Context, name string) (User, error)
GetUserByToken(ctx context.Context, token []byte) (User, error)
// Returns a list of users less than, or equal to, the configured page size.
// Configuration of the page size is an implementation detail, but should be
// derived from [Config.Services.Users.PageSize].
//
// The cursor is used to paginate the results. [DatabaseCursor.Position] is
// treated as the last read ID. If the cursor is nil, the first page of
// results should be returned.
//
// When the users have been read, but before returning the list of users,
// the cursor must be advanced to the ID of the last user returned. If no
// users are returned, or if it is known that a subsequent call would return
// zero users, the cursors Reset method must be called.
//
// The returned list of users should be ordered by ID in ascending order.
GetUsers(ctx context.Context, cursor *DatabaseCursor) ([]User, error)
CreateUser(ctx context.Context, user CreateUserRequest) (User, error)
UpdateUser(ctx context.Context, user UpdateUserRequest) (User, error)
DeleteUser(ctx context.Context, name string) error
RotateToken(ctx context.Context, name string) ([]byte, error)
}
type GetUserRequest struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
func (p GetUserRequest) Validate(interface{}) error {
if p.ID == "" && p.Name == "" && p.Email == "" {
return fmt.Errorf("one of ID, Name or Email must be provided")
}
if p.ID != "" && (p.Name != "" || p.Email != "") {
return fmt.Errorf("only one of ID, Name or Email must be provided")
}
return nil
}
type GetUserResponse struct {
Name string `json:"name"`
Email string `json:"email"`
IsAdmin bool `json:"is_admin"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt string `json:"deleted_at,omitempty"`
}
func (r *GetUserResponse) FromUser(u User) *GetUserResponse {
r.Name = u.Name
r.Email = u.Email
r.IsAdmin = u.IsAdmin
r.CreatedAt = u.CreatedAt.Format(TimeFormatInResponse)
r.UpdatedAt = u.UpdatedAt.Format(TimeFormatInResponse)
if u.DeletedAt != nil {
r.DeletedAt = u.DeletedAt.Format(TimeFormatInResponse)
}
return r
}
type GetUsersRequest struct {
Cursor string `json:"cursor"`
}
type GetUsersResponse struct {
Users []GetUserResponse `json:"users"`
Cursor string `json:"cursor"`
Total int `json:"total"`
}
type CreateUserRequest struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required"`
IsAdmin bool `json:"is_admin"`
}
type CreateUserResponse struct {
Name string `json:"name"`
Email string `json:"email"`
Token []byte `json:"token"`
IsAdmin bool `json:"is_admin"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt string `json:"deleted_at,omitempty"`
}
func (r *CreateUserResponse) FromUser(u User) *CreateUserResponse {
r.Name = u.Name
r.Email = u.Email
r.Token = u.Token
r.IsAdmin = u.IsAdmin
r.CreatedAt = u.CreatedAt.Format(TimeFormatInResponse)
r.UpdatedAt = u.UpdatedAt.Format(TimeFormatInResponse)
if u.DeletedAt != nil {
r.DeletedAt = u.DeletedAt.Format(TimeFormatInResponse)
}
return r
}
type UpdateUserRequest struct {
Name string `json:"name" validate:"required"`
Email string `json:"email"`
IsAdmin bool `json:"is_admin"`
}
type UpdateUserResponse struct {
Name string `json:"name"`
Email string `json:"email"`
IsAdmin bool `json:"is_admin"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt string `json:"deleted_at,omitempty"`
}
func (r *UpdateUserResponse) FromUser(u User) *UpdateUserResponse {
r.Name = u.Name
r.Email = u.Email
r.IsAdmin = u.IsAdmin
r.CreatedAt = u.CreatedAt.Format(TimeFormatInResponse)
r.UpdatedAt = u.UpdatedAt.Format(TimeFormatInResponse)
if u.DeletedAt != nil {
r.DeletedAt = u.DeletedAt.Format(TimeFormatInResponse)
}
return r
}
type DeleteUserRequest struct {
Name string `json:"name" validate:"required"`
}
type RotateTokenRequest struct {
Name string `json:"name" validate:"required"`
}
type RotateTokenResponse struct {
Token []byte `json:"token"`
}
func (r *RotateTokenResponse) FromUser(u User) *RotateTokenResponse {
r.Token = u.Token
return r
}
type UserContextKey struct{}