2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 01:21:17 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2023-03-19 12:52:58 +00:00
|
|
|
"errors"
|
2015-09-30 01:21:17 +00:00
|
|
|
"net/http"
|
2017-07-14 19:58:38 +00:00
|
|
|
"time"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-26 12:01:54 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2023-03-21 22:01:59 +00:00
|
|
|
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2023-03-19 17:32:19 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2023-06-02 10:25:09 +00:00
|
|
|
r.Perm = Perm(c)
|
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 (
|
2021-12-01 13:22:06 +00:00
|
|
|
_store = store.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
owner = c.Param("owner")
|
|
|
|
name = c.Param("name")
|
|
|
|
user = User(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
repo, err := _store.GetRepoName(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
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Cannot find repository %s/%s. %s",
|
2016-07-11 02:08:52 +00:00
|
|
|
owner,
|
|
|
|
name,
|
|
|
|
err.Error(),
|
|
|
|
)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-07-11 02:08:52 +00:00
|
|
|
if user != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
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 {
|
|
|
|
return func(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
user := User(c)
|
|
|
|
repo := Repo(c)
|
2017-09-14 22:20:20 +00:00
|
|
|
perm := new(model.Perm)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2023-03-21 22:01:59 +00:00
|
|
|
if user != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
var err error
|
2021-12-01 13:22:06 +00:00
|
|
|
perm, err = _store.PermFind(user, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("Error fetching permission for %s %s. %s",
|
2017-07-14 19:58:38 +00:00
|
|
|
user.Login, repo.FullName, err)
|
|
|
|
}
|
|
|
|
if time.Unix(perm.Synced, 0).Add(time.Hour).Before(time.Now()) {
|
2023-03-21 22:01:59 +00:00
|
|
|
_repo, err := server.Config.Services.Forge.Repo(c, user, repo.ForgeRemoteID, repo.Owner, repo.Name)
|
2017-07-14 19:58:38 +00:00
|
|
|
if err == nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Synced user permission for %s %s", user.Login, repo.FullName)
|
2023-03-21 22:01:59 +00:00
|
|
|
perm = _repo.Perm
|
2022-09-05 15:08:51 +00:00
|
|
|
perm.Repo = repo
|
2023-03-21 22:01:59 +00:00
|
|
|
perm.RepoID = repo.ID
|
2017-07-14 19:58:38 +00:00
|
|
|
perm.UserID = user.ID
|
|
|
|
perm.Synced = time.Now().Unix()
|
2021-12-01 13:22:06 +00:00
|
|
|
if err := _store.PermUpsert(perm); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-14 19:58:38 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:20:20 +00:00
|
|
|
if perm == nil {
|
|
|
|
perm = new(model.Perm)
|
|
|
|
}
|
|
|
|
|
2017-09-08 00:43:33 +00:00
|
|
|
if user != nil && user.Admin {
|
|
|
|
perm.Pull = true
|
|
|
|
perm.Push = true
|
|
|
|
perm.Admin = true
|
|
|
|
}
|
|
|
|
|
2023-03-21 22:01:59 +00:00
|
|
|
if repo.Visibility == model.VisibilityPublic || (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 {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("%s granted %+v permission to %s",
|
2015-09-30 01:21:17 +00:00
|
|
|
user.Login, perm, repo.FullName)
|
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Guest granted %+v to %s", perm, repo.FullName)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("User %s denied read access to %s",
|
2016-07-11 02:08:52 +00:00
|
|
|
user.Login, c.Request.URL.Path)
|
|
|
|
} else {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Guest denied read access to %s %s",
|
2016-07-11 02:08:52 +00:00
|
|
|
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)
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("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)
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Guest denied write access to %s %s",
|
2015-09-30 01:21:17 +00:00
|
|
|
c.Request.Method,
|
|
|
|
c.Request.URL.Path,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|