-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TA#69961 [16.0] [IMP] admin_light_mail : mail.template security (#214)
- Loading branch information
Showing
6 changed files
with
137 additions
and
16 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
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<odoo> | ||
|
||
<record id="mail.access_mail_template_editor" model="ir.model.access"> | ||
<field name="active" eval="False"/> | ||
</record> | ||
|
||
<record id="group_email_template" model="res.groups"> | ||
<field name="name">Email Templates</field> | ||
<field name="category_id" ref="admin_light_base.module_category_admin" /> | ||
<field name="implied_ids" eval="[(4, ref('admin_light_base.group_admin'))]" /> | ||
</record> | ||
|
||
<record id="rule_mail_template_edit_own" model="ir.rule"> | ||
<field name="name">Users can only change their own templates</field> | ||
<field name="model_id" ref="mail.model_mail_template"/> | ||
<field name="domain_force">[('create_uid', '=', user.id)]</field> | ||
<field name="groups" eval="[Command.link(ref('admin_light_mail.group_email_template'))]"/> | ||
<field name="perm_create" eval="True"/> | ||
<field name="perm_read" eval="False"/> | ||
<field name="perm_write" eval="True"/> | ||
<field name="perm_unlink" eval="True"/> | ||
</record> | ||
|
||
<function name="write" model="ir.model.data"> | ||
<value model="ir.model.data" search="[('module', '=', 'mail'), ('name', '=', 'mail_template_editor_rule')]"/> | ||
<value eval="{'noupdate': False}"/> | ||
</function> | ||
|
||
<record id="mail.mail_template_editor_rule" model="ir.rule"> | ||
<field name="groups" eval="[(3, ref('mail.group_mail_template_editor'))]"/> | ||
</record> | ||
|
||
<function name="write" model="ir.model.data"> | ||
<value model="ir.model.data" search="[('module', '=', 'mail'), ('name', '=', 'mail_template_editor_rule')]"/> | ||
<value eval="{'noupdate': True}"/> | ||
</function> | ||
|
||
</odoo> |
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,4 @@ | ||
# Copyright 2024 Numigi (tm) and all its contributors (https://bit.ly/numigiens) | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from . import test_mail_template_access |
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,86 @@ | ||
# Copyright 2024 Numigi (tm) and all its contributors (https://bit.ly/numigiens) | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from odoo.tests.common import TransactionCase | ||
from odoo.exceptions import AccessError | ||
|
||
|
||
class TestMailTemplateAccess(TransactionCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
|
||
ref = cls.env.ref | ||
cls.user1 = cls.env["res.users"].create( | ||
{ | ||
"name": "User 1", | ||
"login": "user1", | ||
"groups_id": [ | ||
( | ||
6, | ||
0, | ||
[ | ||
ref("base.group_user").id, | ||
ref("admin_light_mail.group_email_template").id, | ||
], | ||
) | ||
], | ||
} | ||
) | ||
cls.user2 = cls.env["res.users"].create( | ||
{ | ||
"name": "User 2", | ||
"login": "user2", | ||
"groups_id": [ | ||
( | ||
6, | ||
0, | ||
[ | ||
ref("base.group_user").id, | ||
ref("admin_light_mail.group_email_template").id, | ||
], | ||
) | ||
], | ||
} | ||
) | ||
cls.mail_template_user1 = ( | ||
cls.env["mail.template"] | ||
.with_user(cls.user1) | ||
.create( | ||
{ | ||
"name": "User1 Template", | ||
"subject": "Template by User1", | ||
"email_from": "[email protected]", | ||
} | ||
) | ||
) | ||
|
||
def test_user_own_record_access(self): | ||
self.mail_template_user1.with_user(self.user1).write( | ||
{"subject": "Updated by User1"} | ||
) | ||
self.assertEqual(self.mail_template_user1.subject, "Updated by User1") | ||
self.mail_template_user1.with_user(self.user1).unlink() | ||
self.assertFalse( | ||
self.env["mail.template"].search([("id", "=", self.mail_template_user1.id)]) | ||
) | ||
|
||
def test_other_user_access_error(self): | ||
self.mail_template_user1 = ( | ||
self.env["mail.template"] | ||
.with_user(self.user1) | ||
.create( | ||
{ | ||
"name": "User1 Template", | ||
"subject": "Template by User1", | ||
"email_from": "[email protected]", | ||
} | ||
) | ||
) | ||
with self.assertRaises(AccessError): | ||
self.mail_template_user1.with_user(self.user2).write( | ||
{"subject": "Unauthorized Update by User2"} | ||
) | ||
with self.assertRaises(AccessError): | ||
self.mail_template_user1.with_user(self.user2).unlink() |
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,26 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<odoo> | ||
|
||
<record id="group_email_template" model="res.groups"> | ||
<field name="name">Email Templates</field> | ||
<field name="category_id" ref="admin_light_base.module_category_admin" /> | ||
<field name="implied_ids" eval="[(4, ref('admin_light_base.group_admin'))]" /> | ||
</record> | ||
|
||
<!-- By default, Odoo allows all users to edit mail templates, | ||
we don't want this after giving admin light access to mail template --> | ||
|
||
<record id="base.group_user" model="res.groups"> | ||
<field name="implied_ids" eval="[(3, ref('mail.group_mail_template_editor'))]"/> | ||
</record> | ||
|
||
<menuitem | ||
id="menu_email_template" | ||
name="Email Templates" | ||
parent="menu_email" | ||
groups="group_email_template" | ||
action="mail.action_email_template_tree_all" | ||
sequence="10" | ||
/> | ||
/> | ||
|
||
</odoo> |