Add deleted_at for User

Do not remove the user from the database. Mark as deleted instead.
This commit is contained in:
Patrick Gansterer 2016-03-04 19:03:52 +01:00
parent b30b4e46d8
commit b05ac2ab64
15 changed files with 82 additions and 12 deletions

View file

@ -2,12 +2,28 @@ class Admin::UsersController < Admin::BaseController
inherit_resources inherit_resources
def index def index
@users = User.natural_order @users = params[:show_deleted] ? User.deleted : User.undeleted
# if somebody uses the search field: # if somebody uses the search field:
@users = @users.natural_search(params[:user_name]) unless params[:user_name].blank? @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 end
def sudo def sudo

View file

@ -20,7 +20,7 @@ class ApplicationController < ActionController::Base
# check if there is a valid session and return the logged-in user (its object) # check if there is a valid session and return the logged-in user (its object)
if session[:user_id] && params[:foodcoop] if session[:user_id] && params[:foodcoop]
# for shared-host installations. check if the cookie-subdomain fits to request. # 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
end end
helper_method :current_user helper_method :current_user

View file

@ -1,7 +1,7 @@
class Foodcoop::UsersController < ApplicationController class Foodcoop::UsersController < ApplicationController
def index def index
@users = User.natural_order @users = User.undeleted.natural_order
# if somebody uses the search field: # if somebody uses the search field:
@users = @users.natural_search(params[:user_name]) unless params[:user_name].blank? @users = @users.natural_search(params[:user_name]) unless params[:user_name].blank?

View file

@ -14,7 +14,7 @@ class LoginController < ApplicationController
redirect_to forgot_password_url, alert: I18n.t('errors.general_again') and return redirect_to forgot_password_url, alert: I18n.t('errors.general_again') and return
end end
if (user = User.find_by_email(params[:user][:email])) if (user = User.undeleted.find_by_email(params[:user][:email]))
user.request_password_reset! user.request_password_reset!
end end
redirect_to login_url, :notice => I18n.t('login.controller.reset_password.notice') redirect_to login_url, :notice => I18n.t('login.controller.reset_password.notice')

View file

@ -20,7 +20,10 @@ class User < ActiveRecord::Base
has_many :created_orders, :class_name => 'Order', :foreign_key => 'created_by_user_id', :dependent => :nullify has_many :created_orders, :class_name => 'Order', :foreign_key => 'created_by_user_id', :dependent => :nullify
attr_accessor :password, :settings_attributes 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 # makes the current_user (logged-in-user) available in models
cattr_accessor :current_user cattr_accessor :current_user
@ -178,6 +181,18 @@ class User < ActiveRecord::Base
self.groups.where(type: '') self.groups.where(type: '')
end 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) def self.authenticate(login, password)
user = find_by_nick(login) || find_by_email(login) user = find_by_nick(login) || find_by_email(login)
if user && user.has_password(password) if user && user.has_password(password)

View file

@ -1,4 +1,4 @@
- if User.count > 20 - if User.undeleted.count > 20
= items_per_page = items_per_page
= pagination_links_remote @users = pagination_links_remote @users
%table.table.table-striped %table.table.table-striped
@ -21,5 +21,10 @@
%td= format_roles(user, true) %td= format_roles(user, true)
%td= format_time(user.last_activity) %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.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)}, %td
:method => :delete, class: 'btn btn-danger btn-mini' - 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'

View file

@ -4,13 +4,16 @@
= link_to t('.new_user'), new_admin_user_path, class: 'btn btn-primary' = link_to t('.new_user'), new_admin_user_path, class: 'btn btn-primary'
- content_for :sidebar do - 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 .well.well-small
= form_tag admin_users_path, :method => :get, :remote => true, = form_tag admin_users_path, :method => :get, :remote => true,
'data-submit-onchange' => true, class: 'form-search' do 'data-submit-onchange' => true, class: 'form-search' do
= text_field_tag :user_name, params[:user_name], class: 'input-medium search-query', = text_field_tag :user_name, params[:user_name], class: 'input-medium search-query',
placeholder: t('admin.search_placeholder') placeholder: t('admin.search_placeholder')
%label{:for => 'show_deleted'}
= check_box_tag 'show_deleted', 1, params[:show_deleted]
= t '.show_deleted'
#users #users
= render "users" = render "users"

View file

@ -1,4 +1,4 @@
- if User.count > 20 - if User.undeleted.count > 20
= items_per_page = items_per_page
= pagination_links_remote @users = pagination_links_remote @users
%table.table.table-striped %table.table.table-striped

