Try ERPNext Buy Support Partners Foundation

Add Block_Modules on new User creation via Hooks

I am trying to add Block_Modules list for Employees when the user is created via hooks.py.

I have created a custom app mycustomization, in that apps hook.py I have the following statement.

doc_events = {
	"User": {
      "validate": "mycustomization.mycustomization.user_addblockmodules"
	  }
}

and then in the mycustomization folder I have a file named user_addblockmodules.py

the code for the file is as follows

from __future__ import unicode_literals

import frappe

from frappe.model.document import Document


@frappe.whitelist()
def user_addblockmodules(doc, method=None):

    if len(doc.get("roles")) == 1:
        if "Employee" in [d.role for d in doc.get("roles")]:
            doc.append("block_modules", {
                'module': "Users and Permissions",
                'module': "Integrations",
                'module': "Social",
                'module': "dashboard",
                'module': "Accounts",
                'module': "Buying",
                'module': "Assets",
                'module': "CRM",
                'module': "Help",
                'module': "Bench Manager",
                'module': "Settings",
                'module': "Customization",
                'module': "Website",
                'module': "Leaderboard",
                'module': "Getting Started",
                'module': "Selling",
                'module': "Stock",
                'module': "Quality Management",
                'module': "Marketplace"})

But I keep getting error when a new user is created. I have restarted bench, reloaded the site.
AttributeError: module ‘mycustomization.mycustomization’ has no attribute ‘user_addblockmodules’

Any idea where I am going wrong? Thanks.

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.erpnext.com/t/get-allowed-modules-from-user-doctype/57448
https://discuss.erpnext.com/t/adding-default-user-roles-modules-and-user-permission/16578
https://alainber.medium.com/bench-execute-and-bench-console-frappe-erpnext-77ebbe6cf50a