-
Notifications
You must be signed in to change notification settings - Fork 13
/
user.go
159 lines (127 loc) · 5.04 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
152
153
154
155
156
157
158
159
package service
import (
"io"
"github.com/ProxeusApp/proxeus-core/storage"
"github.com/ProxeusApp/proxeus-core/storage/database/db"
"github.com/ProxeusApp/proxeus-core/sys/model"
)
type (
// UserService is an interface that provides user functions
UserService interface {
// Put adds a user
Put(auth model.Auth, user *model.User) error
// GetUser returns the currently logged in user
GetUser(auth model.Auth) (*model.User, error)
// GetById returns the User with the provided id
GetById(auth model.Auth, id string) (*model.User, error)
// GetById returns the UserDataItem for the provided id
GetUserDataById(auth model.Auth, id string) (*model.UserDataItem, error)
// DeleteUser removes a user and all related data from the database
DeleteUser(auth model.Auth) error
// Deletes the UserData of the user with the provided id
DeleteUserData(auth model.Auth, id string) error
// GetByBCAddress returns the user associated with the provided blockchainAddress
GetByBCAddress(blockchainAddress string) (*model.User, error)
// GetByEmail returns the user associated with the provided email
GetByEmail(email string) (*model.User, error)
// PutPassword sets the password for a user
PutPassword(userId, password string) error
// GetProfilePhoto returns the profile photo for a user
GetProfilePhoto(auth model.Auth, id string, writer io.Writer) error
// PutProfilePhoto sets the profile photo for a user
PutProfilePhoto(auth model.Auth, userId string, reader io.ReadCloser) error
// List returns references to all the user object matching the supplied filter criteria
List(sess model.Auth, contains string, settings storage.Options) ([]*model.User, error)
}
defaultUserService struct {
}
)
func NewUserService() *defaultUserService {
return &defaultUserService{}
}
// GetUser returns the currently logged in user
func (me *defaultUserService) GetUser(auth model.Auth) (*model.User, error) {
return userDB().Get(auth, auth.UserID())
}
// GetById returns the User with the provided id
func (me *defaultUserService) GetById(auth model.Auth, id string) (*model.User, error) {
return userDB().Get(auth, id)
}
// GetById returns the UserDataItem for the provided id
func (me *defaultUserService) GetUserDataById(auth model.Auth, id string) (*model.UserDataItem, error) {
return userDataDB().Get(auth, id)
}
// DeleteUser removes a user and all related data from the database
func (me *defaultUserService) DeleteUser(auth model.Auth) error {
//remove documents / workflow instances of user
workflowInstances, err := userDataDB().List(auth, "", storage.Options{}, false)
if err != nil && !db.NotFound(err) {
return err
}
for _, workflowInstance := range workflowInstances {
//err = userDataDB().Delete(auth, c.System().DB.Files, workflowInstance.ID)
err = me.DeleteUserData(auth, workflowInstance.ID)
if err != nil {
return err
}
}
//set workflow templates to deactivated
workflows, err := workflowDB().List(auth, "", storage.Options{})
if err != nil && !db.NotFound(err) {
return err
}
for _, workflow := range workflows {
if workflow.OwnedBy(auth) {
workflow.Deactivated = true
err = workflowDB().Put(auth, workflow)
if err != nil {
return err
}
}
}
// unset user data and set inactive
user, err := userDB().Get(auth, auth.UserID())
if err != nil {
return err
}
user.Active = false
user.EthereumAddr = "0x"
user.Email = ""
user.Name = ""
user.Photo = ""
user.PhotoPath = ""
user.WantToBeFound = false
return userDB().Put(auth, user)
}
// Deletes the UserData of the user with the provided id
func (me *defaultUserService) DeleteUserData(auth model.Auth, id string) error {
return userDataDB().Delete(auth, filesDB(), id)
}
// GetByBCAddress returns the user associated with the provided blockchainAddress
func (me *defaultUserService) GetByBCAddress(blockchainAddress string) (*model.User, error) {
return userDB().GetByBCAddress(blockchainAddress)
}
// GetByEmail returns the user associated with the provided email
func (me *defaultUserService) GetByEmail(email string) (*model.User, error) {
return userDB().GetByEmail(email)
}
// Put adds a user
func (me *defaultUserService) Put(auth model.Auth, user *model.User) error {
return userDB().Put(auth, user)
}
// List returns references to all the user object matching the supplied filter criteria
func (me *defaultUserService) List(sess model.Auth, contains string, settings storage.Options) ([]*model.User, error) {
return userDB().List(sess, contains, settings)
}
// PutPassword sets the password for a user
func (me *defaultUserService) PutPassword(userId, password string) error {
return userDB().PutPw(userId, password)
}
// GetProfilePhoto returns the profile photo for a user
func (me *defaultUserService) GetProfilePhoto(auth model.Auth, userId string, writer io.Writer) error {
return userDB().GetProfilePhoto(auth, userId, writer)
}
// PutProfilePhoto sets the profile photo for a user
func (me *defaultUserService) PutProfilePhoto(auth model.Auth, userId string, reader io.ReadCloser) error {
return userDB().PutProfilePhoto(auth, userId, reader)
}