Skip to content

Commit

Permalink
v3.27.1
Browse files Browse the repository at this point in the history
- *Fixed:* Updated `Microsoft.Extensions.Caching.Memory` package depenedency to latest (including related); resolve [Microsoft Security Advisory CVE-2024-43483](GHSA-qj66-m88j-hmgj).
- *Fixed:* Fixed the `ExecutionContext.UserIsAuthorized` to have base implementation similar to `UserIsInRole`.
- *Fixed:* Rationalize the `UtcNow` usage to be consistent, where applicable `ExecutionContext.SystemTime.UtcNow` is leveraged.
  • Loading branch information
chullybun committed Oct 15, 2024
1 parent 6738a92 commit 331a1dc
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Represents the **NuGet** versions.

## v3.27.1
- *Fixed:* Updated `Microsoft.Extensions.Caching.Memory` package depenedency to latest (including related); resolve [Microsoft Security Advisory CVE-2024-43483](https://github.com/advisories/GHSA-qj66-m88j-hmgj).
- *Fixed:* Fixed the `ExecutionContext.UserIsAuthorized` to have base implementation similar to `UserIsInRole`.
- *Fixed:* Rationalize the `UtcNow` usage to be consistent, where applicable `ExecutionContext.SystemTime.UtcNow` is leveraged.

## v3.27.0
- *Fixed:* The `ValueContentResult.TryCreateValueContentResult` would return `NotModified` where the request `ETag` was `null`; this has been corrected to return `OK` with the resulting `value`.
- *Fixed:* The `ValueContentResult.TryCreateValueContentResult` now returns `ExtendedStatusCodeResult` versus `StatusCodeResult` as this offers additional capabilities where required.
Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.27.0</Version>
<Version>3.27.1</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
2 changes: 1 addition & 1 deletion src/CoreEx/CoreEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.31" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/CoreEx/Entities/ChangeLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public static IChangeLogAudit PrepareUpdated(IChangeLogAudit changeLog, Executio
/// <summary>
/// Gets the timestamp.
/// </summary>
private static DateTime GetTimestamp(ExecutionContext? ec) => ec != null ? ec.Timestamp : (ExecutionContext.HasCurrent ? ExecutionContext.Current.Timestamp : DateTime.UtcNow);
private static DateTime GetTimestamp(ExecutionContext? ec) => ec != null ? ec.Timestamp : ExecutionContext.SystemTime.UtcNow;
}
}
2 changes: 1 addition & 1 deletion src/CoreEx/Events/EventDataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public virtual void Format(EventData @event)
var value = @event.Value;

@event.Id ??= Guid.NewGuid().ToString();
@event.Timestamp ??= DateTimeOffset.UtcNow;
@event.Timestamp ??= new DateTimeOffset(ExecutionContext.SystemTime.UtcNow);

