|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +namespace Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model; |
| 4 | + |
| 5 | +public enum StatementEffect |
| 6 | +{ |
| 7 | + Deny = 0, |
| 8 | + Allow = 1 |
| 9 | +} |
| 10 | + |
| 11 | +public class ControlPolicyModel |
| 12 | +{ |
| 13 | + public Guid Id { get; set; } |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// 策略名称,便于识别和管理 |
| 17 | + /// </summary> |
| 18 | + public string Name { get; set; } = string.Empty; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// 策略效果:Allow 或 Deny |
| 22 | + /// </summary> |
| 23 | + public StatementEffect Effect { get; set; } = StatementEffect.Deny; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// 策略优先级,数值越大优先级越高 |
| 27 | + /// </summary> |
| 28 | + public int Priority { get; set; } = 0; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// 是否启用此策略 |
| 32 | + /// </summary> |
| 33 | + public bool Enabled { get; set; } = true; |
| 34 | + |
| 35 | + [JsonConverter(typeof(ActionIdentifierConverter))] |
| 36 | + public List<ActionIdentifierModel> Actions { get; set; } = new(); |
| 37 | + |
| 38 | + [JsonConverter(typeof(ResourceIdentifierConverter))] |
| 39 | + public List<ResourceIdentifierModel> Resources { get; set; } = new(); |
| 40 | +} |
| 41 | + |
| 42 | +public class ActionIdentifierModel |
| 43 | +{ |
| 44 | + public string Resource { get; set; } = "*"; |
| 45 | + |
| 46 | + public string Type { get; set; } = "*"; |
| 47 | + |
| 48 | + public string Operation { get; set; } = "*"; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// 从字符串解析ActionIdentifier,格式为 Resource:Type:Operation |
| 52 | + /// </summary> |
| 53 | + /// <param name="actionName">操作标识符字符串</param> |
| 54 | + public ActionIdentifierModel(string? actionName = null) |
| 55 | + { |
| 56 | + if (string.IsNullOrWhiteSpace(actionName)) |
| 57 | + { |
| 58 | + return; // 使用默认值 "*" |
| 59 | + } |
| 60 | + |
| 61 | + var parts = actionName.Split(':', StringSplitOptions.None); |
| 62 | + |
| 63 | + if (parts.Length >= 1 && !string.IsNullOrEmpty(parts[0])) |
| 64 | + Resource = parts[0]; |
| 65 | + |
| 66 | + if (parts.Length >= 2 && !string.IsNullOrEmpty(parts[1])) |
| 67 | + Type = parts[1]; |
| 68 | + |
| 69 | + if (parts.Length >= 3 && !string.IsNullOrEmpty(parts[2])) |
| 70 | + Operation = parts[2]; |
| 71 | + } |
| 72 | + |
| 73 | + public override string ToString() |
| 74 | + { |
| 75 | + return $"{Resource}:{Type}:{Operation}"; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +public class ActionIdentifierConverter : JsonConverter<List<ActionIdentifierModel>> |
| 80 | +{ |
| 81 | + public override List<ActionIdentifierModel> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 82 | + { |
| 83 | + if (reader.TokenType == JsonTokenType.String) |
| 84 | + { |
| 85 | + var actionName = reader.GetString(); |
| 86 | + return new List<ActionIdentifierModel> { new ActionIdentifierModel(actionName) }; |
| 87 | + } |
| 88 | + else if (reader.TokenType == JsonTokenType.StartArray) |
| 89 | + { |
| 90 | + var actionNames = JsonSerializer.Deserialize<List<string>>(ref reader, options); |
| 91 | + return actionNames?.ConvertAll(actionName => new ActionIdentifierModel(actionName)) ?? new List<ActionIdentifierModel>(); |
| 92 | + } |
| 93 | + throw new JsonException("Unexpected token type."); |
| 94 | + } |
| 95 | + |
| 96 | + public override void Write(Utf8JsonWriter writer, List<ActionIdentifierModel> value, JsonSerializerOptions options) |
| 97 | + { |
| 98 | + if (value == null || value.Count == 0) |
| 99 | + { |
| 100 | + writer.WriteStringValue("*"); |
| 101 | + } |
| 102 | + else if (value.Count == 1) |
| 103 | + { |
| 104 | + writer.WriteStringValue(value[0].ToString()); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + var actionNames = value?.Select(action => action.ToString()) ?? new List<string>(); |
| 109 | + JsonSerializer.Serialize(writer, actionNames, options); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +public class ResourceIdentifierModel |
| 115 | +{ |
| 116 | + public string Service { get; set; } = "*"; |
| 117 | + |
| 118 | + public string Region { get; set; } = "*"; |
| 119 | + |
| 120 | + public string Identifier { get; set; } = "*"; |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// 从字符串解析Resource,格式为 Service:Region:Identifier |
| 124 | + /// </summary> |
| 125 | + /// <param name="resource">资源标识符字符串</param> |
| 126 | + public ResourceIdentifierModel(string? resource = null) |
| 127 | + { |
| 128 | + if (string.IsNullOrWhiteSpace(resource)) |
| 129 | + { |
| 130 | + return; // 使用默认值 "*" |
| 131 | + } |
| 132 | + |
| 133 | + var parts = resource.Split(':', StringSplitOptions.None); |
| 134 | + |
| 135 | + if (parts.Length >= 1 && !string.IsNullOrEmpty(parts[0])) |
| 136 | + Service = parts[0]; |
| 137 | + |
| 138 | + if (parts.Length >= 2 && !string.IsNullOrEmpty(parts[1])) |
| 139 | + Region = parts[1]; |
| 140 | + |
| 141 | + if (parts.Length >= 3 && !string.IsNullOrEmpty(parts[2])) |
| 142 | + Identifier = parts[2]; |
| 143 | + } |
| 144 | + |
| 145 | + public override string ToString() |
| 146 | + { |
| 147 | + return $"{Service}:{Region}:{Identifier}"; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/// <summary> |
| 152 | +/// Resource的JSON转换器 |
| 153 | +/// </summary> |
| 154 | +public class ResourceIdentifierConverter : JsonConverter<List<ResourceIdentifierModel>> |
| 155 | +{ |
| 156 | + public override List<ResourceIdentifierModel> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 157 | + { |
| 158 | + if (reader.TokenType == JsonTokenType.String) |
| 159 | + { |
| 160 | + var resource = reader.GetString(); |
| 161 | + return new List<ResourceIdentifierModel> { new ResourceIdentifierModel(resource) }; |
| 162 | + } |
| 163 | + else if (reader.TokenType == JsonTokenType.StartArray) |
| 164 | + { |
| 165 | + var resources = JsonSerializer.Deserialize<List<string>>(ref reader, options); |
| 166 | + return resources?.ConvertAll(resource => new ResourceIdentifierModel(resource)) ?? new List<ResourceIdentifierModel>(); |
| 167 | + } |
| 168 | + throw new JsonException("Unexpected token type."); |
| 169 | + } |
| 170 | + |
| 171 | + public override void Write(Utf8JsonWriter writer, List<ResourceIdentifierModel> value, JsonSerializerOptions options) |
| 172 | + { |
| 173 | + if (value == null || value.Count == 0) |
| 174 | + { |
| 175 | + writer.WriteStringValue("*"); |
| 176 | + } |
| 177 | + else if (value.Count == 1) |
| 178 | + { |
| 179 | + writer.WriteStringValue(value[0].ToString()); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + var resources = value?.Select(action => action.ToString()) ?? new List<string>(); |
| 184 | + JsonSerializer.Serialize(writer, resources, options); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments