private void UpdateFormDefinitionWithActiveLocales(IUser user, XElement bindingsElement, XElement placeHolderElement)
        {
            var helper = new ActiveLocalesFormsHelper(
                GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocalesFieldLabel"),
                GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocalesMultiSelectLabel"),
                GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocalesMultiSelectHelp")
                );

            bindingsElement.Add(helper.GetBindingsMarkup());
            placeHolderElement.Add(helper.GetFormMarkup());

            helper.UpdateWithNewBindings(this.Bindings, UserSettings.GetActiveLocaleCultureInfos(user.Username));
        }
        private void saveCodeActivity_ExecuteCode(object sender, EventArgs e)
        {
            IUser user          = this.GetBinding <IUser>(BindingNames.User);
            var   userFormLogin = GetBinding <IUserFormLogin>(BindingNames.UserFormLogin);

            var userFormLoginFromDatabase = user.GetUserFormLogin();

            bool userValidated = true;

            ValidationResults validationResults = ValidationFacade.Validate(user);

            foreach (ValidationResult result in validationResults)
            {
                this.ShowFieldMessage($"{BindingNames.User}.{result.Key}", result.Message);
                userValidated = false;
            }


            List <CultureInfo> newActiveLocales     = ActiveLocalesFormsHelper.GetSelectedLocalesTypes(this.Bindings).ToList();
            List <CultureInfo> currentActiveLocales = null;
            CultureInfo        selectedActiveLocal  = null;

            if (newActiveLocales.Count > 0)
            {
                currentActiveLocales = UserSettings.GetActiveLocaleCultureInfos(user.Username).ToList();


                string selectedActiveLocaleName = (user.Username != UserSettings.Username ?
                                                   this.GetBinding <string>("ActiveLocaleName") :
                                                   UserSettings.ActiveLocaleCultureInfo.ToString());

                if (selectedActiveLocaleName != null)
                {
                    selectedActiveLocal = CultureInfo.CreateSpecificCulture(selectedActiveLocaleName);
                    if (!newActiveLocales.Contains(selectedActiveLocal))
                    {
                        if (user.Username != UserSettings.Username)
                        {
                            this.ShowFieldMessage("ActiveLocaleName", GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocaleNotChecked"));
                        }
                        else
                        {
                            this.ShowFieldMessage("ActiveLocalesFormsHelper_Selected", GetText("Website.Forms.Administrative.EditUserStep1.NoActiveLocaleSelected"));
                        }
                        userValidated = false;
                    }
                }
            }
            else
            {
                this.ShowFieldMessage("ActiveLocalesFormsHelper_Selected", GetText("Website.Forms.Administrative.EditUserStep1.NoActiveLocaleSelected"));
                userValidated = false;
            }


            string systemPerspectiveEntityToken = EntityTokenSerializer.Serialize(AttachingPoint.SystemPerspective.EntityToken);

            List <Guid>   newUserGroupIds            = UserGroupsFormsHelper.GetSelectedUserGroupIds(this.Bindings);
            List <string> newSerializedEnitityTokens = ActivePerspectiveFormsHelper.GetSelectedSerializedEntityTokens(this.Bindings).ToList();


            if (string.Compare(user.Username, UserSettings.Username, StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                // Current user shouldn't be able to lock itself
                if (userFormLogin.IsLocked)
                {
                    this.ShowMessage(DialogType.Message,
                                     Texts.EditUserWorkflow_EditErrorTitle,
                                     Texts.EditUserWorkflow_LockingOwnUserAccount);

                    userValidated = false;
                }

                // Current user shouldn't be able to remove its own access to "System" perspective
                var groupsWithAccessToSystemPerspective = new HashSet <Guid>(GetGroupsThatHasAccessToPerspective(systemPerspectiveEntityToken));

                if (!newSerializedEnitityTokens.Contains(systemPerspectiveEntityToken) &&
                    !newUserGroupIds.Any(groupsWithAccessToSystemPerspective.Contains))
                {
                    this.ShowMessage(DialogType.Message,
                                     Texts.EditUserWorkflow_EditErrorTitle,
                                     Texts.EditUserWorkflow_EditOwnAccessToSystemPerspective);

                    userValidated = false;
                }
            }

            string newPassword = this.GetBinding <string>(BindingNames.NewPassword);

            if (newPassword == NotPassword || UserFormLoginManager.ValidatePassword(userFormLoginFromDatabase, newPassword))
            {
                newPassword = null;
            }
            else
            {
                IList <string> validationMessages;
                if (!PasswordPolicyFacade.ValidatePassword(user, newPassword, out validationMessages))
                {
                    foreach (var message in validationMessages)
                    {
                        this.ShowFieldMessage(BindingNames.NewPassword, message);
                    }

                    userValidated = false;
                }
            }

            if (!userValidated)
            {
                return;
            }

            if (!userFormLogin.IsLocked)
            {
                userFormLogin.LockoutReason = (int)UserLockoutReason.Undefined;
            }
            else
            {
                bool wasLockedBefore = userFormLoginFromDatabase.IsLocked;

                if (!wasLockedBefore)
                {
                    userFormLoginFromDatabase.LockoutReason = (int)UserLockoutReason.LockedByAdministrator;
                }
            }

            UpdateTreeRefresher updateTreeRefresher = this.CreateUpdateTreeRefresher(this.EntityToken);

            bool reloadUsersConsoles = false;

            using (var transactionScope = TransactionsFacade.CreateNewScope())
            {
                DataFacade.Update(user);

                userFormLoginFromDatabase.Folder   = userFormLogin.Folder;
                userFormLoginFromDatabase.IsLocked = userFormLogin.IsLocked;
                DataFacade.Update(userFormLoginFromDatabase);

                if (newPassword != null)
                {
                    UserFormLoginManager.SetPassword(userFormLoginFromDatabase, newPassword);
                }

                string cultureName             = this.GetBinding <string>("CultureName");
                string c1ConsoleUiLanguageName = this.GetBinding <string>("C1ConsoleUiLanguageName");

                UserSettings.SetUserCultureInfo(user.Username, CultureInfo.CreateSpecificCulture(cultureName));
                UserSettings.SetUserC1ConsoleUiLanguage(user.Username, CultureInfo.CreateSpecificCulture(c1ConsoleUiLanguageName));

                List <string> existingSerializedEntityTokens = UserPerspectiveFacade.GetSerializedEntityTokens(user.Username).ToList();

                int intersectCount = existingSerializedEntityTokens.Intersect(newSerializedEnitityTokens).Count();
                if ((intersectCount != newSerializedEnitityTokens.Count) ||
                    (intersectCount != existingSerializedEntityTokens.Count))
                {
                    UserPerspectiveFacade.SetSerializedEntityTokens(user.Username, newSerializedEnitityTokens);

                    if (UserSettings.Username == user.Username)
                    {
                        reloadUsersConsoles = true;
                    }
                }

                if (DataLocalizationFacade.ActiveLocalizationCultures.Any())
                {
                    foreach (CultureInfo cultureInfo in newActiveLocales)
                    {
                        if (!currentActiveLocales.Contains(cultureInfo))
                        {
                            UserSettings.AddActiveLocaleCultureInfo(user.Username, cultureInfo);
                        }
                    }

                    foreach (CultureInfo cultureInfo in currentActiveLocales)
                    {
                        if (!newActiveLocales.Contains(cultureInfo))
                        {
                            UserSettings.RemoveActiveLocaleCultureInfo(user.Username, cultureInfo);
                        }
                    }

                    if (selectedActiveLocal != null)
                    {
                        if (!UserSettings.GetCurrentActiveLocaleCultureInfo(user.Username).Equals(selectedActiveLocal))
                        {
                            reloadUsersConsoles = true;
                        }

                        UserSettings.SetCurrentActiveLocaleCultureInfo(user.Username, selectedActiveLocal);
                    }
                    else if (UserSettings.GetActiveLocaleCultureInfos(user.Username).Any())
                    {
                        UserSettings.SetCurrentActiveLocaleCultureInfo(user.Username, UserSettings.GetActiveLocaleCultureInfos(user.Username).First());
                    }
                }


                List <IUserUserGroupRelation> oldRelations = DataFacade.GetData <IUserUserGroupRelation>(f => f.UserId == user.Id).ToList();

                IEnumerable <IUserUserGroupRelation> deleteRelations =
                    from r in oldRelations
                    where !newUserGroupIds.Contains(r.UserGroupId)
                    select r;

                DataFacade.Delete(deleteRelations);


                foreach (Guid newUserGroupId in newUserGroupIds)
                {
                    Guid groupId = newUserGroupId;
                    if (oldRelations.Any(f => f.UserGroupId == groupId))
                    {
                        continue;
                    }

                    var userUserGroupRelation = DataFacade.BuildNew <IUserUserGroupRelation>();
                    userUserGroupRelation.UserId      = user.Id;
                    userUserGroupRelation.UserGroupId = newUserGroupId;

                    DataFacade.AddNew(userUserGroupRelation);
                }

                LoggingService.LogEntry("UserManagement",
                                        $"C1 Console user '{user.Username}' updated by '{UserValidationFacade.GetUsername()}'.",
                                        LoggingService.Category.Audit,
                                        TraceEventType.Information);

                transactionScope.Complete();
            }

            if (reloadUsersConsoles)
            {
                foreach (string consoleId in GetConsoleIdsOpenedByCurrentUser())
                {
                    ConsoleMessageQueueFacade.Enqueue(new RebootConsoleMessageQueueItem(), consoleId);
                }
            }

            SetSaveStatus(true);
            updateTreeRefresher.PostRefreshMesseges(user.GetDataEntityToken());
        }
Exemplo n.º 3
0
        private void UpdateFormDefinitionWithActiveLocales(IUser user, XElement bindingsElement, XElement placeHolderElement)
        {
            var helper = new ActiveLocalesFormsHelper(
                    GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocalesFieldLabel"),
                    GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocalesMultiSelectLabel"),
                    GetText("Website.Forms.Administrative.EditUserStep1.ActiveLocalesMultiSelectHelp")
                );

            bindingsElement.Add(helper.GetBindingsMarkup());
            placeHolderElement.Add(helper.GetFormMarkup());

            helper.UpdateWithNewBindings(this.Bindings, UserSettings.GetActiveLocaleCultureInfos(user.Username));
        }