Ejemplo n.º 1
0
        internal void Process(HttpRequestArgs args)
        {
            if (Done)
            {
                return;
            }

            lock (SyncRoot)
            {
                if (Done)
                {
                    return;
                }

                new Thread(() =>
                {
                    try
                    {
                        Event.RaiseEvent(SitecoreStarted.EventName, new EventArgs());
                    }
                    catch (Exception ex)
                    {
                        FridayLog.Error(SitecoreStarted.FeatureName, $"Failed to process {SitecoreStarted.EventName} event", ex);
                    }
                }).Start();

                Done = true;
            }
        }
Ejemplo n.º 2
0
        private void InstallPackage(FileInfo file)
        {
            // in case file disappeared since retrieved
            file.Refresh();
            if (!file.Exists)
            {
                return;
            }

            FridayLog.Info(AutoPackages.FeatureName, $"Install auto package (Skip on conflicts): {file.FullName}");

            using (new SecurityDisabler())
            {
                var context = new SimpleProcessingContext();
                // IItemInstallerEvents type cast is essential
                var items = (IItemInstallerEvents) new DefaultItemInstallerEvents(new BehaviourOptions(InstallMode.Skip, MergeMode.Undefined));
                context.AddAspect(items);

                // IFileInstallerEvents type cast is essential
                var files = (IFileInstallerEvents) new DefaultFileInstallerEvents(false);
                context.AddAspect(files);

                new Installer().InstallPackage(file.FullName, context);
                new Installer().InstallSecurity(file.FullName, context);

                var previewContext = Installer.CreatePreviewContext();
                var packageReader  = new PackageReader(file.FullName);
                var view           = new MetadataView(previewContext);
                var metadataSink   = new MetadataSink(view);
                metadataSink.Initialize(previewContext);
                packageReader.Populate(metadataSink);

                new Installer().ExecutePostStep(view.PostStep, previewContext);
            }
        }
Ejemplo n.º 3
0
        internal void Process(HttpRequestArgs args)
        {
            if (Done)
            {
                return;
            }

            lock (SyncRoot)
            {
                if (Done)
                {
                    return;
                }

                Timer.Stop();
                FridayLog.Info(SitecoreStarted.FeatureName, $"Sitecore is up and serving requests. Elapsed: \"{Timer.Elapsed}\", First Request: \"{HttpContext.Current.Request.RawUrl}\"");

                new Thread(
                    () =>
                {
                    try
                    {
                        Event.RaiseEvent(SitecoreStarted.EventName, new EventArgs());
                    }
                    catch (Exception ex)
                    {
                        FridayLog.Error(SitecoreStarted.FeatureName, $"Failed to process \"{SitecoreStarted.EventName}\" event", ex);
                    }
                }).Start();

                Done = true;
            }
        }
Ejemplo n.º 4
0
        private void DoWork(object args)
        {
            try
            {
                var loginFilePath = HostingEnvironment.MapPath("/sitecore/login/default.aspx") ?? throw new ArgumentNullException();
                if (!File.Exists(loginFilePath))
                {
                    return;
                }

                var lines          = File.ReadAllLines(loginFilePath);
                var signupFilePath = HostingEnvironment.MapPath("/sitecore/signup/default.aspx");
                if (SignUpRules.Enabled)
                {
                    InjectLink(loginFilePath, lines);
                    DeployPage(signupFilePath);
                }
                else
                {
                    RemoveInjectedLink(loginFilePath, lines);
                    RemovePage(signupFilePath);
                }
            }
            catch (Exception ex)
            {
                FridayLog.Error(SignUpRules.FeatureName, "Failed to modify login page or bootstrap /sitecore/signup page", ex);
            }
        }
Ejemplo n.º 5
0
        protected void SignUpClick(object sender, EventArgs e)
        {
            FailureHolder.Visible = false;
            SuccessHolder.Visible = false;

            Validate();
            if (!IsValid)
            {
                RenderError("The provided data is not valid.", false);

                return;
            }

            var email  = Email.Text.Trim();
            var name   = UserName.Text.Trim();
            var secret = Secret.Text.Trim();

            try
            {
                DoSignUp(name, email, secret);
            }
            catch (Exception ex)
            {
                var error = "Failed to create user account, " +
                            $"UserName: \"{name}\", " +
                            $"Email: \"{email}\"";

                FridayLog.Error(SignUpRules.FeatureName, error, ex);

                RenderError($"Failed to create user account, contact your administrator with this reference: {Settings.InstanceName}-{DateTime.Now:yyMMddhhmmss}", true);
            }
        }
