#1535 Removing deploy key does not remove key

This commit is contained in:
Unknwon 2015-08-25 03:30:39 +08:00
parent 90f5b8edaf
commit 260301bf2e
2 changed files with 14 additions and 10 deletions

View File

@ -298,9 +298,13 @@ func saveAuthorizedKeyFile(keys ...*PublicKey) error {
return nil return nil
} }
// checkKeyContent onlys checks if key content has been used as public key,
// it is OK to use same key as deploy key for multiple repositories/users.
func checkKeyContent(content string) error { func checkKeyContent(content string) error {
// Same key can only be added once. has, err := x.Get(&PublicKey{
has, err := x.Where("content=?", content).Get(new(PublicKey)) Content: content,
Type: KEY_TYPE_USER,
})
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
@ -440,10 +444,11 @@ func UpdatePublicKey(key *PublicKey) error {
return err return err
} }
func deletePublicKey(e *xorm.Session, key *PublicKey) error { func deletePublicKey(e *xorm.Session, keyID int64) error {
sshOpLocker.Lock() sshOpLocker.Lock()
defer sshOpLocker.Unlock() defer sshOpLocker.Unlock()
key := &PublicKey{ID: keyID}
has, err := e.Get(key) has, err := e.Get(key)
if err != nil { if err != nil {
return err return err
@ -451,7 +456,7 @@ func deletePublicKey(e *xorm.Session, key *PublicKey) error {
return nil return nil
} }
if _, err = e.Id(key.ID).Delete(key); err != nil { if _, err = e.Id(key.ID).Delete(new(PublicKey)); err != nil {
return err return err
} }
@ -467,8 +472,7 @@ func deletePublicKey(e *xorm.Session, key *PublicKey) error {
// DeletePublicKey deletes SSH key information both in database and authorized_keys file. // DeletePublicKey deletes SSH key information both in database and authorized_keys file.
func DeletePublicKey(id int64) (err error) { func DeletePublicKey(id int64) (err error) {
key := &PublicKey{ID: id} has, err := x.Id(id).Get(new(PublicKey))
has, err := x.Id(key.ID).Get(key)
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
@ -481,7 +485,7 @@ func DeletePublicKey(id int64) (err error) {
return err return err
} }
if err = deletePublicKey(sess, key); err != nil { if err = deletePublicKey(sess, id); err != nil {
return err return err
} }
@ -658,7 +662,7 @@ func DeleteDeployKey(id int64) error {
return err return err
} }
if _, err = sess.Id(key.ID).Delete(key); err != nil { if _, err = sess.Id(key.ID).Delete(new(DeployKey)); err != nil {
return fmt.Errorf("delete deploy key[%d]: %v", key.ID, err) return fmt.Errorf("delete deploy key[%d]: %v", key.ID, err)
} }
@ -667,7 +671,7 @@ func DeleteDeployKey(id int64) error {
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
if err = deletePublicKey(sess, &PublicKey{ID: key.KeyID}); err != nil { if err = deletePublicKey(sess, key.KeyID); err != nil {
return err return err
} }
} }

View File

@ -592,7 +592,7 @@ func DeleteUser(u *User) error {
return fmt.Errorf("get all public keys: %v", err) return fmt.Errorf("get all public keys: %v", err)
} }
for _, key := range keys { for _, key := range keys {
if err = deletePublicKey(sess, key); err != nil { if err = deletePublicKey(sess, key.ID); err != nil {
return fmt.Errorf("deletePublicKey: %v", err) return fmt.Errorf("deletePublicKey: %v", err)
} }
} }