@@ -68,6 +68,7 @@ type RepoTransfer struct { //nolint
68
68
RecipientID int64
69
69
Recipient * user_model.User `xorm:"-"`
70
70
RepoID int64
71
+ Repo * Repository `xorm:"-"`
71
72
TeamIDs []int64
72
73
Teams []* organization.Team `xorm:"-"`
73
74
@@ -79,48 +80,65 @@ func init() {
79
80
db .RegisterModel (new (RepoTransfer ))
80
81
}
81
82
82
- // LoadAttributes fetches the transfer recipient from the database
83
- func (r * RepoTransfer ) LoadAttributes (ctx context.Context ) error {
83
+ func (r * RepoTransfer ) LoadRecipient (ctx context.Context ) error {
84
84
if r .Recipient == nil {
85
85
u , err := user_model .GetUserByID (ctx , r .RecipientID )
86
86
if err != nil {
87
87
return err
88
88
}
89
-
90
89
r .Recipient = u
91
90
}
92
91
93
- if r .Recipient .IsOrganization () && len (r .TeamIDs ) != len (r .Teams ) {
94
- for _ , v := range r .TeamIDs {
95
- team , err := organization .GetTeamByID (ctx , v )
96
- if err != nil {
97
- return err
98
- }
92
+ return nil
93
+ }
99
94
100
- if team .OrgID != r .Recipient .ID {
101
- return fmt .Errorf ("team %d belongs not to org %d" , v , r .Recipient .ID )
102
- }
95
+ func (r * RepoTransfer ) LoadRepo (ctx context.Context ) error {
96
+ if r .Repo == nil {
97
+ repo , err := GetRepositoryByID (ctx , r .RepoID )
98
+ if err != nil {
99
+ return err
100
+ }
101
+ r .Repo = repo
102
+ }
103
+
104
+ return nil
105
+ }
106
+
107
+ // LoadAttributes fetches the transfer recipient from the database
108
+ func (r * RepoTransfer ) LoadAttributes (ctx context.Context ) error {
109
+ if err := r .LoadRecipient (ctx ); err != nil {
110
+ return err
111
+ }
103
112
113
+ if r .Recipient .IsOrganization () && r .Teams == nil {
114
+ teamsMap , err := organization .GetTeamsByIDs (ctx , r .TeamIDs )
115
+ if err != nil {
116
+ return err
117
+ }
118
+ for _ , team := range teamsMap {
104
119
r .Teams = append (r .Teams , team )
105
120
}
106
121
}
107
122
123
+ if err := r .LoadRepo (ctx ); err != nil {
124
+ return err
125
+ }
126
+
108
127
if r .Doer == nil {
109
128
u , err := user_model .GetUserByID (ctx , r .DoerID )
110
129
if err != nil {
111
130
return err
112
131
}
113
-
114
132
r .Doer = u
115
133
}
116
134
117
135
return nil
118
136
}
119
137
120
- // CanUserAcceptOrRejectTransfer checks if the user has the rights to accept/decline a repo transfer.
138
+ // CanUserAcceptTransfer checks if the user has the rights to accept/decline a repo transfer.
121
139
// For user, it checks if it's himself
122
140
// For organizations, it checks if the user is able to create repos
123
- func (r * RepoTransfer ) CanUserAcceptOrRejectTransfer (ctx context.Context , u * user_model.User ) bool {
141
+ func (r * RepoTransfer ) CanUserAcceptTransfer (ctx context.Context , u * user_model.User ) bool {
124
142
if err := r .LoadAttributes (ctx ); err != nil {
125
143
log .Error ("LoadAttributes: %v" , err )
126
144
return false
@@ -139,6 +157,32 @@ func (r *RepoTransfer) CanUserAcceptOrRejectTransfer(ctx context.Context, u *use
139
157
return allowed
140
158
}
141
159
160
+ func (r * RepoTransfer ) CanUserCancelTransfer (ctx context.Context , u * user_model.User ) bool {
161
+ if r .DoerID == u .ID { // sender can cancel the transfer
162
+ return true
163
+ }
164
+ if err := r .Repo .LoadOwner (ctx ); err != nil {
165
+ log .Error ("LoadOwner: %v" , err )
166
+ return false
167
+ }
168
+ if ! r .Repo .Owner .IsOrganization () {
169
+ if u .ID == r .Repo .OwnerID { // owner can cancel the transfer
170
+ return true
171
+ }
172
+ } else {
173
+ allowed , err := organization .CanCreateOrgRepo (ctx , r .Repo .OwnerID , u .ID )
174
+ if err != nil {
175
+ log .Error ("CanCreateOrgRepo: %v" , err )
176
+ return false
177
+ }
178
+ if allowed {
179
+ return true
180
+ }
181
+ }
182
+
183
+ return r .CanUserAcceptTransfer (ctx , u ) // the user who can accept the transfer can also reject it
184
+ }
185
+
142
186
type PendingRepositoryTransferOptions struct {
143
187
RepoID int64
144
188
SenderID int64
0 commit comments