@@ -455,15 +455,6 @@ func reqRepoWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) {
455
455
}
456
456
}
457
457
458
- // reqRepoBranchWriter user should have a permission to write to a branch, or be a site admin
459
- func reqRepoBranchWriter (ctx * context.APIContext ) {
460
- options , ok := web .GetForm (ctx ).(api.FileOptionInterface )
461
- if ! ok || (! ctx .Repo .CanWriteToBranch (ctx , ctx .Doer , options .Branch ()) && ! ctx .IsUserSiteAdmin ()) {
462
- ctx .APIError (http .StatusForbidden , "user should have a permission to write to this branch" )
463
- return
464
- }
465
- }
466
-
467
458
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
468
459
func reqRepoReader (unitType unit.Type ) func (ctx * context.APIContext ) {
469
460
return func (ctx * context.APIContext ) {
@@ -744,9 +735,17 @@ func mustEnableWiki(ctx *context.APIContext) {
744
735
}
745
736
}
746
737
738
+ // FIXME: for consistency, maybe most mustNotBeArchived checks should be replaced with mustEnableEditor
747
739
func mustNotBeArchived (ctx * context.APIContext ) {
748
740
if ctx .Repo .Repository .IsArchived {
749
- ctx .APIError (http .StatusLocked , fmt .Errorf ("%s is archived" , ctx .Repo .Repository .LogString ()))
741
+ ctx .APIError (http .StatusLocked , fmt .Errorf ("%s is archived" , ctx .Repo .Repository .FullName ()))
742
+ return
743
+ }
744
+ }
745
+
746
+ func mustEnableEditor (ctx * context.APIContext ) {
747
+ if ! ctx .Repo .Repository .CanEnableEditor () {
748
+ ctx .APIError (http .StatusLocked , fmt .Errorf ("%s is not allowed to edit" , ctx .Repo .Repository .FullName ()))
750
749
return
751
750
}
752
751
}
@@ -1424,24 +1423,27 @@ func Routes() *web.Router {
1424
1423
m .Get ("/tags/{sha}" , repo .GetAnnotatedTag )
1425
1424
m .Get ("/notes/{sha}" , repo .GetNote )
1426
1425
}, context .ReferencesGitRepo (true ), reqRepoReader (unit .TypeCode ))
1427
- m .Post ("/diffpatch" , reqRepoWriter (unit .TypeCode ), reqToken (), bind (api.ApplyDiffPatchFileOptions {}), mustNotBeArchived , repo .ApplyDiffPatch )
1428
1426
m .Group ("/contents" , func () {
1429
1427
m .Get ("" , repo .GetContentsList )
1430
1428
m .Get ("/*" , repo .GetContents )
1431
- m .Post ("" , reqToken (), bind (api.ChangeFilesOptions {}), reqRepoBranchWriter , mustNotBeArchived , repo .ChangeFiles )
1432
- m .Group ("/*" , func () {
1433
- m .Post ("" , bind (api.CreateFileOptions {}), reqRepoBranchWriter , mustNotBeArchived , repo .CreateFile )
1434
- m .Put ("" , bind (api.UpdateFileOptions {}), reqRepoBranchWriter , mustNotBeArchived , repo .UpdateFile )
1435
- m .Delete ("" , bind (api.DeleteFileOptions {}), reqRepoBranchWriter , mustNotBeArchived , repo .DeleteFile )
1436
- }, reqToken ())
1429
+ m .Group ("" , func () {
1430
+ // "change file" operations, need permission to write to the target branch provided by the form
1431
+ m .Post ("" , bind (api.ChangeFilesOptions {}), repo .ReqChangeRepoFileOptionsAndCheck , repo .ChangeFiles )
1432
+ m .Group ("/*" , func () {
1433
+ m .Post ("" , bind (api.CreateFileOptions {}), repo .ReqChangeRepoFileOptionsAndCheck , repo .CreateFile )
1434
+ m .Put ("" , bind (api.UpdateFileOptions {}), repo .ReqChangeRepoFileOptionsAndCheck , repo .UpdateFile )
1435
+ m .Delete ("" , bind (api.DeleteFileOptions {}), repo .ReqChangeRepoFileOptionsAndCheck , repo .DeleteFile )
1436
+ })
1437
+ m .Post ("/diffpatch" , bind (api.ApplyDiffPatchFileOptions {}), repo .ReqChangeRepoFileOptionsAndCheck , repo .ApplyDiffPatch )
1438
+ }, mustEnableEditor , reqToken ())
1437
1439
}, reqRepoReader (unit .TypeCode ), context .ReferencesGitRepo ())
1438
1440
m .Group ("/contents-ext" , func () {
1439
1441
m .Get ("" , repo .GetContentsExt )
1440
1442
m .Get ("/*" , repo .GetContentsExt )
1441
1443
}, reqRepoReader (unit .TypeCode ), context .ReferencesGitRepo ())
1442
1444
m .Combo ("/file-contents" , reqRepoReader (unit .TypeCode ), context .ReferencesGitRepo ()).
1443
1445
Get (repo .GetFileContentsGet ).
1444
- Post (bind (api.GetFilesOptions {}), repo .GetFileContentsPost ) // POST method requires "write" permission, so we also support "GET" method above
1446
+ Post (bind (api.GetFilesOptions {}), repo .GetFileContentsPost ) // the POST method requires "write" permission, so we also support "GET" method above
1445
1447
m .Get ("/signing-key.gpg" , misc .SigningKeyGPG )
1446
1448
m .Get ("/signing-key.pub" , misc .SigningKeySSH )
1447
1449
m .Group ("/topics" , func () {
0 commit comments