/
service.go
242 lines (203 loc) · 5.48 KB
/
service.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"context"
"database/sql"
_ "database/sql"
"encoding/hex"
"errors"
"fmt"
"io"
sharedPb "github.com/open-exam/open-exam-backend/grpc-shared"
pb "github.com/open-exam/open-exam-backend/user-db-service/grpc-user-db-service"
"github.com/open-exam/open-exam-backend/util"
)
type MySQLError struct {
Number uint16
Message string
}
func (me *MySQLError) Error() string {
return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
}
type Server struct {
pb.UnimplementedUserServiceServer
}
func NewServer() (*Server, error) {
return &Server{}, nil
}
func (s *Server) FindOne(ctx context.Context, req *pb.FindOneRequest) (*pb.User, error) {
if len(req.Id) != 0 {
user, err := getUser(0, req.Id, req.Password)
if err != nil {
return nil, err
}
return user, nil
} else if len(req.Email) != 0 {
user, err := getUser(1, req.Email, req.Password)
if err != nil {
return nil, err
}
return user, nil
}
return nil, errors.New("id or email not given")
}
func (s *Server) CreateUser(stream pb.UserService_CreateUserServer) error {
handleStreamSend := func(user *pb.User) {
err := stream.Send(user)
if err != nil {
fmt.Println("could not send response")
}
}
for {
req, err := stream.Recv()
if err == io.EOF {
return nil
}
if len(req.Email) == 0 {
handleStreamSend(&pb.User{
Error: "email is required",
})
continue
}
password := generatePassword(standardPasswordSize)
passHash, err := generateFromPassword(password)
if err != nil {
handleStreamSend(&pb.User{
Error: "unknown error while generating password",
})
continue
}
Id := hex.EncodeToString(util.GenerateRandomBytes(32))
_, err = db.Exec("INSERT INTO users VALUES (?, ?, ?, ?, ?)", Id, req.Email, req.Type, passHash, req.Name)
if err != nil {
if err.(*MySQLError).Number == 1062 {
rows := db.QueryRow("SELECT id FROM users WHERE email=?", req.Email)
err = rows.Scan(&Id)
if err != nil {
handleStreamSend(&pb.User{
Error: "an unknown error occurred",
})
continue
}
handleStreamSend(&pb.User{
Id: Id,
})
continue
}
handleStreamSend(&pb.User{
Error: "an unknown error occurred",
})
continue
}
handleStreamSend(&pb.User{
Id: Id,
})
}
}
func (s *Server) AddUserToScope(stream pb.UserService_AddUserToScopeServer) error {
handleStreamSend := func(user *sharedPb.StandardStatusResponse) {
err := stream.Send(user)
if err != nil {
fmt.Println("could not send response")
}
}
for {
req, err := stream.Recv()
if err == io.EOF {
return nil
}
if len(req.Id) == 0 {
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: false,
Message: "id is required",
})
continue
}
if req.Scope == 0 || req.ScopeType == 0 {
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: false,
Message: "scope and scopeType are required",
})
continue
}
row := db.QueryRow("SELECT type FROM users WHERE id=?", req.Id)
var userType uint32
err = row.Scan(&userType)
if err != nil {
if err == sql.ErrNoRows {
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: false,
Message: "user does not exist",
})
continue
}
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: false,
Message: err.Error(),
})
continue
}
if userType == 1 && req.ScopeType != 4 {
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: false,
Message: "student can only be assigned to teams",
})
continue
}
if userType == 1 {
_, err = db.Exec("INSERT INTO students(id, team_id) VALUES (?,?)", req.Id, req.Scope)
} else {
_, err = db.Exec("INSERT INTO standard_users VALUES (?,?,?)", req.Id, req.Scope, req.ScopeType)
}
if err != nil {
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: false,
Message: "an unknown error occurred",
})
continue
}
handleStreamSend(&sharedPb.StandardStatusResponse{
Status: true,
})
}
}
func (s *Server) GetUser(ctx context.Context, req *sharedPb.StandardIdRequest) (*pb.DetailedUser, error) {
if len(req.IdString) == 0 {
return nil, errors.New("id is required")
}
student := &pb.DetailedUser{}
userRow := db.QueryRow("SELECT id, name, email FROM users WHERE id=? AND type=1", req.IdString)
err := userRow.Scan(&student.Id, &student.Name, &student.Email)
if err != nil {
return nil, errors.New("student does not exist")
}
res, err := db.Query("SELECT teams.id, teams.name, teams.display_name, `groups`.id, `groups`.name, organizations.id, organizations.name FROM students RIGHT JOIN teams ON students.team_id = teams.id RIGHT JOIN `groups` ON teams.group_id = `groups`.id RIGHT JOIN organizations on `groups`.org_id = organizations.id WHERE students.id=?", req.IdString)
if err != nil {
return nil, err
}
orgs := make([]*pb.Organization, 0)
grps := make([]*pb.Group, 0)
orgList := make(map[uint64]bool, 0)
groupList := make(map[uint64]int, 0)
for res.Next() {
team := &pb.Team{}
group := &pb.Group{}
org := &pb.Organization{}
if err := res.Scan(&team.Id, &team.Name, &team.DisplayName, &group.Id, &group.Name, &org.Id, &org.Name); err != nil {
return nil, err
}
_, ok := orgList[org.Id]
if !ok {
orgs = append(orgs, org)
orgList[org.Id] = true
}
_, ok = groupList[group.Id]
if !ok {
grps = append(grps, group)
groupList[group.Id] = len(grps) - 1
}
idx, _ := groupList[group.Id]
grps[idx].Teams = append(grps[idx].Teams, team)
}
student.Organizations = orgs
return student, nil
}