Login Form - Custom password recovery URL

Added by Álvaro Formoso 9 months ago

Hello everyone,

I'm trying to customize the 'Password Recovery URL' that's shown on the Redmine login page to redirect to an external URL, as the instance I am working with is integrated with an external user base source.

Following the code trace we can see that the link HTML tag is something like:

  <a class="lost_password" href="/account/lost_password">Recover your password</a>

Looking for the code piece responsible for this part among the Redmine source code, on the 'app/views/account/login.html.erb' file can be found:

  <label for="password">
    <%=l(:field_password)%>
    <%= link_to l(:label_password_lost), lost_password_path, :class => "lost_password" if Setting.lost_password? %>
  </label>
  <%= password_field_tag 'password', nil, :tabindex => '2' %>

From this code I understand that the '/account/lost_password' path that's the value of the href attribute on the HTML link comes from the lost_password_path variable. What I cannot find is where this lost_password_path is defined. Maybe someone can point me on the right direction. :)

In the end, what I'm looking for is a way to customize the password recovery URL in a clean way, not overwriting the variable with the desired value on the template, for the obvious maintainability reasons.

The versions of the instance are:

 Environment:
  Redmine version                3.4.6.stable
  Ruby version                   2.2.6-p396 (2016-11-15) [x86_64-linux]
  Rails version                  4.2.8
  Environment                    funandgames
  Database adapter               Mysql2

Thank you.

Replies (3)

RE: Login Form - Custom password recovery URL - Added by Mayama Takeshi 9 months ago

Hi,
I don't know much about rails but out of curiosity I grepped the code and could not find the definition of lost_password_path either:

root@takeshi:redmine-3.4.2# find . -type f|xargs grep lost_password_path
./app/views/account/login.html.erb:    <%= link_to l(:label_password_lost), lost_password_path, :class => "lost_password" if Setting.lost_password? %>
./app/views/account/lost_password.html.erb:<%= form_tag(lost_password_path) do %>
./app/views/account/password_recovery.html.erb:<%= form_tag(lost_password_path) do %>
./app/controllers/account_controller.rb:          handle_inactive_user(user, lost_password_path)

Then I googled and found this:

If you append _path you will get relative URL like /school/check_teachers and if you append _url you will get absolute URL like ` local host:3000/school/check_teachers` depending on the rails environment and the host specified in the config files.

Ref: https://stackoverflow.com/questions/8477301/link-to-path-definition

So, I understand this is not configurable and you would have indeed to replace lost_password_path with the external URL you want to call (I tested changing it to "http://google.com" and it worked).
You could patch the code to check if a lost_password_url is defined in configuration file and use it instead of lost_password_path and try to have it pushed to redmine repo so to not keep patching the code every time you update your redmine version.

RE: Login Form - Custom password recovery URL - Added by Go MAEDA 9 months ago

The path of lost_password is defined on this line: source:tags/4.0.0/config/routes.rb#L24

I am not sure whether you can connect an external URL to lost_password, but this page may help you to understand config/routes.rb.
https://guides.rubyonrails.org/routing.html

RE: Login Form - Custom password recovery URL - Added by Mayama Takeshi 9 months ago

Following Maeda's tip, I found this:
https://stackoverflow.com/questions/3622706/creating-a-rails-route-to-an-external-url

Then I tried this change in config/routes.rb and it worked:

  match 'account/lost_password' => redirect("http://yahoo.com"), :via => [:get, :post], :as => 'lost_password'

(1-3/3)