sys_role_data.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from . import mpfun
  4. from models import *
  5. from utils import *
  6. from sqlalchemy.orm import Session
  7. from database import get_local_db
  8. # 角色表
  9. # 加密和HMAC签名
  10. def sign_row(db: Session, row: SysRole) -> None:
  11. if row.sign != '':
  12. return
  13. role_id = str(row.role_id) # 角色ID
  14. role_name = mpfun.base64_data(row.role_name) # 角色名称
  15. role_key = row.role_key # 角色代码
  16. role_sort = str(row.role_sort) # 排序
  17. data_scope = row.data_scope # 数据范围
  18. menu_check_strictly = str(row.menu_check_strictly) # 菜单树选择项是否关联显示
  19. dept_check_strictly = str(row.dept_check_strictly) # 部门树选择项是否关联显示
  20. status = str(row.status) # 状态
  21. del_flag = row.del_flag # 删除标志
  22. sign_data = ",".join([role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag])
  23. sign_hmac = mpfun.sign_data(sign_data)
  24. # print('sign_tbl_post sign_data:', sign_data)
  25. # print('sign_tbl_post sign_hmac:', sign_hmac)
  26. row.sign = sign_hmac
  27. db.commit()
  28. # 比较字段合并字符串是否和MAC值匹配上,调用密码服务器[验证HMAC]接口
  29. def sign_valid_row(row: SysRole) -> bool:
  30. return True
  31. if row.sign == '':
  32. return True
  33. # 关键字段合并字符串
  34. sign_data = get_sign_str(row)
  35. # print('sys_post sign_data:', sign_data)
  36. # 原HMACSM3数值
  37. sign_hmac = row.sign
  38. # print('sign_hmac:', sign_hmac)
  39. return mpfun.hmac_verify(sign_data, sign_hmac)
  40. # 生成待签名的字符串
  41. def get_sign_str(row: SysRole) -> str:
  42. role_id = str(row.role_id) # 角色ID
  43. role_name = mpfun.base64_data(row.role_name) # 角色名称
  44. role_key = row.role_key # 角色代码
  45. role_sort = str(row.role_sort) # 排序
  46. data_scope = row.data_scope # 数据范围
  47. menu_check_strictly = str(row.menu_check_strictly) # 菜单树选择项是否关联显示
  48. dept_check_strictly = str(row.dept_check_strictly) # 部门树选择项是否关联显示
  49. status = str(row.status) # 状态
  50. del_flag = row.del_flag # 删除标志
  51. # 关键字段合并字符串
  52. sign_data = ",".join([role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag])
  53. return sign_data
  54. # 生成HAMC签名值
  55. def get_sign_hmac(row: SysRole) -> str:
  56. sign_data = get_sign_str(row)
  57. return mpfun.sign_data(sign_data)
  58. # 对所有数据进行签名
  59. def sign_table():
  60. print('sign_sys_role table =====>>>')
  61. with get_local_db() as db:
  62. rows = db.query(SysRole).filter(SysRole.sign == '').all()
  63. for row in rows:
  64. sign_row(db, row)