-
-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] Add option to auto encrypt password based on python syntax
- Loading branch information
Showing
10 changed files
with
143 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
# Copyright 2020 Creu Blanca | ||
# Copyright 2020 Ecosoft Co., Ltd. | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
{ | ||
'name': 'Report Qweb Encrypt', | ||
'summary': """ | ||
Allow to encrypt qweb pdfs""", | ||
'version': '12.0.1.0.0', | ||
'license': 'AGPL-3', | ||
'author': 'Creu Blanca,Odoo Community Association (OCA)', | ||
'website': 'https://github.com/OCA/reporting-engine', | ||
'depends': [ | ||
'web', | ||
"name": "Report Qweb Encrypt", | ||
"summary": "Allow to encrypt qweb pdfs", | ||
"version": "12.0.1.0.0", | ||
"license": "AGPL-3", | ||
"author": "Creu Blanca,Ecosoft,Odoo Community Association (OCA)", | ||
"website": "https://github.com/OCA/reporting-engine", | ||
"depends": [ | ||
"web", | ||
], | ||
'data': [ | ||
'views/ir_actions_report.xml', | ||
'templates/assets.xml' | ||
], | ||
'demo': [ | ||
"data": [ | ||
"views/ir_actions_report.xml", | ||
"templates/assets.xml" | ||
], | ||
"installable": True, | ||
"maintainers": ["kittiu"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,33 @@ | ||
# Copyright 2020 Creu Blanca | ||
# Copyright 2020 Ecosoft Co., Ltd. | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo.addons.web.controllers import main as report | ||
from odoo.http import route | ||
from odoo.http import route, request | ||
from werkzeug.urls import url_decode | ||
import json | ||
import logging | ||
from io import BytesIO | ||
|
||
_logger = logging.getLogger(__name__) | ||
try: | ||
from PyPDF2 import PdfFileReader, PdfFileWriter | ||
except ImportError as err: | ||
_logger.debug(err) | ||
|
||
|
||
class ReportController(report.ReportController): | ||
@route() | ||
def report_download(self, data, token): | ||
result = super().report_download(data, token) | ||
# When report is downloaded from print action, this function is called, | ||
# but this function cannot pass context (manually entered password) to | ||
# report.render_qweb_pdf(), encrypton for manual password is done here. | ||
requestcontent = json.loads(data) | ||
url, type = requestcontent[0], requestcontent[1] | ||
url, ttype = requestcontent[0], requestcontent[1] | ||
if ( | ||
type in ['qweb-pdf'] and | ||
result.headers['Content-Type'] == "application/pdf" and | ||
'?' in url | ||
ttype in ["qweb-pdf"] and | ||
result.headers["Content-Type"] == "application/pdf" and | ||
"?" in url | ||
): | ||
url_data = dict(url_decode(url.split('?')[1]).items()) | ||
if 'context' in url_data: | ||
context_data = json.loads(url_data['context']) | ||
if 'encrypt_password' in context_data: | ||
# We need to encrypt here because this function is not | ||
# passing context, so we need to implement this again | ||
|
||
url_data = dict(url_decode(url.split("?")[1]).items()) | ||
if "context" in url_data: | ||
context = json.loads(url_data["context"]) | ||
if "encrypt_password" in context: | ||
Report = request.env["ir.actions.report"] | ||
data = result.get_data() | ||
output_pdf = PdfFileWriter() | ||
in_buff = BytesIO(data) | ||
pdf = PdfFileReader(in_buff) | ||
output_pdf.appendPagesFromReader(pdf) | ||
output_pdf.encrypt(context_data['encrypt_password']) | ||
buff = BytesIO() | ||
output_pdf.write(buff) | ||
result.set_data(buff.getvalue()) | ||
encrypted_data = Report._encrypt_pdf( | ||
data, context["encrypt_password"]) | ||
result.set_data(encrypted_data) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* Enric Tobella <[email protected]> | ||
* Jaime Arroyo <[email protected]> | ||
* Kitti U. <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
This module allows you to encrypt pdf files with a password when | ||
downloading them. | ||
This module allow you to encrypt PDF with a password seting option, | ||
|
||
* Manually keyin password (only applicable for record print action) | ||
* Auto generated password based on object data (python) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
To make a report encryptable mark the field `Encryptable` in the report record. | ||
To make a report encryptable mark the field `Encryption` in the report record. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_report_qweb_encrypt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# © 2016 Therp BV <http://therp.nl> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo.exceptions import ValidationError | ||
from odoo.tests.common import HttpCase | ||
|
||
|
||
class TestReportQwebEncrypt(HttpCase): | ||
|
||
def test_report_qweb_no_encrypt(self): | ||
ctx = {"force_report_rendering": True} | ||
report = self.env.ref("web.action_report_internalpreview") | ||
report.encrypt = False | ||
pdf, _ = report.with_context(ctx).render_qweb_pdf([1]) | ||
self.assertFalse(pdf.count(b"/Encrypt")) | ||
|
||
def test_report_qweb_auto_encrypt(self): | ||
ctx = {"force_report_rendering": True} | ||
report = self.env.ref("web.action_report_internalpreview") | ||
report.encrypt = "auto" | ||
report.encrypt_password = False | ||
# If no encrypt_password, still not encrypted | ||
pdf, _ = report.with_context(ctx).render_qweb_pdf([1]) | ||
self.assertFalse(pdf.count(b"/Encrypt")) | ||
# If invalid encrypt_password, show error | ||
report.encrypt_password = "invalid python syntax" | ||
with self.assertRaises(ValidationError): | ||
pdf, _ = report.with_context(ctx).render_qweb_pdf([1]) | ||
# Valid python string for password | ||
report.encrypt_password = "'secretcode'" | ||
pdf, _ = report.with_context(ctx).render_qweb_pdf([1]) | ||
self.assertTrue(pdf.count(b"/Encrypt")) | ||
|
||
# TODO: test_report_qweb_manual_encrypt, require JS test? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters