diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index a6c34827b6..5385b938f5 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -110,6 +110,7 @@ "loading": "Loading…", "files": "Files", "error_title": "Error", + "error403": "You do not have permission to access this resource. If you believe this is an error, contact the repository owner.", "error404": "The page you are trying to reach either does not exist or you are not authorized to view it.", "error503": "The server could not complete your request. Please try again later.", "go_back": "Go Back", diff --git a/services/context/context_response.go b/services/context/context_response.go index 5667b7e13a..ed238f0376 100644 --- a/services/context/context_response.go +++ b/services/context/context_response.go @@ -169,6 +169,26 @@ func (ctx *Context) notFoundInternal(logMsg string, logErr error) { ctx.HTML(http.StatusNotFound, "status/404") } +// Forbidden displays a styled 403 (Access Denied) page, matching the 404 page layout. +func (ctx *Context) Forbidden() { + showHTML := false + for _, part := range ctx.Req.Header["Accept"] { + if strings.Contains(part, "text/html") { + showHTML = true + break + } + } + + if !showHTML { + ctx.plainTextInternal(3, http.StatusForbidden, []byte("Access denied.\n")) + return + } + + ctx.Data["IsRepo"] = ctx.Repo.Repository != nil + ctx.Data["Title"] = "Access Denied" + ctx.HTML(http.StatusForbidden, "status/403") +} + // ServerError displays a 500 (Internal Server Error) page and prints the given error, if any. // If the error is controlled by our error system, a related 404 page can be displayed instead. func (ctx *Context) ServerError(logMsg string, logErr error) { diff --git a/services/context/repo.go b/services/context/repo.go index a710a3f80f..5bd6653a1c 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -451,11 +451,11 @@ func repoAssignmentLegacy(ctx *Context, data *repoAssignmentPrepareDataStruct) { ctx.Data["LicensedReadOnly"] = true // Continue — don't block access. } else { - ctx.HTTPError(http.StatusForbidden, "You do not have permission to access this repository") + ctx.Forbidden() return } } else { - ctx.HTTPError(http.StatusForbidden, "You do not have permission to access this repository") + ctx.Forbidden() return } } diff --git a/templates/status/403.tmpl b/templates/status/403.tmpl new file mode 100644 index 0000000000..05eaa9dccc --- /dev/null +++ b/templates/status/403.tmpl @@ -0,0 +1,17 @@ +{{template "base/head" .}} +