From 7826d2f6747af7f0f839ba969c91a4289f02a905 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 15 Jun 2013 02:04:44 +0200 Subject: [PATCH 1/2] move login and logout to separate methods Conflicts: app/controllers/sessions_controller.rb Conflicts: app/controllers/application_controller.rb app/controllers/sessions_controller.rb --- app/controllers/application_controller.rb | 15 +++++++++++++-- app/controllers/sessions_controller.rb | 8 ++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3a01550a..d112a86a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -31,12 +31,23 @@ class ApplicationController < ActionController::Base end private - + + def login(user) + session[:user_id] = user.id + session[:scope] = FoodsoftConfig.scope # Save scope in session to not allow switching between foodcoops with one account + session[:locale] = user.locale + end + + def logout + session[:user_id] = nil + session[:return_to] = nil + end + def authenticate(role = 'any') # Attempt to retrieve authenticated user from controller instance or session... if !current_user # No user at all: redirect to login page. - session[:user_id] = nil + logout session[:return_to] = request.original_url redirect_to_login :alert => I18n.t('application.controller.error_authn') else diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 68dfc2d1..dad2f6fb 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -9,10 +9,7 @@ class SessionsController < ApplicationController def create user = User.authenticate(params[:nick], params[:password]) if user - session[:user_id] = user.id - session[:scope] = FoodsoftConfig.scope # Save scope in session to not allow switching between foodcoops with one account - session[:locale] = user.locale - + login user if session[:return_to].present? redirect_to_url = session[:return_to] session[:return_to] = nil @@ -27,8 +24,7 @@ class SessionsController < ApplicationController end def destroy - session[:user_id] = nil - session[:return_to] = nil + logout redirect_to login_url, :notice => I18n.t('sessions.logged_out') end From 68d54e7e253743c777303047792e0a648b9f0c58 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 18 Oct 2014 17:52:06 +0200 Subject: [PATCH 2/2] add sudo functionality --- app/controllers/admin/users_controller.rb | 6 ++++++ app/views/admin/users/show.html.haml | 6 ++++-- config/locales/en.yml | 6 +++++- config/locales/fr.yml | 2 +- config/locales/nl.yml | 4 +++- config/routes.rb | 4 +++- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index c86b173e..9154c8c1 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -9,4 +9,10 @@ class Admin::UsersController < Admin::BaseController @users = @users.page(params[:page]).per(@per_page) end + + def sudo + @user = User.find(params[:id]) + login @user + redirect_to root_path, notice: I18n.t('admin.users.controller.sudo_done', user: @user.name) + end end diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index 830a88da..51fae993 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -35,5 +35,7 @@ %hr/ %p = link_to t('ui.edit'), edit_admin_user_path(@user), class: 'btn' - = link_to t('ui.delete'), [:admin, @user], :data => {:confirm => t('.confirm', user: @user.first_name)}, - :method => :delete, class: 'btn btn-danger' + = link_to t('ui.delete'), [:admin, @user], method: :delete, class: 'btn btn-danger', + data: {confirm: t('.confirm_delete', user: @user.first_name)} + = link_to t('.sudo'), sudo_admin_user_path(@user), method: :post, class: 'btn', + data: {confirm: t('.confirm_sudo', user: @user.first_name)} diff --git a/config/locales/en.yml b/config/locales/en.yml index 2ac04f46..f7e9c440 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -267,6 +267,8 @@ en: title: Ordergroup %{name} search_placeholder: name .. users: + controller: + sudo_done: You are now logged in as %{user}. Be careful, and do not forget to log out when done! edit: title: Edit user index: @@ -277,12 +279,14 @@ en: new: title: Create new user show: - confirm: Do you really want to kick out %{user}? + confirm_delete: Do you really want to delete %{user}? + confirm_sudo: "If you continue, you will take on the identity of %{user}. Do not forget to log out when you're done!" groupabos: Group subscriptions member_since: Member since %{time} person: Person preference: Preferences send_message: Send message + sudo: Take on identity workgroups: destroy: error: 'Workgroup could not be deleted: %{error}' diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 4566cdd9..8958286f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -275,7 +275,7 @@ fr: new: title: Ajouter unE nouveLLE_eau membre show: - confirm: Veux-tu vraiment expulser %{user}? + confirm_delete: Veux-tu vraiment expulser %{user}? groupabos: Participation à des équipes member_since: Membre depuis %{time} person: Personne diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 0e4a7b09..5d29b9c1 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -275,12 +275,14 @@ nl: new: title: Nieuwe gebruiker toevoegen show: - confirm: Wil je %{user} daadwerkelijk verwijderen? + confirm_delete: Wil je %{user} daadwerkelijk verwijderen? + confirm_sudo: Als je doorgaat, neem je de identiteit aan van gebruiker %{user}. Vergeet hierna niet uit te loggen! groupabos: Groepslidmaatschappen member_since: Lid sinds %{time} person: Persoon preference: Voorkeuren send_message: Bericht versturen + sudo: Inloggen als workgroups: destroy: error: 'Werkgroep kon niet verwijderd worden: %{error}' diff --git a/config/routes.rb b/config/routes.rb index 87e68e0f..e70a1361 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -172,7 +172,9 @@ Foodsoft::Application.routes.draw do namespace :admin do root :to => 'base#index' - resources :users + resources :users do + post :sudo, :on => :member + end resources :workgroups do get :memberships, :on => :member