Browse Source

cmd/serve: minor code improve for PR #4078

pull/4079/head
Unknwon 8 years ago
parent
commit
2bb1de1805
No known key found for this signature in database
GPG Key ID: FB9F411CDD69BEC1
  1. 41
      cmd/serve.go
  2. 2
      gogs.go
  3. 5
      models/ssh_key.go
  4. 2
      templates/.VERSION

41
cmd/serve.go

@ -62,19 +62,6 @@ func parseCmd(cmd string) (string, string) {
return ss[0], strings.Replace(ss[1], "'/", "'", 1) return ss[0], strings.Replace(ss[1], "'/", "'", 1)
} }
func getKey(cmdKey string) *models.PublicKey {
keys := strings.Split(cmdKey, "-")
if len(keys) != 2 {
fail("Key ID format error", "Invalid key argument: %s", cmdKey)
}
key, err := models.GetPublicKeyByID(com.StrTo(keys[1]).MustInt64())
if err != nil {
fail("Invalid key ID", "Invalid key ID[%s]: %v", cmdKey, err)
}
return key
}
func checkDeployKey(key *models.PublicKey, repo *models.Repository) { func checkDeployKey(key *models.PublicKey, repo *models.Repository) {
// Check if this deploy key belongs to current repository. // Check if this deploy key belongs to current repository.
if !models.HasDeployKey(key.ID, repo.ID) { if !models.HasDeployKey(key.ID, repo.ID) {
@ -226,16 +213,17 @@ func runServ(c *cli.Context) error {
fail("mirror repository is read-only", "") fail("mirror repository is read-only", "")
} }
// Allow anonymous clone for public repositories. // Allow anonymous (user is nil) clone for public repositories.
var ( var user *models.User
keyID int64
user *models.User key, err := models.GetPublicKeyByID(com.StrTo(strings.TrimPrefix(c.Args()[0], "key-")).MustInt64())
) if err != nil {
key := getKey(c.Args()[0]) fail("Invalid key ID", "Invalid key ID [%s]: %v", c.Args()[0], err)
keyID = key.ID }
if requestedMode == models.ACCESS_MODE_WRITE || repo.IsPrivate { if requestedMode == models.ACCESS_MODE_WRITE || repo.IsPrivate {
// Check deploy key or user key. // Check deploy key or user key.
if key.Type == models.KEY_TYPE_DEPLOY { if key.IsDeployKey() {
if key.Mode < requestedMode { if key.Mode < requestedMode {
fail("Key permission denied", "Cannot push with deployment key: %d", key.ID) fail("Key permission denied", "Cannot push with deployment key: %d", key.ID)
} }
@ -243,7 +231,7 @@ func runServ(c *cli.Context) error {
} else { } else {
user, err = models.GetUserByKeyID(key.ID) user, err = models.GetUserByKeyID(key.ID)
if err != nil { if err != nil {
fail("internal error", "Failed to get user by key ID(%d): %v", keyID, err) fail("internal error", "Failed to get user by key ID(%d): %v", key.ID, err)
} }
mode, err := models.AccessLevel(user, repo) mode, err := models.AccessLevel(user, repo)
@ -260,12 +248,11 @@ func runServ(c *cli.Context) error {
} }
} }
} else { } else {
// if public and read ...
// Check if the key can access to the repository in case of it is a deploy key (a deploy keys != user key). // Check if the key can access to the repository in case of it is a deploy key (a deploy keys != user key).
// A deploy key doesn't represent a signed in user, so in a site with Service.RequireSignInView activated // A deploy key doesn't represent a signed in user, so in a site with Service.RequireSignInView activated
// we should give read access only in repositories where this deploy key is in use. In other case, a server // we should give read access only in repositories where this deploy key is in use. In other case, a server
// or system using an active deploy key can get read access to all the repositories in a Gogs service. // or system using an active deploy key can get read access to all the repositories in a Gogs service.
if key.Type == models.KEY_TYPE_DEPLOY && setting.Service.RequireSignInView { if key.IsDeployKey() && setting.Service.RequireSignInView {
checkDeployKey(key, repo) checkDeployKey(key, repo)
} }
} }
@ -298,10 +285,10 @@ func runServ(c *cli.Context) error {
} }
// Update user key activity. // Update user key activity.
if keyID > 0 { if key.ID > 0 {
key, err := models.GetPublicKeyByID(keyID) key, err := models.GetPublicKeyByID(key.ID)
if err != nil { if err != nil {
fail("Internal error", "GetPublicKeyById: %v", err) fail("Internal error", "GetPublicKeyByID: %v", err)
} }
key.Updated = time.Now() key.Updated = time.Now()

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.9.131.0201" const APP_VER = "0.9.132.0201"
func init() { func init() {
setting.AppVer = APP_VER setting.AppVer = APP_VER

5
models/ssh_key.go

@ -88,6 +88,11 @@ func (key *PublicKey) AuthorizedString() string {
return fmt.Sprintf(_TPL_PUBLICK_KEY, setting.AppPath, key.ID, setting.CustomConf, key.Content) return fmt.Sprintf(_TPL_PUBLICK_KEY, setting.AppPath, key.ID, setting.CustomConf, key.Content)
} }
// IsDeployKey returns true if the public key is used as deploy key.
func (key *PublicKey) IsDeployKey() bool {
return key.Type == KEY_TYPE_DEPLOY
}
func extractTypeFromBase64Key(key string) (string, error) { func extractTypeFromBase64Key(key string) (string, error) {
b, err := base64.StdEncoding.DecodeString(key) b, err := base64.StdEncoding.DecodeString(key)
if err != nil || len(b) < 4 { if err != nil || len(b) < 4 {

2
templates/.VERSION

@ -1 +1 @@
0.9.131.0201 0.9.132.0201
Loading…
Cancel
Save