1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/server/router/middleware/session/repo.go

187 lines
3.8 KiB
Go
Raw Normal View History

2018-02-20 00:24:10 +02:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2015-09-30 03:21:17 +02:00
package session
import (
"net/http"
2017-07-14 21:58:38 +02:00
"time"
2015-09-30 03:21:17 +02:00
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/remote"
"github.com/woodpecker-ci/woodpecker/server/store"
2015-09-30 03:21:17 +02:00
"github.com/gin-gonic/gin"
2020-05-18 16:46:13 +02:00
log "github.com/sirupsen/logrus"
2015-09-30 03:21:17 +02:00
)
func Repo(c *gin.Context) *model.Repo {
v, ok := c.Get("repo")
if !ok {
return nil
2015-09-30 03:21:17 +02:00
}
r, ok := v.(*model.Repo)
if !ok {
return nil
}
return r
2015-09-30 03:21:17 +02: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 03:21:17 +02:00
)
repo, err := store.GetRepoOwnerName(c, owner, name)
2015-09-30 03:21:17 +02: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 03:21:17 +02:00
if user != nil {
c.AbortWithStatus(http.StatusNotFound)
2015-09-30 03:21:17 +02:00
} else {
c.AbortWithStatus(http.StatusUnauthorized)
2015-09-30 03:21:17 +02: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 {
return func(c *gin.Context) {
user := User(c)
repo := Repo(c)
2017-09-15 00:20:20 +02:00
perm := new(model.Perm)
2015-09-30 03:21:17 +02:00
switch {
2017-05-23 00:44:58 +02:00
case user != nil:
2015-09-30 03:21:17 +02:00
var err error
2017-07-14 21:58:38 +02:00
perm, err = store.FromContext(c).PermFind(user, repo)
2015-09-30 03:21:17 +02:00
if err != nil {
2017-07-14 21:58:38 +02: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(c, user, repo.Owner, repo.Name)
2017-07-14 21:58:38 +02:00
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 03:21:17 +02:00
}
}
2017-09-15 00:20:20 +02:00
if perm == nil {
perm = new(model.Perm)
}
2017-09-08 02:43:33 +02:00
if user != nil && user.Admin {
perm.Pull = true
perm.Push = true
perm.Admin = true
}
2017-05-23 00:44:58 +02:00
switch {
case repo.Visibility == model.VisibilityPublic:
perm.Pull = true
case repo.Visibility == model.VisibilityInternal && user != nil:
2016-02-05 21:13:34 +02:00
perm.Pull = true
}
2015-09-30 03:21:17 +02: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 03:21:17 +02: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 03:21:17 +02:00
user.Login, c.Request.URL.Path)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
2015-09-30 03:21:17 +02:00
log.Debugf("Guest denied write access to %s %s",
c.Request.Method,
c.Request.URL.Path,
)
}
}