Commit ad2b283c authored by Sara Kuqi's avatar Sara Kuqi
Browse files

Password reset-update

parent 904b5418
......@@ -19,3 +19,6 @@
# Ignore Byebug command history file.
.byebug_history
# Ignore config
/config/database.yml
......@@ -23,44 +23,42 @@ class PasswordResetsController < ApplicationController
end
def update
if params[:user][:password].empty?
@user.errors.add(:password, "can't be empty")
if params[@user.type.downcase][:password].empty?
@user.errors.add(:password, "can't be empty")
render 'edit'
elsif @user.update_attributes(user_params)
elsif @user.update_attributes(user_params)
log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit'
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
# Before filters
params.require(@user.type.downcase).permit(:password, :password_confirmation)
end
#Before filters
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
#Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
unless (@user && # @user.activated? &&
@user.authenticated?(:reset, params[:id]))
redirect_to root_url
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
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
......
......@@ -52,15 +52,15 @@ class SamplesController < ApplicationController
end
end
def update
def update
sample = Sample.find(params[:id])
if sample.update_attributes(sample_params)
if sample.update_attributes(sample_params)
sample.touch
flash[:success] = "Sample updated"
else
else
flash[:danger] = sample.errors.full_messages.to_sentence
end
redirect_to (request.referer).pathmap + "?display_sample=#{sample.id}"
end
redirect_to request.referer + "?display_sample=#{sample.id}"
end
def show
......@@ -93,7 +93,7 @@ class SamplesController < ApplicationController
sample.status = "submitted"
sample.save
end
redirect_to (request.referer).pathmap + "?display_sample=#{sample.id}"
redirect_to request.referer + "?display_sample=#{sample.id}"
end
def ReleaseMeasurment
......@@ -102,7 +102,7 @@ class SamplesController < ApplicationController
sample.status = "measured"
sample.save
end
redirect_to (request.referer).pathmap + "?display_sample=#{sample.id}"
redirect_to request.referer + "?display_sample=#{sample.id}"
end
private
......
......@@ -17,7 +17,7 @@ class UsersController < ApplicationController
Sample.find(params[:display_sample].to_i)
else
current_user.samples.build
end
end
@user = User.find(params[:id])
if operator?
@samples = @user.samples.where('status <> ? OR user_id = ?','in preparation',current_user.id).paginate(page: params[:page])
......@@ -88,5 +88,4 @@ class UsersController < ApplicationController
type.constantize
end
end
......@@ -2,13 +2,12 @@ class UserMailer < ApplicationMailer
default from: 'notifications@example.com'
def measured_email(user)
@user = user
#@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Your sample has been measured')
mail to: @user.email, subject: 'Your sample has been measured',from: "elemental_analysis@ioc.kit.edu"
end
def password_reset(user)
@user = user
mail to: user.email, subject: "Password reset"
mail to: user.email, subject: "Password reset", from: "elemental_analysis@ioc.kit.edu"
end
end
......@@ -4,7 +4,9 @@ class User < ApplicationRecord
self.inheritance_column = :type
attr_accessor :remember_token, :activation_token, :reset_token
before_save { self.email = email.downcase }
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
......@@ -19,7 +21,7 @@ class User < ApplicationRecord
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
end
def self.types
%w(Operator Chemist)
......@@ -41,9 +43,9 @@ class User < ApplicationRecord
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
UserMailer.password_reset(self).deliver_now
end
# Returns a random token.
def self.new_token
......@@ -56,14 +58,20 @@ class User < ApplicationRecord
update_attribute(:remember_token, User.digest(remember_token))
end
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
# Forgets a user.
def forget
update_attribute(:remember_token, nil)
end
end
end
class Operator < User; end
class Chemist < User; end
......@@ -12,7 +12,7 @@
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.label :password_confirmation,"Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-success" %>
......
......@@ -12,5 +12,6 @@
To see the report, just follow this link: <%=#%>.
</p>
<p>Have a great day!</p>
<p>*** This is an automatically generated email, please do not reply ***</p>
</body>
</html>
......@@ -6,3 +6,5 @@ Your sample has been successfully measured.
To see the report, just follow this link: <%= @url %>.
Have a great day!
*** This is an automatically generated email, please do not reply ***
......@@ -11,3 +11,7 @@
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
<p>Have a great day!</p>
<p>*** This is an automatically generated email, please do not reply ***</p>
......@@ -6,3 +6,7 @@ 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.
Have a great day!
*** This is an automatically generated email, please do not reply ***
......@@ -29,13 +29,12 @@ Rails.application.configure do
host = 'localhost:3000'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.mailgun.org',
:port => '587',
:authentication => :plain,
:user_name => 'postmaster@sandboxa4c11f13bfcb46bb8ba971d74fb255cc.mailgun.org',
:password => '7a7d729689e994c6f24cb8744f0e11fa',
:domain => 'sandboxa4c11f13bfcb46bb8ba971d74fb255cc.mailgun.org',
:enable_starttls_auto => true
:address => 'smarthost.kit.edu',
:port => '25',
#:authentication => :plain
#:user_name => 'sara@ITG-COMPLAT1-L.itg.kit.edu',
:domain => 'ITG-COMPLAT1-L.itg.kit.edu',
#:enable_starttls_auto => true
}
# Don't care if the mailer can't send.
#3config.action_mailer.raise_delivery_errors = false
......
......@@ -16,17 +16,16 @@ Rails.application.configure do
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'localhost:3000'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.mailgun.org',
:port => '587',
:authentication => :plain,
:user_name => 'postmaster@sandboxa4c11f13bfcb46bb8ba971d74fb255cc.mailgun.org',
:password => '7a7d729689e994c6f24cb8744f0e11fa',
:domain => 'sandboxa4c11f13bfcb46bb8ba971d74fb255cc.mailgun.org',
:enable_starttls_auto => true
}
#host = 'localhost:3000'
# config.action_mailer.default_url_options = { host: host }
# ActionMailer::Base.smtp_settings = {
# :address => 'smarthost.kit.edu',
# :port => '25',
# :authentication => :plain,
# :user_name => 'sara@ITG-COMPLAT1-L.itg.kit.edu',
# :domain => 'ITG-COMPLAT1-L.itg.kit.edu',
# :enable_starttls_auto => true
# }
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
......@@ -99,5 +98,5 @@ Rails.application.configure do
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
#222config.action_mailer.default_url_options = { :host => "www.localhost:3000.com" }
config.action_mailer.default_url_options = { :host => "localhost:3000." }
end
......@@ -41,5 +41,4 @@ Rails.application.routes.draw do
post :sort
end
default_url_options :host => "localhost:3000.com"
end
class AddActivationDigestToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated_at, :datetime
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161026105208) do
ActiveRecord::Schema.define(version: 20161116085551) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -50,13 +50,17 @@ ActiveRecord::Schema.define(version: 20161026105208) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "type", default: "Chemist"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "type", default: "Chemist"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.boolean "activated"
t.string "activation_digest"
t.datetime "activated_at"
t.string "password_reset_token"
t.datetime "password_reset_sent_at"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
end
......
......@@ -5,3 +5,22 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
end
......@@ -2,3 +2,36 @@ michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
lana:
name: Lana Kane
email: hands@example.gov
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
malory:
name: Malory Archer
email: boss@example.gov
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
<% 30.times do |n| %>
user_<%= n %>:
name: <%= "User #{n}" %>
email: <%= "user-#{n}@example.com" %>
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
<% 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