-
Notifications
You must be signed in to change notification settings - Fork 654
/
user.go
92 lines (78 loc) · 2.61 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
package entities
import (
"net/mail"
)
// Role states the role of the user in the portal
type Role string
const (
// RoleAdmin gives the admin permissions to a user
RoleAdmin Role = "admin"
//RoleUser gives the normal user permissions to a user
RoleUser Role = "user"
)
// User contains the user information
type User struct {
Audit `bson:",inline"`
ID string `bson:"_id,omitempty" json:"userID"`
Username string `bson:"username,omitempty" json:"username"`
Password string `bson:"password,omitempty" json:"password,omitempty"`
Email string `bson:"email,omitempty" json:"email,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Role Role `bson:"role,omitempty" json:"role"`
DeactivatedAt *int64 `bson:"deactivated_at,omitempty" json:"deactivatedAt,omitempty"`
}
// UserDetails is used to update user's personal details
type UserDetails struct {
ID string `bson:"id,omitempty"`
Email string `bson:"email,omitempty" json:"email,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Password string `bson:"password,omitempty" json:"password,omitempty"`
}
// UserPassword defines structure for password related requests
type UserPassword struct {
Username string `json:"username,omitempty"`
OldPassword string `json:"oldPassword,omitempty"`
NewPassword string `json:"newPassword,omitempty"`
}
// UpdateUserState defines structure to deactivate or reactivate user
type UpdateUserState struct {
Username string `json:"username"`
IsDeactivate *bool `json:"isDeactivate"`
}
// APIStatus defines structure for APIroute status
type APIStatus struct {
Status string `json:"status"`
}
type UserWithProject struct {
Audit `bson:",inline"`
ID string `bson:"_id" json:"id"`
Username string `bson:"username" json:"username"`
Email string `bson:"email" json:"email"`
Name string `bson:"name" json:"name"`
Projects []*Project `bson:"projects" json:"projects"`
}
func (user User) GetUserWithProject() *UserWithProject {
return &UserWithProject{
ID: user.ID,
Username: user.Username,
Name: user.Name,
Audit: Audit{
IsRemoved: user.IsRemoved,
CreatedAt: user.CreatedAt,
CreatedBy: user.UpdatedBy,
UpdatedAt: user.UpdatedAt,
UpdatedBy: user.UpdatedBy,
},
Email: user.Email,
}
}
// SanitizedUser returns the user object without sensitive information
func (user *User) SanitizedUser() *User {
user.Password = ""
return user
}
// IsEmailValid validates the email
func (user *User) IsEmailValid(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}