-
Notifications
You must be signed in to change notification settings - Fork 12
/
team_user_team.go
161 lines (137 loc) · 5.38 KB
/
team_user_team.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
package makeless_go
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/makeless/makeless-go/http"
"github.com/makeless/makeless-go/model"
"github.com/makeless/makeless-go/security"
"github.com/makeless/makeless-go/struct"
"gorm.io/gorm"
h "net/http"
"strconv"
"sync"
)
func (makeless *Makeless) teamUsersTeam(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().GET(
"/api/auth/team/team-user",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
http.TeamRoleMiddleware(makeless_go_security.RoleTeamOwner),
func(c *gin.Context) {
var err error
var teamId, _ = strconv.Atoi(c.GetHeader("Team"))
var teamUsers []*makeless_go_model.TeamUser
var team = &makeless_go_model.Team{
Model: makeless_go_model.Model{Id: uint(teamId)},
RWMutex: new(sync.RWMutex),
}
if teamUsers, err = http.GetDatabase().GetTeamUsers(http.GetDatabase().GetConnection().WithContext(c), c.Query("search"), teamUsers, team); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, teamUsers))
},
)
return nil
}
func (makeless *Makeless) updateRoleTeamUserTeam(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().PATCH(
"/api/auth/team/team-user/role",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
http.TeamRoleMiddleware(makeless_go_security.RoleTeamOwner),
func(c *gin.Context) {
var err error
var userId = http.GetAuthenticator().GetAuthUserId(c)
var teamId, _ = strconv.Atoi(c.GetHeader("Team"))
var teamUserTeamUpdateRole = &_struct.TeamUserTeamUpdateRole{
RWMutex: new(sync.RWMutex),
}
if err = c.ShouldBind(teamUserTeamUpdateRole); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
var teamUser = &makeless_go_model.TeamUser{
Model: makeless_go_model.Model{Id: *teamUserTeamUpdateRole.GetId()},
RWMutex: new(sync.RWMutex),
}
if teamUser, err = http.GetDatabase().GetTeamUserByFields(http.GetDatabase().GetConnection().WithContext(c), teamUser, map[string]interface{}{
"id": teamUser.GetId(),
"team_id": teamId,
}); err != nil {
switch errors.Is(err, gorm.ErrRecordNotFound) {
case true:
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
default:
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
}
return
}
if teamUser.GetTeam().RWMutex == nil {
teamUser.GetTeam().RWMutex = new(sync.RWMutex)
}
if teamUser.GetTeam().GetUserId() == teamUser.GetUserId() || *teamUser.GetUserId() == userId {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(makeless_go_security.UserNotDeletable, nil))
return
}
if exists := makeless.GetConfig().GetConfiguration().GetTeams().HasRole(*teamUserTeamUpdateRole.GetRole()); !exists {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
}
if teamUser, err = http.GetDatabase().UpdateRoleTeamUser(http.GetDatabase().GetConnection().WithContext(c), teamUser, *teamUserTeamUpdateRole.GetRole()); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, teamUser))
},
)
return nil
}
func (makeless *Makeless) deleteTeamUserTeam(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().DELETE(
"/api/auth/team/team-user",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
http.TeamRoleMiddleware(makeless_go_security.RoleTeamOwner),
func(c *gin.Context) {
var err error
var userId = http.GetAuthenticator().GetAuthUserId(c)
var teamId, _ = strconv.Atoi(c.GetHeader("Team"))
var teamUserTeamDelete = &_struct.TeamUserTeamDelete{
RWMutex: new(sync.RWMutex),
}
if err = c.ShouldBind(teamUserTeamDelete); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
var teamUser = &makeless_go_model.TeamUser{
Model: makeless_go_model.Model{Id: *teamUserTeamDelete.GetId()},
RWMutex: new(sync.RWMutex),
}
if teamUser, err = http.GetDatabase().GetTeamUserByFields(http.GetDatabase().GetConnection().WithContext(c), teamUser, map[string]interface{}{
"id": teamUser.GetId(),
"team_id": teamId,
}); err != nil {
switch errors.Is(err, gorm.ErrRecordNotFound) {
case true:
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
default:
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
}
return
}
if teamUser.GetTeam().RWMutex == nil {
teamUser.GetTeam().RWMutex = new(sync.RWMutex)
}
if teamUser.GetTeam().GetUserId() == teamUser.GetUserId() || *teamUser.GetUserId() == userId {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
if err = http.GetDatabase().DeleteTeamUser(http.GetDatabase().GetConnection().WithContext(c), teamUser); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, nil))
},
)
return nil
}