Commit 83e4ffbe authored by Sara Kuqi's avatar Sara Kuqi
Browse files

password reset and home page if not logged in layout

parent ff32594e
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the PasswordResets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class PasswordResetsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
before_action :check_expiration, only: [:edit, :update]
def new
end
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
def edit
end
def update
if params[:user][:password].empty?
@user.errors.add(:password, "can't be empty")
render 'edit'
elsif @user.update_attributes(user_params)
log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
# Before filters
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
@user.authenticated?(:reset, params[:id]))
#redirect_to root_url
render 'password_resets/edit'
end
end
# Checks expiration of reset token
def check_expiration
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
end
module PasswordResetsHelper
end
class UserMailer < ApplicationMailer
default from: 'notifications@example.com'
def welcome_email(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Welcome to Mol_app')
end
def password_reset(user)
@user = user
mail to: user.email, subject: "Password reset"
end
end
......@@ -5,7 +5,7 @@ class User < ApplicationRecord
self.inheritance_column = :type
attr_accessor :remember_token
attr_accessor :remember_token, :activation_token, :reset_token
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
......@@ -19,6 +19,10 @@ class User < ApplicationRecord
scope :operators, -> { where(type: 'Operator')}
scope :chemists, -> {where(type: 'Chemist')}
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
def self.types
%w(Operator Chemist)
end
......@@ -34,7 +38,16 @@ class User < ApplicationRecord
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
# Returns a random token.
def self.new_token
SecureRandom.urlsafe_base64
......
<% provide(:title, 'Reset password') %>
<h1>Reset password</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages', object: @user%>
<%= hidden_field_tag :email, @user.email %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-success" %>
<% end %>
</div>
</div>
<% provide(:title, "Forgot password") %>
<h1>Forgot password</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(:password_reset, url: password_resets_path) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.submit "Reset my password!", class: "btn btn-success" %>
<% end %>
</div>
</div>
......@@ -9,6 +9,7 @@
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= link_to "(forgot password)", new_password_reset_path %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :remember_me, class: "checkbox inline" do %>
......
<div style="margin: 0px 0px 0px 0px;">
<% provide(:title, "Home") %>
<% if logged_in? && operator? %>
<%= render 'samples/samples', samples:@samples %>
</div>
</div>
<%= render 'samples/samples', samples:@samples %>
<% else %>
<h1>Mol App</h1>
<p>In 2009, the Institute of Organic Chemistry at the KIT in Karlsruhe initiated the formation of a compound platform called MolApp
(Compound Platform former Combinatorial Chemistry Platform).
The platform has been installed to support chemists in their efforts to collect, store and share compounds that have been prepared
in various projects. While there are various compound facilities around the world that store and distribute selected drug-like
compounds, we installed the MolApp with the aim to handle all (stable) compounds of our academic projects to achieve three main aims:</p>
<p>1) Establish a standardized storage of reference compounds</p>
<p>2) Offer the compounds for academic screening initiatives</p>
<p>3) Provide compounds for academic work in fields of research other than high-throughput screening (e.g. materials science, biology).</p>
<div style="margin: 100px 200px 0px 0px;">
<h1> Mol App</h1>
<br>
<p>
<h3>Welcome to Expected Elemental Analysis</h3>
</p>
<br>
<br>
<br>
<br>
<hr>
<h3 class="center"><%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-success" %></h3>
<% end %>
......
<h1>Password reset</h1>
<p>To reset your password click the link below:</p>
<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
email: @user.email) %>
<p>This link will expire in two hours.</p>
<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
To reset your password click the link below:
<%= edit_password_reset_url(@user.reset_token, email: @user.email) %>
This link will expire in two hours.
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p>
You have successfully signed up to Mol_app.com,
your username is: <%= @user.login %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Welcome to example.com, <%= @user.name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
<% provide(:title, @user.name) %>
<h3><%= @user.name %></h3>
<p>Samples (<%= @user.samples.count %>)</p>
<% if !operator?%>
<%=render 'shared/sample_form'%></br>
<% end %>
<div class="row">
<% if @user.samples.any? %>
<% if !operator?%>
<% end %>
<div class="panel panel-default">
<%= render 'samples/samples' %>
</div>
......
......@@ -47,7 +47,7 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = {:host => "localhost:3000"}
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
......
......@@ -83,4 +83,5 @@ Rails.application.configure do
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
config.action_mailer.default_url_options = { :host => "www.localhost:3000.com" }
end
......@@ -39,4 +39,5 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { :host => "test.localhost:3000.com" }
end
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'submitformeasurements/new'
get 'results/new'
......@@ -21,6 +26,7 @@ Rails.application.routes.draw do
resources :operators, controller: 'users', type: 'Operator'
resources :chemists, controller: 'users', type: 'Chemist'
resources :samples, only: [:create, :destroy, :show, :edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :samples do
......
class AddResetToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :reset_digest, :string
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment