From 94fee29d1aa86085d3d572892c97bf91eee177a2 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Fri, 15 Oct 2010 14:33:02 -0700 Subject: [PATCH] Authenticate admin user during setup phase This is so that we don't need code all over the place to set the Owner of the CommonPart. The CommonPart will have direct access to the user. --HG-- branch : dev --- .../Modules/Orchard.Setup/Services/SetupService.cs | 7 +++++-- src/Orchard/Environment/DefaultWorkContextAccessor.cs | 2 -- src/Orchard/Security/IAuthenticationService.cs | 1 + .../Security/Providers/FormsAuthenticationService.cs | 10 ++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Setup/Services/SetupService.cs b/src/Orchard.Web/Modules/Orchard.Setup/Services/SetupService.cs index ed80805d2..6751916c4 100644 --- a/src/Orchard.Web/Modules/Orchard.Setup/Services/SetupService.cs +++ b/src/Orchard.Web/Modules/Orchard.Setup/Services/SetupService.cs @@ -170,6 +170,10 @@ namespace Orchard.Setup.Services { String.Empty, String.Empty, String.Empty, true)); + + var authenticationService = environment.Resolve(); + authenticationService.SetAuthenticatedUserForRequest(user); + // set site name and settings var siteService = environment.Resolve(); var siteSettings = siteService.GetSiteSettings().As(); @@ -265,9 +269,8 @@ namespace Orchard.Setup.Services { menuItem.As().OnMainMenu = true; menuItem.As().Url = ""; - //Temporary fix for running setup on command line + //null check: temporary fix for running setup in command line if (HttpContext.Current != null) { - var authenticationService = environment.Resolve(); authenticationService.SignIn(user, true); } } diff --git a/src/Orchard/Environment/DefaultWorkContextAccessor.cs b/src/Orchard/Environment/DefaultWorkContextAccessor.cs index 767444806..a507cfcdd 100644 --- a/src/Orchard/Environment/DefaultWorkContextAccessor.cs +++ b/src/Orchard/Environment/DefaultWorkContextAccessor.cs @@ -163,6 +163,4 @@ namespace Orchard.Environment { } } } - - } diff --git a/src/Orchard/Security/IAuthenticationService.cs b/src/Orchard/Security/IAuthenticationService.cs index e4af94abf..9edcda2b9 100644 --- a/src/Orchard/Security/IAuthenticationService.cs +++ b/src/Orchard/Security/IAuthenticationService.cs @@ -2,6 +2,7 @@ public interface IAuthenticationService : IDependency { void SignIn(IUser user, bool createPersistentCookie); void SignOut(); + void SetAuthenticatedUserForRequest(IUser user); IUser GetAuthenticatedUser(); } } diff --git a/src/Orchard/Security/Providers/FormsAuthenticationService.cs b/src/Orchard/Security/Providers/FormsAuthenticationService.cs index 37f7f0107..3f2b2cf6c 100644 --- a/src/Orchard/Security/Providers/FormsAuthenticationService.cs +++ b/src/Orchard/Security/Providers/FormsAuthenticationService.cs @@ -11,6 +11,7 @@ namespace Orchard.Security.Providers { private readonly IClock _clock; private readonly IContentManager _contentManager; private readonly IHttpContextAccessor _httpContextAccessor; + private IUser _signedInUser; public FormsAuthenticationService(IClock clock, IContentManager contentManager, IHttpContextAccessor httpContextAccessor) { _clock = clock; @@ -52,13 +53,22 @@ namespace Orchard.Security.Providers { var httpContext = _httpContextAccessor.Current(); httpContext.Response.Cookies.Add(cookie); + _signedInUser = user; } public void SignOut() { + _signedInUser = null; FormsAuthentication.SignOut(); } + public void SetAuthenticatedUserForRequest(IUser user) { + _signedInUser = user; + } + public IUser GetAuthenticatedUser() { + if (_signedInUser != null) + return _signedInUser; + var httpContext = _httpContextAccessor.Current(); if (!httpContext.Request.IsAuthenticated || !(httpContext.User.Identity is FormsIdentity)) { return null;