Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add filtering by partner to form builders #114

Merged
merged 1 commit into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions formio/models/formio_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class Builder(models.Model):
version = fields.Integer("Version", required=True, readonly=True, default=1)
version_comment = fields.Text("Version Comment")
user_id = fields.Many2one('res.users', string='Assigned user', track_visibility='onchange')
partner_ids = fields.Many2many('res.partner', string='Exclusive to partners',
help='Give access only to users related to this partner (either linked directly to the partner or to its children). If left empty, this restriction doesn\'t apply.')
allowed_user_ids = fields.Many2many('res.users', compute='_compute_allowed_user_ids', store=True)
forms = fields.One2many('formio.form', 'builder_id', string='Forms')
portal = fields.Boolean("Portal", track_visibility='onchange', help="Form is accessible by assigned portal user")
portal_submit_done_url = fields.Char(
Expand Down Expand Up @@ -246,6 +249,18 @@ def _compute_display_fields(self):
r.display_name_full = _("{title} (state: {state} - version: {version})").format(
title=r.title, state=r.display_state, version=r.version)

@api.depends('partner_ids')
def _compute_allowed_user_ids(self):
for r in self:
user_domain = []

if r.partner_ids:
partner_ids = r.partner_ids.mapped('commercial_partner_id.id') + r.partner_ids.mapped('id')
user_domain = [('partner_id', 'in', partner_ids)]

user_ids = self.env['res.users'].search(user_domain)
r.allowed_user_ids = [(6, 0, user_ids.ids)]

@api.depends('public')
def _compute_public_url(self):
for r in self:
Expand Down Expand Up @@ -308,7 +323,7 @@ def action_obsolete(self):
@api.returns('self', lambda value: value)
def copy_as_new_version(self):
"""Get last version for builder-forms by traversing-up on parent_id"""

self.ensure_one()
builder = self

Expand Down Expand Up @@ -375,7 +390,7 @@ def _get_js_options(self):
# only set language if exist in i18n translations
if options['i18n'].get(language):
options['language'] = language

return options

def _get_js_params(self):
Expand Down
10 changes: 8 additions & 2 deletions formio/security/formio_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) -->
<field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
</record>

<record id="group_formio_user_partner" model="res.groups">
<field name="name">User: Partner forms</field>
<field name="category_id" ref="formio.module_category_formio"/>
<field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
</record>

<record id="group_formio_user_all_forms" model="res.groups">
<field name="name">User: All forms</field>
<field name="category_id" ref="formio.module_category_formio"/>
<field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
</record>

<record id="group_formio_admin" model="res.groups">
<field name="name">Admin</field>
<field name="category_id" ref="formio.module_category_formio"/>
<field name="implied_ids" eval="[(4, ref('formio.group_formio_user_all_forms'))]"/>
<field name="implied_ids" eval="[(4, ref('formio.group_formio_user_all_forms'))]"/>
<field name="users" eval="[(4, ref('base.user_root')), (4, ref('base.user_admin'))]"/>
</record>
</data>
Expand Down
82 changes: 81 additions & 1 deletion formio/security/ir_model_access.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_builder_user_partner" model="ir.model.access">
<field name="name">formio.builder: Partner forms</field>
<field name="model_id" ref="formio.model_formio_builder"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_builder_portal_user" model="ir.model.access">
<field name="name">formio.builder: Portal User</field>
<field name="model_id" ref="formio.model_formio_builder"/>
Expand Down Expand Up @@ -77,6 +87,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="1"/>
</record>

<record id="access_formio_form_user_partner" model="ir.model.access">
<field name="name">formio.form: Partner forms</field>
<field name="model_id" ref="formio.model_formio_form"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="1"/>
<field name="perm_write" eval="1"/>
<field name="perm_unlink" eval="1"/>
</record>

