From b05ac2ab64c717e074da5e1c5a7aa7a684f97cf2 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Fri, 4 Mar 2016 19:03:52 +0100 Subject: [PATCH] Add deleted_at for User Do not remove the user from the database. Mark as deleted instead. --- app/controllers/admin/users_controller.rb | 20 +++++++++++++++++-- app/controllers/application_controller.rb | 2 +- app/controllers/foodcoop/users_controller.rb | 2 +- app/controllers/login_controller.rb | 2 +- app/models/user.rb | 17 +++++++++++++++- app/views/admin/users/_users.html.haml | 11 +++++++--- app/views/admin/users/index.html.haml | 7 +++++-- app/views/foodcoop/users/_users.html.haml | 2 +- config/locales/de.yml | 9 +++++++++ config/locales/en.yml | 9 +++++++++ config/locales/fr.yml | 3 +++ config/locales/nl.yml | 3 +++ config/routes.rb | 1 + .../20160217115252_add_deleted_at_to_user.rb | 5 +++++ db/schema.rb | 1 + 15 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20160217115252_add_deleted_at_to_user.rb diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 9154c8c1..3c73d63c 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -2,12 +2,28 @@ class Admin::UsersController < Admin::BaseController inherit_resources def index - @users = User.natural_order + @users = params[:show_deleted] ? User.deleted : User.undeleted # if somebody uses the search field: @users = @users.natural_search(params[:user_name]) unless params[:user_name].blank? - @users = @users.page(params[:page]).per(@per_page) + @users = @users.natural_order.page(params[:page]).per(@per_page) + end + + def destroy + @user = User.find(params[:id]) + @user.mark_as_deleted + redirect_to admin_users_url, notice: t('admin.users.destroy.notice') + rescue => error + redirect_to admin_users_url, alert: t('admin.users.destroy.error', error: error.message) + end + + def restore + @user = User.find(params[:id]) + @user.restore + redirect_to admin_users_url, notice: t('admin.users.restore.notice') + rescue => error + redirect_to admin_users_url, alert: t('admin.users.restore.error', error: error.message) end def sudo diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 711874b9..d4a1b908 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -20,7 +20,7 @@ class ApplicationController < ActionController::Base # check if there is a valid session and return the logged-in user (its object) if session[:user_id] && params[:foodcoop] # for shared-host installations. check if the cookie-subdomain fits to request. - @current_user ||= User.find_by_id(session[:user_id]) if session[:scope] == FoodsoftConfig.scope + @current_user ||= User.undeleted.find_by_id(session[:user_id]) if session[:scope] == FoodsoftConfig.scope end end helper_method :current_user diff --git a/app/controllers/foodcoop/users_controller.rb b/app/controllers/foodcoop/users_controller.rb index 31539dce..880d4407 100644 --- a/app/controllers/foodcoop/users_controller.rb +++ b/app/controllers/foodcoop/users_controller.rb @@ -1,7 +1,7 @@ class Foodcoop::UsersController < ApplicationController def index - @users = User.natural_order + @users = User.undeleted.natural_order # if somebody uses the search field: @users = @users.natural_search(params[:user_name]) unless params[:user_name].blank? diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index abf821f7..097e7277 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -14,7 +14,7 @@ class LoginController < ApplicationController redirect_to forgot_password_url, alert: I18n.t('errors.general_again') and return end - if (user = User.find_by_email(params[:user][:email])) + if (user = User.undeleted.find_by_email(params[:user][:email])) user.request_password_reset! end redirect_to login_url, :notice => I18n.t('login.controller.reset_password.notice') diff --git a/app/models/user.rb b/app/models/user.rb index e0164e1c..f9339cd2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,7 +20,10 @@ class User < ActiveRecord::Base has_many :created_orders, :class_name => 'Order', :foreign_key => 'created_by_user_id', :dependent => :nullify attr_accessor :password, :settings_attributes - + + scope :deleted, -> { where.not(deleted_at: nil) } + scope :undeleted, -> { where(deleted_at: nil) } + # makes the current_user (logged-in-user) available in models cattr_accessor :current_user @@ -178,6 +181,18 @@ class User < ActiveRecord::Base self.groups.where(type: '') end + def deleted? + deleted_at.present? + end + + def mark_as_deleted + update_column :deleted_at, Time.now + end + + def restore + update_column :deleted_at, nil + end + def self.authenticate(login, password) user = find_by_nick(login) || find_by_email(login) if user && user.has_password(password) diff --git a/app/views/admin/users/_users.html.haml b/app/views/admin/users/_users.html.haml index 9a4ba433..8219d1c5 100644 --- a/app/views/admin/users/_users.html.haml +++ b/app/views/admin/users/_users.html.haml @@ -1,4 +1,4 @@ -- if User.count > 20 +- if User.undeleted.count > 20 = items_per_page = pagination_links_remote @users %table.table.table-striped @@ -21,5 +21,10 @@ %td= format_roles(user, true) %td= format_time(user.last_activity) %td= link_to t('ui.edit'), edit_admin_user_path(user), class: 'btn btn-mini' - %td= link_to t('ui.delete'), [:admin, user], :data => {:confirm => t('admin.confirm', name: user.name)}, - :method => :delete, class: 'btn btn-danger btn-mini' + %td + - if user.deleted? + = link_to t('ui.restore'), restore_admin_user_path(user), :data => {:confirm => t('ui.confirm_restore', name: user.name)}, + :method => :post, class: 'btn btn-success btn-mini' + - else + = link_to t('ui.delete'), [:admin, user], :data => {:confirm => t('admin.confirm', name: user.name)}, + :method => :delete, class: 'btn btn-danger btn-mini' diff --git a/app/views/admin/users/index.html.haml b/app/views/admin/users/index.html.haml index d9a6805e..afc831a0 100644 --- a/app/views/admin/users/index.html.haml +++ b/app/views/admin/users/index.html.haml @@ -4,13 +4,16 @@ = link_to t('.new_user'), new_admin_user_path, class: 'btn btn-primary' - content_for :sidebar do - %p= t('.first_paragraph', url: link_to(t('.new_users'), new_admin_user_path)).html_safe + %p= t('.first_paragraph', url: link_to(t('.new_users'), new_admin_user_path)).html_safe .well.well-small = form_tag admin_users_path, :method => :get, :remote => true, 'data-submit-onchange' => true, class: 'form-search' do = text_field_tag :user_name, params[:user_name], class: 'input-medium search-query', placeholder: t('admin.search_placeholder') + %label{:for => 'show_deleted'} + = check_box_tag 'show_deleted', 1, params[:show_deleted] + = t '.show_deleted' #users - = render "users" \ No newline at end of file + = render "users" diff --git a/app/views/foodcoop/users/_users.html.haml b/app/views/foodcoop/users/_users.html.haml index 0af48f3f..9ee7ce48 100644 --- a/app/views/foodcoop/users/_users.html.haml +++ b/app/views/foodcoop/users/_users.html.haml @@ -1,4 +1,4 @@ -- if User.count > 20 +- if User.undeleted.count > 20 = items_per_page = pagination_links_remote @users %table.table.table-striped diff --git a/config/locales/de.yml b/config/locales/de.yml index 1d5160be..d14de01e 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -273,15 +273,22 @@ de: users: controller: sudo_done: Du bist jetzt als Benutzer %{user} angemeldet. Sei vorsichtig und melde dich ab, wenn du fertig bist! + destroy: + error: 'Benutzer/in konnte nicht gelöscht werden: %{error}' + notice: Benutzer/in wurde gelöscht edit: title: Benutzer/in bearbeiten index: first_paragraph: Hier kannst du Benutzer/innen %{url}, bearbeiten und natürlich auch löschen. new_user: Neue/n Benutzer/in anlegen new_users: neu anlegen + show_deleted: Gelöschte Benutzer anzeigen title: Benutzer/innen verwalten new: title: Neue/n Benutzer/in anlegen + restore: + error: 'Benutzer/in konnte nicht wiederhergestellt werden: %{error}' + notice: Benutzer/in wurde wiederhergestellt show: confirm_delete: Willst du den Benutzer %{user} wirklich löschen? confirm_sudo: Wenn du fortsetzt, dann wirst du als Benutzer %{user} angemeldet. Sei vorsichtig und melde dich ab, wenn du fertig bist! @@ -1690,6 +1697,7 @@ de: back: Züruck cancel: Abbrechen close: Schließen + confirm_restore: Willst du %{name} wirklich wiederherstellen? copy: kopieren delete: Löschen edit: Bearbeiten @@ -1698,6 +1706,7 @@ de: success: or_cancel: oder abbrechen please_wait: Bitte warten... + restore: Wiederherstellen save: Speichern show: Anzeigen views: diff --git a/config/locales/en.yml b/config/locales/en.yml index 8cb8ca8d..8d0a58d3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -278,15 +278,22 @@ en: users: controller: sudo_done: You are now logged in as %{user}. Be careful, and do not forget to log out when done! + destroy: + error: 'User could not be deleted: %{error}' + notice: User was deleted edit: title: Edit user index: first_paragraph: Here you can %{url}, edit and delete users. new_user: Create new user new_users: create new + show_deleted: Show deleted users title: User admin new: title: Create new user + restore: + error: 'User could not be restored: %{error}' + notice: User was restored show: confirm_delete: Do you really want to remove %{user}? confirm_sudo: If you continue, you will take on the identity of %{user}. Do not forget to log out when you're done! @@ -1712,6 +1719,7 @@ en: back: Back cancel: Cancel close: Close + confirm_restore: Do you really want to restore %{name}? copy: Copy delete: Delete edit: Edit @@ -1720,6 +1728,7 @@ en: success: or_cancel: or cancel please_wait: Please wait... + restore: Restore save: Save show: Show views: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 6eb89785..585da78f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -272,6 +272,9 @@ fr: users: controller: sudo_done: + destroy: + error: 'La ajouter n''a pas pu être dissoute: %{error}' + notice: La ajouter a été supprimée edit: title: modifier les données sur le_la membre index: diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 5ce39c3f..4262f70d 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -272,6 +272,9 @@ nl: users: controller: sudo_done: Je bent nu ingelogd als %{user}. Wees voorzichtig, en vergeet niet uit te loggen als je klaar bent! + destroy: + error: 'Gebruiker kon niet verwijderd worden: %{error}' + notice: Gebruiker is verwijderd edit: title: Lid bewerken index: diff --git a/config/routes.rb b/config/routes.rb index 722cb553..cafc29aa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -173,6 +173,7 @@ Foodsoft::Application.routes.draw do root to: 'base#index' resources :users do + post :restore, on: :member post :sudo, on: :member end diff --git a/db/migrate/20160217115252_add_deleted_at_to_user.rb b/db/migrate/20160217115252_add_deleted_at_to_user.rb new file mode 100644 index 00000000..88f20c9a --- /dev/null +++ b/db/migrate/20160217115252_add_deleted_at_to_user.rb @@ -0,0 +1,5 @@ +class AddDeletedAtToUser < ActiveRecord::Migration + def change + add_column :users, :deleted_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 75953d6c..de1b6c66 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -346,6 +346,7 @@ ActiveRecord::Schema.define(version: 20160217194036) do t.datetime "reset_password_expires" t.datetime "last_login" t.datetime "last_activity" + t.datetime "deleted_at" end add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree