From 56cb805110187d0fa23c890549f2a9d4b05d6524 Mon Sep 17 00:00:00 2001 From: chrisbr Date: Thu, 6 Nov 2014 16:17:11 +0100 Subject: [PATCH 1/2] Adds disabled flag to user and checks it if iChain is enabled --- app/controllers/application_controller.rb | 8 ++++++++ app/models/user.rb | 8 ++++++++ db/migrate/20141106141750_add_is_disabled_flag_to_user.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20141106141750_add_is_disabled_flag_to_user.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 59a9c552..b9961dc5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -54,6 +54,14 @@ class ApplicationController < ActionController::Base redirect_to root_path end + rescue_from UserDisabled do + Rails.logger.debug('User is disabled!') + sign_out(current_user) + mail = User.admin.first ? User.admin.first.email : 'the admin!' + flash[:error] = "This User is disabled. Please contact #{mail}!" + redirect_to root_path + end + def not_found raise ActionController::RoutingError.new('Not Found') end diff --git a/app/models/user.rb b/app/models/user.rb index d0cdabc9..f9f2e40a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,9 @@ class IChainRecordNotFound < StandardError end +class UserDisabled < StandardError +end + class User < ActiveRecord::Base rolify include Gravtastic @@ -42,6 +45,8 @@ class User < ActiveRecord::Base has_many :subscriptions, dependent: :destroy accepts_nested_attributes_for :roles + scope :admin, -> { where(is_admin: true) } + validates :email, presence: true validates :username, @@ -59,6 +64,9 @@ class User < ActiveRecord::Base def self.for_ichain_username(username, attributes) user = find_by(username: username) + + raise UserDisabled if user && user.is_disabled + if user user.update_attributes(email: attributes[:email]) else diff --git a/db/migrate/20141106141750_add_is_disabled_flag_to_user.rb b/db/migrate/20141106141750_add_is_disabled_flag_to_user.rb new file mode 100644 index 00000000..07830d74 --- /dev/null +++ b/db/migrate/20141106141750_add_is_disabled_flag_to_user.rb @@ -0,0 +1,5 @@ +class AddIsDisabledFlagToUser < ActiveRecord::Migration + def change + add_column :users, :is_disabled, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 32fab65b..8d861931 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141104131625) do +ActiveRecord::Schema.define(version: 20141106141750) do create_table "ahoy_events", force: true do |t| t.uuid "visit_id" @@ -505,6 +505,7 @@ ActiveRecord::Schema.define(version: 20141104131625) do t.text "volunteer_experience" t.boolean "is_admin", default: false t.string "username" + t.boolean "is_disabled", default: false end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true From de9229808524b21aea220c0cf0e7ef2a210f265d Mon Sep 17 00:00:00 2001 From: chrisbr Date: Thu, 6 Nov 2014 16:17:30 +0100 Subject: [PATCH 2/2] Implements placeholder for login forms --- app/views/layouts/_navigation.html.haml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 2c8d937d..0e908e95 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -44,13 +44,13 @@ .dropdown-menu{:style => "padding: 17px;"} - if CONFIG['authentication']['ichain']['enabled'] = form_tag User.ichain_login_url do - = text_field_tag 'username', nil, id: 'user_ichain_email_dd' - = password_field_tag 'password', nil, id: 'user_ichain_password_dd' + = text_field_tag 'username', nil, id: 'user_ichain_email_dd', placeholder: 'Username' + = password_field_tag 'password', nil, id: 'user_ichain_password_dd', placeholder: 'Password' %button.btn.btn-success.btn-block Sign in - else = form_tag new_user_session_path do - = text_field_tag 'user[login]', nil, id: 'user_login_dd' - = password_field_tag 'user[password]', nil, id: 'user_password_dd' + = text_field_tag 'user[login]', nil, id: 'user_login_dd', placeholder: 'Username / E-Mail' + = password_field_tag 'user[password]', nil, id: 'user_password_dd', placeholder: 'Password' %p.text-right %small Remember me