Ejemplo n.º 6
0
 private void RemovePage(string signupFilePath)
 {
     if (File.Exists(signupFilePath))
     {
         FridayLog.Info(SignUpRules.FeatureName, "Remove /sitecore/signup page");
         File.Delete(signupFilePath);
     }
 }
Ejemplo n.º 7
0
 internal void Process(PipelineArgs args)
 {
     // AutoPackages.Enabled throws exception when misconfigured
     // this is the main purpose of this processor - not to let
     // Sitecore start successfully when feature is misconfigured
     if (!AutoPackages.Enabled)
     {
         FridayLog.Info(AutoPackages.FeatureName, "Feature is disabled. Refer to the corresponding configuration file for instructions.");
     }
 }
Ejemplo n.º 8
0
        internal void DoWork()
        {
            var directory = AutoPackages.Directory;

            FridayLog.Info(AutoPackages.FeatureName, $"Install auto packages in folder: {directory.FullName}");

            Start(directory);

            FridayLog.Info(AutoPackages.FeatureName, $"All auto packages were installed.");
        }
Ejemplo n.º 9
0
        public void Process(PipelineArgs args)
        {
            if (!AccountResetRules.Enabled)
            {
                FridayLog.Info(AccountResetRules.FeatureName, $"Feature is disabled.");

                return;
            }

            ThreadPool.QueueUserWorkItem(DoWork);
        }
Ejemplo n.º 10
0
        private void RemoveInjectedLink(string loginFilePath, string[] lines)
        {
            for (var i = 0; i < lines.Length - 1; ++i)
            {
                var line = lines[i];
                if (line.Contains(Code))
                {
                    lines[i] = line.Replace(Code, "");

                    FridayLog.Info(SignUpRules.FeatureName, "Remove a link to /sitecore/signup page to /sitecore/login/default.aspx");
                    File.WriteAllLines(loginFilePath, lines);

                    return;
                }
            }
        }
Ejemplo n.º 11
0
        internal void Process(PipelineArgs args)
        {
            try
            {
                if (ConfigExtensions.Enabled)
                {
                    return;
                }

                FridayLog.Info(ConfigExtensions.FeatureName, "Feature is disabled. Refer to the corresponding configuration file for instructions.");
            }
            catch (Exception ex)
            {
                throw new ConfigurationException($"The {ConfigExtensions.FeatureName} extension is configured incorrectly. See inner exception for details.", ex);
            }
        }
Ejemplo n.º 12
0
        internal static User CreateUserAccount(this MembershipProvider provider, string username, string email, bool isAdministrator, IReadOnlyList <string> roles, string feature)
        {
            var password = Membership.GeneratePassword(provider.MinRequiredPasswordLength, provider.MinRequiredNonAlphanumericCharacters);
            var user     = User.Create(username, password);

            try
            {
                var profile = user.Profile;
                profile.IsAdministrator = isAdministrator;
                profile.Email           = email;
                profile.Save();

                // update roles
                foreach (var role in roles)
                {
                    user.Roles.Add(Role.FromName(role));
                }

                var message = "Create user account, " +
                              $"UserName: \"{username}\", " +
                              $"IsAdministrator: {isAdministrator}, " +
                              $"Roles: \"{string.Join(", ", roles)}\", " +
                              $"Email: \"{email}\"";

                FridayLog.Audit(feature, message);

                return(user);
            }
            catch
            {
                try
                {
                    provider.DeleteUser(username, true);
                }
                catch (Exception ex)
                {
                    var error = "Failed to delete incomplete user account, " +
                                $"UserName: \"{username}\"";

                    FridayLog.Error(feature, error, ex);
                }

                throw;
            }
        }
Ejemplo n.º 13
0
        public void Process(PipelineArgs args)
        {
            if (!AccountResetRules.Enabled)
            {
                FridayLog.Info(AccountResetRules.FeatureName, "Feature is disabled. Refer to the corresponding configuration file for instructions.");

                return;
            }

            if (!IsAsync)
            {
                DoWork(null);

                return;
            }

            ThreadPool.QueueUserWorkItem(DoWork);
        }
