Try ERPNext Buy Support Partners Foundation Foundation Members

Rerouting system user to page other than /desk on login

@rmehta. I was hoping you could personally answer this.

I’ve been digging into frappe/erpnext documentation for a few weeks now. I’ve been through most of your video tutorials. I have a dev server that I spun up, but I would like to forward my system users to pages respective to their tasks on login instead of /desk. I’ve been all over these forums attempting to use hooks in a custom application I am writing. I’ve attempted to utilize “role_home_page”, and this is not a surefire way to get the redirect to occur.

On login, my user will get an error saying the page is missing, but once they click “Home” they are forwarded to http://myserversip:8000. I’ve also attempted to create my own boot_session and on_session_creation hooks. These yield similar results of redirecting only after an error is given in the web view. So then I was digging through auth and login in the frappe framework. In auth, I found set_user_info which is called in post_login. I found that run_trigger_(‘on_login’) is called, so I created a hook for it to attempt to redirect the user on login. The issue with this is that set_user_info() happens at the end of the post_login definition, which means no matter what I do, the frappe set_user_info function will forward my user -no matter what- to /desk. So what I did is create a set_user_info of my own, added hooks for it, and called a run_trigger(‘on_after_login’) and coupled those with use of role_home_page in my hooks to force my redirects .

I’m attempting to complete the redirect without having to change any of the frappe files so I can continue to get updates without having to rewrite my changes every time.

Alas, I have two questions.

One: How can I complete a system user redirect without changing ANY FRAPPE CODE?

Two: If this is not possible, how would you recommend I go about it? This is my current solution, but it involves changing frappe code, and I don’t want to do that for this to work. See below:

def post_login(self):
    self.run_trigger('on_login')
    self.validate_ip_address()
    self.validate_hour()
    self.make_session()
    self.set_user_info()
    self.run_trigger('on_after_login')

This hooks up to my own set_user_info function for the redirection. This is what it looks like:

def on_after_login(login_manager):
	set_user_info(login_manager)

def set_user_info(login_manager, resume=False):
	login_manager.info = frappe.db.get_value("User", login_manager.user,
 		["user_type", "first_name", "last_name", "user_image"], as_dict=1)
	if login_manager.info.user_type != "Website User" and not resume:
		frappe.local.response["home_page"] = "/"

This solution works, but it requires me to add the run_trigger in auth.post_login. The other way I could do this in simply change frappe.local.response[“home_page”] = “/desk” to frappe.local.response[“home_page”] = “/”. Again, either one of these will require changes to auth and the frappe framework. If Erp were to accept or make this change in the framework, many people could make use of it, and we could continue our pulls as your team updates ErpNext/Frappe.

1 Like

Sure, we can make this extensible. It can either be default home page on System Settings, or User or Role (or maybe via a method)

Do you want to send a PR?

Also best to ask these on https://discuss.frappe.io

https://github.com/frappe/frappe/pull/4495. Pull request added.

I would also really like to see these settings put into the Role and User settings. You could set a role home page, and if you wanted to be more specific, a user home page - no matter what roles they have.

Hi,
is this available in following versions?

ERPNext: v11.1.49 (master)

Frappe Framework: v11.1.44 (master)

You have a patch for the v12? :smiley:

@asd and @iMoshi
this is currently worked in to boot session under hooks.py
change boot session home page to redirect to whichever page you wish for a given user/role via a method.

1 Like

@mudux, got your comment on Github as well. And I have no clue so far.

I’ve found multiple hooks.py, but I’m guessing the one we need is /erpnext/hooks.py right? I do see "boot_session = “erpnext.startup.boot.boot_session”
" but I have no clue what to make of it :stuck_out_tongue_winking_eye: do please help me further if you find the time :smiley:

Thank you.

the hooks.py you need is the one in your own custom app that you are building. Never think of changing erpnext or frappe files.

In your custom_app

  1. create a method in some controller to do a redirect
    https://github.com/frappe/frappe/blob/b65a819c6da825bc2fc19ca3d8ce8285c85b99d3/frappe/integrations/doctype/google_calendar/google_calendar.py#L121
  2. set boot_session in your custom_app/hooks.py to point to the method above