-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
119 lines (104 loc) · 3.79 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
package main
import (
"context"
"errors"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type Student struct {
// MID primitive.ObjectID `bson:"_id" json:"-"`
ID int `bson:"userID" json:"matricola"` //id of the student (teachers will use the same, but it's 6 digit long instead of 5)
Name string `bson:"name" json:"nome"` //name of the student
LastName string `bson:"lastName" json:"cognome"` //last name of the student
Email string `bson:"email" json:"email"` //email of the student
Pfp string `bson:"pfp" json:"pfp"` //profile picture of the student (autogenerated)
IsMock bool `bson:"isMock" json:"isMock"` //if the user is a mock user (used for testing)
// Applications []string `bson:"applications" json:"applications"` //list of the applications of the student
}
// !currently unesed
type StudentInfo struct {
Class string `json:"classe"`
Year int `json:"anno"`
Field string `json:"indirizzo"`
IsClassPresident bool `json:"rappresentante_classe"`
IsIstiturePresident bool `json:"rappresentante_istituto"`
}
// get student struct from the id (matricola)
func GetStudentFromID(userID int, connection *mongo.Database) (Student, error) {
var student Student
err := connection.Collection("users").
FindOne(context.Background(), bson.M{"userID": userID}).
Decode(&student)
if err != nil {
return student, err
}
return student, nil
}
// check if the userUID is saved in the db
func IsUserRegistered(userID int, connection *mongo.Database) (bool, error) {
var user Student
err := connection.Collection("users").
FindOne(context.Background(), bson.M{"userID": userID}).
Decode(&user)
fmt.Println("user found: ", user)
fmt.Println("student id used:", userID)
if err != nil {
if err == mongo.ErrNoDocuments {
return false, nil
}
return false, err
}
return true, nil
}
// adds the student to the db (it's a pointer because here we generate the pfp)
func AddStudent(student *Student, connection *mongo.Database) error {
//check if it's already registered
exists, err := IsUserRegistered(student.ID, connection)
if err != nil {
return err
}
if exists {
return errors.New("user already registered")
}
student.Pfp = fmt.Sprintf("https://avatars.dicebear.com/api/bottts/%d.svg", student.ID)
// student.MID = primitive.NewObjectID()
_, err = connection.Collection("users").InsertOne(context.Background(), student)
return err
}
// given the paleoid access token and the database connection, it returns the response that
// will be parsed into a json in the response, if the error is client side (4xx or 5xx) and an error
func registerOrGenerateTokenFromPaleoIDAccessToken(paleoidAccess string, connection *mongo.Database) (map[string]interface{}, bool, error) {
//get the student from the paleoid access token
student, err := GetStudentFromPaleoIDAccessToken(paleoidAccess)
if err != nil {
return nil, true, errors.New("invalid paleoid access token")
}
//check if it's in the db
registered, err := IsUserRegistered(student.ID, connection)
log.Printf("the user with %d is registered? %t ", student.ID, registered)
if err != nil {
return nil, false, err
}
//register the user if not already saved
if !registered {
log.Println("registering the user")
err = AddStudent(&student, connection)
if err != nil {
return nil, false, err
}
}
//generate the ipaas tokens
access, refresh, err := GenerateTokenPair(student.ID, connection)
if err != nil {
return nil, false, fmt.Errorf("error generating token pair: %v", err)
}
//this map will be parsed into a json in the response
resp := map[string]interface{}{
"ipaas-access-token": access,
"ipaas-refresh-token": refresh,
"userID": student.ID,
}
return resp, false, nil
}