-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
151 lines (130 loc) · 3.09 KB
/
user.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
package schema
import (
"database/sql/driver"
"strings"
"time"
)
//The different level of Roles
const (
StudentLevel int = iota
TutorLevel
MajorLevel
HeadLevel
AdminLevel
RootLevel
STUDENT Role = "student"
TUTOR Role = "tutor"
ROOT Role = "root"
//DateLayout indicates the date format
DateLayout = "02/01/2006 15:04"
)
//Role aims at giving the possible level of Role for a user.
type Role string
//Level returns the authentication level associated to a given role
func (p Role) Level() int {
if p == "student" {
return StudentLevel
} else if p == "tutor" {
return TutorLevel
} else if strings.Index(string(p), "major") == 0 {
return MajorLevel
} else if p == "head" {
return HeadLevel
} else if p == "admin" {
return AdminLevel
} else if p == "root" {
return RootLevel
}
return -1
}
//SubRole extract the second level role if exists.
func (p Role) SubRole() string {
from := strings.LastIndex(string(p), "-") + 1
return string(p)[from:len(p)]
}
//Value returns String()
func (p Role) Value() (driver.Value, error) {
return p.String(), nil
}
//String() versions of the role
func (p Role) String() string {
return string(p)
}
//Person just gathers contact information for someone.
type Person struct {
Firstname string
Lastname string
Email string
Tel string
}
//Session denotes a user session
type Session struct {
Email string
Token []byte
Expire time.Time
}
//User is a person with an account
type User struct {
Person Person
Role Role
LastVisit *time.Time `,json:"omitempty"`
}
//Alumni denotes the basic information for a student future
type Alumni struct {
Contact string
Position string
France bool
Permanent bool
SameCompany bool
}
//Student denotes a student that as a promotion, a major.
type Student struct {
User User
Promotion string
Major string
Alumni *Alumni `,json:"omitempty"`
//Skip indicates we don't care about this student. Might left the school for example
Skip bool
Male bool
}
//Fullname provides the user fullname, starting with its firstname
func (p Person) Fullname() string {
return strings.Title(p.Firstname) + " " + strings.Title(p.Lastname)
}
//String returns the person email
func (p Person) String() string {
return p.Email
}
//String delegates to Person.String()
func (u User) String() string {
return u.Person.String()
}
//Fullname delegates to Person.String()
func (u User) Fullname() string {
return u.Person.Fullname()
}
//Anonymise the person by removing any personal information
func (p *Person) Anonymise() {
p.Firstname = ""
p.Lastname = ""
p.Email = ""
p.Tel = ""
}
//Students aliases slices of student to exhibit filtering methods
type Students []Student
//Filter returns the students that pass the given filter
func (ss Students) Filter(filter func(Student) bool) Students {
res := make(Students, 0)
for _, s := range ss {
if filter(s) {
res = append(res, s)
}
}
return res
}
//StudentInMajor is a filter that keep only the students in the given major
func StudentInMajor(major string) func(Student) bool {
return func(s Student) bool {
return s.Major == major
}
}