/
organizations.go
310 lines (278 loc) · 10.2 KB
/
organizations.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package endpoints
import (
"errors"
"fmt"
"time"
log "github.com/cihub/seelog"
"github.com/jinzhu/gorm"
"github.com/ok-borg/api/domain"
"github.com/satori/go.uuid"
)
func (e Endpoints) CreateOrganization(
db *gorm.DB,
userId string,
name string,
) (*domain.Organization, error) {
organizationDao := domain.NewOrganizationDao(db)
// first check if organization with same name exists
if _, err := organizationDao.GetByName(name); err == nil {
// no error, we successfully get an organization,
// return an error
return nil, fmt.Errorf("An organization with the name %s already exists", name)
}
// first create organization
newOrganization := domain.Organization{
Id: uuid.NewV4().String(),
Name: name,
// by default, the creator user is admin, in the future we may allow to change the
// admin user
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedBy: userId,
UpdatedBy: userId,
}
if err := organizationDao.Create(newOrganization); err != nil {
log.Errorf("[Endpoints.CreateOrganization] unable to create organization: %s", err.Error())
return nil, err
}
// then create association between organization and the creator user
userOrganizationDao := domain.NewUserOrganizationDao(db)
newUserOrganization := domain.UserOrganization{
Id: uuid.NewV4().String(),
UserId: userId,
OrganizationId: newOrganization.Id,
IsAdmin: 1,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedBy: userId,
UpdatedBy: userId,
}
if err := userOrganizationDao.Create(newUserOrganization); err != nil {
log.Errorf("[Endpoints.CreateOrganization] unable to associate user to newly create organization: %s", err.Error())
return nil, err
}
return &newOrganization, nil
}
func (e Endpoints) CreateOrganizationJoinLink(
db *gorm.DB,
userId string,
organizationId string,
ttl int64,
) (*domain.OrganizationJoinLink, error) {
// Get UserOrganization to check if the user is admin or not
userOrganizationDao := domain.NewUserOrganizationDao(db)
userOrganization, err := userOrganizationDao.GetByUserAndOrganization(userId, organizationId)
if err != nil {
return nil, fmt.Errorf(
"user (id=%s) is not member of the organization (id=%s)",
userId, organizationId)
}
if userOrganization.IsAdmin != 1 {
return nil, fmt.Errorf(
"user (id=%s) is not administrator of organization (id=%s)",
userId, organizationId)
}
// ok so here the organization exists, the user is the admin
// lets check if a join-link already exist, if yes remove it then create a new one hehe.
organizationJoinLinkDao := domain.NewOrganizationJoinLinkDao(db)
if ojl, err := organizationJoinLinkDao.GetByOrganizationId(organizationId); err == nil {
// no error this join link exist for this organization
// lets remove it
organizationJoinLinkDao.Delete(ojl.Id)
}
// now create a new one
ojl := domain.OrganizationJoinLink{
Id: uuid.NewV4().String(),
OrganizationId: organizationId,
Ttl: ttl,
CreatedAt: time.Now(),
CreatedBy: userId,
}
if err := organizationJoinLinkDao.Create(ojl); err != nil {
log.Errorf("[Endpoints.CreateOrganizationjoinlink] unable to create organiation join link for organization: %s, %s", organizationId, err.Error())
return nil, err
}
return &ojl, nil
}
func (e Endpoints) DeleteOrganizationJoinLink(
db *gorm.DB,
userId string,
organizationJoinLinkId string,
) error {
// get the organizastionJoinLink
organizationJoinLinkDao := domain.NewOrganizationJoinLinkDao(db)
ojl, err := organizationJoinLinkDao.GetById(organizationJoinLinkId)
if err != nil {
return fmt.Errorf("cannot fin organization join link (id=%s)",
organizationJoinLinkId)
}
// then get UserOrganization to check if the user is admin or not
userOrganizationDao := domain.NewUserOrganizationDao(db)
userOrganization, err := userOrganizationDao.GetByUserAndOrganization(userId, ojl.OrganizationId)
if err != nil {
return fmt.Errorf(
"user (id=%s) is not member of the organization (id=%s)",
userId, ojl.OrganizationId)
}
if userOrganization.IsAdmin != 1 {
return fmt.Errorf(
"user (id=%s) is not administrator of organization (id=%s)",
userId, ojl.OrganizationId)
}
// ok so here the organization exists, the user is the admin
// DELETE ALL THE SHIT
return organizationJoinLinkDao.Delete(organizationJoinLinkId)
}
func (e Endpoints) GetOrganizationJoinLink(
db *gorm.DB,
organizationJoinLinkId string,
) (domain.OrganizationJoinLink, error) {
organizationJoinLinkDao := domain.NewOrganizationJoinLinkDao(db)
return organizationJoinLinkDao.GetById(organizationJoinLinkId)
}
func (e Endpoints) GetOrganizationJoinLinkForOrganization(
db *gorm.DB,
userId string,
organizationId string,
) (*domain.OrganizationJoinLink, error) {
organizationJoinLinkDao := domain.NewOrganizationJoinLinkDao(db)
organizationJoinLink, err := organizationJoinLinkDao.GetByOrganizationId(organizationId)
if err != nil {
log.Errorf("[Endpoints.GetOrganizationjoinlinkbyorganizationid] cannot get join link from organization: %s", err)
return nil, err
}
// then get UserOrganization to check if the user is admin or not
userOrganizationDao := domain.NewUserOrganizationDao(db)
userOrganization, err := userOrganizationDao.GetByUserAndOrganization(userId, organizationJoinLink.OrganizationId)
if err != nil {
return nil, fmt.Errorf(
"user (id=%s) is not member of the organization (id=%s)",
userId, organizationJoinLink.OrganizationId)
}
if userOrganization.IsAdmin != 1 {
return nil, fmt.Errorf(
"user (id=%s) is not administrator of organization (id=%s)",
userId, organizationJoinLink.OrganizationId)
}
return &organizationJoinLink, err
}
func (e Endpoints) ListUserOrganizations(db *gorm.DB, userId string) ([]domain.Organization, error) {
organizationIds, err := domain.NewUserOrganizationDao(db).ListOrganizationsForUser(userId)
if err != nil {
log.Errorf("[Endpoint.ListUserOrganizations]cannot list organizations for user %s", userId)
return nil, errors.New("cannot read organizations")
}
return domain.NewOrganizationDao(db).GetByIds(organizationIds)
}
func (e Endpoints) JoinOrganization(
db *gorm.DB,
userId string,
organizationJoinLinkId string,
) error {
// get the organizationJoinLink
organizationJoinLinkDao := domain.NewOrganizationJoinLinkDao(db)
ojl, err := organizationJoinLinkDao.GetById(organizationJoinLinkId)
if err != nil {
return fmt.Errorf("cannot fin organization join link (id=%s)",
organizationJoinLinkId)
}
// if not expired continue
if ojl.IsExpired() {
return errors.New("join link expired")
}
userOrganizationDao := domain.NewUserOrganizationDao(db)
// if already member returnn error
if _, err := userOrganizationDao.GetByUserAndOrganization(userId, ojl.OrganizationId); err == nil {
return errors.New("you already joined this organization")
}
userOrganization := domain.UserOrganization{
Id: uuid.NewV4().String(),
UserId: userId,
OrganizationId: ojl.OrganizationId,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedBy: userId,
UpdatedBy: userId,
}
return userOrganizationDao.Create(userOrganization)
}
func (e Endpoints) LeaveOrganization(
db *gorm.DB,
userId string,
organizationId string,
) error {
userOrganizationDao := domain.NewUserOrganizationDao(db)
if userOrganization, err := userOrganizationDao.GetByUserAndOrganization(userId, organizationId); err != nil {
// user is not part of this organization
return fmt.Errorf("[Endpoints.LeaveOrganization] User (id=%s), is not part of organization (id=%s)", userId, organizationId)
} else {
if userOrganization.IsAdmin == 1 {
admins, _ := userOrganizationDao.GetAdmins(organizationId)
// check if you are last admin, is so you can leave only if you
// are the last user of the org`
if len(admins) == 1 {
users, _ := userOrganizationDao.ListUsersInOrganization(organizationId)
if len(users) != 1 {
// you are not the last user, you cannot leave this shit
// lets transfer ownership before
return fmt.Errorf("[Endpoints.LeaveOrganization] User (id=%s), is the last admin for the organization, he can leave only if there is 1 member in the organization (id=%s)",
userId, organizationId)
}
}
}
return userOrganizationDao.Delete(userOrganization.Id)
}
}
func (e Endpoints) ExpelUserFromOrganization(
db *gorm.DB,
userId string,
userIdToExpel string,
organizationId string,
) error {
// first check if the user is admin
userOrganizationDao := domain.NewUserOrganizationDao(db)
adminOjl, err := userOrganizationDao.GetByUserAndOrganization(userId, organizationId)
if err != nil {
return fmt.Errorf(
"user (id=%s) is not member of the organization (id=%s)",
userId, organizationId)
}
if adminOjl.IsAdmin != 1 {
return fmt.Errorf(
"user (id=%s) is not administrator of organization (id=%s)",
userId, organizationId)
}
// then just remove the user organization assorciation
userOrganization, err := userOrganizationDao.GetByUserAndOrganization(userIdToExpel, organizationId)
if err != nil {
return fmt.Errorf("[Endpoints.ExpelUserFromOrganization] user (id=%s) is not part of organization (id=%s)", userIdToExpel, organizationId)
}
return userOrganizationDao.Delete(userOrganization.Id)
}
func (e Endpoints) GrantAdminRightToUser(
db *gorm.DB,
userId string,
userIdToAdmin string,
organizationId string,
) error {
// first check if the user is admin
userOrganizationDao := domain.NewUserOrganizationDao(db)
adminOjl, err := userOrganizationDao.GetByUserAndOrganization(userId, organizationId)
if err != nil {
return errors.New(fmt.Sprintf(
"user (id=%s) is not member of the organization (id=%s)",
userId, organizationId))
}
if adminOjl.IsAdmin != 1 {
return errors.New(fmt.Sprintf(
"user (id=%s) is not administrator of organization (id=%s)",
userId, organizationId))
}
// then just get the user organization assorciation and update it
userOrganization, err := userOrganizationDao.GetByUserAndOrganization(userIdToAdmin, organizationId)
if err != nil {
return fmt.Errorf("[Endpoints.ExpelUserFromOrganization] user (id=%s) is not part of organization (id=%s)", userIdToAdmin, organizationId)
}
userOrganization.IsAdmin = 1
return userOrganizationDao.Update(userOrganization)
}