Skip to content

Commit 6275df5

Browse files
authored
Merge pull request #4 from postsharp/feature/bug-14936
#14936 Licenses with MinPostSharpVersion are considered invalid
2 parents 0f81bb2 + a9952f1 commit 6275df5

13 files changed

+160
-60
lines changed

src/PostSharp.LicenseServer/Admin/AddLicense.aspx.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#endregion
1010

1111
using System;
12+
using System.Linq;
1213
using System.Web.UI;
14+
using PostSharp.Sdk;
1315
using PostSharp.Sdk.Extensibility.Licensing;
1416
using ParsedLicense = PostSharp.Sdk.Extensibility.Licensing.License;
1517

@@ -28,7 +30,7 @@ protected void AddButton_OnClick( object sender, EventArgs e )
2830
return;
2931
}
3032

31-
if ( !parsedLicense.IsLicenseServerElligible() )
33+
if ( !parsedLicense.IsLicenseServerEligible() )
3234
{
3335
this.errorLabel.Text = string.Format( "Cannot add a {0} of {1} to the server.",
3436
parsedLicense.GetLicenseTypeName() ?? "(unknown license type)",
@@ -44,8 +46,16 @@ protected void AddButton_OnClick( object sender, EventArgs e )
4446
CreatedOn = VirtualDateTime.UtcNow,
4547
ProductCode = parsedLicense.Product.ToString(),
4648
};
49+
4750
Database db = new Database();
48-
db.Licenses.InsertOnSubmit( license );
51+
if (db.Licenses.Any(l => l.LicenseId == license.LicenseId))
52+
{
53+
this.errorLabel.Text = "The given license has been added already.";
54+
this.errorLabel.Visible = true;
55+
return;
56+
}
57+
58+
db.Licenses.InsertOnSubmit(license);
4959
db.SubmitChanges();
5060

5161
this.Response.Redirect( ".." );

src/PostSharp.LicenseServer/Admin/GenerateDemoData.aspx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Product:
66
<asp:TextBox ID="ProductTextBox" runat="server"></asp:TextBox>
77
<br />
8+
<asp:TextBox ID="VersionTextBox" runat="server"></asp:TextBox>
9+
<br />
810
Count:
911
<asp:TextBox ID="CountTextBox" runat="server"></asp:TextBox>
1012
<br />

src/PostSharp.LicenseServer/Admin/GenerateDemoData.aspx.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ protected void Button1_Click( object sender, EventArgs e )
129129

130130
LeaseService leaseService = new LeaseService( false );
131131
string product = this.ProductTextBox.Text;
132+
Version version = Version.Parse( this.VersionTextBox.Text );
132133
int maxUsers = int.Parse( this.CountTextBox.Text );
133134

134135
StringReader reader = new StringReader( unparsedNames );
@@ -196,7 +197,7 @@ protected void Button1_Click( object sender, EventArgs e )
196197
Dictionary<int, string> errors = new Dictionary<int, string>();
197198
time = time.AddHours( random.NextDouble()*3.0/activeUsers.Count );
198199
DateTime buildDate = time;
199-
Lease lease = leaseService.GetLease( db, product, buildDate, machine, user.UserName, user.AuthenticatedName, time, errors );
200+
Lease lease = leaseService.GetLease( db, product, version, buildDate, machine, user.UserName, user.AuthenticatedName, time, errors );
200201
if ( lease != null )
201202
{
202203
db.SubmitChanges();

src/PostSharp.LicenseServer/Admin/GenerateDemoData.aspx.designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PostSharp.LicenseServer/Lease.ashx.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ public void ProcessRequest(HttpContext context)
5454
return;
5555
}
5656

57+
// Parse version.
58+
string versionString = context.Request.QueryString["version"];
59+
Version version;
60+
if ( string.IsNullOrEmpty( versionString ) )
61+
{
62+
// Versions < 5.0 do not include version in the request.
63+
version = new Version( 2, 0, 0 );
64+
}
65+
else
66+
{
67+
if ( !Version.TryParse( versionString, out version ) )
68+
{
69+
this.SetError( 400, "Cannot parse the argument: version." );
70+
return;
71+
}
72+
}
73+
5774
// Parse build date.
5875
string buildDateString = context.Request.QueryString["buildDate"];
5976
DateTime? buildDate = null;
@@ -142,7 +159,7 @@ public void ProcessRequest(HttpContext context)
142159

143160
Stopwatch stopwatch = Stopwatch.StartNew();
144161

145-
LicenseLease licenseLease = new LeaseService(true).GetLicenseLease(db, productCode, buildDate, machine, userName, context.User.Identity.Name, VirtualDateTime.UtcNow, errors);
162+
LicenseLease licenseLease = new LeaseService(true).GetLicenseLease(db, productCode, version, buildDate, machine, userName, context.User.Identity.Name, VirtualDateTime.UtcNow, errors);
146163

147164
if (licenseLease != null)
148165
{

src/PostSharp.LicenseServer/LeaseService.cs

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Text.RegularExpressions;
1717
using PostSharp.Sdk.Extensibility.Licensing;
1818
using PostSharp.LicenseServer.Properties;
19+
using PostSharp.Sdk;
1920
using ParsedLicense = PostSharp.Sdk.Extensibility.Licensing.License;
2021

2122
namespace PostSharp.LicenseServer
@@ -60,9 +61,9 @@ internal static void CheckAssertions(Database db, License license, DateTime now)
6061
throw new Exception( "Maximum exceeded." );
6162
}
6263
#endif
63-
}
64-
65-
private static LicenseState GetLicenseState(Database db, License license, DateTime? buildDate, DateTime now, Dictionary<int, LicenseState> cache, Dictionary<int, string> errors)
64+
}
65+
66+
private static LicenseState GetLicenseState(Database db, License license, Version version, DateTime? buildDate, DateTime now, Dictionary<int, LicenseState> cache, Dictionary<int, string> errors)
6667
{
6768
LicenseState licenseState;
6869

@@ -74,9 +75,35 @@ private static LicenseState GetLicenseState(Database db, License license, DateTi
7475
{
7576
errors[license.LicenseId] = string.Format("The license key #{0} is invalid.", license.LicenseId);
7677
return null;
78+
}
79+
80+
if (parsedLicense.MinPostSharpVersion > ApplicationInfo.Version)
81+
{
82+
errors[license.LicenseId] = string.Format(
83+
"The license #{0} requires higher version of PostSharp on the License Server. Please upgrade PostSharp NuGet package of the License Server to >= {1}.{2}.{3}",
84+
license.LicenseId,
85+
parsedLicense.MinPostSharpVersion.Major,
86+
parsedLicense.MinPostSharpVersion.Minor,
87+
parsedLicense.MinPostSharpVersion.Build);
88+
return null;
89+
}
90+
91+
if (parsedLicense.MinPostSharpVersion > version)
92+
{
93+
errors[license.LicenseId] = string.Format(
94+
"The license #{0} of type {1} requires PostSharp version >= {2}.{3}.{4} but the requested version is {5}.{6}.{7}.",
95+
license.LicenseId,
96+
parsedLicense.LicenseType,
97+
parsedLicense.MinPostSharpVersion.Major,
98+
parsedLicense.MinPostSharpVersion.Minor,
99+
parsedLicense.MinPostSharpVersion.Build,
100+
version.Major,
101+
version.Minor,
102+
version.Build);
103+
return null;
77104
}
78105

79-
if (!parsedLicense.IsLicenseServerElligible())
106+
if (!parsedLicense.IsLicenseServerEligible())
80107
{
81108
errors[license.LicenseId] = string.Format("The license #{0}, of type {1}, cannot be used in the license server.",
82109
license.LicenseId, parsedLicense.LicenseType);
@@ -85,20 +112,38 @@ private static LicenseState GetLicenseState(Database db, License license, DateTi
85112

86113

87114
if ( !(buildDate == null || parsedLicense.SubscriptionEndDate == null || buildDate <= parsedLicense.SubscriptionEndDate ) )
88-
{
89-
errors[license.LicenseId] = string.Format("The maintenance subscription of license #{0} ends on {1:d} but the requested version has been built on {2:d}.",
90-
license.LicenseId, parsedLicense.SubscriptionEndDate, buildDate);
115+
{
116+
// PostSharp version number has been introduced in the license server protocol in PostSharp v5.
117+
if (version.Major >= 5)
118+
{
119+
errors[license.LicenseId] = string.Format(
120+
"The maintenance subscription of license #{0} ends on {1:d} but the requested version {2}.{3}.{4} has been built on {5:d}.",
121+
license.LicenseId,
122+
parsedLicense.SubscriptionEndDate,
123+
version.Major,
124+
version.Minor,
125+
version.Build,
126+
buildDate );
127+
}
128+
else
129+
{
130+
errors[license.LicenseId] = string.Format(
131+
"The maintenance subscription of license #{0} ends on {1:d} but the requested version has been built on {2:d}.",
132+
license.LicenseId,
133+
parsedLicense.SubscriptionEndDate,
134+
buildDate );
135+
}
136+
91137
return null;
92138
}
93139

94140

95141
licenseState = new LicenseState(now, db, license, parsedLicense);
96142
cache.Add(license.LicenseId, licenseState);
97143
return licenseState;
98-
99-
}
100-
101-
public LicenseLease GetLicenseLease(Database db, string productCode, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int, string> errors)
144+
}
145+
146+
public LicenseLease GetLicenseLease(Database db, string productCode, Version version, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int, string> errors)
102147
{
103148
Settings settings = Settings.Default;
104149

@@ -122,38 +167,38 @@ orderby license.Priority
122167
}
123168

124169
// try to acquire lease in normal way
125-
}
126-
127-
Lease lease = GetLease( db, productCode, buildDate, machine, userName, authenticatedUserName, now, errors, licenses );
170+
}
171+
172+
Lease lease = GetLease(db, productCode, version, buildDate, machine, userName, authenticatedUserName, now, errors, licenses);
128173
if ( lease == null )
129174
return null;
130175

131176
LicenseLease licenseLease = new LicenseLease(lease.License.LicenseKey,
132177
lease.StartTime, lease.EndTime, lease.EndTime.AddDays(-settings.MinLeaseDays));
133178

134179
return licenseLease;
135-
}
136-
137-
public Lease GetLease( Database db, string productCode, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int,string> errors )
180+
}
181+
182+
public Lease GetLease(Database db, string productCode, Version version, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int, string> errors)
138183
{
139184
License[] licenses = (from license in db.Licenses
140185
where license.ProductCode == productCode
141-
orderby license.Priority, license.LicenseId descending
186+
orderby license.Priority, license.LicenseId descending
142187
select license).ToArray();
143188

144-
return GetLease( db, productCode, buildDate, machine, userName, authenticatedUserName, now, errors, licenses );
189+
return this.GetLease( db, productCode, version, buildDate, machine, userName, authenticatedUserName, now, errors, licenses );
145190
}
146191

