diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index 0c9b9c4..b8ae449 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -12,9 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +import 'dart:convert'; + import 'package:collection/collection.dart'; import 'package:expressions/expressions.dart'; +import '../abac/abac_class.dart'; import '../model/assertion.dart'; class CasbinEvaluator extends ExpressionEvaluator { @@ -22,8 +25,40 @@ class CasbinEvaluator extends ExpressionEvaluator { @override dynamic evalMemberExpression( - MemberExpression expression, Map context) { - var object = eval(expression.object, context).toMap(); + MemberExpression expression, + Map context, + ) { + var objectValue = eval(expression.object, context); + Map object; + + // Handle different types of objects + if (objectValue is String) { + // Try to parse as JSON + try { + var parsed = jsonDecode(objectValue); + if (parsed is Map) { + object = parsed; + } else { + throw Exception('JSON string must represent an object/map'); + } + } catch (e) { + throw Exception('Failed to parse JSON string: $e'); + } + } else if (objectValue is AbacClass) { + object = objectValue.toMap(); + } else if (objectValue is Map) { + object = objectValue; + } else { + // Fall back to trying toMap() for backward compatibility + try { + object = objectValue.toMap(); + } catch (e) { + throw Exception( + 'Object must be a JSON string, Map, or implement AbacClass: ${objectValue.runtimeType}', + ); + } + } + return object[expression.property.name]; } }