Skip to content

Commit b4eb1f9

Browse files
committed
feat: add JSON string support for ABAC enforcement (#59)
1 parent 5f3ce04 commit b4eb1f9

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

lib/src/utils/utils.dart

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,53 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import 'dart:convert';
16+
1517
import 'package:collection/collection.dart';
1618
import 'package:expressions/expressions.dart';
1719

20+
import '../abac/abac_class.dart';
1821
import '../model/assertion.dart';
1922

2023
class CasbinEvaluator extends ExpressionEvaluator {
2124
const CasbinEvaluator();
2225

2326
@override
2427
dynamic evalMemberExpression(
25-
MemberExpression expression, Map<String, dynamic> context) {
26-
var object = eval(expression.object, context).toMap();
28+
MemberExpression expression,
29+
Map<String, dynamic> context,
30+
) {
31+
var objectValue = eval(expression.object, context);
32+
Map<String, dynamic> object;
33+
34+
// Handle different types of objects
35+
if (objectValue is String) {
36+
// Try to parse as JSON
37+
try {
38+
var parsed = jsonDecode(objectValue);
39+
if (parsed is Map<String, dynamic>) {
40+
object = parsed;
41+
} else {
42+
throw Exception('JSON string must represent an object/map');
43+
}
44+
} catch (e) {
45+
throw Exception('Failed to parse JSON string: $e');
46+
}
47+
} else if (objectValue is AbacClass) {
48+
object = objectValue.toMap();
49+
} else if (objectValue is Map<String, dynamic>) {
50+
object = objectValue;
51+
} else {
52+
// Fall back to trying toMap() for backward compatibility
53+
try {
54+
object = objectValue.toMap();
55+
} catch (e) {
56+
throw Exception(
57+
'Object must be a JSON string, Map, or implement AbacClass: ${objectValue.runtimeType}',
58+
);
59+
}
60+
}
61+
2762
return object[expression.property.name];
2863
}
2964
}

0 commit comments

Comments
 (0)