diff --git a/router/middleware/session/repo.go b/router/middleware/session/repo.go index 34dd047c5..6962bdb4b 100644 --- a/router/middleware/session/repo.go +++ b/router/middleware/session/repo.go @@ -2,6 +2,7 @@ package session import ( "net/http" + "os" "github.com/drone/drone/model" "github.com/drone/drone/remote" @@ -104,6 +105,8 @@ func Perm(c *gin.Context) *model.Perm { } func SetPerm() gin.HandlerFunc { + PUBLIC_MODE := os.Getenv("PUBLIC_MODE") + return func(c *gin.Context) { user := User(c) repo := Repo(c) @@ -164,6 +167,11 @@ func SetPerm() gin.HandlerFunc { } } + // all build logs are visible in public mode + if PUBLIC_MODE != "" { + perm.Pull = true + } + if user != nil { log.Debugf("%s granted %+v permission to %s", user.Login, perm, repo.FullName) diff --git a/router/middleware/session/repo_test.go b/router/middleware/session/repo_test.go new file mode 100644 index 000000000..6d524a9b7 --- /dev/null +++ b/router/middleware/session/repo_test.go @@ -0,0 +1,44 @@ +package session + +import ( + "os" + "testing" + + "github.com/drone/drone/model" + "github.com/franela/goblin" + "github.com/gin-gonic/gin" +) + +func TestSetPerm(t *testing.T) { + g := goblin.Goblin(t) + g.Describe("SetPerm", func() { + g.BeforeEach(func() { + os.Unsetenv("PUBLIC_MODE") + }) + g.It("Should set pull to false (private repo, user not logged in)", func() { + c := gin.Context{} + c.Set("repo", &model.Repo{ + IsPrivate: true, + }) + SetPerm()(&c) + v, ok := c.Get("perm") + g.Assert(ok).IsTrue("perm was not set") + p, ok := v.(*model.Perm) + g.Assert(ok).IsTrue("perm was the wrong type") + g.Assert(p.Pull).IsFalse("pull should be false") + }) + g.It("Should set pull to true (private repo, user not logged in, public mode)", func() { + os.Setenv("PUBLIC_MODE", "true") + c := gin.Context{} + c.Set("repo", &model.Repo{ + IsPrivate: true, + }) + SetPerm()(&c) + v, ok := c.Get("perm") + g.Assert(ok).IsTrue("perm was not set") + p, ok := v.(*model.Perm) + g.Assert(ok).IsTrue("perm was the wrong type") + g.Assert(p.Pull).IsTrue("pull should be true") + }) + }) +}