From e02132578ab15cbc74291da6d428717b256462ec Mon Sep 17 00:00:00 2001 From: zmagod Date: Mon, 24 Oct 2016 20:07:20 +0200 Subject: [PATCH 1/2] first run --- app/controllers/users/settings_controller.rb | 15 ++++++++++++++- app/models/user.rb | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/controllers/users/settings_controller.rb b/app/controllers/users/settings_controller.rb index 2d301d3d9..cc77df0b7 100644 --- a/app/controllers/users/settings_controller.rb +++ b/app/controllers/users/settings_controller.rb @@ -11,7 +11,8 @@ class Users::SettingsController < ApplicationController :tutorial, :reset_tutorial, :notifications_settings, - :user_current_organization + :user_current_organization, + :destroy_user_organization ] before_action :check_organization_permission, only: [ @@ -401,6 +402,7 @@ class Users::SettingsController < ApplicationController end if !invalid + reset_current_organization if params[:leave] then flash[:notice] = I18n.t( "users.settings.organizations.index.leave_flash", @@ -598,4 +600,15 @@ class Users::SettingsController < ApplicationController UserNotification.create(notification: notification, user: target_user) end end + + # checks if the user is still in the current organization + def reset_current_organization + if @user.organizations_ids.exclude? @user.current_organization_id + @user.current_organization_id = @user.organizations_ids.first + end + end + + # def reset_current_organization_of_removed_users + # + # end end diff --git a/app/models/user.rb b/app/models/user.rb index 809389459..8227d8b90 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -259,6 +259,10 @@ class User < ActiveRecord::Base .uniq end + def organizations_ids + organizations.pluck(:id) + end + protected def time_zone_check From 6a328969f026994e271b2f9c85b39c8ac0376a19 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 25 Oct 2016 09:53:02 +0200 Subject: [PATCH 2/2] reset current_user if the user leaves the team [fixes SCI-599] --- app/controllers/users/settings_controller.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/controllers/users/settings_controller.rb b/app/controllers/users/settings_controller.rb index cc77df0b7..931773186 100644 --- a/app/controllers/users/settings_controller.rb +++ b/app/controllers/users/settings_controller.rb @@ -393,7 +393,7 @@ class Users::SettingsController < ApplicationController # the user from the organization) new_owner = current_user end - + reset_user_current_organization(@user_org) @user_org.destroy(new_owner) end rescue Exception @@ -402,7 +402,6 @@ class Users::SettingsController < ApplicationController end if !invalid - reset_current_organization if params[:leave] then flash[:notice] = I18n.t( "users.settings.organizations.index.leave_flash", @@ -601,14 +600,10 @@ class Users::SettingsController < ApplicationController end end - # checks if the user is still in the current organization - def reset_current_organization - if @user.organizations_ids.exclude? @user.current_organization_id - @user.current_organization_id = @user.organizations_ids.first - end + def reset_user_current_organization(user_org) + ids = user_org.user.organizations_ids + ids -= [user_org.organization.id] + user_org.user.current_organization_id = ids.first + user_org.user.save end - - # def reset_current_organization_of_removed_users - # - # end end