147-
public Lease GetLease(Database db, string productCode, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int, string> errors, License[] licenses)
192+
public Lease GetLease( Database db, string productCode, Version version, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int, string> errors, License[] licenses )
148193
{
149194
Dictionary<int, LicenseState> licenseStates = new Dictionary<int, LicenseState>();
150195

151196
Settings settings = Settings.Default;
152197

153198
// Check if we have a lease that we can use.
154199
foreach (License license in licenses)
155-
{
156-
LicenseState licenseState = GetLicenseState( db, license, buildDate, now, licenseStates, errors );
200+
{
201+
LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors);
157202
if ( licenseState == null ) continue;
158203

159204

@@ -207,8 +252,8 @@ orderby l.StartTime
207252

208253
// We don't have a current lease. Create a new one.
209254
foreach (License license in licenses)
210-
{
211-
LicenseState licenseState = GetLicenseState( db, license, buildDate, now, licenseStates, errors );
255+
{
256+
LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors);
212257
if (licenseState == null) continue;
213258

214259

@@ -226,8 +271,8 @@ orderby l.StartTime
226271

227272
// We did not find a suitable license. See if we can use the grace period.
228273
foreach (License license in licenses)
229-
{
230-
LicenseState licenseState = GetLicenseState(db, license, buildDate, now, licenseStates, errors);
274+
{
275+
LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors);
231276

232277
if ( licenseState == null ) continue;
233278

src/PostSharp.LicenseServer/PostSharp.LicenseServer.csproj

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\..\packages\PostSharp.5.0.26-rc\build\PostSharp.props" Condition="Exists('..\..\packages\PostSharp.5.0.26-rc\build\PostSharp.props')" />
34
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45
<PropertyGroup>
56
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -26,7 +27,8 @@
2627
<IISExpressUseClassicPipelineMode />
2728
<TargetFrameworkProfile />
2829
<MSBuildCommunityTasksPath>$(MSBuildThisFileDirectory)..\..\packages\MSBuildTasks.1.4.0.88\tools</MSBuildCommunityTasksPath>
29-
<DontImportPostSharp>True</DontImportPostSharp>
30+
<NuGetPackageImportStamp>
31+
</NuGetPackageImportStamp>
3032
</PropertyGroup>
3133
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3234
<DebugSymbols>true</DebugSymbols>
@@ -52,6 +54,7 @@
5254
<UseVSHostingProcess>true</UseVSHostingProcess>
5355
<PublishDatabases>false</PublishDatabases>
5456
<Prefer32Bit>false</Prefer32Bit>
57+
<SkipPostSharp>True</SkipPostSharp>
5558
</PropertyGroup>
5659
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
5760
<DebugType>pdbonly</DebugType>
@@ -67,18 +70,19 @@
6770
</PublishDatabaseSettings>
6871
<ExcludeGeneratedDebugSymbol>true</ExcludeGeneratedDebugSymbol>
6972
<Prefer32Bit>false</Prefer32Bit>
73+
<SkipPostSharp>True</SkipPostSharp>
7074
</PropertyGroup>
7175
<ItemGroup>
72-
<Reference Include="PostSharp, Version=4.3.25.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
73-
<HintPath>..\..\packages\PostSharp.4.3.25\lib\net35-client\PostSharp.dll</HintPath>
76+
<Reference Include="PostSharp, Version=5.0.26.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
77+
<HintPath>..\..\packages\PostSharp.Redist.5.0.26-rc\lib\net45\PostSharp.dll</HintPath>
7478
<Private>True</Private>
7579
</Reference>
76-
<Reference Include="PostSharp.Compiler.Common, Version=4.3.25.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
77-
<HintPath>..\..\packages\PostSharp.Compiler.Common.4.3.25\lib\net40\PostSharp.Compiler.Common.dll</HintPath>
80+
<Reference Include="PostSharp.Compiler.Common, Version=5.0.26.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
81+
<HintPath>..\..\packages\PostSharp.Compiler.Common.5.0.26-rc\lib\net45\PostSharp.Compiler.Common.dll</HintPath>
7882
<Private>True</Private>
7983
</Reference>
80-
<Reference Include="PostSharp.Compiler.Settings, Version=4.3.25.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
81-
<HintPath>..\..\packages\PostSharp.Compiler.Settings.4.3.25\lib\net40\PostSharp.Compiler.Settings.dll</HintPath>
84+
<Reference Include="PostSharp.Compiler.Settings, Version=5.0.26.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
85+
<HintPath>..\..\packages\PostSharp.Compiler.Settings.5.0.26-rc\lib\net45\PostSharp.Compiler.Settings.dll</HintPath>
8286
<Private>True</Private>
8387
</Reference>
8488
<Reference Include="System" />
@@ -239,6 +243,7 @@
239243
<ErrorReport>prompt</ErrorReport>
240244
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
241245
<Prefer32Bit>false</Prefer32Bit>
246+
<SkipPostSharp>True</SkipPostSharp>
242247
</PropertyGroup>
243248
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
244249
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" />
@@ -271,11 +276,14 @@
271276
<Zip ZipFileName="$(OutputPath)PostSharp.LicenseServer.zip" Files="@(_ZipFiles)" WorkingDirectory="obj\$(Configuration)\Package\PackageTmp" />
272277
<Message Importance="High" Text="The license server release is ready in $(MSBuildThisFileDirectory)$(OutputPath)PostSharp.LicenseServer.zip." />
273278
</Target>
274-
<Import Project="..\..\packages\PostSharp.4.3.25\tools\PostSharp.targets" Condition="Exists('..\..\packages\PostSharp.4.3.25\tools\PostSharp.targets')" />
275-
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
276-
<Error Condition="!Exists('..\..\packages\PostSharp.4.3.25\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." />
277-
<Error Condition="Exists('..\..\packages\PostSharp.4.3.25\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
279+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
280+
<PropertyGroup>
281+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
282+
</PropertyGroup>
283+
<Error Condition="!Exists('..\..\packages\PostSharp.5.0.26-rc\build\PostSharp.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.5.0.26-rc\build\PostSharp.props'))" />
284+
<Error Condition="!Exists('..\..\packages\PostSharp.5.0.26-rc\build\PostSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.5.0.26-rc\build\PostSharp.targets'))" />
278285
</Target>
286+
<Import Project="..\..\packages\PostSharp.5.0.23-preview\build\PostSharp.targets" Condition="Exists('..\..\packages\PostSharp.5.0.23-preview\build\PostSharp.targets')" />
279287
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
280288
Other similar extension points exist, see Microsoft.Common.targets.
281289
<Target Name="BeforeBuild">

0 commit comments

Comments
 (0)