mirror of
https://github.com/casdoor/casdoor.git
synced 2025-09-06 18:10:29 +08:00
feat: append HTML document title and favicon to cookie (#3519)
* feat: append HTML document title and favicon to cookie * feat: remove useless cookie
This commit is contained in:
@@ -133,7 +133,7 @@ func StaticFilter(ctx *context.Context) {
|
|||||||
path += urlPath
|
path += urlPath
|
||||||
}
|
}
|
||||||
|
|
||||||
err := appendThemeCookie(ctx, urlPath)
|
organizationThemeCookie, err := appendThemeCookie(ctx, urlPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@@ -154,13 +154,13 @@ func StaticFilter(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if oldStaticBaseUrl == newStaticBaseUrl {
|
if oldStaticBaseUrl == newStaticBaseUrl {
|
||||||
makeGzipResponse(ctx.ResponseWriter, ctx.Request, path)
|
makeGzipResponse(ctx.ResponseWriter, ctx.Request, path, organizationThemeCookie)
|
||||||
} else {
|
} else {
|
||||||
serveFileWithReplace(ctx.ResponseWriter, ctx.Request, path)
|
serveFileWithReplace(ctx.ResponseWriter, ctx.Request, path, organizationThemeCookie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveFileWithReplace(w http.ResponseWriter, r *http.Request, name string) {
|
func serveFileWithReplace(w http.ResponseWriter, r *http.Request, name string, organizationThemeCookie *OrganizationThemeCookie) {
|
||||||
f, err := os.Open(filepath.Clean(name))
|
f, err := os.Open(filepath.Clean(name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -173,7 +173,13 @@ func serveFileWithReplace(w http.ResponseWriter, r *http.Request, name string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
oldContent := util.ReadStringFromPath(name)
|
oldContent := util.ReadStringFromPath(name)
|
||||||
newContent := strings.ReplaceAll(oldContent, oldStaticBaseUrl, newStaticBaseUrl)
|
newContent := oldContent
|
||||||
|
if organizationThemeCookie != nil {
|
||||||
|
newContent = strings.ReplaceAll(newContent, "https://cdn.casbin.org/img/favicon.png", organizationThemeCookie.Favicon)
|
||||||
|
newContent = strings.ReplaceAll(newContent, "<title>Casdoor</title>", fmt.Sprintf("<title>%s</title>", organizationThemeCookie.DisplayName))
|
||||||
|
}
|
||||||
|
|
||||||
|
newContent = strings.ReplaceAll(newContent, oldStaticBaseUrl, newStaticBaseUrl)
|
||||||
|
|
||||||
http.ServeContent(w, r, d.Name(), d.ModTime(), strings.NewReader(newContent))
|
http.ServeContent(w, r, d.Name(), d.ModTime(), strings.NewReader(newContent))
|
||||||
}
|
}
|
||||||
@@ -187,14 +193,14 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
|||||||
return w.Writer.Write(b)
|
return w.Writer.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeGzipResponse(w http.ResponseWriter, r *http.Request, path string) {
|
func makeGzipResponse(w http.ResponseWriter, r *http.Request, path string, organizationThemeCookie *OrganizationThemeCookie) {
|
||||||
if !enableGzip || !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
if !enableGzip || !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
serveFileWithReplace(w, r, path)
|
serveFileWithReplace(w, r, path, organizationThemeCookie)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Encoding", "gzip")
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
gz := gzip.NewWriter(w)
|
gz := gzip.NewWriter(w)
|
||||||
defer gz.Close()
|
defer gz.Close()
|
||||||
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
|
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
|
||||||
serveFileWithReplace(gzw, r, path)
|
serveFileWithReplace(gzw, r, path, organizationThemeCookie)
|
||||||
}
|
}
|
||||||
|
@@ -23,79 +23,112 @@ import (
|
|||||||
"github.com/casdoor/casdoor/object"
|
"github.com/casdoor/casdoor/object"
|
||||||
)
|
)
|
||||||
|
|
||||||
func appendThemeCookie(ctx *context.Context, urlPath string) error {
|
type OrganizationThemeCookie struct {
|
||||||
|
ThemeData *object.ThemeData
|
||||||
|
LogoUrl string
|
||||||
|
FooterHtml string
|
||||||
|
Favicon string
|
||||||
|
DisplayName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendThemeCookie(ctx *context.Context, urlPath string) (*OrganizationThemeCookie, error) {
|
||||||
if urlPath == "/login" {
|
if urlPath == "/login" {
|
||||||
application, err := object.GetDefaultApplication(fmt.Sprintf("admin/built-in"))
|
application, err := object.GetDefaultApplication(fmt.Sprintf("admin/built-in"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
|
||||||
if application.ThemeData != nil {
|
|
||||||
return setThemeDataCookie(ctx, application.ThemeData, application.Logo, application.FooterHtml)
|
|
||||||
}
|
}
|
||||||
organization := application.OrganizationObj
|
organization := application.OrganizationObj
|
||||||
if organization == nil {
|
if organization == nil {
|
||||||
organization, err = object.GetOrganization(fmt.Sprintf("admin/built-in"))
|
organization, err = object.GetOrganization(fmt.Sprintf("admin/built-in"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if organization != nil {
|
if organization != nil {
|
||||||
return setThemeDataCookie(ctx, organization.ThemeData, organization.Logo, application.FooterHtml)
|
organizationThemeCookie := &OrganizationThemeCookie{
|
||||||
|
application.ThemeData,
|
||||||
|
application.Logo,
|
||||||
|
application.FooterHtml,
|
||||||
|
organization.Favicon,
|
||||||
|
organization.DisplayName,
|
||||||
|
}
|
||||||
|
|
||||||
|
if application.ThemeData != nil {
|
||||||
|
organizationThemeCookie.ThemeData = organization.ThemeData
|
||||||
|
}
|
||||||
|
return organizationThemeCookie, setThemeDataCookie(ctx, organizationThemeCookie)
|
||||||
}
|
}
|
||||||
} else if strings.HasPrefix(urlPath, "/login/oauth/authorize") {
|
} else if strings.HasPrefix(urlPath, "/login/oauth/authorize") {
|
||||||
clientId := ctx.Input.Query("client_id")
|
clientId := ctx.Input.Query("client_id")
|
||||||
if clientId == "" {
|
if clientId == "" {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
application, err := object.GetApplicationByClientId(clientId)
|
application, err := object.GetApplicationByClientId(clientId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
if application != nil {
|
if application != nil {
|
||||||
organization, err := object.GetOrganization(fmt.Sprintf("admin/%s", application.Organization))
|
organization := application.OrganizationObj
|
||||||
if err != nil {
|
if organization == nil {
|
||||||
return err
|
organization, err = object.GetOrganization(fmt.Sprintf("admin/%s", application.Owner))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
organizationThemeCookie := &OrganizationThemeCookie{
|
||||||
|
application.ThemeData,
|
||||||
|
application.Logo,
|
||||||
|
application.FooterHtml,
|
||||||
|
organization.Favicon,
|
||||||
|
organization.DisplayName,
|
||||||
|
}
|
||||||
|
|
||||||
if application.ThemeData != nil {
|
if application.ThemeData != nil {
|
||||||
return setThemeDataCookie(ctx, application.ThemeData, application.Logo, application.FooterHtml)
|
organizationThemeCookie.ThemeData = organization.ThemeData
|
||||||
}
|
|
||||||
if organization != nil {
|
|
||||||
return setThemeDataCookie(ctx, organization.ThemeData, organization.Logo, application.FooterHtml)
|
|
||||||
}
|
}
|
||||||
|
return organizationThemeCookie, setThemeDataCookie(ctx, organizationThemeCookie)
|
||||||
}
|
}
|
||||||
} else if strings.HasPrefix(urlPath, "/login/") {
|
} else if strings.HasPrefix(urlPath, "/login/") {
|
||||||
owner := strings.Replace(urlPath, "/login/", "", -1)
|
owner := strings.Replace(urlPath, "/login/", "", -1)
|
||||||
if owner != "undefined" && owner != "oauth/undefined" {
|
if owner != "undefined" && owner != "oauth/undefined" {
|
||||||
application, err := object.GetDefaultApplication(fmt.Sprintf("admin/%s", owner))
|
application, err := object.GetDefaultApplication(fmt.Sprintf("admin/%s", owner))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
|
||||||
if application.ThemeData != nil {
|
|
||||||
return setThemeDataCookie(ctx, application.ThemeData, application.Logo, application.FooterHtml)
|
|
||||||
}
|
}
|
||||||
organization := application.OrganizationObj
|
organization := application.OrganizationObj
|
||||||
if organization == nil {
|
if organization == nil {
|
||||||
organization, err = object.GetOrganization(fmt.Sprintf("admin/%s", owner))
|
organization, err = object.GetOrganization(fmt.Sprintf("admin/%s", owner))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if organization != nil {
|
if organization != nil {
|
||||||
return setThemeDataCookie(ctx, organization.ThemeData, organization.Logo, application.FooterHtml)
|
organizationThemeCookie := &OrganizationThemeCookie{
|
||||||
|
application.ThemeData,
|
||||||
|
application.Logo,
|
||||||
|
application.FooterHtml,
|
||||||
|
organization.Favicon,
|
||||||
|
organization.DisplayName,
|
||||||
|
}
|
||||||
|
|
||||||
|
if application.ThemeData != nil {
|
||||||
|
organizationThemeCookie.ThemeData = organization.ThemeData
|
||||||
|
}
|
||||||
|
return organizationThemeCookie, setThemeDataCookie(ctx, organizationThemeCookie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setThemeDataCookie(ctx *context.Context, themeData *object.ThemeData, logoUrl string, footerHtml string) error {
|
func setThemeDataCookie(ctx *context.Context, organizationThemeCookie *OrganizationThemeCookie) error {
|
||||||
themeDataString, err := json.Marshal(themeData)
|
themeDataString, err := json.Marshal(organizationThemeCookie.ThemeData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx.SetCookie("organizationTheme", string(themeDataString))
|
ctx.SetCookie("organizationTheme", string(themeDataString))
|
||||||
ctx.SetCookie("organizationLogo", logoUrl)
|
ctx.SetCookie("organizationLogo", organizationThemeCookie.LogoUrl)
|
||||||
ctx.SetCookie("organizationFootHtml", footerHtml)
|
ctx.SetCookie("organizationFootHtml", organizationThemeCookie.FooterHtml)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user