Skip to content

Commit 25aae38

Browse files
committed
Explictly uses int.TryParse Invariant when the input is not from a user
1 parent 8060d41 commit 25aae38

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

src/Umbraco.Infrastructure/Security/BackOfficeUserStore.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Data;
5+
using System.Globalization;
56
using System.Linq;
67
using System.Security.Claims;
78
using System.Threading;
@@ -133,15 +134,14 @@ public override Task<IdentityResult> UpdateAsync(BackOfficeIdentityUser user, Ca
133134
throw new ArgumentNullException(nameof(user));
134135
}
135136

136-
Attempt<int> asInt = user.Id.TryConvertTo<int>();
137-
if (asInt == false)
137+
if (!int.TryParse(user.Id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var asInt))
138138
{
139139
throw new InvalidOperationException("The user id must be an integer to work with the Umbraco");
140140
}
141141

142142
using (IScope scope = _scopeProvider.CreateScope())
143143
{
144-
IUser found = _userService.GetUserById(asInt.Result);
144+
IUser found = _userService.GetUserById(asInt);
145145
if (found != null)
146146
{
147147
// we have to remember whether Logins property is dirty, since the UpdateMemberProperties will reset it.
@@ -441,7 +441,7 @@ private bool UpdateMemberProperties(IUser user, BackOfficeIdentityUser identityU
441441
if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.InviteDateUtc))
442442
|| (user.InvitedDate?.ToUniversalTime() != identityUser.InviteDateUtc))
443443
{
444-
anythingChanged = true;
444+
anythingChanged = true;
445445
user.InvitedDate = identityUser.InviteDateUtc?.ToLocalTime();
446446
}
447447

src/Umbraco.Infrastructure/Security/MemberUserStore.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Globalization;
45
using System.Linq;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -116,16 +117,14 @@ public override Task<IdentityResult> UpdateAsync(MemberIdentityUser user, Cancel
116117
throw new ArgumentNullException(nameof(user));
117118
}
118119

119-
Attempt<int> asInt = user.Id.TryConvertTo<int>();
120-
if (asInt == false)
120+
if (!int.TryParse(user.Id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var asInt))
121121
{
122122
//TODO: should this be thrown, or an identity result?
123-
throw new InvalidOperationException("The user id must be an integer to work with Umbraco");
123+
throw new InvalidOperationException("The user id must be an integer to work with the Umbraco");
124124
}
125-
126125
using IScope scope = _scopeProvider.CreateScope(autoComplete: true);
127126

128-
IMember found = _memberService.GetById(asInt.Result);
127+
IMember found = _memberService.GetById(asInt);
129128
if (found != null)
130129
{
131130
// we have to remember whether Logins property is dirty, since the UpdateMemberProperties will reset it.

src/Umbraco.Web.BackOffice/Trees/ContentTypeTreeController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ protected override ActionResult<TreeNode> CreateRootNode(FormCollection queryStr
5555

5656
protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, FormCollection queryStrings)
5757
{
58-
var intId = id.TryConvertTo<int>();
59-
if (intId == false) throw new InvalidOperationException("Id must be an integer");
58+
if (!int.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intId))
59+
{
60+
throw new InvalidOperationException("Id must be an integer");
61+
}
6062

6163
var nodes = new TreeNodeCollection();
6264

6365
nodes.AddRange(
64-
_entityService.GetChildren(intId.Result, UmbracoObjectTypes.DocumentTypeContainer)
66+
_entityService.GetChildren(intId, UmbracoObjectTypes.DocumentTypeContainer)
6567
.OrderBy(entity => entity.Name)
6668
.Select(dt =>
6769
{
@@ -76,7 +78,7 @@ protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, Form
7678
//if the request is for folders only then just return
7779
if (queryStrings["foldersonly"].ToString().IsNullOrWhiteSpace() == false && queryStrings["foldersonly"] == "1") return nodes;
7880

79-
var children = _entityService.GetChildren(intId.Result, UmbracoObjectTypes.DocumentType).ToArray();
81+
var children = _entityService.GetChildren(intId, UmbracoObjectTypes.DocumentType).ToArray();
8082
var contentTypes = _contentTypeService.GetAll(children.Select(c => c.Id).ToArray()).ToDictionary(c => c.Id);
8183
nodes.AddRange(
8284
children

src/Umbraco.Web.BackOffice/Trees/DataTypeTreeController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public DataTypeTreeController(ILocalizedTextService localizedTextService, Umbrac
4343

4444
protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, FormCollection queryStrings)
4545
{
46-
var intId = id.TryConvertTo<int>();
47-
if (intId == false) throw new InvalidOperationException("Id must be an integer");
46+
if (!int.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intId))
47+
{
48+
throw new InvalidOperationException("Id must be an integer");
49+
}
4850

4951
var nodes = new TreeNodeCollection();
5052

5153
//Folders first
5254
nodes.AddRange(
53-
_entityService.GetChildren(intId.Result, UmbracoObjectTypes.DataTypeContainer)
55+
_entityService.GetChildren(intId, UmbracoObjectTypes.DataTypeContainer)
5456
.OrderBy(entity => entity.Name)
5557
.Select(dt =>
5658
{
@@ -68,7 +70,7 @@ protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, Form
6870
//System ListView nodes
6971
var systemListViewDataTypeIds = GetNonDeletableSystemListViewDataTypeIds();
7072

71-
var children = _entityService.GetChildren(intId.Result, UmbracoObjectTypes.DataType).ToArray();
73+
var children = _entityService.GetChildren(intId, UmbracoObjectTypes.DataType).ToArray();
7274
var dataTypes = Enumerable.ToDictionary(_dataTypeService.GetAll(children.Select(c => c.Id).ToArray()), dt => dt.Id);
7375

7476
nodes.AddRange(

src/Umbraco.Web.BackOffice/Trees/MediaTypeTreeController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public MediaTypeTreeController(ILocalizedTextService localizedTextService, Umbra
4242

4343
protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, FormCollection queryStrings)
4444
{
45-
var intId = id.TryConvertTo<int>();
46-
if (intId == false) throw new InvalidOperationException("Id must be an integer");
45+
if (!int.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intId))
46+
{
47+
throw new InvalidOperationException("Id must be an integer");
48+
}
4749

4850
var nodes = new TreeNodeCollection();
4951

5052
nodes.AddRange(
51-
_entityService.GetChildren(intId.Result, UmbracoObjectTypes.MediaTypeContainer)
53+
_entityService.GetChildren(intId, UmbracoObjectTypes.MediaTypeContainer)
5254
.OrderBy(entity => entity.Name)
5355
.Select(dt =>
5456
{
@@ -66,7 +68,7 @@ protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, Form
6668
var mediaTypes = _mediaTypeService.GetAll();
6769

6870
nodes.AddRange(
69-
_entityService.GetChildren(intId.Result, UmbracoObjectTypes.MediaType)
71+
_entityService.GetChildren(intId, UmbracoObjectTypes.MediaType)
7072
.OrderBy(entity => entity.Name)
7173
.Select(dt =>
7274
{

0 commit comments

Comments
 (0)