<record id="access_formio_form_portal_user" model="ir.model.access">
<field name="name">formio.form: Portal User</field>
<field name="model_id" ref="formio.model_formio_form"/>
Expand Down Expand Up @@ -118,6 +138,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_version_user_partner" model="ir.model.access">
<field name="name">formio.version: User partner forms</field>
<field name="model_id" ref="formio.model_formio_version"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_version_portal_user" model="ir.model.access">
<field name="name">formio.version: Portal User</field>
<field name="model_id" ref="formio.model_formio_version"/>
Expand Down Expand Up @@ -159,6 +189,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_version_asset_user_partner" model="ir.model.access">
<field name="name">formio.version.asset: User partner forms</field>
<field name="model_id" ref="formio.model_formio_version_asset"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_version_asset_portal_user" model="ir.model.access">
<field name="name">formio.version.asset: Portal User</field>
<field name="model_id" ref="formio.model_formio_version_asset"/>
Expand Down Expand Up @@ -242,6 +282,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_translation_source_user_partner" model="ir.model.access">
<field name="name">formio.translation.source: User partner forms</field>
<field name="model_id" ref="formio.model_formio_translation_source"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_translation_source_portal_user" model="ir.model.access">
<field name="name">formio.translation.source: Portal User</field>
<field name="model_id" ref="formio.model_formio_translation_source"/>
Expand All @@ -251,7 +301,7 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_translation_admin" model="ir.model.access">
<field name="name">formio.translation: Admin</field>
<field name="model_id" ref="formio.model_formio_translation"/>
Expand Down Expand Up @@ -282,6 +332,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_translation_user_partner" model="ir.model.access">
<field name="name">formio.translation: User partner forms</field>
<field name="model_id" ref="formio.model_formio_translation"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_translation_portal_user" model="ir.model.access">
<field name="name">formio.translation: Portal User</field>
<field name="model_id" ref="formio.model_formio_translation"/>
Expand Down Expand Up @@ -323,6 +383,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_res_model_user_partner" model="ir.model.access">
<field name="name">formio.res.model: User partner forms</field>
<field name="model_id" ref="formio.model_formio_res_model"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_res_model_portal_user" model="ir.model.access">
<field name="name">formio.res.model: Portal User</field>
<field name="model_id" ref="formio.model_formio_res_model"/>
Expand Down Expand Up @@ -364,6 +434,16 @@ See LICENSE file for full copyright and licensing details. -->
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_builder_translation_user_partner" model="ir.model.access">
<field name="name">formio.builder.translation: User partner forms</field>
<field name="model_id" ref="formio.model_formio_builder_translation"/>
<field name="group_id" ref="formio.group_formio_user_partner"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>

<record id="access_formio_builder_translation_portal_user" model="ir.model.access">
<field name="name">formio.builder.translation: Portal User</field>
<field name="model_id" ref="formio.model_formio_builder_translation"/>
Expand Down
26 changes: 25 additions & 1 deletion formio/security/ir_rule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ See LICENSE file for full copyright and licensing details. -->

<odoo>
<data>
<!-- formio.form -->
<record id="formio_form_user_all" model="ir.rule">
<field name="name">formio.form: User all forms</field>
<field name="model_id" ref="model_formio_form"/>
Expand All @@ -26,6 +27,17 @@ See LICENSE file for full copyright and licensing details. -->
<field name="domain_force">[('user_id', '=', user.id)]</field>
</record>

<record id="formio_form_user_partner" model="ir.rule">
<field name="name">formio.form: User partner forms</field>
<field name="model_id" ref="model_formio_form"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="False"/>
<field name="perm_create" eval="False"/>
<field name="perm_unlink" eval="False"/>
<field name="groups" eval="[(4, ref('formio.group_formio_user_partner'))]"/>
<field name="domain_force">[('builder_id.allowed_user_ids', '=', user.id)]</field>
</record>

<record id="formio_form_unlink_user_assigned" model="ir.rule">
<field name="name">formio.form unlink: User assigned forms</field>
<field name="model_id" ref="model_formio_form"/>
Expand All @@ -38,7 +50,7 @@ See LICENSE file for full copyright and licensing details. -->
[('create_uid', '=', user.id), ('user_id', '=', user.id), ('state', '!=', 'COMPLETE')]
</field>
</record>

<record id="formio_form_portal_user" model="ir.rule">
<field name="name">formio.form read, write: Portal User</field>
<field name="model_id" ref="model_formio_form"/>
Expand Down Expand Up @@ -67,6 +79,18 @@ See LICENSE file for full copyright and licensing details. -->
</field>
</record>

<!-- formio.builder -->
<record id="formio_form_builder_user_partner" model="ir.rule">
<field name="name">formio.builder: User partner forms</field>
<field name="model_id" ref="model_formio_builder"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="False"/>
<field name="perm_create" eval="False"/>
<field name="perm_unlink" eval="False"/>
<field name="groups" eval="[(4, ref('formio.group_formio_user_partner'))]"/>
<field name="domain_force">[('allowed_user_ids', '=', user.id)]</field>
</record>

<!-- ir.attachment -->
<record id="ir_attachment_unlink_formio_form" model="ir.rule">
<field name="name">Attachments of completed Forms can't be deleted</field>
Expand Down
2 changes: 2 additions & 0 deletions formio/views/formio_builder_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ See LICENSE file for full licensing details. -->
<field name="public_url" optional="hide"/>
<field name="formio_version_id"/>
<field name="user_id" optional="hide"/>
<field name="partner_ids" optional="hide" widget="many2many_tags"/>
<field name="parent_id" optional="hide"/>
<field name="write_date" optional="hide"/>
<field name="create_date" optional="hide"/>
Expand Down Expand Up @@ -87,6 +88,7 @@ See LICENSE file for full licensing details. -->
<group>
<field name="uuid" attrs="{'invisible': [('id', '=', False)]}"/>
<field name="user_id"/>
<field name="partner_ids" widget="many2many_tags"/>
</group>
</group>
<notebook>
Expand Down