Skip to content

Commit

Permalink
refactor!: #443 rename AllowAuthorized to AllowAuthenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Sep 16, 2024
1 parent c23736a commit 7fea229
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
27 changes: 20 additions & 7 deletions src/IntelliTect.Coalesce/DataAnnotations/SecurityAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using IntelliTect.Coalesce.Helpers;
using IntelliTect.Coalesce.TypeDefinition;
using IntelliTect.Coalesce.TypeDefinition;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,7 +9,7 @@ namespace IntelliTect.Coalesce.DataAnnotations
public abstract class SecurityAttribute : Attribute
#pragma warning restore RCS1203 // Use AttributeUsageAttribute.
{
public virtual SecurityPermissionLevels PermissionLevel { get; set; } = SecurityPermissionLevels.AllowAuthorized;
public virtual SecurityPermissionLevels PermissionLevel { get; set; } = SecurityPermissionLevels.AllowAuthenticated;

public string Roles { get; set; } = "";
}
Expand All @@ -26,7 +25,7 @@ public static SecurityPermission GetSecurityPermission<TAttribute>(this IAttribu
return new SecurityPermission(name);
}

var level = parent.GetAttributeValue<TAttribute, SecurityPermissionLevels>(a => a.PermissionLevel) ?? SecurityPermissionLevels.AllowAuthorized;
var level = parent.GetAttributeValue<TAttribute, SecurityPermissionLevels>(a => a.PermissionLevel) ?? SecurityPermissionLevels.AllowAuthenticated;
object attributeRoles = parent.GetAttributeValue<TAttribute>(nameof(SecurityAttribute.Roles)) ?? "";

// This will happen in roslyn-based contexts due to us also accepting string arrays for the roles.
Expand All @@ -41,7 +40,7 @@ public static SecurityPermission GetSecurityPermission<TAttribute>(this IAttribu
}

return new SecurityPermission(
level: parent.GetAttributeValue<TAttribute, SecurityPermissionLevels>(a => a.PermissionLevel) ?? SecurityPermissionLevels.AllowAuthorized,
level: parent.GetAttributeValue<TAttribute, SecurityPermissionLevels>(a => a.PermissionLevel) ?? SecurityPermissionLevels.AllowAuthenticated,
roles: rolesString,
name: name
);
Expand All @@ -50,8 +49,22 @@ public static SecurityPermission GetSecurityPermission<TAttribute>(this IAttribu

public enum SecurityPermissionLevels
{
/// <summary>
/// Allow all users, including anonymous, unauthenticated users.
/// </summary>
AllowAll = 1,
AllowAuthorized = 2,
DenyAll = 3

/// <summary>
/// Allow only authenticated users. This can be further restricted by specifying one or more allowed roles on the attribute.
/// </summary>
AllowAuthenticated = 2,

/// <summary>
/// Disable the action/endpoint. Where applicable, generated code for the endpoint will be omitted entirely.
/// </summary>
DenyAll = 3,

[Obsolete("Renamed to AllowAuthenticated")]
AllowAuthorized = AllowAuthenticated,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ClassSecurityInfo(ClassViewModel classViewModel)
notReadable ? SecurityPermissionLevels.DenyAll :
readAttribute.NoAccess ? SecurityPermissionLevels.DenyAll :
allowAnonymousAny ? SecurityPermissionLevels.AllowAll :
SecurityPermissionLevels.AllowAuthorized,
SecurityPermissionLevels.AllowAuthenticated,
roles: readAttribute.HasRoles
? readAttribute.RoleLists.Union(editAttribute.RoleLists).Union(createAttribute.RoleLists).Union(deleteAttribute.RoleLists)
.SelectMany(r => r)
Expand Down Expand Up @@ -87,7 +87,7 @@ public ClassSecurityInfo(ClassViewModel classViewModel)
notMutable ? SecurityPermissionLevels.DenyAll :
createAttribute.NoAccess && editAttribute.NoAccess ? SecurityPermissionLevels.DenyAll :
createAttribute.AllowAnonymous || editAttribute.AllowAnonymous ? SecurityPermissionLevels.AllowAll :
SecurityPermissionLevels.AllowAuthorized,
SecurityPermissionLevels.AllowAuthenticated,
roles: createAttribute.HasRoles && editAttribute.HasRoles
? $"{createAttribute.Roles},{editAttribute.Roles}"
: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal SecurityPermission(SecurityPermissionLevels level, IEnumerable<string>?
}

public bool HasAttribute { get; }
public SecurityPermissionLevels PermissionLevel { get; } = SecurityPermissionLevels.AllowAuthorized;
public SecurityPermissionLevels PermissionLevel { get; } = SecurityPermissionLevels.AllowAuthenticated;

public bool AllowAnonymous => PermissionLevel == SecurityPermissionLevels.AllowAll;

Expand Down

0 comments on commit 7fea229

Please sign in to comment.