Auto-Provision step results in setup screen

Topics: Troubleshooting, Writing modules
Mar 19, 2014 at 4:32 PM
Hoping someone has a suggestion for me...

I'm working on a project where I need to auto-provision tenants. I'm currently testing this in 1.x. I have the tenant and website setup working perfectly. What I'm trying to do now is to add on to the end of process and logout the superuser that is created when the setup process runs.

The reason I want to do this, is because I'm defaulting the superuser account to one that I know and will be setting up a different account for the user creating the tenant to use that doesn't have as many privileges.

When you run the setup method in Orchard.Setup, the superuser that is created is automatically logged in. So, I'm trying to log that user out.

What happens when I access the tenant after setup has occurred, is that the new tenant url responds with the setup page...even thought the tenant has already been setup.

Here is the code I'm using to access the tenant:
        private void SetupUser(string Url) {
            var tenant = _tenantService.GetTenants().Where(x => x.Name == Url).FirstOrDefault();

            if (tenant != null) {
                tenant.State = TenantState.Running;

                using (var environment = _orchardHost.CreateStandaloneEnvironment(tenant)) {
                    //var authenticationService = environment.Resolve<IAuthenticationService>();
                    //var membershipService = environment.Resolve<IMembershipService>();

                    //authenticationService.SignOut();
                }

                _shellSettingsManager.SaveSettings(tenant);
            }
        }
I read the comments in Orchard.Setup that you should set the tenant state to running to prevent exactly what seems to be happening for me, but it still seems to be doing it. As you can see, I commented out any actions...so I'm simply accessing the newly created tenant. If I remove this code, I don't get the setup page for the new tenant, however, the superuser is then logged in which I don't want.

Any possible suggestions on something I may have missed or a way to get the desired functionality? Thanks =)
Mar 19, 2014 at 4:38 PM
after reading over my post and code, I moved the SaveSettings line inside the using statement, but it didn't make a difference.

Note that after I do this, if I go into admin under the default tenant and edit/save the tenant that was just created, it behaves normally and takes me to the home page.

So I'm pretty sure it's my accessing of the tenant after setup that is causing this.
Mar 28, 2014 at 12:03 AM
Argh...still struggling with this.

I discovered that it's not my accessing of the tenant after setup...it's the actual setup itself that is leaving the tenant in setup mode.

Here's the weird thing. If I create a tenant and run the setup on it via code, it gets left in setup mode. If I don't do anything with that tenant, but create a new tenant, the new tenant is then left in setup mode, but now the first tenant is accessible as if setup has completed.

So...here's the code I'm using to automatically setup a tenant after it's been created.
        private void SetupTenant(ProvisionViewModel viewModel) {
            var tenant = _tenantService.GetTenants().Where(x => x.Name == viewModel.SiteAddress).FirstOrDefault();

            if (tenant != null) {
                using (var environment = _orchardHost.CreateStandaloneEnvironment(tenant)) {
                    var setupService = environment.Resolve<ISetupService>();

                    setupService.Setup(new SetupContext {
                        SiteName = viewModel.SiteTitle,
                        AdminUsername = "admin",
                        AdminPassword = "password",
                        DatabaseProvider = _shellSettings.DataProvider,
                        DatabaseConnectionString = _shellSettings.DataConnectionString,
                        DatabaseTablePrefix = viewModel.SiteAddress.ToLowerInvariant(),
                        EnabledFeatures = null, // default list
                        Recipe = "Tenant"
                    });
                }

                _shellSettingsManager.SaveSettings(tenant);
            }
        }
Does anyone have suggestions on how I might setup a tenant via code so that it's not left in setup mode?

Thanks for any help
Apr 10, 2014 at 8:13 PM
Figured this out with the help of the guys on orchardpros.net. The issue appears to have been doing too much in a single post. Creating the tenant, setting up the tenant. and then attempting to modify roles and users on the new tenant kicked the tenant back into setup mode for some reason. Splitting out that process so that the first post creates and sets the tenant up, then the second post creates the role/permissions/user and assigns the user to the role seems to have resolved the issue.
Marked as answer by psenechal on 4/10/2014 at 12:13 PM