-
Notifications
You must be signed in to change notification settings - Fork 12
/
token_team.go
128 lines (109 loc) · 4.01 KB
/
token_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
package makeless_go
import (
"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"
h "net/http"
"strconv"
"sync"
)
func (makeless *Makeless) tokensTeam(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().GET(
"/api/auth/team/token",
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 tokens []*makeless_go_model.Token
var team = &makeless_go_model.Team{
Model: makeless_go_model.Model{Id: uint(teamId)},
UserId: &userId,
RWMutex: new(sync.RWMutex),
}
if tokens, err = http.GetDatabase().GetTokensTeam(http.GetDatabase().GetConnection().WithContext(c), team, tokens); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, tokens))
},
)
return nil
}
func (makeless *Makeless) createTokenTeam(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().POST(
"/api/auth/team/token",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
http.TeamRoleMiddleware(makeless_go_security.RoleTeamOwner),
func(c *gin.Context) {
var err error
var teamUser bool
var tmpTeamId, _ = strconv.Atoi(c.GetHeader("Team"))
var teamId = uint(tmpTeamId)
var tokenTeamCreate = &_struct.TokenTeamCreate{
RWMutex: new(sync.RWMutex),
}
if err := c.ShouldBind(tokenTeamCreate); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
if teamUser, err = http.GetSecurity().IsTeamUser(http.GetSecurity().GetDatabase().GetConnection(), teamId, *tokenTeamCreate.GetUserId()); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if !teamUser {
c.AbortWithStatusJSON(h.StatusForbidden, http.Response(makeless_go_security.NoTeamUserErr, nil))
return
}
var token = &makeless_go_model.Token{
Note: tokenTeamCreate.GetNote(),
Token: tokenTeamCreate.GetToken(),
UserId: tokenTeamCreate.GetUserId(),
TeamId: &teamId,
RWMutex: new(sync.RWMutex),
}
if token, err = http.GetDatabase().CreateTokenTeam(http.GetDatabase().GetConnection().WithContext(c), token); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, token))
},
)
return nil
}
func (makeless *Makeless) deleteTokenTeam(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().DELETE(
"/api/auth/team/token",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
http.TeamRoleMiddleware(makeless_go_security.RoleTeamOwner),
func(c *gin.Context) {
var err error
var tmpTeamId, _ = strconv.Atoi(c.GetHeader("Team"))
var teamId = uint(tmpTeamId)
var tokenTeamDelete = &_struct.TokenTeamDelete{
RWMutex: new(sync.RWMutex),
}
if err := c.ShouldBind(tokenTeamDelete); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
var token = &makeless_go_model.Token{
Model: makeless_go_model.Model{Id: *tokenTeamDelete.GetId()},
TeamId: &teamId,
RWMutex: new(sync.RWMutex),
}
if err = http.GetDatabase().DeleteTokenTeam(http.GetDatabase().GetConnection().WithContext(c), token); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, nil))
},
)
return nil
}