sys_role_data.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if row.sign == '':
  31. return True
  32. # 关键字段合并字符串
  33. sign_data = get_sign_str(row)
  34. # print('sys_post sign_data:', sign_data)
  35. # 原HMACSM3数值
  36. sign_hmac = row.sign
  37. # print('sign_hmac:', sign_hmac)
  38. return mpfun.hmac_verify(sign_data, sign_hmac)
  39. # 生成待签名的字符串
  40. def get_sign_str(row: SysRole) -> str:
  41. role_id = str(row.role_id) # 角色ID
  42. role_name = mpfun.base64_data(row.role_name) # 角色名称
  43. role_key = row.role_key # 角色代码
  44. role_sort = str(row.role_sort) # 排序
  45. data_scope = row.data_scope # 数据范围
  46. menu_check_strictly = str(row.menu_check_strictly) # 菜单树选择项是否关联显示
  47. dept_check_strictly = str(row.dept_check_strictly) # 部门树选择项是否关联显示
  48. status = str(row.status) # 状态
  49. del_flag = row.del_flag # 删除标志
  50. # 关键字段合并字符串
  51. sign_data = ",".join([role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag])
  52. return sign_data
  53. # 生成HAMC签名值
  54. def get_sign_hmac(row: SysRole) -> str:
  55. sign_data = get_sign_str(row)
  56. return mpfun.sign_data(sign_data)
  57. # 对所有数据进行签名
  58. def sign_table():
  59. print('sign_sys_role table =====>>>')
  60. with get_local_db() as db:
  61. rows = db.query(SysRole).filter(SysRole.sign == '').all()
  62. for row in rows:
  63. sign_row(db, row)