-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
133 lines (120 loc) · 3.19 KB
/
profile.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
package controller
import (
"encoding/json"
"fmt"
"io"
"net/http"
web "github.com/JIexa24/chef-webapi"
"github.com/JIexa24/chef-webapi/database/interfaces"
"github.com/JIexa24/chef-webapi/encryption"
)
// ProfileUserInfo containы profile info.
type ProfileUserInfo struct {
ID string `json:"id"`
FullName string `json:"fullName"`
Avatar string `json:"avatar"`
Username string `json:"username,omitempty"`
UserID string `json:"userID,omitempty"`
}
// ProfileUserRequest contains request of profile.
type ProfileUserRequest struct {
ProfileUserInfo
Password string `json:"password"`
}
// ExtractProfileUserInfo extract all needed fields from user's entry.
func ExtractProfileUserInfo(
user *interfaces.UserEntry) *ProfileUserInfo {
if user == nil {
return nil
}
return &ProfileUserInfo{
ID: user.ID,
UserID: "",
Username: "",
FullName: user.FullName,
Avatar: user.Avatar,
}
}
// ImplementToProfileUserInfo implement all needed fields into user's entry.
func ImplementToProfileUserInfo(profile *ProfileUserRequest,
user *interfaces.UserEntry) {
if user == nil || profile == nil {
return
}
user.FullName = profile.FullName
user.Avatar = profile.Avatar
if profile.Password != "" {
passwd, err := encryption.RSADecrypt(profile.Password, *web.App.AppKey)
if err == nil {
var pwd []byte
var err error
if pwd, err = encryption.GetPasswordHASH([]byte(passwd)); err == nil {
user.Password = string(pwd)
} else {
web.App.Logger.Errorf(
"ImplementToProfileUserInfo [encryption.GetPasswordHASH]: %s",
err.Error())
}
if user.NeedPasswordChange {
user.NeedPasswordChange = false
}
} else {
web.App.Logger.Errorf(
"ImplementToProfileUserInfo [encryption.RSADecrypt]: %s", err.Error())
}
}
}
// GetUserProfiles return user profile information.
// Information get only by id in url query.
func GetUserProfiles(params *APIParameters) ([]ProfileUserInfo, error) {
profiles := make([]ProfileUserInfo, 0)
for _, id := range params.ID {
user, err := web.App.DB.GetUserByID(id)
if err != nil {
continue
}
if user != nil {
profile := ExtractProfileUserInfo(user)
profiles = append(profiles, *profile)
}
}
return profiles, nil
}
// GetUserProfileByID returns information about the user's profile.
// Information is requested by id.
func GetUserProfileByID(r *http.Request) (*ProfileUserInfo, error) {
user, err := GetUserByID(r)
if err != nil {
return nil, err
}
if user == nil {
return nil, fmt.Errorf("user not found")
}
profile := ExtractProfileUserInfo(user)
return profile, nil
}
// UpdateCurrentUserProfile updates user information.
// Information update by id.
func UpdateCurrentUserProfile(r *http.Request) error {
user, err := GetUserBySession(r)
if err != nil {
web.App.Logger.Errorf(
"profileGetCurrentUserAPIHandler [controller.GetUserBySession]: %s",
err.Error())
return err
}
if user == nil {
return fmt.Errorf("user not found")
}
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
profile := &ProfileUserRequest{}
err = json.Unmarshal(body, profile)
if err != nil {
return err
}
ImplementToProfileUserInfo(profile, user)
return web.App.DB.UpdateUserByID(user.ID, user)
}