View file

@ -273,15 +273,22 @@ de:
users: users:
controller: controller:
sudo_done: Du bist jetzt als Benutzer %{user} angemeldet. Sei vorsichtig und melde dich ab, wenn du fertig bist! 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: edit:
title: Benutzer/in bearbeiten title: Benutzer/in bearbeiten
index: index:
first_paragraph: Hier kannst du Benutzer/innen %{url}, bearbeiten und natürlich auch löschen. first_paragraph: Hier kannst du Benutzer/innen %{url}, bearbeiten und natürlich auch löschen.
new_user: Neue/n Benutzer/in anlegen new_user: Neue/n Benutzer/in anlegen
new_users: neu anlegen new_users: neu anlegen
show_deleted: Gelöschte Benutzer anzeigen
title: Benutzer/innen verwalten title: Benutzer/innen verwalten
new: new:
title: Neue/n Benutzer/in anlegen title: Neue/n Benutzer/in anlegen
restore:
error: 'Benutzer/in konnte nicht wiederhergestellt werden: %{error}'
notice: Benutzer/in wurde wiederhergestellt
show: show:
confirm_delete: Willst du den Benutzer %{user} wirklich löschen? 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! 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 back: Züruck
cancel: Abbrechen cancel: Abbrechen
close: Schließen close: Schließen
confirm_restore: Willst du %{name} wirklich wiederherstellen?
copy: kopieren copy: kopieren
delete: Löschen delete: Löschen
edit: Bearbeiten edit: Bearbeiten
@ -1698,6 +1706,7 @@ de:
success: <i class="icon icon-ok"></i> success: <i class="icon icon-ok"></i>
or_cancel: oder abbrechen or_cancel: oder abbrechen
please_wait: Bitte warten... please_wait: Bitte warten...
restore: Wiederherstellen
save: Speichern save: Speichern
show: Anzeigen show: Anzeigen
views: views:

View file

@ -278,15 +278,22 @@ en:
users: users:
controller: controller:
sudo_done: You are now logged in as %{user}. Be careful, and do not forget to log out when done! 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: edit:
title: Edit user title: Edit user
index: index:
first_paragraph: Here you can %{url}, edit and delete users. first_paragraph: Here you can %{url}, edit and delete users.
new_user: Create new user new_user: Create new user
new_users: create new new_users: create new
show_deleted: Show deleted users
title: User admin title: User admin
new: new:
title: Create new user title: Create new user
restore:
error: 'User could not be restored: %{error}'
notice: User was restored
show: show:
confirm_delete: Do you really want to remove %{user}? 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! 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 back: Back
cancel: Cancel cancel: Cancel
close: Close close: Close
confirm_restore: Do you really want to restore %{name}?
copy: Copy copy: Copy
delete: Delete delete: Delete
edit: Edit edit: Edit
@ -1720,6 +1728,7 @@ en:
success: <i class="icon icon-ok"></i> success: <i class="icon icon-ok"></i>
or_cancel: or cancel or_cancel: or cancel
please_wait: Please wait... please_wait: Please wait...
restore: Restore
save: Save save: Save
show: Show show: Show
views: views:

View file

@ -272,6 +272,9 @@ fr:
users: users:
controller: controller:
sudo_done: sudo_done:
destroy:
error: 'La ajouter n''a pas pu être dissoute: %{error}'
notice: La ajouter a été supprimée
edit: edit:
title: modifier les données sur le_la membre title: modifier les données sur le_la membre
index: index:

View file

@ -272,6 +272,9 @@ nl:
users: users:
controller: controller:
sudo_done: Je bent nu ingelogd als %{user}. Wees voorzichtig, en vergeet niet uit te loggen als je klaar bent! 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: edit:
title: Lid bewerken title: Lid bewerken
index: index:

View file

@ -173,6 +173,7 @@ Foodsoft::Application.routes.draw do
root to: 'base#index' root to: 'base#index'
resources :users do resources :users do
post :restore, on: :member
post :sudo, on: :member post :sudo, on: :member
end end

View file

@ -0,0 +1,5 @@
class AddDeletedAtToUser < ActiveRecord::Migration
def change
add_column :users, :deleted_at, :datetime
end
end

View file

@ -346,6 +346,7 @@ ActiveRecord::Schema.define(version: 20160217194036) do
t.datetime "reset_password_expires" t.datetime "reset_password_expires"
t.datetime "last_login" t.datetime "last_login"
t.datetime "last_activity" t.datetime "last_activity"
t.datetime "deleted_at"
end end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree