Skip to content

Commit ae05921

Browse files
committed
file handling for json policies
1 parent a5c18b0 commit ae05921

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

backend/app/alembic/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ def update_casbin_policies(conn: Connection, file_path: str):
99
Update Casbin policies from a JSON file and insert into the casbin_rule table.
1010
Warning : This will delete all the existing policies
1111
"""
12-
with open(file_path) as f:
13-
data = json.load(f)
12+
try:
13+
with open(file_path) as f:
14+
data = json.load(f)
15+
except FileNotFoundError:
16+
raise ValueError(f'Policy file not found: {file_path}')
1417

1518
# Clear all existing policies
1619
conn.execute(text("DELETE FROM casbin_rule WHERE ptype = 'p'"))
1720

1821
for policy in data.get("permissions", []):
19-
role = policy["role"]
20-
resource = policy["resource"]
21-
actions = policy["actions"]
22+
try:
23+
role = policy["role"]
24+
resource = policy["resource"]
25+
actions = policy["actions"]
26+
except KeyError as e:
27+
raise ValueError(f'Missing required field in policy: {str(e)}')
2228

2329
for action in actions:
2430
conn.execute(

0 commit comments

Comments
 (0)