@@ -455,15 +455,6 @@ func reqRepoWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) {
455455 }
456456}
457457
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-
467458// reqRepoReader user should have specific read permission or be a repo admin or a site admin
468459func reqRepoReader (unitType unit.Type ) func (ctx * context.APIContext ) {
469460 return func (ctx * context.APIContext ) {
@@ -744,9 +735,17 @@ func mustEnableWiki(ctx *context.APIContext) {
744735 }
745736}
746737
738+ // FIXME: for consistency, maybe most mustNotBeArchived checks should be replaced with mustEnableEditor
747739func mustNotBeArchived (ctx * context.APIContext ) {
748740 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 ()))
750749 return
751750 }
752751}
@@ -1424,24 +1423,27 @@ func Routes() *web.Router {
14241423 m .Get ("/tags/{sha}" , repo .GetAnnotatedTag )
14251424 m .Get ("/notes/{sha}" , repo .GetNote )
14261425 }, context .ReferencesGitRepo (true ), reqRepoReader (unit .TypeCode ))
1427- m .Post ("/diffpatch" , reqRepoWriter (unit .TypeCode ), reqToken (), bind (api.ApplyDiffPatchFileOptions {}), mustNotBeArchived , repo .ApplyDiffPatch )
14281426 m .Group ("/contents" , func () {
14291427 m .Get ("" , repo .GetContentsList )
14301428 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
1431+ m .Post ("" , bind (api.ChangeFilesOptions {}), repo .ChangeFiles )
1432+ m .Group ("/*" , func () {
1433+ m .Post ("" , bind (api.CreateFileOptions {}), repo .CreateFile )
1434+ m .Put ("" , bind (api.UpdateFileOptions {}), repo .UpdateFile )
1435+ m .Delete ("" , bind (api.DeleteFileOptions {}), repo .DeleteFile )
1436+ })
1437+ m .Post ("/diffpatch" , bind (api.ApplyDiffPatchFileOptions {}), repo .ApplyDiffPatch )
1438+ }, mustEnableEditor , reqToken (), repo .ReqChangeRepoFileOptionsAndCheck ) // need permission to write to the branch
14371439 }, reqRepoReader (unit .TypeCode ), context .ReferencesGitRepo ())
14381440 m .Group ("/contents-ext" , func () {
14391441 m .Get ("" , repo .GetContentsExt )
14401442 m .Get ("/*" , repo .GetContentsExt )
14411443 }, reqRepoReader (unit .TypeCode ), context .ReferencesGitRepo ())
14421444 m .Combo ("/file-contents" , reqRepoReader (unit .TypeCode ), context .ReferencesGitRepo ()).
14431445 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
14451447 m .Get ("/signing-key.gpg" , misc .SigningKeyGPG )
14461448 m .Get ("/signing-key.pub" , misc .SigningKeySSH )
14471449 m .Group ("/topics" , func () {
0 commit comments