From 54af1a770b30f3f26d27628dcd152ef2abe97bd7 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 5 Feb 2019 13:36:52 +0100 Subject: [PATCH] jj: license --- Modules.License/License.vbproj | 1 + Modules.License/LicenseCreator.vb | 34 ++++++++ Modules.License/LicenseFile.vb | 36 ++++++++- Modules.License/LicenseSchema.vb | 128 +++++++++++++++--------------- 4 files changed, 133 insertions(+), 66 deletions(-) create mode 100644 Modules.License/LicenseCreator.vb diff --git a/Modules.License/License.vbproj b/Modules.License/License.vbproj index e944f4ee..8254f7c8 100644 --- a/Modules.License/License.vbproj +++ b/Modules.License/License.vbproj @@ -72,6 +72,7 @@ + LicenseSchema.xsd diff --git a/Modules.License/LicenseCreator.vb b/Modules.License/LicenseCreator.vb new file mode 100644 index 00000000..eb62ec4a --- /dev/null +++ b/Modules.License/LicenseCreator.vb @@ -0,0 +1,34 @@ +Public Class LicenseCreator + Public Shared Function NewUser(Type As UserType, Count As Integer, Optional ValidUntil As Date = Nothing) As LicenseModuleUser + Dim oValidUntilSpecified = Not IsNothing(ValidUntil) + Dim oTest = IsNothing(ValidUntil) + + Return New LicenseModuleUser() With { + .Count = Count, + .Type = Type, + .Test = oTest, + .ValidUntil = ValidUntil, + .ValidUntilSpecified = oValidUntilSpecified + } + End Function + + Public Shared Function NewModule(Name As String, Users As List(Of LicenseModuleUser), Optional ValidUntil As Date = Nothing) As LicenseModule + Dim oUsers = Users.ToArray() + Dim oValidUntilSpecified = Not IsNothing(ValidUntil) + + Return New LicenseModule() With { + .Name = Name, + .Users = oUsers, + .ValidUntil = ValidUntil, + .ValidUntilSpecified = oValidUntilSpecified + } + End Function + + Public Shared Function NewLicense(Modules As List(Of LicenseModule)) As LicenseSchema + Dim oModules = Modules.ToArray() + + Return New LicenseSchema With { + .Modules = oModules + } + End Function +End Class diff --git a/Modules.License/LicenseFile.vb b/Modules.License/LicenseFile.vb index 1be668c3..0e842d37 100644 --- a/Modules.License/LicenseFile.vb +++ b/Modules.License/LicenseFile.vb @@ -1,4 +1,5 @@ Imports System.IO +Imports System.Xml Imports System.Xml.Serialization Imports DigitalData.Modules.Logging @@ -18,6 +19,21 @@ Public Class LicenseFile Return oSerializer End Function + Public Function TestFileValid(Optional FileName As String = LICENSE_FILENAME) As Boolean + Try + Dim oSerializer = GetSerializer() + Dim oFilePath As String = IO.Path.Combine(Path, FileName) + + Using oReader As New StreamReader(oFilePath), + oXmlReader = XmlReader.Create(oReader) + Return oSerializer.CanDeserialize(oXmlReader) + End Using + Catch ex As Exception + _Logger.Error(ex) + Throw ex + End Try + End Function + Public Function LoadFile(Optional FileName As String = LICENSE_FILENAME) As LicenseSchema Try Dim oSerializer = GetSerializer() @@ -35,13 +51,27 @@ Public Class LicenseFile End Try End Function - Public Sub SaveFile(License As LicenseSchema, Optional FileName As String = LICENSE_FILENAME) + Public Function Serialize(License As LicenseSchema) As Byte() Try Dim oSerializer = GetSerializer() - Using oStream = New FileStream(FileName, FileMode.Create, FileAccess.Write) + Using oStream = New MemoryStream() oSerializer.Serialize(oStream, License) - oStream.Flush() + Return oStream.ToArray() + End Using + Catch ex As Exception + _Logger.Error(ex) + Throw ex + End Try + End Function + + Public Sub SaveFile(License As LicenseSchema, Optional FileName As String = LICENSE_FILENAME) + Try + Dim oBytes = Serialize(License) + Dim oFilePath As String = IO.Path.Combine(Path, FileName) + + Using oFileStream = New FileStream(oFilePath, FileMode.Create, FileAccess.Write) + oFileStream.Write(oBytes, 0, oBytes.Length) End Using Catch ex As Exception _Logger.Error(ex) diff --git a/Modules.License/LicenseSchema.vb b/Modules.License/LicenseSchema.vb index 1fcda6f8..877fe8c0 100644 --- a/Modules.License/LicenseSchema.vb +++ b/Modules.License/LicenseSchema.vb @@ -12,24 +12,26 @@ Option Strict Off Option Explicit On Imports System.Xml.Serialization +Imports System.ComponentModel +Imports System.CodeDom.Compiler ' 'This source code was auto-generated by xsd, Version=4.6.1586.0. ' ''' - + Partial Public Class LicenseSchema Private modulesField() As LicenseModule ''' - + Public Property Modules() As LicenseModule() Get Return Me.modulesField @@ -41,161 +43,161 @@ Partial Public Class LicenseSchema End Class ''' - _ + Partial Public Class LicenseModule - + Private usersField() As LicenseModuleUser - + Private nameField As String - + Private validUntilField As Date - + Private validUntilFieldSpecified As Boolean - + ''' - _ + Public Property Users() As LicenseModuleUser() Get Return Me.usersField End Get Set - Me.usersField = value + Me.usersField = Value End Set End Property - + ''' - _ + Public Property Name() As String Get Return Me.nameField End Get Set - Me.nameField = value + Me.nameField = Value End Set End Property - + ''' - _ + Public Property ValidUntil() As Date Get Return Me.validUntilField End Get Set - Me.validUntilField = value + Me.validUntilField = Value End Set End Property - + ''' - _ + Public Property ValidUntilSpecified() As Boolean Get Return Me.validUntilFieldSpecified End Get Set - Me.validUntilFieldSpecified = value + Me.validUntilFieldSpecified = Value End Set End Property End Class ''' - _ + Partial Public Class LicenseModuleUser - + Private typeField As UserType - + Private countField As String - + Private testField As Boolean - + Private validUntilField As Date - + Private validUntilFieldSpecified As Boolean - + Public Sub New() MyBase.New - Me.testField = true + Me.testField = True End Sub - + ''' - _ + Public Property Type() As UserType Get Return Me.typeField End Get Set - Me.typeField = value + Me.typeField = Value End Set End Property - + ''' - _ + Public Property Count() As String Get Return Me.countField End Get Set - Me.countField = value + Me.countField = Value End Set End Property - + ''' - _ + Public Property Test() As Boolean Get Return Me.testField End Get Set - Me.testField = value + Me.testField = Value End Set End Property - + ''' - _ + Public Property ValidUntil() As Date Get Return Me.validUntilField End Get Set - Me.validUntilField = value + Me.validUntilField = Value End Set End Property - + ''' - _ + Public Property ValidUntilSpecified() As Boolean Get Return Me.validUntilFieldSpecified End Get Set - Me.validUntilFieldSpecified = value + Me.validUntilFieldSpecified = Value End Set End Property End Class ''' - _ + Public Enum UserType - + ''' PowerUser - + ''' [ReadOnly] - + ''' [WriteOnly] - + ''' ReadWrite End Enum