<?xml version="1.0" encoding="utf-8" ?>
<odoo>

    <record id="project.ir_rule_private_task" model="ir.rule">
        <field name="active" eval="False" />
    </record>

    <record id="project.report_project_task_user_rule" model="ir.rule">
        <field name="active" eval="False" />
    </record>

    <!-- Projects rules for internal users -->
    <record id="project.project_public_members_rule" model="ir.rule">
      <field name="name">Internal: Project Visibility</field>
      <field name="model_id" ref="project.model_project_project" />
      <field name="domain_force">
        [
          '|',
            ('privacy_visibility', 'not in', ['followers', 'portal_internal']),
            ('message_partner_ids', 'in', [user.partner_id.id])
        ]
      </field>
      <field name="groups" eval="[(4, ref('base.group_user'))]" />
    </record>

    <!-- Tasks rules for internal users -->
    <record id="project.task_visibility_rule" model="ir.rule">
      <field name="name">Internal: Task Visibility</field>
      <field name="model_id" ref="project.model_project_task" />
      <field name="domain_force">
        [
          '|',
            '&amp;',
              ('project_id', '!=', False),
              '|',
                ('project_id.privacy_visibility', 'not in', ['followers', 'portal_internal']),
                ('project_id.message_partner_ids', 'in', [user.partner_id.id]),
          '|',
            ('message_partner_ids', 'in', [user.partner_id.id]),
            ('user_ids', 'in', user.id)
        ]
      </field>
      <field name="groups" eval="[(4, ref('base.group_user'))]" />
    </record>

  <!-- Portal users see projects if privacy_visibility = portal_internal AND they are followers -->
    <record id="rule_portal_read_internal_projects" model="ir.rule">
      <field name="name">Portal: read invited internal projects</field>
      <field name="model_id" ref="project.model_project_project" />
      <field name="domain_force">
        [ ('privacy_visibility','=','portal_internal'),
          ('active', '=', True),
          ('message_partner_ids','child_of',[user.partner_id.commercial_partner_id.id]) ]
      </field>
      <field name="groups" eval="[(4, ref('base.group_portal'))]" />
      <field name="perm_read" eval="True" />
      <field name="perm_write" eval="False" />
      <field name="perm_create" eval="False" />
      <field name="perm_unlink" eval="False" />
    </record>

    <!-- Portal users see tasks of those internal projects only if they follow the project or the task itself -->
    <record id="rule_portal_read_internal_tasks" model="ir.rule">
      <field name="name">Portal: read invited internal tasks</field>
      <field name="model_id" ref="project.model_project_task" />
      <field name="domain_force">
        [
          ('project_id.privacy_visibility','=','portal_internal'),
          ('active','=',True),
          '|',
            ('project_id.message_partner_ids','child_of',[user.partner_id.commercial_partner_id.id]),
            ('message_partner_ids','child_of',[user.partner_id.commercial_partner_id.id])
        ]
      </field>
      <field name="groups" eval="[(4, ref('base.group_portal'))]" />
      <field name="perm_read" eval="True" />
      <field name="perm_write" eval="False" />
      <field name="perm_create" eval="False" />
      <field name="perm_unlink" eval="False" />
    </record>

</odoo>
