forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
242 lines (207 loc) · 4.86 KB
/
repo.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
package session
import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/remote"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
)
func Repo(c *gin.Context) *model.Repo {
v, ok := c.Get("repo")
if !ok {
return nil
}
r, ok := v.(*model.Repo)
if !ok {
return nil
}
return r
}
func Repos(c *gin.Context) []*model.RepoLite {
v, ok := c.Get("repos")
if !ok {
return nil
}
r, ok := v.([]*model.RepoLite)
if !ok {
return nil
}
return r
}
func SetRepo() gin.HandlerFunc {
return func(c *gin.Context) {
var (
owner = c.Param("owner")
name = c.Param("name")
)
user := User(c)
repo, err := store.GetRepoOwnerName(c, owner, name)
if err == nil {
c.Set("repo", repo)
c.Next()
return
}
// if the user is not nil, check the remote system
// to see if the repository actually exists. If yes,
// we can prompt the user to add.
if user != nil {
remote := remote.FromContext(c)
repo, err = remote.Repo(user, owner, name)
if err != nil {
log.Errorf("Cannot find remote repository %s/%s for user %s. %s",
owner, name, user.Login, err)
} else {
log.Debugf("Found remote repository %s/%s for user %s",
owner, name, user.Login)
}
}
data := gin.H{
"User": user,
"Repo": repo,
}
// if we found a repository, we should display a page
// to the user allowing them to activate.
if repo != nil && len(repo.FullName) != 0 {
// we should probably move this code to a
// separate route, but for now we need to
// add a CSRF token.
data["Csrf"], _ = token.New(
token.CsrfToken,
user.Login,
).Sign(user.Hash)
c.HTML(http.StatusNotFound, "repo_activate.html", data)
} else {
c.HTML(http.StatusNotFound, "404.html", data)
}
c.Abort()
}
}
func Perm(c *gin.Context) *model.Perm {
v, ok := c.Get("perm")
if !ok {
return nil
}
u, ok := v.(*model.Perm)
if !ok {
return nil
}
return u
}
func SetPerm() gin.HandlerFunc {
return func(c *gin.Context) {
user := User(c)
repo := Repo(c)
perm := &model.Perm{}
if user != nil {
// attempt to get the permissions from a local cache
// just to avoid excess API calls to GitHub
val, ok := c.Get("perm")
if ok {
c.Next()
log.Debugf("%s using cached %+v permission to %s",
user.Login, val, repo.FullName)
return
}
}
switch {
// if the user is not authenticated, and the
// repository is private, the user has NO permission
// to view the repository.
case user == nil && repo.IsPrivate == true:
perm.Pull = false
perm.Push = false
perm.Admin = false
// if the user is not authenticated, but the repository
// is public, the user has pull-rights only.
case user == nil && repo.IsPrivate == false:
perm.Pull = true
perm.Push = false
perm.Admin = false
case user.Admin:
perm.Pull = true
perm.Push = true
perm.Admin = true
// otherwise if the user is authenticated we should
// check the remote system to get the users permissiosn.
default:
var err error
perm, err = remote.FromContext(c).Perm(user, repo.Owner, repo.Name)
if err != nil {
perm.Pull = false
perm.Push = false
perm.Admin = false
// debug
log.Errorf("Error fetching permission for %s %s",
user.Login, repo.FullName)
}
// if we couldn't fetch permissions, but the repository
// is public, we should grant the user pull access.
if err != nil && repo.IsPrivate == false {
perm.Pull = true
}
}
if user != nil {
log.Debugf("%s granted %+v permission to %s",
user.Login, perm, repo.FullName)
} else {
log.Debugf("Guest granted %+v to %s", perm, repo.FullName)
}
c.Set("perm", perm)
c.Next()
}
}
func MustPull(c *gin.Context) {
user := User(c)
repo := Repo(c)
perm := Perm(c)
if perm.Pull {
c.Next()
return
}
// if the user doesn't have pull permission to the
// repository we display a 404 error to avoid leaking
// repository information.
c.HTML(http.StatusNotFound, "404.html", gin.H{
"User": user,
"Repo": repo,
"Perm": perm,
})
c.Abort()
}
func MustPush(c *gin.Context) {
user := User(c)
repo := Repo(c)
perm := Perm(c)
// if the user has push access, immediately proceed
// the middleware execution chain.
if perm.Push {
c.Next()
return
}
data := gin.H{
"User": user,
"Repo": repo,
"Perm": perm,
}
// if the user has pull access we should tell them
// the operation is not authorized. Otherwise we should
// give a 404 to avoid leaking information.
if !perm.Pull {
c.HTML(http.StatusNotFound, "404.html", data)
} else {
c.HTML(http.StatusUnauthorized, "401.html", data)
}
// debugging
if user != nil {
log.Debugf("%s denied write access to %s",
user.Login, c.Request.URL.Path)
} else {
log.Debugf("Guest denied write access to %s %s",
c.Request.Method,
c.Request.URL.Path,
)
}
c.Abort()
}