2015-10-13 09:08:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2015-10-20 23:45:24 +00:00
|
|
|
"github.com/drone/drone/cache"
|
2015-10-13 09:08:08 +00:00
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
const permKey = "perm"
|
|
|
|
|
|
|
|
// Perms is a middleware function that attempts to cache the
|
|
|
|
// user's remote rempository permissions (ie in GitHub) to minimize
|
|
|
|
// remote calls that might be expensive, slow or rate-limited.
|
|
|
|
func Perms(c *gin.Context) {
|
|
|
|
var (
|
|
|
|
owner = c.Param("owner")
|
|
|
|
name = c.Param("name")
|
|
|
|
user, _ = c.Get("user")
|
|
|
|
)
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the item already exists in the cache
|
|
|
|
// we can continue the middleware chain and
|
|
|
|
// exit afterwards.
|
2015-10-20 23:45:24 +00:00
|
|
|
v := cache.GetPerms(c,
|
|
|
|
user.(*model.User),
|
|
|
|
owner,
|
|
|
|
name,
|
|
|
|
)
|
2015-10-13 09:08:08 +00:00
|
|
|
if v != nil {
|
|
|
|
c.Set("perm", v)
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, if the item isn't cached we execute
|
|
|
|
// the middleware chain and then cache the permissions
|
|
|
|
// after the request is processed.
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
perm, ok := c.Get("perm")
|
|
|
|
if ok {
|
2015-10-20 23:45:24 +00:00
|
|
|
cache.SetPerms(c,
|
|
|
|
user.(*model.User),
|
|
|
|
perm.(*model.Perm),
|
|
|
|
owner,
|
|
|
|
name,
|
|
|
|
)
|
2015-10-13 09:08:08 +00:00
|
|
|
}
|
|
|
|
}
|