I resolved the error. Below is my solution, if it helps anyone.
hooks.py
doc_events = {
"User": {
"validate": "mycustomization.mycustomization.user_addblockmodules.user_addblockmodules"
}
}
user_addclickmodules.py
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
@frappe.whitelist()
def user_addblockmodules(doc, method=None):
# Apply only if the user has 1 role
if len(doc.get("roles")) == 1:
# check if the the role is employee
if "Employee" in [d.role for d in doc.get("roles")]:
#remove all block modules for the Employee user first
for d in doc.block_modules:
doc.block_modules.remove(d)
default_block_module = {"Users and Permissions", "Integrations", "Social", "dashboard", "Accounts", "Buying", "Assets", "CRM", "Help", "Settings","Customization", "Website","Leaderboard","Getting Started","Selling","Stock","Quality Management","Marketplace","Core"}
for b_module in default_block_module:
doc.append("block_modules", {"module": b_module})
Just make sure that you have the same list of modules that I have, else adjust it. Looking to enhance this code by getting the list of modules from Frappe and then only allowing whitelisted modules. But have not found a good way to do that yet.
Thanks to https://discuss.frappe.io/t/get-allowed-modules-from-user-doctype/57448
https://discuss.frappe.io/t/adding-default-user-roles-modules-and-user-permission/16578
https://alainber.medium.com/bench-execute-and-bench-console-frappe-erpnext-77ebbe6cf50a