@@ -518,7 +518,7 @@ func UpdatePublicKeyUpdated(id int64) error {
518
518
}
519
519
520
520
// deletePublicKeys does the actual key deletion but does not update authorized_keys file.
521
- func deletePublicKeys (e * xorm. Session , keyIDs ... int64 ) error {
521
+ func deletePublicKeys (e Engine , keyIDs ... int64 ) error {
522
522
if len (keyIDs ) == 0 {
523
523
return nil
524
524
}
@@ -766,8 +766,12 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
766
766
767
767
// GetDeployKeyByID returns deploy key by given ID.
768
768
func GetDeployKeyByID (id int64 ) (* DeployKey , error ) {
769
+ return getDeployKeyByID (x , id )
770
+ }
771
+
772
+ func getDeployKeyByID (e Engine , id int64 ) (* DeployKey , error ) {
769
773
key := new (DeployKey )
770
- has , err := x .ID (id ).Get (key )
774
+ has , err := e .ID (id ).Get (key )
771
775
if err != nil {
772
776
return nil , err
773
777
} else if ! has {
@@ -778,11 +782,15 @@ func GetDeployKeyByID(id int64) (*DeployKey, error) {
778
782
779
783
// GetDeployKeyByRepo returns deploy key by given public key ID and repository ID.
780
784
func GetDeployKeyByRepo (keyID , repoID int64 ) (* DeployKey , error ) {
785
+ return getDeployKeyByRepo (x , keyID , repoID )
786
+ }
787
+
788
+ func getDeployKeyByRepo (e Engine , keyID , repoID int64 ) (* DeployKey , error ) {
781
789
key := & DeployKey {
782
790
KeyID : keyID ,
783
791
RepoID : repoID ,
784
792
}
785
- has , err := x .Get (key )
793
+ has , err := e .Get (key )
786
794
if err != nil {
787
795
return nil , err
788
796
} else if ! has {
@@ -805,7 +813,19 @@ func UpdateDeployKey(key *DeployKey) error {
805
813
806
814
// DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed.
807
815
func DeleteDeployKey (doer * User , id int64 ) error {
808
- key , err := GetDeployKeyByID (id )
816
+ sess := x .NewSession ()
817
+ defer sess .Close ()
818
+ if err := sess .Begin (); err != nil {
819
+ return err
820
+ }
821
+ if err := deleteDeployKey (sess , doer , id ); err != nil {
822
+ return err
823
+ }
824
+ return sess .Commit ()
825
+ }
826
+
827
+ func deleteDeployKey (sess Engine , doer * User , id int64 ) error {
828
+ key , err := getDeployKeyByID (sess , id )
809
829
if err != nil {
810
830
if IsErrDeployKeyNotExist (err ) {
811
831
return nil
@@ -815,24 +835,18 @@ func DeleteDeployKey(doer *User, id int64) error {
815
835
816
836
// Check if user has access to delete this key.
817
837
if ! doer .IsAdmin {
818
- repo , err := GetRepositoryByID ( key .RepoID )
838
+ repo , err := getRepositoryByID ( sess , key .RepoID )
819
839
if err != nil {
820
840
return fmt .Errorf ("GetRepositoryByID: %v" , err )
821
841
}
822
- has , err := IsUserRepoAdmin ( repo , doer )
842
+ has , err := isUserRepoAdmin ( sess , repo , doer )
823
843
if err != nil {
824
844
return fmt .Errorf ("GetUserRepoPermission: %v" , err )
825
845
} else if ! has {
826
846
return ErrKeyAccessDenied {doer .ID , key .ID , "deploy" }
827
847
}
828
848
}
829
849
830
- sess := x .NewSession ()
831
- defer sess .Close ()
832
- if err = sess .Begin (); err != nil {
833
- return err
834
- }
835
-
836
850
if _ , err = sess .ID (key .ID ).Delete (new (DeployKey )); err != nil {
837
851
return fmt .Errorf ("delete deploy key [%d]: %v" , key .ID , err )
838
852
}
@@ -854,13 +868,17 @@ func DeleteDeployKey(doer *User, id int64) error {
854
868
}
855
869
}
856
870
857
- return sess . Commit ()
871
+ return nil
858
872
}
859
873
860
874
// ListDeployKeys returns all deploy keys by given repository ID.
861
875
func ListDeployKeys (repoID int64 ) ([]* DeployKey , error ) {
876
+ return listDeployKeys (x , repoID )
877
+ }
878
+
879
+ func listDeployKeys (e Engine , repoID int64 ) ([]* DeployKey , error ) {
862
880
keys := make ([]* DeployKey , 0 , 5 )
863
- return keys , x .
881
+ return keys , e .
864
882
Where ("repo_id = ?" , repoID ).
865
883
Find (& keys )
866
884
}
0 commit comments