2015-09-30 01:21:17 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-02-05 19:13:34 +00:00
|
|
|
"os"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-03-05 05:15:50 +00:00
|
|
|
"github.com/drone/drone/cache"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/model"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/store"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2015-10-13 09:08:08 +00:00
|
|
|
func Repo(c *gin.Context) *model.Repo {
|
|
|
|
v, ok := c.Get("repo")
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2015-10-13 09:08:08 +00:00
|
|
|
r, ok := v.(*model.Repo)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:08:08 +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
|
|
|
|
}
|
2015-10-13 09:08:08 +00:00
|
|
|
r, ok := v.([]*model.RepoLite)
|
2015-09-30 01:21:17 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-13 09:08:08 +00:00
|
|
|
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")
|
2016-07-11 02:08:52 +00:00
|
|
|
user = User(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
2015-10-21 23:14:02 +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
|
|
|
|
}
|
|
|
|
|
2016-07-11 02:08:52 +00:00
|
|
|
// debugging
|
|
|
|
log.Debugf("Cannot find repository %s/%s. %s",
|
|
|
|
owner,
|
|
|
|
name,
|
|
|
|
err.Error(),
|
|
|
|
)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-07-11 02:08:52 +00:00
|
|
|
if user != nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2015-09-30 01:21:17 +00:00
|
|
|
} else {
|
2016-07-11 02:08:52 +00:00
|
|
|
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
|
|
|
PUBLIC_MODE := os.Getenv("PUBLIC_MODE")
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
user := User(c)
|
|
|
|
repo := Repo(c)
|
|
|
|
perm := &model.Perm{}
|
|
|
|
|
|
|
|
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
|
2016-03-05 05:15:50 +00:00
|
|
|
perm, err = cache.GetPerms(c, user, repo.Owner, repo.Name)
|
2015-09-30 01:21:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 19:13:34 +00:00
|
|
|
// all build logs are visible in public mode
|
|
|
|
if PUBLIC_MODE != "" {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-11 02:08:52 +00:00
|
|
|
// 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 {
|
2016-07-11 02:08:52 +00:00
|
|
|
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 {
|
2016-07-11 02:08:52 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|