I was trying to add a singup via API, and located this: Forget password and Sign Up rest api in frappe app
As said above, I was able to locate this method
@frappe.whitelist(allow_guest=True)
def sign_up(email, full_name, redirect_to):
if not is_signup_enabled():
frappe.throw(_('Sign Up is disabled'), title='Not Allowed')
user = frappe.db.get("User", {"email": email})
if user:
if user.disabled:
return 0, _("Registered but disabled")
else:
return 0, _("Already Registered")
else:
if frappe.db.sql("""select count(*) from tabUser where
HOUR(TIMEDIFF(CURRENT_TIMESTAMP, TIMESTAMP(modified)))=1""")[0][0] > 300:
frappe.respond_as_web_page(_('Temperorily Disabled'),
_('Too many users signed up recently, so the registration is disabled. Please try back in an hour'),
http_status_code=429)
from frappe.utils import random_string
user = frappe.get_doc({
"doctype":"User",
"email": email,
"first_name": full_name,
"enabled": 1,
"new_password": random_string(10),
"user_type": "Website User"
})
user.flags.ignore_permissions = True
user.insert()
# set default signup role as per Portal Settings
default_role = frappe.db.get_value("Portal Settings", None, "default_role")
if default_role:
user.add_roles(default_role)
if redirect_to:
frappe.cache().hset('redirect_after_login', user.name, redirect_to)
if user.flags.email_sent:
return 1, _("Please check your email for verification")
else:
return 2, _("Please ask your administrator to verify your sign-up")
So I copied this function to modify my requirements.
Here is my modified function:
@frappe.whitelist(allow_guest=True)
def sign_up(email, phone, full_name, password, confirm_password):
user = frappe.db.get("User", {"email": email})
if password != confirm_password:
return {'status': 0, 'message': _("Passwords doesn't match.")}
if user:
if user.disabled:
return {'status': 0, 'message': _("Registered but disabled")}
else:
return {'status': 0, 'message': _("Already Registered")}
else:
if frappe.db.sql("""select count(*) from tabUser where
HOUR(TIMEDIFF(CURRENT_TIMESTAMP, TIMESTAMP(modified)))=1""")[0][0] > 300:
frappe.respond_as_web_page(_('Temperorily Disabled'),
_('Too many users signed up recently, so the registration is disabled. Please try back in an hour'),
http_status_code=429)
from frappe.utils import random_string
user = frappe.get_doc({
"doctype":"User",
"email": email,
"first_name": full_name,
"enabled": 1,
"phone": phone,
"new_password": password,
'user_type': 'Website User'
})
user.flags.ignore_permissions = True
user.insert()
u = frappe.get_doc('User', email)
u.append('roles',{
"doctype": "Has Role",
"role":"Owner"
})
u.role_profile_name = 'Owner'
u.save(ignore_permissions=True)
if user.flags.email_sent:
return {'status': 1, 'message': _("Please check your email for verification")}
else:
return {'status': 2, 'message': _("Please ask your administrator to verify your sign-up")}
Everything works here, except adding roles to the user. What am I missing?
I also located this question (Adding role to user via code) , having similar answer, but doesn’t seem to work.
This is the code I am using to add the user profile as said here (Adding role to a user by code - #2 by ManasSolanki)
u = frappe.get_doc('User', email)
u.append('roles',{
"doctype": "Has Role",
"role":"Owner"
})
u.role_profile_name = 'Owner'
u.save(ignore_permissions=True)
Please let me know what I am missing.