# -*- coding: utf-8 -*- ################################################################################ # # Cybrosys Technologies Pvt. Ltd. # Copyright (C) 2025-TODAY Cybrosys Technologies(). # Author: Bhagyadev KP (odoo@cybrosys.com) # # This program is free software: you can modify # it under the terms of the GNU Affero General Public License (AGPL) as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # ################################################################################ from odoo import Command, fields, models class ResUsers(models.Model): """ Inherited the res_users model for adding the products and product category""" _inherit = 'res.users' restricted_type = fields.Selection( [('product', 'Product'), ('category', 'Category')], string='Restriction Type', default='product', help='choose Product and Product category depends upon your need') allowed_product_ids = fields.Many2many( comodel_name='product.template', string="Products", store=True, help='Show to allow the products for assigned users') allowed_product_category_ids = fields.Many2many( comodel_name='product.category', string="Product Category", store=True, help='Show to allow the product category for assigned users') is_admin = fields.Boolean(compute='_compute_is_admin', help='Check the user is admin or not') def write(self, vals): """Write the values of restrict user ids """ res = super(ResUsers, self).write(vals) for user in self: if user.restricted_type == 'product': products = self.env['product.template'].sudo(). \ search([('restrict_user_ids', 'in', user.id)]) if user.allowed_product_ids: for product in products: product.is_product = True for product in self.env['product.template'].sudo(). \ search([('restrict_user_ids', 'not in', [rec.id for rec in products])]): product.is_product = False else: for product in self.env['product.template'].sudo().search( []): product.is_product = True for user_product in self.allowed_product_ids: user_product.sudo().write({ 'restrict_user_ids': [(4, user.id)] }) else: products = self.env['product.template'].sudo().search([]) if user.allowed_product_category_ids: products.is_product = False products_categ = self.env['product.template']. \ search([('categ_id', 'in', [int(categ.id) for categ in user.allowed_product_category_ids])]) for pro in products: if pro not in products_categ: pro.sudo().write({'restrict_user_ids': [ Command.unlink(user.id)]}) for product in products_categ: product.is_product = False product.sudo().write({ 'restrict_user_ids': [(4, user.id)] }) else: products.is_product = True for product in products: if user in product.restrict_user_ids: product.sudo().write( {'restrict_user_ids': [ Command.unlink(user.id)]}) return res def _compute_is_admin(self): """ Compute the value of is_admin based on the user id admin or not""" for admin in self: admin.is_admin = False if admin.has_group('base.group_erp_manager'): admin.is_admin = True