Files
2024-06-14 16:24:02 +08:00

75 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import datetime
import logging
import rsa
import base64
from os.path import join as opj
from odoo.modules import get_module_path
from odoo import api, release, SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.models import AbstractModel
_logger = logging.getLogger(__name__)
class PublisherWarrantyContract(AbstractModel):
_inherit = "publisher_warranty.contract"
# rsa解密
def rsaDecrypt(self, str1):
modpath = get_module_path("database_activation_code")
file_path = opj(modpath, "static", "privkey.pem")
with open(file_path) as privatefile:
p = privatefile.read()
# 验证密钥
try:
privkey = rsa.PrivateKey.load_pkcs1(p)
except:
raise UserError("请联系管理员!")
try:
str1 = base64.b16decode(str1)
# 私钥解密
content = rsa.decrypt(str1, privkey)
con = content.decode("utf-8").split("#")
if len(con) != 2:
raise
database_partner = con[0]
old_database_partner = (
self.sudo().env["ir.config_parameter"].get_param("database.partner")
)
if database_partner != old_database_partner:
raise
expiration_date = con[1]
old_enterprise_code = (
self.sudo()
.env["ir.config_parameter"]
.get_param("database.expiration_date")
)
except:
raise UserError("激活码错误")
if expiration_date <= old_enterprise_code:
raise UserError("激活码已过期")
# 设置过期时间
set_param = self.env["ir.config_parameter"].sudo().set_param
set_param("database.expiration_date", expiration_date)
def update_notification(self, cron_mode=True):
# 获取激活码
enterprise_code = (
self.sudo().env["ir.config_parameter"].get_param("database.enterprise_code")
)
if enterprise_code:
try:
enterprise_code = str.encode(enterprise_code)
except:
raise UserError("激活码错误")
# 验证激活码
self.rsaDecrypt(enterprise_code)
return True