if (PropertySelection.HasFlag(EventDataProperty.Key))
{
Expand Down
30 changes: 21 additions & 9 deletions src/CoreEx/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ExecutionContext : ITenantId, IDisposable
private Lazy<ConcurrentDictionary<string, object?>> _properties = new(true);
private IReferenceDataContext? _referenceDataContext;
private HashSet<string>? _roles;
private HashSet<string>? _permissions;
private bool _disposed;
private readonly object _lock = new();

Expand Down Expand Up @@ -170,7 +171,7 @@ public static object GetRequiredService(Type type)
/// Gets or sets the timestamp for the <see cref="ExecutionContext"/> lifetime; i.e (to enable consistent execution-related timestamping).
/// </summary>
/// <remarks>Defaults the value from <see cref="ISystemTime"/>, where this has not been registered it will default to <see cref="DateTime.UtcNow"/>. The value will also be passed through <see cref="Cleaner.Clean(DateTime)"/>.</remarks>
public DateTime Timestamp { get => _timestamp ??= Cleaner.Clean(GetService<ISystemTime>()?.UtcNow ?? DateTime.UtcNow); set => _timestamp = Cleaner.Clean(value); }
public DateTime Timestamp { get => _timestamp ??= SystemTime.UtcNow; set => _timestamp = Cleaner.Clean(value); }

/// <summary>
/// Gets the <see cref="MessageItemCollection"/> to be passed back to the originating consumer.
Expand Down Expand Up @@ -249,18 +250,28 @@ public void Dispose()
/// Sets (replaces) the roles the current user is in (the roles should be unique).
/// </summary>
/// <param name="roles">The <see cref="IEnumerable{String}"/> of roles the user is in.</param>
public virtual void SetRoles(IEnumerable<string> roles) => _roles = new HashSet<string>(roles.Distinct());
public virtual void SetRoles(IEnumerable<string> roles) => _roles = new HashSet<string>(roles);

/// <summary>
/// Checks whether the user has the required <paramref name="permission"/>.
/// Gets the list of permissions for the <see cref="UserName"/> (as previously <see cref="SetPermissions(IEnumerable{string})">set</see>).
/// </summary>
public IEnumerable<string> GetPermissions() => _permissions == null ? Array.Empty<string>() : _permissions;

/// <summary>
/// Sets (replaces) the permissions the current user is in (the roles should be unique).
/// </summary>
/// <param name="roles">The <see cref="IEnumerable{String}"/> of roles the user is in.</param>
public virtual void SetPermissions(IEnumerable<string> roles) => _permissions = new HashSet<string>(roles);

/// <summary>
/// Checks whether the user has the required <paramref name="permission"/> (see <see cref="SetPermissions"/> and <see cref="GetPermissions"/>).
/// </summary>
/// <param name="permission">The permission to validate.</param>
/// <returns>The corresponding <see cref="Result"/>.</returns>
/// <remarks>This method is intended to be overridden; this implementation always returns <see cref="Result.AuthorizationError"/>.</remarks>
public virtual Result UserIsAuthorized(string permission)
{
permission.ThrowIfNullOrEmpty(nameof(permission));
return Result.AuthorizationError();
return _permissions is not null && _permissions.Contains(permission) ? Result.Success : Result.AuthorizationError();
}

/// <summary>
Expand All @@ -269,12 +280,13 @@ public virtual Result UserIsAuthorized(string permission)
/// <param name="entity">The entity name.</param>
/// <param name="action">The action name.</param>
/// <returns>The corresponding <see cref="Result"/>.</returns>
/// <remarks>This method is intended to be overridden; this implementation always returns <see cref="Result.AuthorizationError"/>.</remarks>
/// <remarks>This default implementation formats as <c>{entity}.{action}</c> and invokes <see cref="UserIsAuthorized(string)"/>.
/// <para>An example is <c>Customer</c> and <c>Create</c> formatted as <c>Customer.Create</c>.</para></remarks>
public virtual Result UserIsAuthorized(string entity, string action)
{
entity.ThrowIfNullOrEmpty(nameof(entity));
action.ThrowIfNullOrEmpty(nameof(action));
return Result.AuthorizationError();
return UserIsAuthorized($"{entity}.{action}");
}

/// <summary>
Expand All @@ -284,8 +296,8 @@ public virtual Result UserIsAuthorized(string entity, string action)
/// <returns>The corresponding <see cref="Result"/>.</returns>
public virtual Result UserIsInRole(string role)
{
var isInRole = (_roles != null) && _roles.TryGetValue(role, out _);
return isInRole ? Result.Success : Result.AuthorizationError();
role.ThrowIfNullOrEmpty(nameof(role));
return _roles is not null && _roles.Contains(role) ? Result.Success : Result.AuthorizationError();
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/CoreEx/RefData/Extended/ReferenceDataBaseEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public virtual bool IsActive

if (StartDate != null || EndDate != null)
{
var date = ExecutionContext.HasCurrent ? ExecutionContext.Current.ReferenceDataContext[GetType()] : Cleaner.Clean(DateTime.UtcNow, DateTimeTransform.DateOnly);
var date = ExecutionContext.HasCurrent ? ExecutionContext.Current.ReferenceDataContext[GetType()] : Cleaner.Clean(ExecutionContext.SystemTime.UtcNow, DateTimeTransform.DateOnly);
if (StartDate != null && date < StartDate)
return false;

Expand Down

0 comments on commit 331a1dc

Please sign in to comment.