|
1 | 1 | import frappe |
2 | 2 | from frappe import _ |
| 3 | +from frappe.rate_limiter import rate_limit |
| 4 | +from frappe.utils import today |
3 | 5 |
|
4 | 6 | ONBOARDING_ROLES = ("System Manager", "Moderator") |
5 | 7 |
|
@@ -52,14 +54,55 @@ def get_setup_state() -> dict: |
52 | 54 | } |
53 | 55 |
|
54 | 56 |
|
55 | | -def needs_password_setup() -> bool: |
56 | | - """A Frappe Cloud site owner arrives signed in but with no password; on a framework that knows |
57 | | - the case, the setup flow asks for one first.""" |
| 57 | +def needs_password_setup(user: str | None = None) -> bool: |
| 58 | + """A Frappe Cloud site owner arrives signed in but with no password, and the setup flow asks |
| 59 | + for one first. Frappe's own check skips Frappe Cloud v2 and sites with email login links, |
| 60 | + which is every trial site, so Learning asks wherever a password can be used to sign in.""" |
| 61 | + from frappe.utils.password import has_password |
| 62 | + |
| 63 | + user = user or frappe.session.user |
| 64 | + if user in ("Administrator", "Guest") or has_password(user): |
| 65 | + return False |
| 66 | + if frappe.get_system_settings("disable_user_pass_login"): |
| 67 | + return False |
| 68 | + if frappe.db.get_single_value("LDAP Settings", "enabled"): |
| 69 | + return False |
| 70 | + return not frappe.db.exists( |
| 71 | + "User Social Login", |
| 72 | + {"parenttype": "User", "parent": user, "provider": ["!=", "frappe"], "userid": ["is", "set"]}, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@frappe.whitelist(methods=["POST"]) |
| 77 | +@rate_limit(limit=5, seconds=60) |
| 78 | +def set_first_password(new_password: str) -> None: |
| 79 | + """Frappe's set_first_password with Learning's check, so the framework's password step saves |
| 80 | + on the sites Frappe's own check skips. hooks.py points the framework's method here.""" |
| 81 | + from frappe.core.doctype.user.user import handle_password_test_fail, test_password_strength |
| 82 | + from frappe.utils.password import update_password |
| 83 | + |
58 | 84 | try: |
59 | | - from frappe.core.doctype.user.user import needs_password_setup as framework_says |
| 85 | + from frappe.utils.password import MAX_PASSWORD_SIZE |
60 | 86 | except ImportError: |
61 | | - return False |
62 | | - return bool(framework_says()) |
| 87 | + from frappe.auth import MAX_PASSWORD_SIZE |
| 88 | + |
| 89 | + if frappe.request and frappe.get_request_header("Authorization"): |
| 90 | + frappe.throw(_("Set your first password from a signed-in browser."), frappe.PermissionError) |
| 91 | + if not new_password or not new_password.strip(): |
| 92 | + frappe.throw(_("Enter a password."), title=_("Invalid Password")) |
| 93 | + if len(new_password) > MAX_PASSWORD_SIZE: |
| 94 | + frappe.throw(_("{0} size exceeded the maximum allowed size.").format(_("New Password"))) |
| 95 | + if not needs_password_setup(): |
| 96 | + frappe.throw(_("You already have a password. Change it from your settings."), frappe.PermissionError) |
| 97 | + user = frappe.get_doc("User", frappe.session.user) |
| 98 | + feedback = test_password_strength( |
| 99 | + new_password, |
| 100 | + user_data=(user.first_name, user.middle_name, user.last_name, user.email, user.birth_date), |
| 101 | + ) |
| 102 | + if feedback and not feedback.get("feedback", {}).get("password_policy_validation_passed", False): |
| 103 | + handle_password_test_fail(feedback) |
| 104 | + update_password(user.name, new_password, logout_all_sessions=False) |
| 105 | + frappe.db.set_value("User", user.name, {"last_password_reset_date": today(), "reset_password_key": ""}) |
63 | 106 |
|
64 | 107 |
|
65 | 108 | def can_send_email() -> bool: |
|
0 commit comments