/// <summary> /// Updates database representation of given form field /// </summary> /// <param name="oldFieldInfo">Form field prior to the change</param> /// <param name="updatedFieldInfo">Form field after the change has been made.</param> /// <param name="tm">Table manager allowing the database changes.</param> /// <param name="tableName">Name of the table to be changed.</param> /// <returns>Possible error message</returns> protected string UpdateDatabaseColumn(FormFieldInfo oldFieldInfo, FormFieldInfo updatedFieldInfo, TableManager tm, string tableName) { if (updatedFieldInfo.External) { if (!oldFieldInfo.External) { tm.DropTableColumn(tableName, oldFieldInfo.Name); } } else { // Validate the default value string errorMessage; string newDBDefaultValue = GetDefaultValueInDBCulture(updatedFieldInfo, out errorMessage); if (!String.IsNullOrEmpty(errorMessage)) { return(errorMessage); } // Set column type and size string newColumnType = DataTypeManager.GetSqlType(updatedFieldInfo.DataType, updatedFieldInfo.Size, updatedFieldInfo.Precision); string oldColumnName = oldFieldInfo.Name; string newColumnName = updatedFieldInfo.Name; if (oldFieldInfo.External) { tm.AddTableColumn(tableName, newColumnName, newColumnType, updatedFieldInfo.AllowEmpty, newDBDefaultValue); } else { // Change table column tm.AlterTableColumn(tableName, oldColumnName, newColumnName, newColumnType, updatedFieldInfo.AllowEmpty, newDBDefaultValue); } } return(null); }
private void EnsureCmsUserAzureCustomField() { var cmsUserDataClass = DataClassInfoProvider.GetDataClassInfo("cms.user"); if (cmsUserDataClass == null) { return; } var formInfo = new FormInfo(cmsUserDataClass.ClassFormDefinition); if (formInfo.FieldExists("AzureADUsername")) { EventLogProvider.LogInformation("AzureADAuthentication", "Skip Create Field", "AzureADUsername"); return; } // Create "AzureADUsername" field if it doesn't exist EventLogProvider.LogInformation("AzureADAuthentication", "Create Field", "AzureADUsername"); var azureAdUsernameTextField = new FormFieldInfo { Name = "AzureADUsername", DataType = "text", Size = 200, Precision = -1, AllowEmpty = true, DefaultValue = string.Empty, System = false, FieldType = FormFieldControlTypeEnum.TextBoxControl, Visible = true, Caption = "Azure AD Username", Enabled = true }; using (var tr = new CMSLateBoundTransaction()) { var tm = new TableManager(cmsUserDataClass.ClassConnectionString); tr.BeginTransaction(); var newFieldHandler = (AbstractAdvancedHandler)null; try { newFieldHandler = DataDefinitionItemEvents.AddItem.StartEvent(cmsUserDataClass, azureAdUsernameTextField); var sqlType = DataTypeManager.GetSqlType(azureAdUsernameTextField.DataType, azureAdUsernameTextField.Size, azureAdUsernameTextField.Precision); tm.AddTableColumn(cmsUserDataClass.ClassTableName, azureAdUsernameTextField.Name, sqlType, azureAdUsernameTextField.AllowEmpty, azureAdUsernameTextField.DefaultValue); formInfo.AddFormItem(azureAdUsernameTextField); cmsUserDataClass.ClassFormDefinition = formInfo.GetXmlDefinition(); cmsUserDataClass.ClassXmlSchema = tm.GetXmlSchema(cmsUserDataClass.ClassTableName); DataClassInfoProvider.SetDataClassInfo(cmsUserDataClass); FormHelper.UpdateInheritedClasses(cmsUserDataClass); QueryInfoProvider.ClearDefaultQueries(cmsUserDataClass, true, true); newFieldHandler.FinishEvent(); tr.Commit(); ClearHashtables("cms.user"); } catch (Exception ex) { EventLogProvider.LogException("AzureADAuthentication", "Create Field", ex); } finally { newFieldHandler?.Dispose(); } } }
/// <summary> /// Adds form field info to the form to the specified position. /// </summary> /// <param name="ffi">Form field info which will be added</param> /// <param name="category">Category name</param> /// <param name="position">Field position in the category</param> private string AddField(FormFieldInfo ffi, string category, int position) { if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.form", "EditForm")) { RedirectToAccessDenied("cms.form", "EditForm"); } var dci = DataClassInfoProvider.GetDataClassInfo(ClassName); if (dci != null) { // Ensure the transaction using (var tr = new CMSLateBoundTransaction()) { string tableName = dci.ClassTableName; string columnType = DataTypeManager.GetSqlType(ffi.DataType, ffi.Size, ffi.Precision); TableManager tm = new TableManager(dci.ClassConnectionString); tr.BeginTransaction(); // Add new column tm.AddTableColumn(tableName, ffi.Name, columnType, true, null); // Add field to form mFormInfo.AddFormItem(ffi); if (!String.IsNullOrEmpty(category) || position >= 0) { mFormInfo.MoveFormFieldToPositionInCategory(ffi.Name, category, position); } // Update form definition dci.ClassFormDefinition = mFormInfo.GetXmlDefinition(); // Update class schema dci.ClassXmlSchema = tm.GetXmlSchema(dci.ClassTableName); try { // Save the class data DataClassInfoProvider.SetDataClassInfo(dci); } catch (Exception) { return(GetString("FormBuilder.ErrorSavingForm")); } // Generate default view SqlGenerator.GenerateDefaultView(dci, SiteContext.CurrentSiteName); QueryInfoProvider.ClearDefaultQueries(dci, true, true); // Hide field for alternative forms that require it string where = "FormClassID=" + dci.ClassID; where = SqlHelper.AddWhereCondition(where, "FormHideNewParentFields=1"); var altforms = AlternativeFormInfoProvider.GetAlternativeForms(where, null); foreach (AlternativeFormInfo afi in altforms) { afi.HideField(ffi); AlternativeFormInfoProvider.SetAlternativeFormInfo(afi); } // Commit the transaction tr.Commit(); } ClearHashtables(); // Update inherited classes with new fields FormHelper.UpdateInheritedClasses(dci); } else { return(GetString("FormBuilder.ErrorSavingForm")); } return(string.Empty); }
/// <summary> /// Adds form field info to the form to the specified position. /// </summary> /// <param name="ffi">Form field info which will be added</param> /// <param name="category">Category name</param> /// <param name="position">Field position in the category</param> private string AddField(FormFieldInfo ffi, string category, int position) { if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.form", "EditForm")) { RedirectToAccessDenied("cms.form", "EditForm"); } var dci = DataClassInfoProvider.GetDataClassInfo(ClassName); if (dci != null) { RaiseBeforeDefinitionUpdate(); // Ensure the transaction using (var tr = new CMSLateBoundTransaction()) { // Raise event for field addition using (var h = DataDefinitionItemEvents.AddItem.StartEvent(dci, ffi)) { string columnType = DataTypeManager.GetSqlType(ffi.DataType, ffi.Size, ffi.Precision); TableManager tm = new TableManager(dci.ClassConnectionString); tr.BeginTransaction(); // Add new column tm.AddTableColumn(dci.ClassTableName, ffi.Name, columnType, true, null); // Add field to form FormInfo.AddFormItem(ffi); if (!String.IsNullOrEmpty(category) || position >= 0) { FormInfo.MoveFormFieldToPositionInCategory(ffi.Name, category, position); } // Update form definition dci.ClassFormDefinition = FormInfo.GetXmlDefinition(); // Update class schema dci.ClassXmlSchema = tm.GetXmlSchema(dci.ClassTableName); try { // Save the class data DataClassInfoProvider.SetDataClassInfo(dci); } catch (Exception) { return(GetString("FormBuilder.ErrorSavingForm")); } h.FinishEvent(); } QueryInfoProvider.ClearDefaultQueries(dci, true, true); // Hide field for alternative forms that require it FormHelper.HideFieldInAlternativeForms(ffi, dci); // Commit the transaction tr.Commit(); } ClearHashtables(); RaiseAfterDefinitionUpdate(); // Update inherited classes with new fields FormHelper.UpdateInheritedClasses(dci); } else { return(GetString("FormBuilder.ErrorSavingForm")); } return(string.Empty); }