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
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

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)
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

View file

@ -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?

View file

@ -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')

View file

@ -21,6 +21,9 @@ class User < ActiveRecord::Base
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)

View file

@ -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'

View file

@ -11,6 +11,9 @@
'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"

View file

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

View file

@ -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: <i class="icon icon-ok"></i>
or_cancel: oder abbrechen
please_wait: Bitte warten...
restore: Wiederherstellen
save: Speichern
show: Anzeigen
views:

View file

@ -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: <i class="icon icon-ok"></i>
or_cancel: or cancel
please_wait: Please wait...
restore: Restore
save: Save
show: Show
views:

View file

@ -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:

View file

@ -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:

View file

@ -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

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 "last_login"
t.datetime "last_activity"
t.datetime "deleted_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree