Merge pull request #406 from foodcoop1040/delete_user
Add deleted_at for User
This commit is contained in:
commit
97f81d9826
15 changed files with 82 additions and 12 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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?
|
||||||
|
|
|
@ -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')
|
||||||
|
|
|
@ -21,6 +21,9 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -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
|
||||||
|
- 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'
|
:method => :delete, class: 'btn btn-danger btn-mini'
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
'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"
|
|
@ -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
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
5
db/migrate/20160217115252_add_deleted_at_to_user.rb
Normal file
5
db/migrate/20160217115252_add_deleted_at_to_user.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddDeletedAtToUser < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :users, :deleted_at, :datetime
|
||||||
|
end
|
||||||
|
end
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue