Hi, i need to make each user to only be able to see their own data. But currently all users can see contacts from other users and CRM from other users.
for example there are User A and User B. User A will only be able to see contacts and CRM from user A. User B will only be able to see contacts and CRM from user B.
How to make users only able to see their own Contacts and CRM? thank you
@Gando Loudo thank you, that works but it also applies to administrator.
any idea how to make it only applies to certain users (or type of users)?
ive tried added "group" to the record rule for contact. but it doesnt work for contacts. there's no "contacts" "application"
my bad. turns out @gando loudo's script only work for admin. while non admin got this error
Due to security restrictions, you are not allowed to access 'User' (res.users) records.
Records: test2 (id=7)
User: test2 (id=7)
This restriction is due to the following rules:
Contact your administrator to request access if necessary.
To restrict users to only see their own contacts and CRM records, you can use record rules in Odoo. Record rules allow you to restrict access to specific records based on certain conditions, such as the owner of the record.
Here's an example of how to create a record rule that restricts access to Contacts and CRM records based on the owner:
Go to Settings > Technical > Security > Record Rules.
Create a new record rule for the model you want to restrict access to (e.g. Contacts or CRM).
Set the Domain to ['|', ('user_id', '=', user.id), ('user_id', '=', False)]. This domain will restrict access to records where the user is the owner or the owner is not set.
Set the Apply for Read field to Yes to apply this rule when reading records.
Set the Apply for Write field to Yes to apply this rule when writing records.
Set the Groups field to the appropriate group(s) that should have this restriction applied to them.
With this record rule in place, users will only be able to see Contacts and CRM records that they own, or records where the owner is not set. Note that you will need to adjust the domain and group settings to fit your specific requirements.
Regarding the issue you mentioned with the script only working for administrators, you can modify the script to check if the current user has permission to read the res.users model before running the search. Here's an example:
python
from odoo import api, fields, models
class ResPartner(models.Model):
_inherit = 'res.partner'
@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
if self.env.user.has_group('base.group_user'):
# Only apply restriction for non-admin users
user_id = self.env.user.id
args += [('user_id', '=', user_id)]
return super().search(args, offset, limit, order, count)
In this modified code snippet, the has_group() method is used to check if the current user belongs to the base.group_user group, which includes all non-admin users. If the user has this group, the search will be restricted to records where the user is the owner. If the user does not have this group (i.e. they are an administrator), the search will return all records.
At the moment I have the same problem.
Thank you so much for you post, your post really help!!!
Bhavin Patel is a mastert!!!!