How to hide Create Workspace from certain user roles?

How do I hide the create workspace button from certain user roles?
I already tried to set permissions for the workspace doctype, but that doesn’t hide the create workspace button

Hey,
Got any solution??

Hey did u got any solution?

any solution on this??

any solution?

Any update on this?

Have you found the solution?

Any update?

Removing the ‘Workspace Manager’ role from any particular user would remove the ‘Create workspace’ button as well.

Removing the ‘Workspace Manager’ role from any particular user would remove the ‘Create workspace’ button as well.

This doesn’t work any other solution ?

The only solution that worked for me is to write javascript code in my custom app and put it in hooks so it will be accessible in the desk.

Frappe version - 14

$(document).on(‘DOMNodeInserted’, “#body”, function(e){
$id = $(e.target).attr(“id”);
if($id){
if ($id.localeCompare(‘page-Workspaces’) == 0) {
if(frappe.user_roles.includes(“Sales Manager”)){
$(function() {
$(“#page-Workspaces .page-actions”).html(“”)
})
}
}
}
});

3 Likes

more optimum solution using frappe.router

frappe.router.on('change', () => {
    route = frappe.get_route()
    if(route.length == 2 && !frappe.user_roles.includes("System Manager")){
        if (route[0] == "Workspaces"){
            $("#page-Workspaces .page-actions").html("")
        }
    }
})

2 Likes

Where do you put this function?

  1. create .js file inside the public folder in your custom app
  2. add the .js file in the build.json
  3. then run bench build --site [yoursitename]
  4. add the build file in your custom app hooks.py app_include_js
2 Likes

After searching for a lot of time and trying to inject custom css and js to hide those buttons for certain roles nothing really worked
so i changed the core code itself, nothing much only one function.
there is a function called as setup_actions in workspace.js
replace that code with the following code snippet

setup_actions(page) {
let pages = page.public ? this.public_pages : this.private_pages;
let current_page = pages.filter((p) => p.title == page.name)[0];

if (!this.is_read_only) {
    this.setup_customization_buttons(current_page);
    return;
}

this.clear_page_actions();

// Check if the user has the "System Manager" role
frappe.call({
    method: 'bytenba.custom_utilities.get_roles',
			args:{
				"session_user": frappe.session.user
			},
    callback: (r) => {
        if (r.message.includes("System Manager")) {
            this.page.set_secondary_action(
                __("Edit"),
                async () => {
			if (!this.editor || !this.editor.readOnly) return;
			this.is_read_only = false;
			this.toggle_hidden_workspaces(true);
			await this.editor.readOnly.toggle();
			this.editor.isReady.then(() => {
				this.body.addClass("edit-mode");
				this.initialize_editorjs_undo();
				this.setup_customization_buttons(current_page);
				this.show_sidebar_actions();
				this.make_blocks_sortable();
			});
                },
                "es-line-edit"
            );

            // Your "Create Workspace" button logic here
            this.page.add_inner_button(__("Create Workspace"), () => {
                this.initialize_new_page();
            });
        }
    }
});

}

u can also use frappe.session.user.has_role() instead of calling a server method

2 Likes

its generally not advisable to modify core code unless you’re maintaining your own fork. Although i do understand why you had to go this far. i’ve been trying to achieve this for quite a while without success. I hope an easier way to accomplish this without having to go to the extreme is implemented soon so admins can at least have the option to hide the workspace and edit buttons based on role.

2 Likes