mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +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:
parent
db551eb24a
commit
4b0a2fdbfc
@ -133,7 +133,7 @@ func StaticFilter(ctx *context.Context) {
|
||||
path += urlPath
|
||||
}
|
||||
|
||||
err := appendThemeCookie(ctx, urlPath)
|
||||
organizationThemeCookie, err := appendThemeCookie(ctx, urlPath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
@ -154,13 +154,13 @@ func StaticFilter(ctx *context.Context) {
|
||||
}
|
||||
|
||||
if oldStaticBaseUrl == newStaticBaseUrl {
|
||||
makeGzipResponse(ctx.ResponseWriter, ctx.Request, path)
|
||||
makeGzipResponse(ctx.ResponseWriter, ctx.Request, path, organizationThemeCookie)
|
||||
} 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))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -173,7 +173,13 @@ func serveFileWithReplace(w http.ResponseWriter, r *http.Request, name string) {
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
@ -187,14 +193,14 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
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") {
|
||||
serveFileWithReplace(w, r, path)
|
||||
serveFileWithReplace(w, r, path, organizationThemeCookie)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz := gzip.NewWriter(w)
|
||||
defer gz.Close()
|
||||
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"
|
||||
)
|
||||
|
||||
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" {
|
||||
application, err := object.GetDefaultApplication(fmt.Sprintf("admin/built-in"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if application.ThemeData != nil {
|
||||
return setThemeDataCookie(ctx, application.ThemeData, application.Logo, application.FooterHtml)
|
||||
return nil, err
|
||||
}
|
||||
organization := application.OrganizationObj
|
||||
if organization == nil {
|
||||
organization, err = object.GetOrganization(fmt.Sprintf("admin/built-in"))
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
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") {
|
||||
clientId := ctx.Input.Query("client_id")
|
||||
if clientId == "" {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
application, err := object.GetApplicationByClientId(clientId)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if application != nil {
|
||||
organization, err := object.GetOrganization(fmt.Sprintf("admin/%s", application.Organization))
|
||||
if err != nil {
|
||||
return err
|
||||
organization := application.OrganizationObj
|
||||
if organization == nil {
|
||||
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 {
|
||||
return setThemeDataCookie(ctx, application.ThemeData, application.Logo, application.FooterHtml)
|
||||
}
|
||||
if organization != nil {
|
||||
return setThemeDataCookie(ctx, organization.ThemeData, organization.Logo, application.FooterHtml)
|
||||
organizationThemeCookie.ThemeData = organization.ThemeData
|
||||
}
|
||||
return organizationThemeCookie, setThemeDataCookie(ctx, organizationThemeCookie)
|
||||
}
|
||||
} else if strings.HasPrefix(urlPath, "/login/") {
|
||||
owner := strings.Replace(urlPath, "/login/", "", -1)
|
||||
if owner != "undefined" && owner != "oauth/undefined" {
|
||||
application, err := object.GetDefaultApplication(fmt.Sprintf("admin/%s", owner))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if application.ThemeData != nil {
|
||||
return setThemeDataCookie(ctx, application.ThemeData, application.Logo, application.FooterHtml)
|
||||
return nil, err
|
||||
}
|
||||
organization := application.OrganizationObj
|
||||
if organization == nil {
|
||||
organization, err = object.GetOrganization(fmt.Sprintf("admin/%s", owner))
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
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 {
|
||||
themeDataString, err := json.Marshal(themeData)
|
||||
func setThemeDataCookie(ctx *context.Context, organizationThemeCookie *OrganizationThemeCookie) error {
|
||||
themeDataString, err := json.Marshal(organizationThemeCookie.ThemeData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.SetCookie("organizationTheme", string(themeDataString))
|
||||
ctx.SetCookie("organizationLogo", logoUrl)
|
||||
ctx.SetCookie("organizationFootHtml", footerHtml)
|
||||
ctx.SetCookie("organizationLogo", organizationThemeCookie.LogoUrl)
|
||||
ctx.SetCookie("organizationFootHtml", organizationThemeCookie.FooterHtml)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user