Skip to content

Commit 60f0407

Browse files
committed
Add api for password resets
1 parent 96a74fb commit 60f0407

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
module V8
5+
module Users
6+
class PasswordResetController < Api::V8::BaseController
7+
skip_authorization_check
8+
9+
def create
10+
@email = params['email'].to_s.strip
11+
if @email.empty?
12+
return render json: {
13+
success: false,
14+
errors: 'No email address provided'
15+
}
16+
end
17+
18+
user = User.find_by_email(@email)
19+
unless user
20+
return render json: {
21+
success: false,
22+
errors: 'No such email address registered'
23+
}
24+
end
25+
26+
key = ActionToken.generate_password_reset_key_for(user)
27+
# TODO: Whitelist origins
28+
PasswordResetKeyMailer.reset_link_email(user, key, params['origin']).deliver
29+
render json: {
30+
success: true
31+
}
32+
end
33+
34+
end
35+
end
36+
end
37+
end

app/mailers/password_reset_key_mailer.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class PasswordResetKeyMailer < ActionMailer::Base
2-
def reset_link_email(user, key)
2+
def reset_link_email(user, key, origin = nil)
3+
@user = user
4+
@origin = origin
35
settings = SiteSetting.value('emails')
46

5-
subject = '[TMC] Password Reset'
7+
subject = 'Reset your mooc.fi account password'
68
@url = settings['baseurl'].sub(/\/+$/, '') + '/reset_password/' + key.token
79
mail(from: settings['from'], to: user.email, subject: subject)
810
end

app/views/password_reset_key_mailer/reset_link_email.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
</head>
66
<body>
7-
<p>Please go to the following URL to change your password</p>
7+
<p>Someone requested a password reset for your mooc.fi account <%= @user.login %>.<%= @origin ? " The request originated from '#{@origin}'." : '' %> Please proceed to the following URL to change your password</p>
88

99
<a href="<%= @url %>"><%= @url %></a>
1010

app/views/password_reset_key_mailer/reset_link_email.text.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Please go to the following URL to change your password
1+
Someone requested a password reset for your mooc.fi account <%= @user.login %>.<% @origin.nil? ? '' : " The request originated from #{@origin}." %> Please proceed to the following URL to change your password
22

33
<%= @url %>
44

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444

4545
resources :users, only: [:show, :create]
4646

47+
namespace :users do
48+
resources :password_reset, only: [:create]
49+
end
50+
4751
resources :organizations, param: :slug, path: 'org', only: %i{index show} do
4852
resources :courses, module: :organizations, param: :name, only: :show do
4953
resources :points, module: :courses, only: :index

0 commit comments

Comments
 (0)