Ejemplo n.º 14
0
        private void DoWork(object args)
        {
            foreach (var account in AccountResetRules.Accounts)
            {
                var username = account.Name;

                try
                {
                    var user = MembershipProvider.GetUser(username, false);
                    if (user == null)
                    {
                        MembershipProvider.CreateUserAccount(username, "", true, new string[0], AccountResetRules.FeatureName);
                        user = MembershipProvider.GetUser(username, false)
                               ?? throw new InvalidOperationException($"Failed to find user after creating, UserName: {username}");
                    }

                    // first change password and only then it is safe to unlock
                    var password = MembershipProvider.ResetPassword(username, null);
                    if (user.IsLockedOut)
                    {
                        user.UnlockUser();
                    }

                    var desiredPassword = account.Password;
                    if (!string.IsNullOrEmpty(desiredPassword))
                    {
                        try
                        {
                            user.ChangePassword(password, desiredPassword);

                            FridayLog.Info(AccountResetRules.FeatureName, $"User account was reset, UserName: {username}, Password: {desiredPassword}, IsLockedOut: false");
                        }
                        catch (Exception ex)
                        {
                            FridayLog.Error(AccountResetRules.FeatureName, $"User account was reset, but failed to change password to desired one, UserName: {username}, CurrentPassword: {password}, DesiredPassword: {desiredPassword} IsLockedOut: false", ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    FridayLog.Error(AccountResetRules.FeatureName, $"Failed to reset user account, UserName: {username}", ex);
                }
            }
        }
Ejemplo n.º 15
0
        internal void Process(PipelineArgs args)
        {
            try
            {
                var domains = SignUpRules.Rules;
                if (domains.Count == 0)
                {
                    FridayLog.Info(SignUpRules.FeatureName, "Feature is disabled. Refer to the corresponding configuration file for instructions.");

                    return;
                }

                if (string.IsNullOrEmpty(Settings.MailServer))
                {
                    FridayLog.Info(SignUpRules.FeatureName, "Feature is disabled (mail server is not configured).");

                    return;
                }

                foreach (var domain in domains)
                {
                    var name            = domain.Domain;
                    var roles           = string.Join(", ", domain.Roles);
                    var secret          = domain.Secret;
                    var isAdministrator = domain.IsAdministrator;

                    FridayLog.Info(
                        SignUpRules.FeatureName,
                        $"Enable sign up rule, " +
                        $"Domain: \"{name}\", " +
                        $"IsAdministrator: {isAdministrator}, " +
                        $"Roles: \"{roles}\", " +
                        $"Secret: \"{secret}\", " +
                        $"Profile: \"/sitecore/system/Settings/Security/Profiles/User\"");
                }
            }
            catch (Exception ex)
            {
                throw new ConfigurationException($"The {SignUpRules.FeatureName} extension is configured incorrectly. See inner exception for details.", ex);
            }
        }
Ejemplo n.º 16
0
        private void InjectLink(string loginFilePath, string[] lines)
        {
            for (var i = 0; i < lines.Length - 1; ++i)
            {
                var line = lines[i];
                if (line.Contains("ID=\"forgotPasswordLinkMessage\""))
                {
                    var next = lines[i + 1];
                    if (next.Contains("new-user-account"))
                    {
                        return;
                    }

                    lines[i + 1] = next + Code;

                    FridayLog.Info(SignUpRules.FeatureName, "Inject a link to /sitecore/signup page to /sitecore/login/default.aspx");
                    File.WriteAllLines(loginFilePath, lines);

                    return;
                }
            }
        }
Ejemplo n.º 17
0
        private void InstallPackage(FileInfo file)
        {
            // in case file disappeared since retrieved
            file.Refresh();
            if (!file.Exists)
            {
                return;
            }

            var filenameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name);

            filenameWithoutExtension = ItemUtil.ProposeValidItemName(filenameWithoutExtension);
            Assert.IsTrue(!string.IsNullOrWhiteSpace(filenameWithoutExtension), "The package should be renamed to something that item can have name of: " + file.Name);

            using (new SecurityDisabler())
            {
                var db          = Factory.GetDatabase("core");
                var historyItem = db.GetItem("/sitecore/system/Packages/Installation history");
                if (historyItem.Children.Any(x => string.Equals(x.Name, filenameWithoutExtension, StringComparison.OrdinalIgnoreCase)))
                {
                    FridayLog.Info(AutoPackages.FeatureName, $"Install auto package skipped (already installed): \"{file.FullName}\"");

                    return;
                }
            }

            // The way how SC creates history item it cumbersome - it has some non-obvious length limitations
            // so add extra logic to work around it.
            var historyFile = new FileInfo(file.FullName + ".history.txt");

            if (historyFile.Exists)
            {
                FridayLog.Info(AutoPackages.FeatureName, $"Install auto package skipped (already installed): \"{file.FullName}\"");

                return;
            }

            File.AppendAllText(historyFile.FullName, DateTime.UtcNow.ToString("o") + "\r\n");

            FridayLog.Info(AutoPackages.FeatureName, $"Install auto package (Skip on conflicts): \"{file.FullName}\"");

            using (new SecurityDisabler())
            {
                var context = new SimpleProcessingContext();
                // IItemInstallerEvents type cast is essential
                var items = (IItemInstallerEvents) new DefaultItemInstallerEvents(new BehaviourOptions(InstallMode.Overwrite, MergeMode.Undefined));
                context.AddAspect(items);

                // IFileInstallerEvents type cast is essential
                var files = (IFileInstallerEvents) new DefaultFileInstallerEvents(true);
                context.AddAspect(files);

                new Installer().InstallPackage(file.FullName, context);
                new Installer().InstallSecurity(file.FullName, context);

                var previewContext = Installer.CreatePreviewContext();
                var packageReader  = new PackageReader(file.FullName);
                var view           = new MetadataView(previewContext);
                var metadataSink   = new MetadataSink(view);
                metadataSink.Initialize(previewContext);
                packageReader.Populate(metadataSink);

                new Installer().ExecutePostStep(view.PostStep, previewContext);
            }
        }
Ejemplo n.º 18
0
 public static void Initialize()
 {
     Timer.Start();
     FridayLog.Info(SitecoreStarted.FeatureName, $"Sitecore is initializing...");
 }
Ejemplo n.º 19
0
        internal void LogMeasurements(object obj, EventArgs args)
        {
            Timer.Stop();

            FridayLog.Info(SitecoreStarted.FeatureName, $"Sitecore is up and serving requests. Elapsed: {Timer.Elapsed}");
        }
Ejemplo n.º 20
0
        private void DoWork(object args)
        {
            foreach (var account in AccountResetRules.Accounts)
            {
                var username = account.Name;

                try
                {
                    var user = MembershipProvider.GetUser(username, false);
                    if (user == null)
                    {
                        MembershipProvider.CreateUserAccount(username, "", true, new string[0], AccountResetRules.FeatureName);

                        var error = "Failed to find user after creating, " +
                                    $"UserName: {username}";

                        user = MembershipProvider.GetUser(username, false) ?? throw new InvalidOperationException(error);
                    }

                    // first change password and only then it is safe to unlock
                    var password = MembershipProvider.ResetPassword(username, null);
                    if (user.IsLockedOut)
                    {
                        user.UnlockUser();
                    }

                    var desiredPassword = account.Password;
                    if (!string.IsNullOrEmpty(desiredPassword))
                    {
                        try
                        {
                            Assert.IsTrue(user.ChangePassword(password, desiredPassword), "Membership provider rejected password change request.");
                        }
                        catch (Exception ex)
                        {
                            var error = "User account was reset, but failed to change password to desired one, " +
                                        $"UserName: \"{username}\", " +
                                        $"CurrentPassword: \"{password}\", " +
                                        $"DesiredPassword: \"{desiredPassword}\", " +
                                        "IsLockedOut: false";

                            FridayLog.Error(AccountResetRules.FeatureName, error, ex);

                            continue;
                        }
                    }

                    var message = "User account was reset, " +
                                  $"UserName: \"{username}\", " +
                                  $"Password: \"{(account.WritePasswordToLog ? desiredPassword ?? password : "******")}\", " +
                                  "IsLockedOut: false";

                    FridayLog.Info(AccountResetRules.FeatureName, message);

                    var recepients = account.EmailPasswordToRecepients;
                    if (recepients.Any())
                    {
                        Helper.SendMailMessageAsync(user, desiredPassword ?? password, recepients);
                    }
                }
                catch (Exception ex)
                {
                    var error = "Failed to reset user account, " +
                                $"UserName: \"{username}\"";

                    FridayLog.Error(AccountResetRules.FeatureName, error, ex);
                }
            }
        }