Browse Source
* Clean up user routes * Make user registerable * Merge * Turbo devise strikes again * URL naming * Dashes not underscores * Consistent syntax * Turning off turbo changes our html * Update password link not working yet * New user path * Password edit path * Updating password keeps you signed in and redirects to show * Set new user org * Write a failing spec for user creation * Reset user password and redirect back to org users page * Test redirect * Use invite template * Request specs over feature specs * Add email validationpull/137/head
baarkerlounger
3 years ago
committed by
GitHub
24 changed files with 207 additions and 147 deletions
@ -1,4 +1,4 @@ |
|||||||
class Users::PasswordsController < Devise::PasswordsController |
class Auth::PasswordsController < Devise::PasswordsController |
||||||
include Helpers::Email |
include Helpers::Email |
||||||
|
|
||||||
def reset_confirmation |
def reset_confirmation |
@ -1,4 +1,4 @@ |
|||||||
class Users::SessionsController < Devise::SessionsController |
class Auth::SessionsController < Devise::SessionsController |
||||||
include Helpers::Email |
include Helpers::Email |
||||||
|
|
||||||
def create |
def create |
@ -1,25 +0,0 @@ |
|||||||
class Users::AccountController < ApplicationController |
|
||||||
def check_logged_in |
|
||||||
if current_user.nil? |
|
||||||
redirect_to(new_user_session_path) |
|
||||||
end |
|
||||||
end |
|
||||||
|
|
||||||
def index |
|
||||||
check_logged_in |
|
||||||
end |
|
||||||
|
|
||||||
def personal_details |
|
||||||
check_logged_in |
|
||||||
end |
|
||||||
|
|
||||||
def update |
|
||||||
if current_user.update(user_params) |
|
||||||
redirect_to(users_account_path) |
|
||||||
end |
|
||||||
end |
|
||||||
|
|
||||||
def user_params |
|
||||||
params.require(:user).permit(:email, :name, :password) |
|
||||||
end |
|
||||||
end |
|
@ -1,7 +0,0 @@ |
|||||||
class Users::RegistrationsController < Devise::RegistrationsController |
|
||||||
protected |
|
||||||
|
|
||||||
def after_update_path_for(_resource) |
|
||||||
users_account_path |
|
||||||
end |
|
||||||
end |
|
@ -0,0 +1,50 @@ |
|||||||
|
class UsersController < ApplicationController |
||||||
|
include Devise::Controllers::SignInOut |
||||||
|
include Helpers::Email |
||||||
|
before_action :authenticate_user! |
||||||
|
|
||||||
|
def update |
||||||
|
if current_user.update(user_params) |
||||||
|
bypass_sign_in current_user |
||||||
|
redirect_to user_path(current_user) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def new |
||||||
|
@resource = User.new |
||||||
|
end |
||||||
|
|
||||||
|
def create |
||||||
|
@resource = User.new |
||||||
|
if user_params["email"].empty? |
||||||
|
@resource.errors.add :email, "Enter an email address" |
||||||
|
elsif !email_valid?(user_params["email"]) |
||||||
|
@resource.errors.add :email, "Enter an email address in the correct format, like name@example.com" |
||||||
|
end |
||||||
|
if @resource.errors.present? |
||||||
|
render :new, status: :unprocessable_entity |
||||||
|
else |
||||||
|
@user = User.create!(user_params.merge(org_params).merge(password_params)) |
||||||
|
@user.send_reset_password_instructions |
||||||
|
redirect_to users_organisation_path(current_user.organisation) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def edit_password |
||||||
|
render :edit_password |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def password_params |
||||||
|
{ password: SecureRandom.hex(8) } |
||||||
|
end |
||||||
|
|
||||||
|
def org_params |
||||||
|
{ organisation: current_user.organisation } |
||||||
|
end |
||||||
|
|
||||||
|
def user_params |
||||||
|
params.require(:user).permit(:email, :name, :password) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,8 @@ |
|||||||
|
<p>Hello <%= @resource.email %>!</p> |
||||||
|
|
||||||
|
<p>Someone has requested a link to change your password. You can do this through the link below.</p> |
||||||
|
|
||||||
|
<p><%= govuk_link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p> |
||||||
|
|
||||||
|
<p>If you didn't request this, please ignore this email.</p> |
||||||
|
<p>Your password won't change until you access the link above and create a new one.</p> |
@ -0,0 +1,6 @@ |
|||||||
|
<p>Hello <%= @resource.name %>!</p> |
||||||
|
|
||||||
|
<p>An account has been created for you to submit CORE data on behalf of @resource.organisation.</p> |
||||||
|
|
||||||
|
<p>Your username is <% @resource.email %>, use the link below to set your password. |
||||||
|
<p><%= govuk_link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p> |
@ -1,8 +1,5 @@ |
|||||||
<p>Hello <%= @resource.email %>!</p> |
<% if @resource.last_sign_in_at.nil? %> |
||||||
|
<%= render partial: "password_change_initial" %> |
||||||
<p>Someone has requested a link to change your password. You can do this through the link below.</p> |
<% else %> |
||||||
|
<%= render partial: "password_change_forgotten" %> |
||||||
<p><%= govuk_link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p> |
<% end %> |
||||||
|
|
||||||
<p>If you didn't request this, please ignore this email.</p> |
|
||||||
<p>Your password won't change until you access the link above and create a new one.</p> |
|
||||||
|
@ -1,18 +0,0 @@ |
|||||||
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %> |
|
||||||
<div class="govuk-grid-row"> |
|
||||||
<div class="govuk-grid-column-two-thirds"> |
|
||||||
<h1 class="govuk-heading-l">Reset your password</h1> |
|
||||||
<%= render "devise/shared/error_messages", resource: resource %> |
|
||||||
|
|
||||||
<%= f.hidden_field :reset_password_token %> |
|
||||||
|
|
||||||
<%= f.govuk_password_field :password, |
|
||||||
label: { text: "New password" }, |
|
||||||
hint: @minimum_password_length ? { text: "Your password must be at least #{@minimum_password_length} characters and hard to guess." } : nil, |
|
||||||
autocomplete: "new-password" |
|
||||||
%> |
|
||||||
|
|
||||||
<%= f.govuk_submit "Reset password" %> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<% end %> |
|
@ -1,23 +0,0 @@ |
|||||||
<h2>Sign up</h2> |
|
||||||
|
|
||||||
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> |
|
||||||
<%= render "devise/shared/error_messages", resource: resource %> |
|
||||||
|
|
||||||
<%= f.govuk_email_field :email, |
|
||||||
label: { text: "Email address" }, |
|
||||||
autocomplete: "email" |
|
||||||
%> |
|
||||||
|
|
||||||
<%= f.govuk_password_field :password, |
|
||||||
hint: @minimum_password_length ? { text: "#{@minimum_password_length} characters minimum" } : nil, |
|
||||||
autocomplete: "new-password" |
|
||||||
%> |
|
||||||
|
|
||||||
<%= f.govuk_password_field :password_confirmation, |
|
||||||
autocomplete: "new-password" |
|
||||||
%> |
|
||||||
|
|
||||||
<%= f.govuk_submit "Sign up" %> |
|
||||||
<% end %> |
|
||||||
|
|
||||||
<%= render "devise/shared/links" %> |
|
@ -1,15 +0,0 @@ |
|||||||
<% if resource.errors.any? %> |
|
||||||
<div id="error_explanation"> |
|
||||||
<h2> |
|
||||||
<%= I18n.t("errors.messages.not_saved", |
|
||||||
count: resource.errors.count, |
|
||||||
resource: resource.class.model_name.human.downcase) |
|
||||||
%> |
|
||||||
</h2> |
|
||||||
<ul> |
|
||||||
<% resource.errors.full_messages.each do |message| %> |
|
||||||
<li><%= message %></li> |
|
||||||
<% end %> |
|
||||||
</ul> |
|
||||||
</div> |
|
||||||
<% end %> |
|
@ -0,0 +1,28 @@ |
|||||||
|
<% content_for :before_content do %> |
||||||
|
<%= govuk_back_link( |
||||||
|
text: 'Back', |
||||||
|
href: :back, |
||||||
|
) %> |
||||||
|
<% end %> |
||||||
|
|
||||||
|
<%= form_for(@resource, as: :user, html: { method: :post }) do |f| %> |
||||||
|
<div class="govuk-grid-row"> |
||||||
|
<div class="govuk-grid-column-two-thirds"> |
||||||
|
<%= f.govuk_error_summary %> |
||||||
|
|
||||||
|
<h1 class="govuk-heading-l">Invite user to submit CORE data</h1> |
||||||
|
|
||||||
|
<%= f.govuk_text_field :name, |
||||||
|
autocomplete: "name" |
||||||
|
%> |
||||||
|
|
||||||
|
<%= f.govuk_email_field :email, |
||||||
|
label: { text: "Email address" }, |
||||||
|
autocomplete: "email", |
||||||
|
value: @resource.email |
||||||
|
%> |
||||||
|
|
||||||
|
<%= f.govuk_submit "Continue" %> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<% end %> |
@ -0,0 +1,41 @@ |
|||||||
|
require "rails_helper" |
||||||
|
require_relative "../support/devise" |
||||||
|
|
||||||
|
RSpec.describe UsersController, type: :request do |
||||||
|
let(:user) { FactoryBot.create(:user) } |
||||||
|
let(:headers) { { "Accept" => "text/html" } } |
||||||
|
let(:page) { Capybara::Node::Simple.new(response.body) } |
||||||
|
|
||||||
|
describe "#show" do |
||||||
|
before do |
||||||
|
sign_in user |
||||||
|
get "/users/#{user.id}", headers: headers, params: {} |
||||||
|
end |
||||||
|
|
||||||
|
it "show the user details" do |
||||||
|
expect(page).to have_content("Your account") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe "#edit" do |
||||||
|
before do |
||||||
|
sign_in user |
||||||
|
get "/users/#{user.id}/edit", headers: headers, params: {} |
||||||
|
end |
||||||
|
|
||||||
|
it "show the edit personal details page" do |
||||||
|
expect(page).to have_content("Change your personal details") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe "#edit_password" do |
||||||
|
before do |
||||||
|
sign_in user |
||||||
|
get "/users/#{user.id}/password/edit", headers: headers, params: {} |
||||||
|
end |
||||||
|
|
||||||
|
it "show the edit password page" do |
||||||
|
expect(page).to have_content("Change your password") |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue