2014-02-26 16:01:31 +01:00
using System ;
using System.Collections.Generic ;
2014-04-02 11:20:35 +02:00
using System.Collections.Specialized ;
2014-03-04 19:20:36 +11:00
using System.Configuration ;
2016-05-25 14:36:00 +02:00
using System.Web ;
2014-02-26 16:01:31 +01:00
using System.Web.Security ;
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
2014-03-13 20:14:56 +11:00
using Umbraco.Core.Persistence ;
2014-02-26 16:01:31 +01:00
using Umbraco.Web.Install.Models ;
namespace Umbraco.Web.Install.InstallSteps
{
2014-03-05 14:30:17 +11:00
/// <summary>
/// This is the first UI step for a brand new install
/// </summary>
/// <remarks>
/// By default this will show the user view which is the most basic information to configure a new install, but if an install get's interupted because of an
/// error, etc... and the end-user refreshes the installer then we cannot show the user screen because they've already entered that information so instead we'll
/// display a simple continue installation view.
/// </remarks>
2014-03-04 19:20:36 +11:00
[ InstallSetupStep ( InstallationType . NewInstall ,
2014-03-05 20:03:45 +01:00
"User" , 20 , "" ) ]
2014-03-05 14:30:17 +11:00
internal class NewInstallStep : InstallSetupStep < UserModel >
2014-02-26 16:01:31 +01:00
{
2016-05-25 14:36:00 +02:00
private readonly HttpContextBase _http ;
2014-02-26 16:01:31 +01:00
private readonly ApplicationContext _applicationContext ;
2016-05-25 14:36:00 +02:00
public NewInstallStep ( HttpContextBase http , ApplicationContext applicationContext )
2014-02-26 16:01:31 +01:00
{
2016-05-25 14:36:00 +02:00
_http = http ;
2014-02-26 16:01:31 +01:00
_applicationContext = applicationContext ;
}
2017-07-20 12:53:09 +10:00
//TODO: Change all logic in this step to use ASP.NET Identity NOT MembershipProviders
2014-02-26 16:01:31 +01:00
private MembershipProvider CurrentProvider
{
get
{
2014-03-18 20:36:02 +11:00
var provider = Core . Security . MembershipProviderExtensions . GetUsersMembershipProvider ( ) ;
2014-02-26 16:01:31 +01:00
return provider ;
}
}
2014-03-04 11:16:42 +11:00
public override InstallSetupResult Execute ( UserModel user )
2014-02-26 16:01:31 +01:00
{
var admin = _applicationContext . Services . UserService . GetUserById ( 0 ) ;
if ( admin = = null )
{
throw new InvalidOperationException ( "Could not find the admi user!" ) ;
}
var membershipUser = CurrentProvider . GetUser ( 0 , true ) ;
if ( membershipUser = = null )
{
throw new InvalidOperationException ( "No user found in membership provider with id of 0" ) ;
}
try
{
var success = membershipUser . ChangePassword ( "default" , user . Password . Trim ( ) ) ;
if ( success = = false )
{
throw new FormatException ( "Password must be at least " + CurrentProvider . MinRequiredPasswordLength + " characters long and contain at least " + CurrentProvider . MinRequiredNonAlphanumericCharacters + " symbols" ) ;
}
}
catch ( Exception ex )
{
throw new FormatException ( "Password must be at least " + CurrentProvider . MinRequiredPasswordLength + " characters long and contain at least " + CurrentProvider . MinRequiredNonAlphanumericCharacters + " symbols" ) ;
}
admin . Email = user . Email . Trim ( ) ;
admin . Name = user . Name . Trim ( ) ;
admin . Username = user . Email . Trim ( ) ;
_applicationContext . Services . UserService . Save ( admin ) ;
2014-04-02 11:20:35 +02:00
if ( user . SubscribeToNewsLetter )
{
try
{
var client = new System . Net . WebClient ( ) ;
var values = new NameValueCollection { { "name" , admin . Name } , { "email" , admin . Email } } ;
2017-02-02 10:05:14 +01:00
client . UploadValues ( "https://shop.umbraco.com/base/Ecom/SubmitEmail/installer.aspx" , values ) ;
2014-04-02 11:20:35 +02:00
}
catch { /* fail in silence */ }
}
2014-02-26 16:01:31 +01:00
return null ;
}
2014-03-06 12:11:14 +11:00
/// <summary>
/// Return a custom view model for this step
/// </summary>
public override object ViewModel
{
get
{
2014-03-18 19:05:07 +11:00
var provider = Core . Security . MembershipProviderExtensions . GetUsersMembershipProvider ( ) ;
2014-03-06 12:11:14 +11:00
return new
{
2014-03-18 19:05:07 +11:00
minCharLength = provider . MinRequiredPasswordLength ,
minNonAlphaNumericLength = provider . MinRequiredNonAlphanumericCharacters
2014-03-06 12:11:14 +11:00
} ;
}
}
2014-02-27 10:16:30 +01:00
public override string View
{
2014-03-05 14:30:17 +11:00
get { return RequiresExecution ( null )
//the user UI
? "user"
//the continue install UI
: "continueinstall" ; }
2014-03-04 11:16:42 +11:00
}
2016-05-25 14:36:00 +02:00
2014-03-05 14:30:17 +11:00
public override bool RequiresExecution ( UserModel model )
2014-03-04 11:16:42 +11:00
{
2014-03-04 19:20:36 +11:00
//now we have to check if this is really a new install, the db might be configured and might contain data
2017-01-12 17:33:30 +11:00
var databaseSettings = ConfigurationManager . ConnectionStrings [ Constants . System . UmbracoConnectionName ] ;
2014-03-04 19:20:36 +11:00
2014-03-13 20:14:56 +11:00
//if there's already a version then there should def be a user but in some cases someone may have
// left a version number in there but cleared out their db conn string, in that case, it's really a new install.
if ( GlobalSettings . ConfigurationStatus . IsNullOrWhiteSpace ( ) = = false & & databaseSettings ! = null ) return false ;
2014-03-04 19:20:36 +11:00
if ( _applicationContext . DatabaseContext . IsConnectionStringConfigured ( databaseSettings )
& & _applicationContext . DatabaseContext . IsDatabaseConfigured )
{
//check if we have the default user configured already
var result = _applicationContext . DatabaseContext . Database . ExecuteScalar < int > (
"SELECT COUNT(*) FROM umbracoUser WHERE id=0 AND userPassword='default'" ) ;
if ( result = = 1 )
{
//the user has not been configured
return true ;
}
return false ;
}
else
{
2016-05-25 14:36:00 +02:00
// In this one case when it's a brand new install and nothing has been configured, make sure the
// back office cookie is cleared so there's no old cookies lying around causing problems
_http . ExpireCookie ( UmbracoConfig . For . UmbracoSettings ( ) . Security . AuthCookieName ) ;
2014-03-04 19:20:36 +11:00
return true ;
}
2014-02-27 10:16:30 +01:00
}
2014-02-26 16:01:31 +01:00
}
2017-02-02 10:05:14 +01:00
}