harness-drone/router/middleware/session/repo.go

181 lines
3.3 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package session
import (
"net/http"
2017-07-14 19:58:38 +00:00
"time"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/model"
2017-07-14 19:58:38 +00:00
"github.com/drone/drone/remote"
"github.com/drone/drone/store"
2015-09-30 01:21:17 +00:00
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
2015-09-30 01:21:17 +00:00
}
r, ok := v.(*model.Repo)
if !ok {
return nil
}
return r
2015-09-30 01:21:17 +00:00
}
func Repos(c *gin.Context) []*model.RepoLite {
v, ok := c.Get("repos")
2015-09-30 01:21:17 +00:00
if !ok {
return nil
}
r, ok := v.([]*model.RepoLite)
2015-09-30 01:21:17 +00:00
if !ok {
return nil
}
return r
2015-09-30 01:21:17 +00:00
}
func SetRepo() gin.HandlerFunc {
return func(c *gin.Context) {
var (
owner = c.Param("owner")
name = c.Param("name")
user = User(c)
2015-09-30 01:21:17 +00:00
)
repo, err := store.GetRepoOwnerName(c, owner, name)
2015-09-30 01:21:17 +00:00
if err == nil {
c.Set("repo", repo)
c.Next()
return
}
// debugging
log.Debugf("Cannot find repository %s/%s. %s",
owner,
name,
err.Error(),
)
2015-09-30 01:21:17 +00:00
if user != nil {
c.AbortWithStatus(http.StatusNotFound)
2015-09-30 01:21:17 +00:00
} else {
c.AbortWithStatus(http.StatusUnauthorized)
2015-09-30 01:21:17 +00:00
}
}
}
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 {
2016-02-05 19:13:34 +00:00
2015-09-30 01:21:17 +00:00
return func(c *gin.Context) {
user := User(c)
repo := Repo(c)
perm := &model.Perm{}
switch {
2017-05-22 22:44:58 +00:00
case user != nil && user.Admin:
2015-09-30 01:21:17 +00:00
perm.Pull = true
perm.Push = true
perm.Admin = true
2017-05-22 22:44:58 +00:00
case user != nil:
2015-09-30 01:21:17 +00:00
var err error
2017-07-14 19:58:38 +00:00
perm, err = store.FromContext(c).PermFind(user, repo)
2015-09-30 01:21:17 +00:00
if err != nil {
2017-07-14 19:58:38 +00:00
log.Errorf("Error fetching permission for %s %s. %s",
user.Login, repo.FullName, err)
}
if time.Unix(perm.Synced, 0).Add(time.Hour).Before(time.Now()) {
perm, err = remote.FromContext(c).Perm(user, repo.Owner, repo.Name)
if err == nil {
log.Debugf("Synced user permission for %s %s", user.Login, repo.FullName)
perm.Repo = repo.FullName
perm.UserID = user.ID
perm.Synced = time.Now().Unix()
store.FromContext(c).PermUpsert(perm)
}
2015-09-30 01:21:17 +00:00
}
}
2017-05-22 22:44:58 +00:00
switch {
case repo.Visibility == model.VisibilityPublic:
perm.Pull = true
case repo.Visibility == model.VisibilityInternal && user != nil:
2016-02-05 19:13:34 +00:00
perm.Pull = true
}
2015-09-30 01:21:17 +00:00
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)
perm := Perm(c)
if perm.Pull {
c.Next()
return
}
// debugging
if user != nil {
c.AbortWithStatus(http.StatusNotFound)
log.Debugf("User %s denied read access to %s",
user.Login, c.Request.URL.Path)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
log.Debugf("Guest denied read access to %s %s",
c.Request.Method,
c.Request.URL.Path,
)
}
2015-09-30 01:21:17 +00:00
}
func MustPush(c *gin.Context) {
user := User(c)
perm := Perm(c)
// if the user has push access, immediately proceed
// the middleware execution chain.
if perm.Push {
c.Next()
return
}
// debugging
if user != nil {
c.AbortWithStatus(http.StatusNotFound)
log.Debugf("User %s denied write access to %s",
2015-09-30 01:21:17 +00:00
user.Login, c.Request.URL.Path)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
2015-09-30 01:21:17 +00:00
log.Debugf("Guest denied write access to %s %s",
c.Request.Method,
c.Request.URL.Path,
)
}
}