You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2015 Akretion (<http://www.akretion.com>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import re
  22. from openerp import models, fields, api
  23. class SqlExport(models.Model):
  24. _name = "sql.export"
  25. _description = "SQL export"
  26. PROHIBITED_WORDS = [
  27. 'delete',
  28. 'drop',
  29. 'insert',
  30. 'alter',
  31. 'truncate',
  32. 'execute',
  33. 'create',
  34. 'update'
  35. ]
  36. @api.multi
  37. def _check_query_allowed(self):
  38. for obj in self:
  39. query = obj.query.lower()
  40. for word in self.PROHIBITED_WORDS:
  41. expr = r'\b%s\b' % word
  42. is_not_safe = re.search(expr, query)
  43. if is_not_safe:
  44. return False
  45. return True
  46. @api.model
  47. def _get_editor_group(self):
  48. ir_model_obj = self.env['ir.model.data']
  49. return [ir_model_obj.xmlid_to_res_id(
  50. 'sql_export.group_sql_request_editor')]
  51. name = fields.Char('Name', required=True)
  52. query = fields.Text(
  53. 'Query',
  54. required=True,
  55. help="You can't use the following word : delete, drop, create, "
  56. "insert, alter, truncate, execute, update")
  57. copy_options = fields.Char(
  58. 'Copy Options',
  59. required=True,
  60. default="CSV HEADER DELIMITER ';'")
  61. group_ids = fields.Many2many(
  62. 'res.groups',
  63. 'groups_sqlquery_rel',
  64. 'sql_id',
  65. 'group_id',
  66. 'Allowed Groups',
  67. default=_get_editor_group)
  68. user_ids = fields.Many2many(
  69. 'res.users',
  70. 'users_sqlquery_rel',
  71. 'sql_id',
  72. 'user_id',
  73. 'Allowed Users')
  74. field_ids = fields.Many2many(
  75. 'ir.model.fields',
  76. 'fields_sqlquery_rel',
  77. 'sql_id',
  78. 'field_id',
  79. 'Parameters',
  80. domain=[('model', '=', 'sql.file.wizard')])
  81. valid = fields.Boolean()
  82. _constraints = [(_check_query_allowed,
  83. 'The query you want make is not allowed : prohibited '
  84. 'actions (%s)' % ', '.join(PROHIBITED_WORDS),
  85. ['query'])]
  86. @api.multi
  87. def export_sql_query(self):
  88. self.ensure_one()
  89. wiz = self.env['sql.file.wizard'].create({
  90. 'valid': self.valid,
  91. 'sql_export_id': self.id})
  92. return {
  93. 'view_type': 'form',
  94. 'view_mode': 'form',
  95. 'res_model': 'sql.file.wizard',
  96. 'res_id': wiz.id,
  97. 'type': 'ir.actions.act_window',
  98. 'target': 'new',
  99. 'context': self._context,
  100. 'nodestroy': True,
  101. }
  102. @api.model
  103. def check_query_syntax(self, vals):
  104. if vals.get('query', False):
  105. vals['query'] = vals['query'].strip()
  106. if vals['query'][-1] == ';':
  107. vals['query'] = vals['query'][:-1]
  108. # Can't test the query because of variables
  109. # try:
  110. # self.env.cr.execute(vals['query'])
  111. # except:
  112. # raise exceptions.Warning(
  113. # _("The Sql query is not valid."))
  114. # finally:
  115. # self.env.cr.rollback()
  116. return vals
  117. @api.multi
  118. def write(self, vals):
  119. vals = self.check_query_syntax(vals)
  120. if 'query' in vals:
  121. vals['valid'] = False
  122. return super(SqlExport, self).write(vals)
  123. @api.model
  124. def create(self, vals):
  125. vals = self.check_query_syntax(vals)
  126. return super(SqlExport, self).create(vals)