From dc0a00e4aa2e60ceb05850308d6c096aeb2492f9 Mon Sep 17 00:00:00 2001 From: Roman Ettlinger Date: Fri, 22 Dec 2023 22:49:25 +0100 Subject: [PATCH 1/5] add Users Database as SQL Database --- Samples/GDS/Server/App.config | 25 +- Samples/GDS/Server/DB/usersdb.edmx.sql | 55 ++ .../GDS/Server/GlobalDiscoveryServer.csproj | 41 + Samples/GDS/Server/Program.cs | 32 +- Samples/GDS/Server/SqlUsersDatabase.cs | 245 ++++++ Samples/GDS/Server/User.cs | 22 + Samples/GDS/Server/usersdb.Context.cs | 30 + Samples/GDS/Server/usersdb.Context.tt | 636 +++++++++++++++ Samples/GDS/Server/usersdb.Designer.cs | 10 + Samples/GDS/Server/usersdb.cs | 9 + Samples/GDS/Server/usersdb.edmx | 74 ++ Samples/GDS/Server/usersdb.edmx.diagram | 12 + Samples/GDS/Server/usersdb.edmx.sql | 58 ++ Samples/GDS/Server/usersdb.tt | 733 ++++++++++++++++++ 14 files changed, 1963 insertions(+), 19 deletions(-) create mode 100644 Samples/GDS/Server/DB/usersdb.edmx.sql create mode 100644 Samples/GDS/Server/SqlUsersDatabase.cs create mode 100644 Samples/GDS/Server/User.cs create mode 100644 Samples/GDS/Server/usersdb.Context.cs create mode 100644 Samples/GDS/Server/usersdb.Context.tt create mode 100644 Samples/GDS/Server/usersdb.Designer.cs create mode 100644 Samples/GDS/Server/usersdb.cs create mode 100644 Samples/GDS/Server/usersdb.edmx create mode 100644 Samples/GDS/Server/usersdb.edmx.diagram create mode 100644 Samples/GDS/Server/usersdb.edmx.sql create mode 100644 Samples/GDS/Server/usersdb.tt diff --git a/Samples/GDS/Server/App.config b/Samples/GDS/Server/App.config index 179a277aa..997a25c06 100644 --- a/Samples/GDS/Server/App.config +++ b/Samples/GDS/Server/App.config @@ -1,16 +1,13 @@ - + -
-
+
+
- - - - - + + - + - - + + @@ -69,16 +66,16 @@ - + - + - + diff --git a/Samples/GDS/Server/DB/usersdb.edmx.sql b/Samples/GDS/Server/DB/usersdb.edmx.sql new file mode 100644 index 000000000..bcff32bd8 --- /dev/null +++ b/Samples/GDS/Server/DB/usersdb.edmx.sql @@ -0,0 +1,55 @@ + +-- -------------------------------------------------- +-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure +-- -------------------------------------------------- +-- Date Created: 12/22/2023 22:14:49 +-- Generated from EDMX file: C:\Users\roman\source\repos\UA-.NETStandard-Samples\Samples\GDS\Server\usersdb.edmx +-- -------------------------------------------------- + +SET QUOTED_IDENTIFIER OFF; +GO +USE [usersdb]; +GO +IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]'); +GO + +-- -------------------------------------------------- +-- Dropping existing FOREIGN KEY constraints +-- -------------------------------------------------- + + +-- -------------------------------------------------- +-- Dropping existing tables +-- -------------------------------------------------- + + +-- -------------------------------------------------- +-- Creating all tables +-- -------------------------------------------------- + +-- Creating table 'UserSet' +CREATE TABLE [dbo].[UserSet] ( + [ID] uniqueidentifier NOT NULL, + [UserName] nvarchar(max) NOT NULL, + [Hash] nvarchar(max) NOT NULL, + [GdsRole] int NOT NULL +); +GO + +-- -------------------------------------------------- +-- Creating all PRIMARY KEY constraints +-- -------------------------------------------------- + +-- Creating primary key on [ID] in table 'UserSet' +ALTER TABLE [dbo].[UserSet] +ADD CONSTRAINT [PK_UserSet] + PRIMARY KEY CLUSTERED ([ID] ASC); +GO + +-- -------------------------------------------------- +-- Creating all FOREIGN KEY constraints +-- -------------------------------------------------- + +-- -------------------------------------------------- +-- Script has ended +-- -------------------------------------------------- \ No newline at end of file diff --git a/Samples/GDS/Server/GlobalDiscoveryServer.csproj b/Samples/GDS/Server/GlobalDiscoveryServer.csproj index 6185d8e32..52a04b880 100644 --- a/Samples/GDS/Server/GlobalDiscoveryServer.csproj +++ b/Samples/GDS/Server/GlobalDiscoveryServer.csproj @@ -67,13 +67,16 @@ 3.5 + 3.0 + + @@ -82,6 +85,24 @@ gdsdb.tt + + usersdb.tt + + + True + True + usersdb.Context.tt + + + True + True + usersdb.tt + + + True + True + usersdb.edmx + gdsdb.tt @@ -110,6 +131,19 @@ gdsdb.tt + + + TextTemplatingFileGenerator + usersdb.Context.cs + usersdb.edmx + + + + + TextTemplatingFileGenerator + usersdb.cs + usersdb.edmx + ResXFileCodeGenerator Resources.Designer.cs @@ -146,6 +180,13 @@ EntityModelCodeGenerator gdsdb.Designer.cs + + EntityModelCodeGenerator + usersdb.Designer.cs + + + usersdb.edmx + gdsdb.edmx diff --git a/Samples/GDS/Server/Program.cs b/Samples/GDS/Server/Program.cs index 4bbaaa4c3..5b556e157 100644 --- a/Samples/GDS/Server/Program.cs +++ b/Samples/GDS/Server/Program.cs @@ -27,11 +27,13 @@ * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ +using Microsoft.Extensions.Logging; using Opc.Ua.Configuration; using Opc.Ua.Gds.Server.Database.Linq; using Opc.Ua.Gds.Server.Database.Sql; using Opc.Ua.Server.Controls; using System; +using System.Data.Entity; namespace Opc.Ua.Gds.Server { @@ -66,12 +68,32 @@ static void Main() throw new Exception("Application instance certificate invalid!"); } - // get the DatabaseStorePath configuration parameter. - GlobalDiscoveryServerConfiguration gdsConfiguration = config.ParseExtension(); - string userdatabaseStorePath = Utils.ReplaceSpecialFolderNames(gdsConfiguration.UsersDatabaseStorePath); + + // load the user database. + var userDatabase = new SqlUsersDatabase(); + //check if database Works, else initialize + try + { + userDatabase.CheckCredentials("Test", "Test"); + } + catch (Exception e) + { + Utils.LogError(e, "Could not connect to the Database!"); + + var ie = e.InnerException; + + while (ie != null) + { + Utils.LogInfo(ie, ""); + ie = ie.InnerException; + } + + Utils.LogInfo("Initialize Database tables!"); + userDatabase.Initialize(); + + Utils.LogInfo("Database Initialized!"); + } - // load the user database. TODO: map to Sql database - var userDatabase = JsonUsersDatabase.Load(userdatabaseStorePath); // start the server. var database = new SqlApplicationsDatabase(); diff --git a/Samples/GDS/Server/SqlUsersDatabase.cs b/Samples/GDS/Server/SqlUsersDatabase.cs new file mode 100644 index 000000000..2a2c43cdc --- /dev/null +++ b/Samples/GDS/Server/SqlUsersDatabase.cs @@ -0,0 +1,245 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Opc.Ua.Gds.Server; +using Opc.Ua.Gds.Server.Database; +using Opc.Ua.Gds.Server.Database.Sql; + +namespace Opc.Ua.Gds.Server +{ + public class SqlUsersDatabase: IUsersDatabase + { + + #region IUsersDatabase + public void Initialize() + { + using (usersdbEntities entities = new usersdbEntities()) + { + Assembly assembly = typeof(SqlApplicationsDatabase).GetTypeInfo().Assembly; + StreamReader istrm = new StreamReader(assembly.GetManifestResourceStream("Opc.Ua.Gds.Server.DB.usersdb.edmx.sql")); + string tables = istrm.ReadToEnd(); + entities.Database.Initialize(true); + entities.Database.CreateIfNotExists(); + var parts = tables.Split(new string[] { "GO" }, System.StringSplitOptions.None); + foreach (var part in parts) { entities.Database.ExecuteSqlCommand(part); } + entities.SaveChanges(); + } + } + + public bool CreateUser(string userName, string password, GdsRole role) + { + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentException("UserName cannot be empty.", nameof(userName)); + } + if (string.IsNullOrEmpty(password)) + { + throw new ArgumentException("Password cannot be empty.", nameof(password)); + } + using (usersdbEntities entities = new usersdbEntities()) + { + if (//User Exists + entities.UserSet.SingleOrDefault(x => x.UserName == userName) != null) + { + return false; + } + + string hash = Hash(password); + + var user = new User { ID = Guid.NewGuid(), UserName = userName, Hash = hash, GdsRole = (int)role }; + + entities.UserSet.Add(user); + + entities.SaveChanges(); + + return true; + } + } + + public bool DeleteUser(string userName) + { + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentException("UserName cannot be empty.", nameof(userName)); + } + using (usersdbEntities entities = new usersdbEntities()) + { + var user = entities.UserSet.SingleOrDefault(x => x.UserName == userName); + + if (user == null) + { + return false; + } + entities.UserSet.Remove(user); + entities.SaveChanges(); + return true; + } + } + + public bool CheckCredentials(string userName, string password) + { + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentException("UserName cannot be empty.", nameof(userName)); + } + if (string.IsNullOrEmpty(password)) + { + throw new ArgumentException("Password cannot be empty.", nameof(password)); + } + using (usersdbEntities entities = new usersdbEntities()) + { + var user = entities.UserSet.SingleOrDefault(x => x.UserName == userName); + + if (user == null) + { + return false; + } + + return Check(user.Hash, password); + } + } + + public GdsRole GetUserRole(string userName) + { + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentException("UserName cannot be empty.", nameof(userName)); + } + using (usersdbEntities entities = new usersdbEntities()) + { + var user = entities.UserSet.SingleOrDefault(x => x.UserName == userName); + + if (user == null) + { + throw new ArgumentException("No user found with the UserName " + userName); + } + + return (GdsRole)user.GdsRole; + } + } + + public bool ChangePassword(string userName, string oldPassword, string newPassword) + { + if (string.IsNullOrEmpty(userName)) + { + throw new ArgumentException("UserName cannot be empty.", nameof(userName)); + } + if (string.IsNullOrEmpty(oldPassword)) + { + throw new ArgumentException("Current Password cannot be empty.", nameof(oldPassword)); + } + if (string.IsNullOrEmpty(newPassword)) + { + throw new ArgumentException("New Password cannot be empty.", nameof(newPassword)); + } + using (usersdbEntities entities = new usersdbEntities()) + { + var user = entities.UserSet.SingleOrDefault(x => x.UserName == userName); + + + if (user == null) + { + return false; + } + + if (Check(user.Hash, oldPassword)) + { + var newHash = Hash(newPassword); + user.Hash = newHash; + entities.SaveChanges(); + return true; + } + return false; + } + } + #endregion + + #region IPasswordHasher + private string Hash(string password) + { +#if NETSTANDARD2_0 || NET462 +#pragma warning disable CA5379 // Ensure Key Derivation Function algorithm is sufficiently strong + using (var algorithm = new Rfc2898DeriveBytes( + password, + kSaltSize, + kIterations)) + { +#pragma warning restore CA5379 // Ensure Key Derivation Function algorithm is sufficiently strong +#else + using (var algorithm = new Rfc2898DeriveBytes( + password, + kSaltSize, + kIterations, + HashAlgorithmName.SHA512)) + { +#endif + var key = Convert.ToBase64String(algorithm.GetBytes(kKeySize)); + var salt = Convert.ToBase64String(algorithm.Salt); + + return $"{kIterations}.{salt}.{key}"; + } + } + + private bool Check(string hash, string password) + { + var separator = new Char[] { '.' }; + var parts = hash.Split(separator, 3); + + if (parts.Length != 3) + { + throw new FormatException("Unexpected hash format. " + + "Should be formatted as `{iterations}.{salt}.{hash}`"); + } + + var iterations = Convert.ToInt32(parts[0], CultureInfo.InvariantCulture.NumberFormat); + var salt = Convert.FromBase64String(parts[1]); + var key = Convert.FromBase64String(parts[2]); + +#if NETSTANDARD2_0 || NET462 +#pragma warning disable CA5379 // Ensure Key Derivation Function algorithm is sufficiently strong + using (var algorithm = new Rfc2898DeriveBytes( + password, + salt, + iterations)) + { +#pragma warning restore CA5379 // Ensure Key Derivation Function algorithm is sufficiently strong +#else + using (var algorithm = new Rfc2898DeriveBytes( + password, + salt, + iterations, + HashAlgorithmName.SHA512)) + { +#endif + var keyToCheck = algorithm.GetBytes(kKeySize); + + var verified = keyToCheck.SequenceEqual(key); + + return verified; + } + } + + #endregion + #region Internal Members + + #endregion + + #region Internal Fields + private const int kSaltSize = 16; // 128 bit + private const int kIterations = 10000; // 10k + private const int kKeySize = 32; // 256 bit + + #endregion + } +} + + + diff --git a/Samples/GDS/Server/User.cs b/Samples/GDS/Server/User.cs new file mode 100644 index 000000000..5788ad1eb --- /dev/null +++ b/Samples/GDS/Server/User.cs @@ -0,0 +1,22 @@ +//------------------------------------------------------------------------------ +// +// This code was generated from a template. +// +// Manual changes to this file may cause unexpected behavior in your application. +// Manual changes to this file will be overwritten if the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Opc.Ua.Gds.Server +{ + using System; + using System.Collections.Generic; + + public partial class User + { + public System.Guid ID { get; set; } + public string UserName { get; set; } + public string Hash { get; set; } + public int GdsRole { get; set; } + } +} diff --git a/Samples/GDS/Server/usersdb.Context.cs b/Samples/GDS/Server/usersdb.Context.cs new file mode 100644 index 000000000..ddb67ea3a --- /dev/null +++ b/Samples/GDS/Server/usersdb.Context.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated from a template. +// +// Manual changes to this file may cause unexpected behavior in your application. +// Manual changes to this file will be overwritten if the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Opc.Ua.Gds.Server +{ + using System; + using System.Data.Entity; + using System.Data.Entity.Infrastructure; + + public partial class usersdbEntities : DbContext + { + public usersdbEntities() + : base("name=usersdbEntities") + { + } + + protected override void OnModelCreating(DbModelBuilder modelBuilder) + { + throw new UnintentionalCodeFirstException(); + } + + public virtual DbSet UserSet { get; set; } + } +} diff --git a/Samples/GDS/Server/usersdb.Context.tt b/Samples/GDS/Server/usersdb.Context.tt new file mode 100644 index 000000000..8177e0411 --- /dev/null +++ b/Samples/GDS/Server/usersdb.Context.tt @@ -0,0 +1,636 @@ +<#@ template language="C#" debug="false" hostspecific="true"#> +<#@ include file="EF6.Utility.CS.ttinclude"#><#@ + output extension=".cs"#><# + +const string inputFile = @"usersdb.edmx"; +var textTransform = DynamicTextTransformation.Create(this); +var code = new CodeGenerationTools(this); +var ef = new MetadataTools(this); +var typeMapper = new TypeMapper(code, ef, textTransform.Errors); +var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors); +var itemCollection = loader.CreateEdmItemCollection(inputFile); +var modelNamespace = loader.GetModelNamespace(inputFile); +var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef); + +var container = itemCollection.OfType().FirstOrDefault(); +if (container == null) +{ + return string.Empty; +} +#> +//------------------------------------------------------------------------------ +// +// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#> +// +// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#> +// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#> +// +//------------------------------------------------------------------------------ + +<# + +var codeNamespace = code.VsNamespaceSuggestion(); +if (!String.IsNullOrEmpty(codeNamespace)) +{ +#> +namespace <#=code.EscapeNamespace(codeNamespace)#> +{ +<# + PushIndent(" "); +} + +#> +using System; +using System.Data.Entity; +using System.Data.Entity.Infrastructure; +<# +if (container.FunctionImports.Any()) +{ +#> +using System.Data.Entity.Core.Objects; +using System.Linq; +<# +} +#> + +<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext +{ + public <#=code.Escape(container)#>() + : base("name=<#=container.Name#>") + { +<# +if (!loader.IsLazyLoadingEnabled(container)) +{ +#> + this.Configuration.LazyLoadingEnabled = false; +<# +} + +foreach (var entitySet in container.BaseEntitySets.OfType()) +{ + // Note: the DbSet members are defined below such that the getter and + // setter always have the same accessibility as the DbSet definition + if (Accessibility.ForReadOnlyProperty(entitySet) != "public") + { +#> + <#=codeStringGenerator.DbSetInitializer(entitySet)#> +<# + } +} +#> + } + + protected override void OnModelCreating(DbModelBuilder modelBuilder) + { + throw new UnintentionalCodeFirstException(); + } + +<# + foreach (var entitySet in container.BaseEntitySets.OfType()) + { +#> + <#=codeStringGenerator.DbSet(entitySet)#> +<# + } + + foreach (var edmFunction in container.FunctionImports) + { + WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: false); + } +#> +} +<# + +if (!String.IsNullOrEmpty(codeNamespace)) +{ + PopIndent(); +#> +} +<# +} +#> +<#+ + +private void WriteFunctionImport(TypeMapper typeMapper, CodeStringGenerator codeStringGenerator, EdmFunction edmFunction, string modelNamespace, bool includeMergeOption) +{ + if (typeMapper.IsComposable(edmFunction)) + { +#> + + [DbFunction("<#=edmFunction.NamespaceName#>", "<#=edmFunction.Name#>")] + <#=codeStringGenerator.ComposableFunctionMethod(edmFunction, modelNamespace)#> + { +<#+ + codeStringGenerator.WriteFunctionParameters(edmFunction, WriteFunctionParameter); +#> + <#=codeStringGenerator.ComposableCreateQuery(edmFunction, modelNamespace)#> + } +<#+ + } + else + { +#> + + <#=codeStringGenerator.FunctionMethod(edmFunction, modelNamespace, includeMergeOption)#> + { +<#+ + codeStringGenerator.WriteFunctionParameters(edmFunction, WriteFunctionParameter); +#> + <#=codeStringGenerator.ExecuteFunction(edmFunction, modelNamespace, includeMergeOption)#> + } +<#+ + if (typeMapper.GenerateMergeOptionFunction(edmFunction, includeMergeOption)) + { + WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: true); + } + } +} + +public void WriteFunctionParameter(string name, string isNotNull, string notNullInit, string nullInit) +{ +#> + var <#=name#> = <#=isNotNull#> ? + <#=notNullInit#> : + <#=nullInit#>; + +<#+ +} + +public const string TemplateId = "CSharp_DbContext_Context_EF6"; + +public class CodeStringGenerator +{ + private readonly CodeGenerationTools _code; + private readonly TypeMapper _typeMapper; + private readonly MetadataTools _ef; + + public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef) + { + ArgumentNotNull(code, "code"); + ArgumentNotNull(typeMapper, "typeMapper"); + ArgumentNotNull(ef, "ef"); + + _code = code; + _typeMapper = typeMapper; + _ef = ef; + } + + public string Property(EdmProperty edmProperty) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1} {2} {{ {3}get; {4}set; }}", + Accessibility.ForProperty(edmProperty), + _typeMapper.GetTypeName(edmProperty.TypeUsage), + _code.Escape(edmProperty), + _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), + _code.SpaceAfter(Accessibility.ForSetter(edmProperty))); + } + + public string NavigationProperty(NavigationProperty navProp) + { + var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType()); + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1} {2} {{ {3}get; {4}set; }}", + AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)), + navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, + _code.Escape(navProp), + _code.SpaceAfter(Accessibility.ForGetter(navProp)), + _code.SpaceAfter(Accessibility.ForSetter(navProp))); + } + + public string AccessibilityAndVirtual(string accessibility) + { + return accessibility + (accessibility != "private" ? " virtual" : ""); + } + + public string EntityClassOpening(EntityType entity) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1}partial class {2}{3}", + Accessibility.ForType(entity), + _code.SpaceAfter(_code.AbstractOption(entity)), + _code.Escape(entity), + _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); + } + + public string EnumOpening(SimpleType enumType) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} enum {1} : {2}", + Accessibility.ForType(enumType), + _code.Escape(enumType), + _code.Escape(_typeMapper.UnderlyingClrType(enumType))); + } + + public void WriteFunctionParameters(EdmFunction edmFunction, Action writeParameter) + { + var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef); + foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable)) + { + var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null"; + var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")"; + var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + TypeMapper.FixNamespaces(parameter.RawClrTypeName) + "))"; + writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit); + } + } + + public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace) + { + var parameters = _typeMapper.GetParameters(edmFunction); + + return string.Format( + CultureInfo.InvariantCulture, + "{0} IQueryable<{1}> {2}({3})", + AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)), + _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace), + _code.Escape(edmFunction), + string.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray())); + } + + public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace) + { + var parameters = _typeMapper.GetParameters(edmFunction); + + return string.Format( + CultureInfo.InvariantCulture, + "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});", + _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace), + edmFunction.NamespaceName, + edmFunction.Name, + string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()), + _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()))); + } + + public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption) + { + var parameters = _typeMapper.GetParameters(edmFunction); + var returnType = _typeMapper.GetReturnType(edmFunction); + + var paramList = String.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray()); + if (includeMergeOption) + { + paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption"; + } + + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1} {2}({3})", + AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)), + returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">", + _code.Escape(edmFunction), + paramList); + } + + public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption) + { + var parameters = _typeMapper.GetParameters(edmFunction); + var returnType = _typeMapper.GetReturnType(edmFunction); + + var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())); + if (includeMergeOption) + { + callParams = ", mergeOption" + callParams; + } + + return string.Format( + CultureInfo.InvariantCulture, + "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});", + returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">", + edmFunction.Name, + callParams); + } + + public string DbSet(EntitySet entitySet) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} virtual DbSet<{1}> {2} {{ get; set; }}", + Accessibility.ForReadOnlyProperty(entitySet), + _typeMapper.GetTypeName(entitySet.ElementType), + _code.Escape(entitySet)); + } + + public string DbSetInitializer(EntitySet entitySet) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} = Set<{1}>();", + _code.Escape(entitySet), + _typeMapper.GetTypeName(entitySet.ElementType)); + } + + public string UsingDirectives(bool inHeader, bool includeCollections = true) + { + return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) + ? string.Format( + CultureInfo.InvariantCulture, + "{0}using System;{1}" + + "{2}", + inHeader ? Environment.NewLine : "", + includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "", + inHeader ? "" : Environment.NewLine) + : ""; + } +} + +public class TypeMapper +{ + private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName"; + + private readonly System.Collections.IList _errors; + private readonly CodeGenerationTools _code; + private readonly MetadataTools _ef; + + public static string FixNamespaces(string typeName) + { + return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial."); + } + + public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors) + { + ArgumentNotNull(code, "code"); + ArgumentNotNull(ef, "ef"); + ArgumentNotNull(errors, "errors"); + + _code = code; + _ef = ef; + _errors = errors; + } + + public string GetTypeName(TypeUsage typeUsage) + { + return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null); + } + + public string GetTypeName(EdmType edmType) + { + return GetTypeName(edmType, isNullable: null, modelNamespace: null); + } + + public string GetTypeName(TypeUsage typeUsage, string modelNamespace) + { + return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace); + } + + public string GetTypeName(EdmType edmType, string modelNamespace) + { + return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace); + } + + public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace) + { + if (edmType == null) + { + return null; + } + + var collectionType = edmType as CollectionType; + if (collectionType != null) + { + return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace)); + } + + var typeName = _code.Escape(edmType.MetadataProperties + .Where(p => p.Name == ExternalTypeNameAttributeName) + .Select(p => (string)p.Value) + .FirstOrDefault()) + ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ? + _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) : + _code.Escape(edmType)); + + if (edmType is StructuralType) + { + return typeName; + } + + if (edmType is SimpleType) + { + var clrType = UnderlyingClrType(edmType); + if (!IsEnumType(edmType)) + { + typeName = _code.Escape(clrType); + } + + typeName = FixNamespaces(typeName); + + return clrType.IsValueType && isNullable == true ? + String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) : + typeName; + } + + throw new ArgumentException("edmType"); + } + + public Type UnderlyingClrType(EdmType edmType) + { + ArgumentNotNull(edmType, "edmType"); + + var primitiveType = edmType as PrimitiveType; + if (primitiveType != null) + { + return primitiveType.ClrEquivalentType; + } + + if (IsEnumType(edmType)) + { + return GetEnumUnderlyingType(edmType).ClrEquivalentType; + } + + return typeof(object); + } + + public object GetEnumMemberValue(MetadataItem enumMember) + { + ArgumentNotNull(enumMember, "enumMember"); + + var valueProperty = enumMember.GetType().GetProperty("Value"); + return valueProperty == null ? null : valueProperty.GetValue(enumMember, null); + } + + public string GetEnumMemberName(MetadataItem enumMember) + { + ArgumentNotNull(enumMember, "enumMember"); + + var nameProperty = enumMember.GetType().GetProperty("Name"); + return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null); + } + + public System.Collections.IEnumerable GetEnumMembers(EdmType enumType) + { + ArgumentNotNull(enumType, "enumType"); + + var membersProperty = enumType.GetType().GetProperty("Members"); + return membersProperty != null + ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null) + : Enumerable.Empty(); + } + + public bool EnumIsFlags(EdmType enumType) + { + ArgumentNotNull(enumType, "enumType"); + + var isFlagsProperty = enumType.GetType().GetProperty("IsFlags"); + return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null); + } + + public bool IsEnumType(GlobalItem edmType) + { + ArgumentNotNull(edmType, "edmType"); + + return edmType.GetType().Name == "EnumType"; + } + + public PrimitiveType GetEnumUnderlyingType(EdmType enumType) + { + ArgumentNotNull(enumType, "enumType"); + + return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null); + } + + public string CreateLiteral(object value) + { + if (value == null || value.GetType() != typeof(TimeSpan)) + { + return _code.CreateLiteral(value); + } + + return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks); + } + + public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable types, string sourceFile) + { + ArgumentNotNull(types, "types"); + ArgumentNotNull(sourceFile, "sourceFile"); + + var hash = new HashSet(StringComparer.InvariantCultureIgnoreCase); + if (types.Any(item => !hash.Add(item))) + { + _errors.Add( + new CompilerError(sourceFile, -1, -1, "6023", + String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict")))); + return false; + } + return true; + } + + public IEnumerable GetEnumItemsToGenerate(IEnumerable itemCollection) + { + return GetItemsToGenerate(itemCollection) + .Where(e => IsEnumType(e)); + } + + public IEnumerable GetItemsToGenerate(IEnumerable itemCollection) where T: EdmType + { + return itemCollection + .OfType() + .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName)) + .OrderBy(i => i.Name); + } + + public IEnumerable GetAllGlobalItems(IEnumerable itemCollection) + { + return itemCollection + .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i)) + .Select(g => GetGlobalItemName(g)); + } + + public string GetGlobalItemName(GlobalItem item) + { + if (item is EdmType) + { + return ((EdmType)item).Name; + } + else + { + return ((EntityContainer)item).Name; + } + } + + public IEnumerable GetSimpleProperties(EntityType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type); + } + + public IEnumerable GetSimpleProperties(ComplexType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type); + } + + public IEnumerable GetComplexProperties(EntityType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type); + } + + public IEnumerable GetComplexProperties(ComplexType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type); + } + + public IEnumerable GetPropertiesWithDefaultValues(EntityType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null); + } + + public IEnumerable GetPropertiesWithDefaultValues(ComplexType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null); + } + + public IEnumerable GetNavigationProperties(EntityType type) + { + return type.NavigationProperties.Where(np => np.DeclaringType == type); + } + + public IEnumerable GetCollectionNavigationProperties(EntityType type) + { + return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many); + } + + public FunctionParameter GetReturnParameter(EdmFunction edmFunction) + { + ArgumentNotNull(edmFunction, "edmFunction"); + + var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters"); + return returnParamsProperty == null + ? edmFunction.ReturnParameter + : ((IEnumerable)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault(); + } + + public bool IsComposable(EdmFunction edmFunction) + { + ArgumentNotNull(edmFunction, "edmFunction"); + + var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute"); + return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null); + } + + public IEnumerable GetParameters(EdmFunction edmFunction) + { + return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef); + } + + public TypeUsage GetReturnType(EdmFunction edmFunction) + { + var returnParam = GetReturnParameter(edmFunction); + return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage); + } + + public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption) + { + var returnType = GetReturnType(edmFunction); + return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType; + } +} + +public static void ArgumentNotNull(T arg, string name) where T : class +{ + if (arg == null) + { + throw new ArgumentNullException(name); + } +} +#> \ No newline at end of file diff --git a/Samples/GDS/Server/usersdb.Designer.cs b/Samples/GDS/Server/usersdb.Designer.cs new file mode 100644 index 000000000..975f38744 --- /dev/null +++ b/Samples/GDS/Server/usersdb.Designer.cs @@ -0,0 +1,10 @@ +// T4 code generation is enabled for model 'C:\Users\roman\source\repos\UA-.NETStandard-Samples\Samples\GDS\Server\usersdb.edmx'. +// To enable legacy code generation, change the value of the 'Code Generation Strategy' designer +// property to 'Legacy ObjectContext'. This property is available in the Properties Window when the model +// is open in the designer. + +// If no context and entity classes have been generated, it may be because you created an empty model but +// have not yet chosen which version of Entity Framework to use. To generate a context class and entity +// classes for your model, open the model in the designer, right-click on the designer surface, and +// select 'Update Model from Database...', 'Generate Database from Model...', or 'Add Code Generation +// Item...'. \ No newline at end of file diff --git a/Samples/GDS/Server/usersdb.cs b/Samples/GDS/Server/usersdb.cs new file mode 100644 index 000000000..7cc066228 --- /dev/null +++ b/Samples/GDS/Server/usersdb.cs @@ -0,0 +1,9 @@ +//------------------------------------------------------------------------------ +// +// This code was generated from a template. +// +// Manual changes to this file may cause unexpected behavior in your application. +// Manual changes to this file will be overwritten if the code is regenerated. +// +//------------------------------------------------------------------------------ + diff --git a/Samples/GDS/Server/usersdb.edmx b/Samples/GDS/Server/usersdb.edmx new file mode 100644 index 000000000..26c384c2c --- /dev/null +++ b/Samples/GDS/Server/usersdb.edmx @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Samples/GDS/Server/usersdb.edmx.diagram b/Samples/GDS/Server/usersdb.edmx.diagram new file mode 100644 index 000000000..d52a9330a --- /dev/null +++ b/Samples/GDS/Server/usersdb.edmx.diagram @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Samples/GDS/Server/usersdb.edmx.sql b/Samples/GDS/Server/usersdb.edmx.sql new file mode 100644 index 000000000..0dc631b53 --- /dev/null +++ b/Samples/GDS/Server/usersdb.edmx.sql @@ -0,0 +1,58 @@ + +-- -------------------------------------------------- +-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure +-- -------------------------------------------------- +-- Date Created: 12/22/2023 22:34:02 +-- Generated from EDMX file: C:\Users\roman\source\repos\UA-.NETStandard-Samples\Samples\GDS\Server\usersdb.edmx +-- -------------------------------------------------- + +SET QUOTED_IDENTIFIER OFF; +GO +USE [usersdb]; +GO +IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]'); +GO + +-- -------------------------------------------------- +-- Dropping existing FOREIGN KEY constraints +-- -------------------------------------------------- + + +-- -------------------------------------------------- +-- Dropping existing tables +-- -------------------------------------------------- + +IF OBJECT_ID(N'[dbo].[UserSet]', 'U') IS NOT NULL + DROP TABLE [dbo].[UserSet]; +GO + +-- -------------------------------------------------- +-- Creating all tables +-- -------------------------------------------------- + +-- Creating table 'UserSet' +CREATE TABLE [dbo].[UserSet] ( + [ID] uniqueidentifier NOT NULL, + [UserName] nvarchar(max) NOT NULL, + [Hash] nvarchar(max) NOT NULL, + [GdsRole] int NOT NULL +); +GO + +-- -------------------------------------------------- +-- Creating all PRIMARY KEY constraints +-- -------------------------------------------------- + +-- Creating primary key on [ID] in table 'UserSet' +ALTER TABLE [dbo].[UserSet] +ADD CONSTRAINT [PK_UserSet] + PRIMARY KEY CLUSTERED ([ID] ASC); +GO + +-- -------------------------------------------------- +-- Creating all FOREIGN KEY constraints +-- -------------------------------------------------- + +-- -------------------------------------------------- +-- Script has ended +-- -------------------------------------------------- \ No newline at end of file diff --git a/Samples/GDS/Server/usersdb.tt b/Samples/GDS/Server/usersdb.tt new file mode 100644 index 000000000..5e1c5bc68 --- /dev/null +++ b/Samples/GDS/Server/usersdb.tt @@ -0,0 +1,733 @@ +<#@ template language="C#" debug="false" hostspecific="true"#> +<#@ include file="EF6.Utility.CS.ttinclude"#><#@ + output extension=".cs"#><# + +const string inputFile = @"usersdb.edmx"; +var textTransform = DynamicTextTransformation.Create(this); +var code = new CodeGenerationTools(this); +var ef = new MetadataTools(this); +var typeMapper = new TypeMapper(code, ef, textTransform.Errors); +var fileManager = EntityFrameworkTemplateFileManager.Create(this); +var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile); +var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef); + +if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile)) +{ + return string.Empty; +} + +WriteHeader(codeStringGenerator, fileManager); + +foreach (var entity in typeMapper.GetItemsToGenerate(itemCollection)) +{ + fileManager.StartNewFile(entity.Name + ".cs"); + BeginNamespace(code); +#> +<#=codeStringGenerator.UsingDirectives(inHeader: false)#> +<#=codeStringGenerator.EntityClassOpening(entity)#> +{ +<# + var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity); + var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity); + var complexProperties = typeMapper.GetComplexProperties(entity); + + if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any()) + { +#> + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] + public <#=code.Escape(entity)#>() + { +<# + foreach (var edmProperty in propertiesWithDefaultValues) + { +#> + this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>; +<# + } + + foreach (var navigationProperty in collectionNavigationProperties) + { +#> + this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>(); +<# + } + + foreach (var complexProperty in complexProperties) + { +#> + this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>(); +<# + } +#> + } + +<# + } + + var simpleProperties = typeMapper.GetSimpleProperties(entity); + if (simpleProperties.Any()) + { + foreach (var edmProperty in simpleProperties) + { +#> + <#=codeStringGenerator.Property(edmProperty)#> +<# + } + } + + if (complexProperties.Any()) + { +#> + +<# + foreach(var complexProperty in complexProperties) + { +#> + <#=codeStringGenerator.Property(complexProperty)#> +<# + } + } + + var navigationProperties = typeMapper.GetNavigationProperties(entity); + if (navigationProperties.Any()) + { +#> + +<# + foreach (var navigationProperty in navigationProperties) + { + if (navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) + { +#> + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] +<# + } +#> + <#=codeStringGenerator.NavigationProperty(navigationProperty)#> +<# + } + } +#> +} +<# + EndNamespace(code); +} + +foreach (var complex in typeMapper.GetItemsToGenerate(itemCollection)) +{ + fileManager.StartNewFile(complex.Name + ".cs"); + BeginNamespace(code); +#> +<#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#> +<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#> +{ +<# + var complexProperties = typeMapper.GetComplexProperties(complex); + var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(complex); + + if (propertiesWithDefaultValues.Any() || complexProperties.Any()) + { +#> + public <#=code.Escape(complex)#>() + { +<# + foreach (var edmProperty in propertiesWithDefaultValues) + { +#> + this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>; +<# + } + + foreach (var complexProperty in complexProperties) + { +#> + this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>(); +<# + } +#> + } + +<# + } + + var simpleProperties = typeMapper.GetSimpleProperties(complex); + if (simpleProperties.Any()) + { + foreach(var edmProperty in simpleProperties) + { +#> + <#=codeStringGenerator.Property(edmProperty)#> +<# + } + } + + if (complexProperties.Any()) + { +#> + +<# + foreach(var edmProperty in complexProperties) + { +#> + <#=codeStringGenerator.Property(edmProperty)#> +<# + } + } +#> +} +<# + EndNamespace(code); +} + +foreach (var enumType in typeMapper.GetEnumItemsToGenerate(itemCollection)) +{ + fileManager.StartNewFile(enumType.Name + ".cs"); + BeginNamespace(code); +#> +<#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#> +<# + if (typeMapper.EnumIsFlags(enumType)) + { +#> +[Flags] +<# + } +#> +<#=codeStringGenerator.EnumOpening(enumType)#> +{ +<# + var foundOne = false; + + foreach (MetadataItem member in typeMapper.GetEnumMembers(enumType)) + { + foundOne = true; +#> + <#=code.Escape(typeMapper.GetEnumMemberName(member))#> = <#=typeMapper.GetEnumMemberValue(member)#>, +<# + } + + if (foundOne) + { + this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 3, 1); + } +#> +} +<# + EndNamespace(code); +} + +fileManager.Process(); + +#> +<#+ + +public void WriteHeader(CodeStringGenerator codeStringGenerator, EntityFrameworkTemplateFileManager fileManager) +{ + fileManager.StartHeader(); +#> +//------------------------------------------------------------------------------ +// +// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#> +// +// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#> +// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#> +// +//------------------------------------------------------------------------------ +<#=codeStringGenerator.UsingDirectives(inHeader: true)#> +<#+ + fileManager.EndBlock(); +} + +public void BeginNamespace(CodeGenerationTools code) +{ + var codeNamespace = code.VsNamespaceSuggestion(); + if (!String.IsNullOrEmpty(codeNamespace)) + { +#> +namespace <#=code.EscapeNamespace(codeNamespace)#> +{ +<#+ + PushIndent(" "); + } +} + +public void EndNamespace(CodeGenerationTools code) +{ + if (!String.IsNullOrEmpty(code.VsNamespaceSuggestion())) + { + PopIndent(); +#> +} +<#+ + } +} + +public const string TemplateId = "CSharp_DbContext_Types_EF6"; + +public class CodeStringGenerator +{ + private readonly CodeGenerationTools _code; + private readonly TypeMapper _typeMapper; + private readonly MetadataTools _ef; + + public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef) + { + ArgumentNotNull(code, "code"); + ArgumentNotNull(typeMapper, "typeMapper"); + ArgumentNotNull(ef, "ef"); + + _code = code; + _typeMapper = typeMapper; + _ef = ef; + } + + public string Property(EdmProperty edmProperty) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1} {2} {{ {3}get; {4}set; }}", + Accessibility.ForProperty(edmProperty), + _typeMapper.GetTypeName(edmProperty.TypeUsage), + _code.Escape(edmProperty), + _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), + _code.SpaceAfter(Accessibility.ForSetter(edmProperty))); + } + + public string NavigationProperty(NavigationProperty navProp) + { + var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType()); + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1} {2} {{ {3}get; {4}set; }}", + AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)), + navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, + _code.Escape(navProp), + _code.SpaceAfter(Accessibility.ForGetter(navProp)), + _code.SpaceAfter(Accessibility.ForSetter(navProp))); + } + + public string AccessibilityAndVirtual(string accessibility) + { + return accessibility + (accessibility != "private" ? " virtual" : ""); + } + + public string EntityClassOpening(EntityType entity) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1}partial class {2}{3}", + Accessibility.ForType(entity), + _code.SpaceAfter(_code.AbstractOption(entity)), + _code.Escape(entity), + _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); + } + + public string EnumOpening(SimpleType enumType) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} enum {1} : {2}", + Accessibility.ForType(enumType), + _code.Escape(enumType), + _code.Escape(_typeMapper.UnderlyingClrType(enumType))); + } + + public void WriteFunctionParameters(EdmFunction edmFunction, Action writeParameter) + { + var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef); + foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable)) + { + var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null"; + var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")"; + var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + TypeMapper.FixNamespaces(parameter.RawClrTypeName) + "))"; + writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit); + } + } + + public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace) + { + var parameters = _typeMapper.GetParameters(edmFunction); + + return string.Format( + CultureInfo.InvariantCulture, + "{0} IQueryable<{1}> {2}({3})", + AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)), + _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace), + _code.Escape(edmFunction), + string.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray())); + } + + public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace) + { + var parameters = _typeMapper.GetParameters(edmFunction); + + return string.Format( + CultureInfo.InvariantCulture, + "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});", + _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace), + edmFunction.NamespaceName, + edmFunction.Name, + string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()), + _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()))); + } + + public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption) + { + var parameters = _typeMapper.GetParameters(edmFunction); + var returnType = _typeMapper.GetReturnType(edmFunction); + + var paramList = String.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray()); + if (includeMergeOption) + { + paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption"; + } + + return string.Format( + CultureInfo.InvariantCulture, + "{0} {1} {2}({3})", + AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)), + returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">", + _code.Escape(edmFunction), + paramList); + } + + public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption) + { + var parameters = _typeMapper.GetParameters(edmFunction); + var returnType = _typeMapper.GetReturnType(edmFunction); + + var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())); + if (includeMergeOption) + { + callParams = ", mergeOption" + callParams; + } + + return string.Format( + CultureInfo.InvariantCulture, + "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});", + returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">", + edmFunction.Name, + callParams); + } + + public string DbSet(EntitySet entitySet) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0} virtual DbSet<{1}> {2} {{ get; set; }}", + Accessibility.ForReadOnlyProperty(entitySet), + _typeMapper.GetTypeName(entitySet.ElementType), + _code.Escape(entitySet)); + } + + public string UsingDirectives(bool inHeader, bool includeCollections = true) + { + return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) + ? string.Format( + CultureInfo.InvariantCulture, + "{0}using System;{1}" + + "{2}", + inHeader ? Environment.NewLine : "", + includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "", + inHeader ? "" : Environment.NewLine) + : ""; + } +} + +public class TypeMapper +{ + private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName"; + + private readonly System.Collections.IList _errors; + private readonly CodeGenerationTools _code; + private readonly MetadataTools _ef; + + public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors) + { + ArgumentNotNull(code, "code"); + ArgumentNotNull(ef, "ef"); + ArgumentNotNull(errors, "errors"); + + _code = code; + _ef = ef; + _errors = errors; + } + + public static string FixNamespaces(string typeName) + { + return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial."); + } + + public string GetTypeName(TypeUsage typeUsage) + { + return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null); + } + + public string GetTypeName(EdmType edmType) + { + return GetTypeName(edmType, isNullable: null, modelNamespace: null); + } + + public string GetTypeName(TypeUsage typeUsage, string modelNamespace) + { + return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace); + } + + public string GetTypeName(EdmType edmType, string modelNamespace) + { + return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace); + } + + public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace) + { + if (edmType == null) + { + return null; + } + + var collectionType = edmType as CollectionType; + if (collectionType != null) + { + return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace)); + } + + var typeName = _code.Escape(edmType.MetadataProperties + .Where(p => p.Name == ExternalTypeNameAttributeName) + .Select(p => (string)p.Value) + .FirstOrDefault()) + ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ? + _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) : + _code.Escape(edmType)); + + if (edmType is StructuralType) + { + return typeName; + } + + if (edmType is SimpleType) + { + var clrType = UnderlyingClrType(edmType); + if (!IsEnumType(edmType)) + { + typeName = _code.Escape(clrType); + } + + typeName = FixNamespaces(typeName); + + return clrType.IsValueType && isNullable == true ? + String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) : + typeName; + } + + throw new ArgumentException("edmType"); + } + + public Type UnderlyingClrType(EdmType edmType) + { + ArgumentNotNull(edmType, "edmType"); + + var primitiveType = edmType as PrimitiveType; + if (primitiveType != null) + { + return primitiveType.ClrEquivalentType; + } + + if (IsEnumType(edmType)) + { + return GetEnumUnderlyingType(edmType).ClrEquivalentType; + } + + return typeof(object); + } + + public object GetEnumMemberValue(MetadataItem enumMember) + { + ArgumentNotNull(enumMember, "enumMember"); + + var valueProperty = enumMember.GetType().GetProperty("Value"); + return valueProperty == null ? null : valueProperty.GetValue(enumMember, null); + } + + public string GetEnumMemberName(MetadataItem enumMember) + { + ArgumentNotNull(enumMember, "enumMember"); + + var nameProperty = enumMember.GetType().GetProperty("Name"); + return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null); + } + + public System.Collections.IEnumerable GetEnumMembers(EdmType enumType) + { + ArgumentNotNull(enumType, "enumType"); + + var membersProperty = enumType.GetType().GetProperty("Members"); + return membersProperty != null + ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null) + : Enumerable.Empty(); + } + + public bool EnumIsFlags(EdmType enumType) + { + ArgumentNotNull(enumType, "enumType"); + + var isFlagsProperty = enumType.GetType().GetProperty("IsFlags"); + return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null); + } + + public bool IsEnumType(GlobalItem edmType) + { + ArgumentNotNull(edmType, "edmType"); + + return edmType.GetType().Name == "EnumType"; + } + + public PrimitiveType GetEnumUnderlyingType(EdmType enumType) + { + ArgumentNotNull(enumType, "enumType"); + + return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null); + } + + public string CreateLiteral(object value) + { + if (value == null || value.GetType() != typeof(TimeSpan)) + { + return _code.CreateLiteral(value); + } + + return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks); + } + + public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable types, string sourceFile) + { + ArgumentNotNull(types, "types"); + ArgumentNotNull(sourceFile, "sourceFile"); + + var hash = new HashSet(StringComparer.InvariantCultureIgnoreCase); + if (types.Any(item => !hash.Add(item))) + { + _errors.Add( + new CompilerError(sourceFile, -1, -1, "6023", + String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict")))); + return false; + } + return true; + } + + public IEnumerable GetEnumItemsToGenerate(IEnumerable itemCollection) + { + return GetItemsToGenerate(itemCollection) + .Where(e => IsEnumType(e)); + } + + public IEnumerable GetItemsToGenerate(IEnumerable itemCollection) where T: EdmType + { + return itemCollection + .OfType() + .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName)) + .OrderBy(i => i.Name); + } + + public IEnumerable GetAllGlobalItems(IEnumerable itemCollection) + { + return itemCollection + .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i)) + .Select(g => GetGlobalItemName(g)); + } + + public string GetGlobalItemName(GlobalItem item) + { + if (item is EdmType) + { + return ((EdmType)item).Name; + } + else + { + return ((EntityContainer)item).Name; + } + } + + public IEnumerable GetSimpleProperties(EntityType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type); + } + + public IEnumerable GetSimpleProperties(ComplexType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type); + } + + public IEnumerable GetComplexProperties(EntityType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type); + } + + public IEnumerable GetComplexProperties(ComplexType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type); + } + + public IEnumerable GetPropertiesWithDefaultValues(EntityType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null); + } + + public IEnumerable GetPropertiesWithDefaultValues(ComplexType type) + { + return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null); + } + + public IEnumerable GetNavigationProperties(EntityType type) + { + return type.NavigationProperties.Where(np => np.DeclaringType == type); + } + + public IEnumerable GetCollectionNavigationProperties(EntityType type) + { + return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many); + } + + public FunctionParameter GetReturnParameter(EdmFunction edmFunction) + { + ArgumentNotNull(edmFunction, "edmFunction"); + + var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters"); + return returnParamsProperty == null + ? edmFunction.ReturnParameter + : ((IEnumerable)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault(); + } + + public bool IsComposable(EdmFunction edmFunction) + { + ArgumentNotNull(edmFunction, "edmFunction"); + + var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute"); + return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null); + } + + public IEnumerable GetParameters(EdmFunction edmFunction) + { + return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef); + } + + public TypeUsage GetReturnType(EdmFunction edmFunction) + { + var returnParam = GetReturnParameter(edmFunction); + return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage); + } + + public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption) + { + var returnType = GetReturnType(edmFunction); + return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType; + } +} + +public static void ArgumentNotNull(T arg, string name) where T : class +{ + if (arg == null) + { + throw new ArgumentNullException(name); + } +} +#> \ No newline at end of file From d1295ef3bb930d29a97f37dd437b41276f999bc0 Mon Sep 17 00:00:00 2001 From: Roman Ettlinger Date: Tue, 26 Dec 2023 09:32:45 +0100 Subject: [PATCH 2/5] hide logic for initialization --- .../Opc.Ua.GlobalDiscoveryServer.Config.xml | 1 - Samples/GDS/Server/Program.cs | 27 +++----------- Samples/GDS/Server/SqlUsersDatabase.cs | 36 ++++++++++++++----- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Samples/GDS/Server/Opc.Ua.GlobalDiscoveryServer.Config.xml b/Samples/GDS/Server/Opc.Ua.GlobalDiscoveryServer.Config.xml index f910360ce..0f5992dbe 100644 --- a/Samples/GDS/Server/Opc.Ua.GlobalDiscoveryServer.Config.xml +++ b/Samples/GDS/Server/Opc.Ua.GlobalDiscoveryServer.Config.xml @@ -148,7 +148,6 @@ 5 - %LocalApplicationData%/OPC Foundation/GDS/gdsuserdb.json diff --git a/Samples/GDS/Server/Program.cs b/Samples/GDS/Server/Program.cs index 5b556e157..b53c449d1 100644 --- a/Samples/GDS/Server/Program.cs +++ b/Samples/GDS/Server/Program.cs @@ -71,29 +71,10 @@ static void Main() // load the user database. var userDatabase = new SqlUsersDatabase(); - //check if database Works, else initialize - try - { - userDatabase.CheckCredentials("Test", "Test"); - } - catch (Exception e) - { - Utils.LogError(e, "Could not connect to the Database!"); - - var ie = e.InnerException; - - while (ie != null) - { - Utils.LogInfo(ie, ""); - ie = ie.InnerException; - } - - Utils.LogInfo("Initialize Database tables!"); - userDatabase.Initialize(); - - Utils.LogInfo("Database Initialized!"); - } - + //initialize users Database + userDatabase.Initialize(); + + // start the server. var database = new SqlApplicationsDatabase(); diff --git a/Samples/GDS/Server/SqlUsersDatabase.cs b/Samples/GDS/Server/SqlUsersDatabase.cs index 2a2c43cdc..8d0a16d61 100644 --- a/Samples/GDS/Server/SqlUsersDatabase.cs +++ b/Samples/GDS/Server/SqlUsersDatabase.cs @@ -23,14 +23,34 @@ public void Initialize() { using (usersdbEntities entities = new usersdbEntities()) { - Assembly assembly = typeof(SqlApplicationsDatabase).GetTypeInfo().Assembly; - StreamReader istrm = new StreamReader(assembly.GetManifestResourceStream("Opc.Ua.Gds.Server.DB.usersdb.edmx.sql")); - string tables = istrm.ReadToEnd(); - entities.Database.Initialize(true); - entities.Database.CreateIfNotExists(); - var parts = tables.Split(new string[] { "GO" }, System.StringSplitOptions.None); - foreach (var part in parts) { entities.Database.ExecuteSqlCommand(part); } - entities.SaveChanges(); + //only run initizailation logic if the database does not work -> throwS an exception + try + { + CheckCredentials("Test", "Test"); + } + catch (Exception e) + { + Utils.LogError(e, "Could not connect to the Database!"); + + var ie = e.InnerException; + + while (ie != null) + { + Utils.LogInfo(ie, ""); + ie = ie.InnerException; + } + Utils.LogInfo("Initialize Database tables!"); + Assembly assembly = typeof(SqlApplicationsDatabase).GetTypeInfo().Assembly; + StreamReader istrm = new StreamReader(assembly.GetManifestResourceStream("Opc.Ua.Gds.Server.DB.usersdb.edmx.sql")); + string tables = istrm.ReadToEnd(); + entities.Database.Initialize(true); + entities.Database.CreateIfNotExists(); + var parts = tables.Split(new string[] { "GO" }, System.StringSplitOptions.None); + foreach (var part in parts) { entities.Database.ExecuteSqlCommand(part); } + entities.SaveChanges(); + Utils.LogInfo("Database Initialized!"); + } + } } From a6a947aae5ed3d251f1c5e39dd1c767d8958317e Mon Sep 17 00:00:00 2001 From: Roman Ettlinger Date: Tue, 26 Dec 2023 09:33:20 +0100 Subject: [PATCH 3/5] make it possible to specify an own user for NETCoreGlobalDiscoveryServer --- Samples/GDS/ConsoleServer/Program.cs | 64 +++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/Samples/GDS/ConsoleServer/Program.cs b/Samples/GDS/ConsoleServer/Program.cs index 50e1aa113..f202f3f3e 100644 --- a/Samples/GDS/ConsoleServer/Program.cs +++ b/Samples/GDS/ConsoleServer/Program.cs @@ -33,9 +33,13 @@ using Opc.Ua.Server; using System; using System.Collections.Generic; +using System.Data; +using System.Diagnostics; using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Xml.Linq; namespace Opc.Ua.Gds.Server { @@ -236,14 +240,37 @@ private async Task ConsoleGlobalDiscoveryServer() string databaseStorePath = Utils.ReplaceSpecialFolderNames(gdsConfiguration.DatabaseStorePath); string userdatabaseStorePath = Utils.ReplaceSpecialFolderNames(gdsConfiguration.UsersDatabaseStorePath); - // start the server. var database = JsonApplicationsDatabase.Load(databaseStorePath); var userDatabase = JsonUsersDatabase.Load(userdatabaseStorePath); + + + //configure Users + ApplicationInstance.MessageDlg.Message("Use default users?", true); + bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; + + if (!createStandardUsers){ + Console.Write("Please specify user name of the application admin user:"); + string username = Console.ReadLine(); + _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); + + Console.Write($"Please specify the password of {username}:"); + + //string password = Console.ReadLine(); + string password = GetPassword(); + _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); + + userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); + } + + // start the server. + server = new GlobalDiscoverySampleServer( database, database, new CertificateGroup(), - userDatabase); + userDatabase, + true, + createStandardUsers); await application.Start(server).ConfigureAwait(false); // print endpoint info @@ -307,5 +334,38 @@ private async void StatusThread() await Task.Delay(1000).ConfigureAwait(false); } } + + private static string GetPassword() + { + StringBuilder input = new StringBuilder(); + while (true) + { + int x = Console.CursorLeft; + int y = Console.CursorTop; + ConsoleKeyInfo key = Console.ReadKey(true); + if (key.Key == ConsoleKey.Enter) + { + Console.WriteLine(); + break; + } + if (key.Key == ConsoleKey.Backspace && input.Length > 0) + { + input.Remove(input.Length - 1, 1); + Console.SetCursorPosition(x - 1, y); + Console.Write(" "); + Console.SetCursorPosition(x - 1, y); + } + else if (key.KeyChar < 32 || key.KeyChar > 126) + { + Trace.WriteLine("Output suppressed: no key char"); //catch non-printable chars, e.g F1, CursorUp and so ... + } + else if (key.Key != ConsoleKey.Backspace) + { + input.Append(key.KeyChar); + Console.Write("*"); + } + } + return input.ToString(); + } } } From 94607b3cc6f9ea6279356c100e0dfabccc7ce010 Mon Sep 17 00:00:00 2001 From: Roman Ettlinger Date: Tue, 26 Dec 2023 10:33:54 +0100 Subject: [PATCH 4/5] make it possible to specifiy an own user for WinForms GDS --- Samples/GDS/ConsoleServer/Program.cs | 6 + Samples/GDS/Server/Program.cs | 28 +++- .../ServerControls.Net4/InputDlg.Designer.cs | 86 +++++++++++++ Samples/ServerControls.Net4/InputDlg.cs | 38 ++++++ Samples/ServerControls.Net4/InputDlg.resx | 120 ++++++++++++++++++ .../UA Server Controls.csproj | 11 +- 6 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 Samples/ServerControls.Net4/InputDlg.Designer.cs create mode 100644 Samples/ServerControls.Net4/InputDlg.cs create mode 100644 Samples/ServerControls.Net4/InputDlg.resx diff --git a/Samples/GDS/ConsoleServer/Program.cs b/Samples/GDS/ConsoleServer/Program.cs index f202f3f3e..3950a97f1 100644 --- a/Samples/GDS/ConsoleServer/Program.cs +++ b/Samples/GDS/ConsoleServer/Program.cs @@ -249,6 +249,12 @@ private async Task ConsoleGlobalDiscoveryServer() bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; if (!createStandardUsers){ + //delete existing standard users + userDatabase.DeleteUser("appadmin"); + userDatabase.DeleteUser("appuser"); + userDatabase.DeleteUser("sysadmin"); + + //Create new admin user Console.Write("Please specify user name of the application admin user:"); string username = Console.ReadLine(); _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); diff --git a/Samples/GDS/Server/Program.cs b/Samples/GDS/Server/Program.cs index b53c449d1..afd107918 100644 --- a/Samples/GDS/Server/Program.cs +++ b/Samples/GDS/Server/Program.cs @@ -73,8 +73,28 @@ static void Main() var userDatabase = new SqlUsersDatabase(); //initialize users Database userDatabase.Initialize(); - - + + //configure users + ApplicationInstance.MessageDlg.Message("Use default users?", true); + bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; + if (!createStandardUsers) + { + //Delete existing standard users + userDatabase.DeleteUser("appadmin"); + userDatabase.DeleteUser("appuser"); + userDatabase.DeleteUser("sysadmin"); + + //Create new admin user + string username = InputDlg.Show("Please specify user name of the application admin user:", false); + _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); + + Console.Write($"Please specify the password of {username}:"); + + string password = InputDlg.Show($"Please specify the password of {username}:", true); + _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); + + userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); + } // start the server. var database = new SqlApplicationsDatabase(); @@ -82,7 +102,9 @@ static void Main() database, database, new CertificateGroup(), - userDatabase); + userDatabase, + true, + createStandardUsers); application.Start(server).Wait(); // run the application interactively. diff --git a/Samples/ServerControls.Net4/InputDlg.Designer.cs b/Samples/ServerControls.Net4/InputDlg.Designer.cs new file mode 100644 index 000000000..9511ab766 --- /dev/null +++ b/Samples/ServerControls.Net4/InputDlg.Designer.cs @@ -0,0 +1,86 @@ +namespace Opc.Ua.Server.Controls +{ + partial class InputDlg + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxInput = new System.Windows.Forms.TextBox(); + this.labelText = new System.Windows.Forms.Label(); + this.buttonOk = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // textBoxInput + // + this.textBoxInput.Location = new System.Drawing.Point(12, 30); + this.textBoxInput.Name = "textBoxInput"; + this.textBoxInput.Size = new System.Drawing.Size(385, 20); + this.textBoxInput.TabIndex = 0; + // + // labelText + // + this.labelText.AutoSize = true; + this.labelText.Location = new System.Drawing.Point(9, 14); + this.labelText.Name = "labelText"; + this.labelText.Size = new System.Drawing.Size(35, 13); + this.labelText.TabIndex = 1; + this.labelText.Text = "label1"; + // + // buttonOk + // + this.buttonOk.Location = new System.Drawing.Point(322, 71); + this.buttonOk.Name = "buttonOk"; + this.buttonOk.Size = new System.Drawing.Size(75, 23); + this.buttonOk.TabIndex = 2; + this.buttonOk.Text = "Ok"; + this.buttonOk.UseVisualStyleBackColor = true; + this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click); + // + // InputDlg + // + this.AcceptButton = this.buttonOk; + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(409, 101); + this.Controls.Add(this.buttonOk); + this.Controls.Add(this.labelText); + this.Controls.Add(this.textBoxInput); + this.Name = "InputDlg"; + this.ShowIcon = false; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "OPC UA"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox textBoxInput; + private System.Windows.Forms.Label labelText; + private System.Windows.Forms.Button buttonOk; + } +} diff --git a/Samples/ServerControls.Net4/InputDlg.cs b/Samples/ServerControls.Net4/InputDlg.cs new file mode 100644 index 000000000..d0907eaa3 --- /dev/null +++ b/Samples/ServerControls.Net4/InputDlg.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; + +namespace Opc.Ua.Server.Controls +{ + public partial class InputDlg : Form + { + public InputDlg() + { + InitializeComponent(); + } + + public static string Show(string text, bool hideInput) + { + var inputDlg = new InputDlg(); + if (hideInput) + inputDlg.textBoxInput.PasswordChar = '*'; + inputDlg.labelText.Text = text; + inputDlg.ShowDialog(); + return inputDlg.textBoxInput.Text; + } + + private void buttonOk_Click(object sender, EventArgs e) + { + Close(); + } + } +} + diff --git a/Samples/ServerControls.Net4/InputDlg.resx b/Samples/ServerControls.Net4/InputDlg.resx new file mode 100644 index 000000000..1af7de150 --- /dev/null +++ b/Samples/ServerControls.Net4/InputDlg.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Samples/ServerControls.Net4/UA Server Controls.csproj b/Samples/ServerControls.Net4/UA Server Controls.csproj index 25403434d..69e4c10ce 100644 --- a/Samples/ServerControls.Net4/UA Server Controls.csproj +++ b/Samples/ServerControls.Net4/UA Server Controls.csproj @@ -1,4 +1,4 @@ - + Debug @@ -76,6 +76,12 @@ ExceptionDlg.cs + + Form + + + InputDlg.cs + @@ -108,6 +114,9 @@ ExceptionDlg.cs + + InputDlg.cs + ResXFileCodeGenerator Resources.Designer.cs From c837abe27695db690c74176a8fa97515283bca17 Mon Sep 17 00:00:00 2001 From: Roman Ettlinger Date: Thu, 28 Dec 2023 10:28:38 +0100 Subject: [PATCH 5/5] refactor & overwrite existing admin user --- Samples/GDS/ConsoleServer/Program.cs | 60 ++++++++++++++++------------ Samples/GDS/Server/Program.cs | 53 ++++++++++++++---------- 2 files changed, 67 insertions(+), 46 deletions(-) diff --git a/Samples/GDS/ConsoleServer/Program.cs b/Samples/GDS/ConsoleServer/Program.cs index 3950a97f1..8599b8199 100644 --- a/Samples/GDS/ConsoleServer/Program.cs +++ b/Samples/GDS/ConsoleServer/Program.cs @@ -29,6 +29,7 @@ using Mono.Options; using Opc.Ua.Configuration; +using Opc.Ua.Gds.Server.Database; using Opc.Ua.Gds.Server.Database.Linq; using Opc.Ua.Server; using System; @@ -243,33 +244,9 @@ private async Task ConsoleGlobalDiscoveryServer() var database = JsonApplicationsDatabase.Load(databaseStorePath); var userDatabase = JsonUsersDatabase.Load(userdatabaseStorePath); - - //configure Users - ApplicationInstance.MessageDlg.Message("Use default users?", true); - bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; - - if (!createStandardUsers){ - //delete existing standard users - userDatabase.DeleteUser("appadmin"); - userDatabase.DeleteUser("appuser"); - userDatabase.DeleteUser("sysadmin"); - - //Create new admin user - Console.Write("Please specify user name of the application admin user:"); - string username = Console.ReadLine(); - _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); - - Console.Write($"Please specify the password of {username}:"); - - //string password = Console.ReadLine(); - string password = GetPassword(); - _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); - - userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); - } + bool createStandardUsers = ConfigureUsers(userDatabase); // start the server. - server = new GlobalDiscoverySampleServer( database, database, @@ -296,6 +273,39 @@ private async Task ConsoleGlobalDiscoveryServer() } + private bool ConfigureUsers(JsonUsersDatabase userDatabase) + { + ApplicationInstance.MessageDlg.Message("Use default users?", true); + bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; + + if (!createStandardUsers) + { + //delete existing standard users + userDatabase.DeleteUser("appadmin"); + userDatabase.DeleteUser("appuser"); + userDatabase.DeleteUser("sysadmin"); + + //Create new admin user + Console.Write("Please specify user name of the application admin user:"); + string username = Console.ReadLine(); + _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); + + Console.Write($"Please specify the password of {username}:"); + + //string password = Console.ReadLine(); + string password = GetPassword(); + _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); + + //create User, if User exists delete & recreate + if (!userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin)) + { + userDatabase.DeleteUser(username); + userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); + } + } + return createStandardUsers; + } + private void EventStatus(Session session, SessionEventReason reason) { lastEventTime = DateTime.UtcNow; diff --git a/Samples/GDS/Server/Program.cs b/Samples/GDS/Server/Program.cs index afd107918..6a1ef3b54 100644 --- a/Samples/GDS/Server/Program.cs +++ b/Samples/GDS/Server/Program.cs @@ -74,27 +74,8 @@ static void Main() //initialize users Database userDatabase.Initialize(); - //configure users - ApplicationInstance.MessageDlg.Message("Use default users?", true); - bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; - if (!createStandardUsers) - { - //Delete existing standard users - userDatabase.DeleteUser("appadmin"); - userDatabase.DeleteUser("appuser"); - userDatabase.DeleteUser("sysadmin"); - - //Create new admin user - string username = InputDlg.Show("Please specify user name of the application admin user:", false); - _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); - - Console.Write($"Please specify the password of {username}:"); - - string password = InputDlg.Show($"Please specify the password of {username}:", true); - _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); - - userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); - } + bool createStandardUsers = ConfigureUsers(userDatabase); + // start the server. var database = new SqlApplicationsDatabase(); @@ -115,5 +96,35 @@ static void Main() ExceptionDlg.Show(application.ApplicationName, e); } } + + private static bool ConfigureUsers(SqlUsersDatabase userDatabase) + { + ApplicationInstance.MessageDlg.Message("Use default users?", true); + bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; + if (!createStandardUsers) + { + //Delete existing standard users + userDatabase.DeleteUser("appadmin"); + userDatabase.DeleteUser("appuser"); + userDatabase.DeleteUser("sysadmin"); + + //Create new admin user + string username = InputDlg.Show("Please specify user name of the application admin user:", false); + _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); + + Console.Write($"Please specify the password of {username}:"); + + string password = InputDlg.Show($"Please specify the password of {username}:", true); + _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); + + //create User, if User exists delete & recreate + if (!userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin)) + { + userDatabase.DeleteUser(username); + userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); + } + } + return createStandardUsers; + } } }