password-reset.tcl

Reset a user's password.

Location:
/packages/acs-admin/www/users/password-reset.tcl

Related Files

[ hide source ] | [ make this the default ]

File Contents

ad_page_contract {
    Reset a user's password.
} {
    user_id:naturalnum,notnull
    {return_url:localurl .}
}

auth::require_login
if {![acs_user::site_wide_admin_p -user_id [ad_conn user_id]]} {
    ns_returnforbidden
    ad_script_abort
}

set user_info [acs_user::get_user_info -user_id $user_id]
if {$user_info eq ""} {
    ns_returnnotfound
    ad_script_abort
}

set authority_id [dict get $user_info authority_id]
if {$authority_id ne [auth::authority::local]} {
    # External authorities retain their existing reset behavior.
    auth::password::reset -admin -authority_id $authority_id \
        -username [dict get $user_info username]
    ad_returnredirect $return_url
    ad_script_abort
}

switch -- [ns_conn method] {
    GET - HEAD {
        set email [party::get -party_id $user_id -element email]
        set csrf_token [security::csrf::new]
        set form_fields [export_vars -form {user_id return_url}]
        set context [list [list "./" "Users"] "Send password reset link"]
    }
    POST {
        security::csrf::new
        security::csrf::validate

        if {[catch {auth::local::password::send_reset_link $user_id} sent]} {
            ns_log Error "Admin password reset email failed for user $user_id"
            ad_return_error "Mail failed" "The system could not send the password reset link."
            ad_script_abort
        }
        if {!$sent} {
            ad_return_complaint 1 "A password reset link requires a local account with an email address."
            ad_script_abort
        }

        ad_returnredirect -message "Password reset link sent." $return_url
        ad_script_abort
    }
    default {
        template::require_post
    }
}

# Local variables:
#    mode: tcl
#    tcl-indent-level: 4
#    indent-tabs-mode: nil
# End: