@@ -217,75 +217,77 @@ func runServ(c *cli.Context) error {
217
217
218
218
// Allow anonymous clone for public repositories.
219
219
var (
220
- keyID int64
221
- user * models.User
220
+ keyID int64
221
+ user * models.User
222
+ userID int64
222
223
)
223
- if requestedMode == models .AccessModeWrite || repo .IsPrivate || setting .Service .RequireSignInView {
224
- keys := strings .Split (c .Args ()[0 ], "-" )
225
- if len (keys ) != 2 {
226
- fail ("Key ID format error" , "Invalid key argument: %s" , c .Args ()[0 ])
227
- }
228
224
229
- key , err := private .GetPublicKeyByID (com .StrTo (keys [1 ]).MustInt64 ())
225
+ keys := strings .Split (c .Args ()[0 ], "-" )
226
+ if len (keys ) != 2 {
227
+ fail ("Key ID format error" , "Invalid key argument: %s" , c .Args ()[0 ])
228
+ }
229
+
230
+ key , err := private .GetPublicKeyByID (com .StrTo (keys [1 ]).MustInt64 ())
231
+ if err != nil {
232
+ fail ("Invalid key ID" , "Invalid key ID[%s]: %v" , c .Args ()[0 ], err )
233
+ }
234
+ keyID = key .ID
235
+ userID = key .OwnerID
236
+
237
+ if key .Type == models .KeyTypeDeploy {
238
+ // Now we have to get the deploy key for this repo
239
+ deployKey , err := private .GetDeployKey (key .ID , repo .ID )
230
240
if err != nil {
231
- fail ("Invalid key ID " , "Invalid key ID[%s] : %v " , c . Args ()[ 0 ], err )
241
+ fail ("Key access denied " , "Failed to access internal api: [key_id: %d, repo_id : %d] " , key . ID , repo . ID )
232
242
}
233
- keyID = key .ID
234
243
235
- // Check deploy key or user key.
236
- if key .Type == models .KeyTypeDeploy {
237
- // Now we have to get the deploy key for this repo
238
- deployKey , err := private .GetDeployKey (key .ID , repo .ID )
239
- if err != nil {
240
- fail ("Key access denied" , "Failed to access internal api: [key_id: %d, repo_id: %d]" , key .ID , repo .ID )
241
- }
242
-
243
- if deployKey == nil {
244
- fail ("Key access denied" , "Deploy key access denied: [key_id: %d, repo_id: %d]" , key .ID , repo .ID )
245
- }
244
+ if deployKey == nil {
245
+ fail ("Key access denied" , "Deploy key access denied: [key_id: %d, repo_id: %d]" , key .ID , repo .ID )
246
+ }
246
247
247
- if deployKey .Mode < requestedMode {
248
- fail ("Key permission denied" , "Cannot push with read-only deployment key: %d to repo_id: %d" , key .ID , repo .ID )
249
- }
248
+ if deployKey .Mode < requestedMode {
249
+ fail ("Key permission denied" , "Cannot push with read-only deployment key: %d to repo_id: %d" , key .ID , repo .ID )
250
+ }
250
251
251
- // Update deploy key activity.
252
- if err = private .UpdateDeployKeyUpdated (key .ID , repo .ID ); err != nil {
253
- fail ("Internal error" , "UpdateDeployKey: %v" , err )
254
- }
252
+ // Update deploy key activity.
253
+ if err = private .UpdateDeployKeyUpdated (key .ID , repo .ID ); err != nil {
254
+ fail ("Internal error" , "UpdateDeployKey: %v" , err )
255
+ }
255
256
256
- // FIXME: Deploy keys aren't really the owner of the repo pushing changes
257
- // however we don't have good way of representing deploy keys in hook.go
258
- // so for now use the owner
259
- os .Setenv (models .EnvPusherName , username )
260
- os .Setenv (models .EnvPusherID , fmt .Sprintf ("%d" , repo .OwnerID ))
261
- } else {
262
- user , err = private .GetUserByKeyID (key .ID )
263
- if err != nil {
264
- fail ("internal error" , "Failed to get user by key ID(%d): %v" , keyID , err )
265
- }
257
+ // FIXME: Deploy keys aren't really the owner of the repo pushing changes
258
+ // however we don't have good way of representing deploy keys in hook.go
259
+ // so for now use the owner
260
+ os .Setenv (models .EnvPusherName , username )
261
+ os .Setenv (models .EnvPusherID , fmt .Sprintf ("%d" , repo .OwnerID ))
262
+ userID = repo .OwnerID
263
+ } else if requestedMode == models .AccessModeWrite || repo .IsPrivate || setting .Service .RequireSignInView {
264
+ // Check deploy key or user key.
265
+ user , err = private .GetUserByKeyID (key .ID )
266
+ if err != nil {
267
+ fail ("internal error" , "Failed to get user by key ID(%d): %v" , keyID , err )
268
+ }
266
269
267
- if ! user .IsActive || user .ProhibitLogin {
268
- fail ("Your account is not active or has been disabled by Administrator" ,
269
- "User %s is disabled and have no access to repository %s" ,
270
- user .Name , repoPath )
271
- }
270
+ if ! user .IsActive || user .ProhibitLogin {
271
+ fail ("Your account is not active or has been disabled by Administrator" ,
272
+ "User %s is disabled and have no access to repository %s" ,
273
+ user .Name , repoPath )
274
+ }
272
275
273
- mode , err := private .CheckUnitUser (user .ID , repo .ID , user .IsAdmin , unitType )
274
- if err != nil {
275
- fail ("Internal error" , "Failed to check access: %v" , err )
276
- } else if * mode < requestedMode {
277
- clientMessage := accessDenied
278
- if * mode >= models .AccessModeRead {
279
- clientMessage = "You do not have sufficient authorization for this action"
280
- }
281
- fail (clientMessage ,
282
- "User %s does not have level %v access to repository %s's " + unitName ,
283
- user .Name , requestedMode , repoPath )
276
+ mode , err := private .CheckUnitUser (user .ID , repo .ID , user .IsAdmin , unitType )
277
+ if err != nil {
278
+ fail ("Internal error" , "Failed to check access: %v" , err )
279
+ } else if * mode < requestedMode {
280
+ clientMessage := accessDenied
281
+ if * mode >= models .AccessModeRead {
282
+ clientMessage = "You do not have sufficient authorization for this action"
284
283
}
285
-
286
- os . Setenv ( models . EnvPusherName , user . Name )
287
- os . Setenv ( models . EnvPusherID , fmt . Sprintf ( "%d" , user . ID ) )
284
+ fail ( clientMessage ,
285
+ "User %s does not have level %v access to repository %s's " + unitName ,
286
+ user . Name , requestedMode , repoPath )
288
287
}
288
+
289
+ os .Setenv (models .EnvPusherName , user .Name )
290
+ os .Setenv (models .EnvPusherID , fmt .Sprintf ("%d" , user .ID ))
289
291
}
290
292
291
293
//LFS token authentication
@@ -299,8 +301,8 @@ func runServ(c *cli.Context) error {
299
301
"exp" : now .Add (setting .LFS .HTTPAuthExpiry ).Unix (),
300
302
"nbf" : now .Unix (),
301
303
}
302
- if user != nil {
303
- claims ["user" ] = user . ID
304
+ if userID > 0 {
305
+ claims ["user" ] = userID
304
306
}
305
307
token := jwt .NewWithClaims (jwt .SigningMethodHS256 , claims )
306
308
0 commit comments