|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +import 'dart:convert'; |
| 16 | + |
15 | 17 | import 'package:collection/collection.dart'; |
16 | 18 | import 'package:expressions/expressions.dart'; |
17 | 19 |
|
| 20 | +import '../abac/abac_class.dart'; |
18 | 21 | import '../model/assertion.dart'; |
19 | 22 |
|
20 | 23 | class CasbinEvaluator extends ExpressionEvaluator { |
21 | 24 | const CasbinEvaluator(); |
22 | 25 |
|
23 | 26 | @override |
24 | 27 | 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 | + |
27 | 62 | return object[expression.property.name]; |
28 | 63 | } |
29 | 64 | } |
|
0 commit comments