How to check user Role in server script

user =frappe.session.user
frappe.msgprint(str(user))

I want to check the user role if it is administrator or not .
Is this possible

@PRaful_9898 use frappe.get_roles

1 Like

To check the current user’s role in a server script, you can use frappe.session.user to get the logged-in user’s ID, and then use frappe.get_roles(user) to get the list of roles that user has. You can then check if a specific role is in that list. If the role is there, you can take action based on it.

Example:

def check_current_user_role():
    current_user = frappe.session.user
    if current_user == "Administrator":
        frappe.msgprint("User is an Administrator.")
    else:
        user_roles = frappe.get_roles(current_user)
    
        if 'Role Name' in user_roles:
            frappe.msgprint(f"User {current_user} has the role.")
        else:
            frappe.msgprint(f"User {current_user} does not have the role.")
2 Likes