This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3c06cf
commit aad60f0
Showing
6 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BrackeysBot.API.Permissions; | ||
|
||
/// <summary> | ||
/// Represents a permission. | ||
/// </summary> | ||
public sealed class Permission : IEquatable<Permission> | ||
{ | ||
[JsonPropertyName("ids")] [JsonInclude] | ||
private string[] _ids; | ||
|
||
[JsonConstructor] | ||
private Permission() | ||
{ | ||
_ids = Array.Empty<string>(); | ||
Name = string.Empty; | ||
Type = PermissionType.Everyone; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Permission" /> class. | ||
/// </summary> | ||
/// <param name="name">The name of the permission.</param> | ||
/// <param name="type">The type of the permission.</param> | ||
/// <param name="ids">The set of applicable IDs.</param> | ||
public Permission(string name, PermissionType type, params string[] ids) | ||
{ | ||
Name = name; | ||
Type = type; | ||
_ids = ids.ToArray(); // defensive copy | ||
} | ||
|
||
/// <summary> | ||
/// Gets the set of IDs applicable to this permission. | ||
/// </summary> | ||
/// <value>The set of applicable IDs.</value> | ||
public IReadOnlyList<string> Ids => _ids.ToArray(); // defensive copy | ||
|
||
/// <summary> | ||
/// Gets the name of the permission. | ||
/// </summary> | ||
/// <value>The name of the permission.</value> | ||
[JsonPropertyName("name")] | ||
[JsonInclude] | ||
public string Name { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the type of the permission. | ||
/// </summary> | ||
/// <value>The type of the permission.</value> | ||
[JsonPropertyName("type")] | ||
[JsonInclude] | ||
public PermissionType Type { get; private set; } | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(Permission? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return Name == other.Name; | ||
} | ||
|
||
public static bool operator ==(Permission? left, Permission? right) | ||
{ | ||
if (left is null) return right is null; | ||
return left.Equals(right); | ||
} | ||
|
||
public static bool operator !=(Permission? left, Permission? right) | ||
{ | ||
return !(left == right); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
{ | ||
return ReferenceEquals(this, obj) || (obj is Permission other && Equals(other)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] | ||
public override int GetHashCode() | ||
{ | ||
return Name.GetHashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace BrackeysBot.API.Permissions; | ||
|
||
/// <summary> | ||
/// An enumeration of permission types. | ||
/// </summary> | ||
public enum PermissionType | ||
{ | ||
/// <summary> | ||
/// Defines that the permission is granted to everybody. | ||
/// </summary> | ||
Everyone, | ||
|
||
/// <summary> | ||
/// Defines that the permission is granted to a set of roles. | ||
/// </summary> | ||
Role, | ||
|
||
/// <summary> | ||
/// Defines that the permission is granted to a set of users. | ||
/// </summary> | ||
User | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using BrackeysBot.API.Extensions; | ||
using DisCatSharp.CommandsNext; | ||
using DisCatSharp.CommandsNext.Attributes; | ||
|
||
namespace BrackeysBot.API.Permissions; | ||
|
||
/// <summary> | ||
/// Defines valid permissions for this command or group. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
public sealed class RequirePermissionAttribute : CheckBaseAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RequirePermissionAttribute" /> class. | ||
/// </summary> | ||
/// <param name="permissionName">The permission node name.</param> | ||
public RequirePermissionAttribute(string permissionName) | ||
{ | ||
if (string.IsNullOrWhiteSpace(permissionName)) throw new ArgumentNullException(nameof(permissionName)); | ||
PermissionName = permissionName; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the permission node. | ||
/// </summary> | ||
/// <value>The permission node name.</value> | ||
public string PermissionName { get; } | ||
|
||
/// <inheritdoc /> | ||
public override Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help) | ||
{ | ||
return Task.FromResult(ctx.HasPermission(PermissionName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters