serenity-is / Serenity

Business Apps Made Simple with Asp.Net Core MVC / TypeScript
https://serenity.is
MIT License
2.59k stars 795 forks source link

Dynamic logo #3360

Closed postonoh closed 6 years ago

postonoh commented 6 years ago

Trying setup dynamic logos'

I created a logo table, columns are ID, LogoImage, TenantId

I can get the logo to upload, now I need to replace the logo and the favicon.

I tried duplicate some of the code on the _Layout.cshtml page

edwardch commented 6 years ago

A little more info would help, but try to take a look at how it is implemented on the User table in the demo...

postonoh commented 6 years ago

I have multiple clients my logo will all ways be the default logo on the sign on page . A couple of my clients would like to see their logo to display once the logged in to application. In the left corner and in favicon.

postonoh commented 6 years ago

I have the following setup to work multi-tenant. I can sign on and each logo image upload to that company. That's working I need to modify the _Layout page pull from the database base on tenant id and user.

gfo2007 commented 6 years ago

@postonoh, do share the code when a solution is found. I have been thinking through this logo thing for different tenants to no avail.

postonoh commented 6 years ago

gfo2007 I am working let you know by the end of the week. I believe I should have it complete.

postonoh commented 6 years ago

Her my solutions: I first created logo column on my clients Tables Then I added the following to _Layout.cshtml page

`@{ Func<string, IHtmlString> json = x => new HtmlString(Serenity.JSON.Stringify(x)); var hideNav = Request["hideNav"] == "1"; var logged = Serenity.Authorization.Username; var themeCookie = Request.Cookies["ThemePreference"]; var theme = themeCookie != null && !themeCookie.Value.IsEmptyOrNull() ? themeCookie.Value : "blue"; var rtl = System.Globalization.CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft; var user = (UserDefinition)Serenity.Authorization.UserDefinition; var logo = ""; var clientlogo = LogoRetrievesServices.RetrieveClientLogo(logo);

var userImage = (user == null || string.IsNullOrEmpty(user.UserImage)) ? "whatever is you default userimage"
    VirtualPathUtility.ToAbsolute("~/upload/") + user.UserImage;

var logoImage = (clientlogo == null || string.IsNullOrEmpty(clientlogo )) ? "whatever is you default logo" :
    VirtualPathUtility.ToAbsolute("~/upload/") + clientlogo ;
}
@if (IsSectionDefined("ContentHeader")) {
@RenderSection("ContentHeader")
}
@RenderBody()
@Texts.Site.Layout.FooterCopyright @Texts.Site.Layout.FooterRights

} `

You can add this code class with name space of the root.

` **public static string RetrieveClientLogo(string logo) { var user = (UserDefinition)Serenity.Authorization.UserDefinition;

        using (var connection = SqlConnections.NewByKey("Default"))
            {
               var result = connection.Single<ClientRow>(q => q
                        .Where(new Criteria(ClientRow.Fields.TenantId) == user.TenantId)
                         .Select(ClientRow.Fields.Logo));
                return result.Logo;
            }
    }**
@if (IsSectionDefined("ContentHeader")) {
@RenderSection("ContentHeader")
}
@RenderBody()
@Texts.Site.Layout.FooterCopyright @Texts.Site.Layout.FooterRights

}

`

I hope this helps.

postonoh commented 6 years ago

Fixed add the to check for null Change from using Single to TrySingle method check to see id data exist if returns null.

`public static string logoImagePath = ""; public static string RetrieveClientLogo(string logo) { var user = (UserDefinition)Serenity.Authorization.UserDefinition;

        using (var connection = SqlConnections.NewByKey("Default"))
            {
               var result = connection.TrySingle<LogoRow>(q => q
                        .Where(new Criteria(LogoRow.Fields.TenantId) == user.TenantId)
                        .Select(LogoRow.Fields.Logo));

            if(result != null)
                {
                  return result.Logo;
                }
            return logoImagePath;
           } 
    }`