-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConnectionCombinationRules.cs
More file actions
198 lines (172 loc) · 7.41 KB
/
ConnectionCombinationRules.cs
File metadata and controls
198 lines (172 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Altinn.AccessManagement.Core.Errors;
using Altinn.AccessMgmt.Core.Validation;
using Altinn.Authorization.Api.Contracts.AccessManagement;
using Altinn.Authorization.ProblemDetails;
namespace Altinn.AccessManagement.Api.Enduser.Validation;
/// <summary>
/// Cross-field (semantic) validation rules that relate multiple connection parameters.
/// Assumes atomic parameter validation (ConnectionParameterRules) has already run.
/// Only adds errors for relational constraints; skips work if any involved value is syntactically invalid.
/// </summary>
internal static class ConnectionCombinationRules
{
/// <summary>
/// party must equal from (used for add connection / add assignment scenarios where from must be actor party).
/// </summary>
internal static RuleExpression PartyEqualsFrom(string party, string from) => () =>
{
if (!Guid.TryParse(party, out var partyId) || partyId == Guid.Empty)
{
return null;
}
if (!Guid.TryParse(from, out var fromId) || fromId == Guid.Empty)
{
return null;
}
if (partyId != fromId)
{
return (ref ValidationErrorBuilder errors) =>
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/from", [new("from", ValidationErrorMessageTexts.PartyMustMatchFrom)]);
}
return null;
};
/// <summary>
/// party must match either from or to (used for read scenarios or remove connection where actor can be either side).
/// </summary>
internal static RuleExpression PartyMatchesFromOrTo(string party, string from, string to) => () =>
{
if (!Guid.TryParse(party, out var partyId) || partyId == Guid.Empty)
{
return null;
}
bool fromValid = Guid.TryParse(from, out var fromId) && fromId != Guid.Empty;
bool toValid = Guid.TryParse(to, out var toId) && toId != Guid.Empty;
if (!fromValid && !toValid)
{
return null;
}
if ((fromValid && fromId == partyId) || (toValid && toId == partyId))
{
return null;
}
return (ref ValidationErrorBuilder errors) =>
{
if (fromValid)
{
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/from", [new("from", ValidationErrorMessageTexts.FromOrToMustMatchParty)]);
}
if (toValid)
{
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/to", [new("to", ValidationErrorMessageTexts.FromOrToMustMatchParty)]);
}
};
};
/// <summary>
/// For remove scenarios: party must match from or to; both from and to required.
/// </summary>
internal static RuleExpression RemovePartyMatchesFromOrTo(string party, string from, string to) => () =>
{
if (!Guid.TryParse(party, out var partyId) || partyId == Guid.Empty)
{
return null;
}
if (!Guid.TryParse(from, out var fromId) || fromId == Guid.Empty)
{
return null;
}
if (!Guid.TryParse(to, out var toId) || toId == Guid.Empty)
{
return null;
}
if (partyId == fromId || partyId == toId)
{
return null;
}
return (ref ValidationErrorBuilder errors) =>
{
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/from", [new("from", ValidationErrorMessageTexts.FromOrToMustMatchParty)]);
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/to", [new("to", ValidationErrorMessageTexts.FromOrToMustMatchParty)]);
};
};
/// <summary>
/// Prevents self-delegation: from and to must be different parties.
/// Used in add/update scenarios to ensure a party cannot delegate to itself.
/// </summary>
internal static RuleExpression FromAndToMustBeDifferent(string from, string to) => () =>
{
if (!Guid.TryParse(from, out var fromId) || fromId == Guid.Empty)
{
return null;
}
if (!Guid.TryParse(to, out var toId) || toId == Guid.Empty)
{
return null;
}
if (fromId == toId)
{
return (ref ValidationErrorBuilder errors) =>
{
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/from", [new("from", ValidationErrorMessageTexts.SelfDelegationNotAllowed)]);
errors.Add(ValidationErrors.InvalidQueryParameter, "QUERY/to", [new("to", ValidationErrorMessageTexts.SelfDelegationNotAllowed)]);
};
}
return null;
};
/// <summary>
/// Package reference must be exactly one of (packageId, packageUrn). Reject empty Guid.
/// </summary>
internal static RuleExpression ExclusivePackageReference(Guid? packageId, string packageUrn, string idName = "packageId", string urnName = "package") => () =>
{
var urnProvided = !string.IsNullOrWhiteSpace(packageUrn);
var idProvided = packageId.HasValue && packageId.Value != Guid.Empty;
if (idProvided ^ urnProvided)
{
return null; // exactly one OK
}
if (idProvided && urnProvided)
{
return (ref ValidationErrorBuilder errors) =>
{
errors.Add(ValidationErrors.InvalidQueryParameter, $"QUERY/{idName}", [new(idName, ValidationErrorMessageTexts.ProvideEitherPackageRef)]);
errors.Add(ValidationErrors.InvalidQueryParameter, $"QUERY/{urnName}", [new(urnName, ValidationErrorMessageTexts.ProvideEitherPackageRef)]);
};
}
if (packageId.HasValue && packageId.Value == Guid.Empty)
{
return (ref ValidationErrorBuilder errors) =>
errors.Add(ValidationErrors.InvalidQueryParameter, $"QUERY/{idName}", [new(idName, ValidationErrorMessageTexts.PackageIdMustNotBeEmpty)]);
}
return (ref ValidationErrorBuilder errors) =>
{
errors.Add(ValidationErrors.InvalidQueryParameter, $"QUERY/{idName}", [new(idName, ValidationErrorMessageTexts.RequireOnePackageRef)]);
errors.Add(ValidationErrors.InvalidQueryParameter, $"QUERY/{urnName}", [new(urnName, ValidationErrorMessageTexts.RequireOnePackageRef)]);
};
};
/// <summary>
/// Validates that user is authorized to delegate all requested rules based on delegation check results.
/// </summary>
/// <param name="requestedRules">The rule keys that the user wants to delegate.</param>
/// <param name="delegationCheckResult">The result from ResourceDelegationCheck indicating which rules can be delegated.</param>
internal static RuleExpression DelegationAuthorization(IEnumerable<string> requestedRules, ResourceCheckDto delegationCheckResult) => () =>
{
var failedRules = new List<string>();
foreach (var ruleKey in requestedRules)
{
if (!delegationCheckResult.Rules.Any(r => r.Rule.Key == ruleKey && r.Result))
{
failedRules.Add(ruleKey);
}
}
if (failedRules.Count == 0)
{
return null;
}
return (ref ValidationErrorBuilder errors) =>
{
foreach (var failedRule in failedRules)
{
errors.Add(ValidationErrors.UserNotAuthorized, $"$BODY/directRuleKeys/{failedRule}", [new("ruleKey", ValidationErrorMessageTexts.NotAuthorizedToDelegateRule)]);
}
};
};
}