diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4d6bd4f..52f9558 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,7 +4,7 @@ def admin_index authorize User - @users = User.where(organisation: current_user.organisation).includes(:project_groups) + @users = current_user.organisation.users.includes(:project_groups) respond_to do |format| format.html # index.html.erb diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 488debf..f857941 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -8,7 +8,8 @@ has_many :guidance_groups has_many :dmptemplates has_many :sections - has_many :users, through: :user_org_roles + has_many :user_org_roles + has_many :users has_many :option_warnings has_many :suggested_answers has_and_belongs_to_many :token_permission_types, join_table: "org_token_permissions" diff --git a/app/models/user.rb b/app/models/user.rb index fa7da4d..484f41b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,7 +13,6 @@ has_many :answers has_many :user_org_roles has_many :project_groups, :dependent => :destroy - #has_many :organisations , through: :user_org_roles has_many :user_role_types, through: :user_org_roles has_one :language @@ -78,85 +77,24 @@ # @param new_organisation_id [Integer] the id for an organisation # @return [String] the empty string as a causality of setting api_token def organisation_id=(new_organisation_id) - # DEPRICATED STRUCTURE ONLY USED HERE -# if !self.user_org_roles.pluck(:organisation_id).include?(new_organisation_id.to_i) then - # if the user has more than one role -# if self.user_org_roles.count != 1 then -# new_user_org_role = UserOrgRole.new -# new_user_org_role.organisation_id = new_organisation_id -# new_user_org_role.user_role_type = UserRoleType.find_by(name: constant("user_role_types.user")); -# self.user_org_roles << new_user_org_role - # if the user has roles other than one(0/2/3?) -# else - # set role to first role -# user_org_role = self.user_org_roles.first - # change org_id to new org_id -# user_org_role.organisation_id = new_organisation_id - # save modified role -# user_org_role.save - # if the user is not part of the new organisation -# if !self.user_org_roles.pluck(:organisation_id).include?(new_organisation_id.to_i) then -# unless self.can_change_org? - # rip all permissions from user -# self.roles.delete_all -# self.save! -# end -# end -# end -# end - - self.organisation = Organisation.find(new_organisation_id) - - # rip api_token from user + unless self.can_change_org? || new_organisation_id.nil? || self.organisation.nil? + # rip all permissions from the user + self.roles.delete_all + end + # set the user's new organisation + super(new_organisation_id) + self.save! + # rip api permissions from the user self.remove_token! end ## - # returns the first organisation id of the user or nil - # - # @return [Integer, nil] the id of the user's organisation - def organisation_id -# if self.organisations.count > 0 then -# return self.organisations.first.id -# else -# return nil -# end - (self.organisation.nil? ? nil : self.organisation.id) - end - - ## - # returns the organisation of the user or nil - # - # @return [Organisation, nil] the organisation of the user -# def organisation -# if self.organisations.count > 0 then -# return self.organisations.first -# else -# return nil -# end -# end - - ## - # returns the last organisation in the list of organisations - # possibly depricated as the user only has one organisation in the current schema - # - # @return [Organisation, nil] the organisation for the user - def current_organisation -# if self.organisations.count > 0 then -# return self.organisations.last -# else -# return nil -# end - self.organisation - end - - ## # sets a new organisation for the user # # @param new_organisation [Organisation] the new organisation for the user -# def organisation=(new_organisation) -# organisation_id = organisation.id -# end + def organisation=(new_organisation) + organisation_id = new_organisation.id unless new_organisation.nil? + end ## # checks if the user is a super admin @@ -319,5 +257,4 @@ user.save! end end - end diff --git a/db/migrate/20161021100420_single_organisation_for_users.rb b/db/migrate/20161021100420_single_organisation_for_users.rb new file mode 100644 index 0000000..2b596f7 --- /dev/null +++ b/db/migrate/20161021100420_single_organisation_for_users.rb @@ -0,0 +1,19 @@ +class SingleOrganisationForUsers < ActiveRecord::Migration + + def up + + User.class_eval do + belongs_to :organisation, + :class_name => "Organisation", + :foreign_key => "organisation_id" + end + + User.includes(:user_org_roles, :roles).all.each do | user | + # NOTE: we'll grab the first organisation (if present), so if there are more, these will be lost! + user.organisation_id = user.user_org_roles.first.organisation_id unless user.user_org_roles.empty? + user.save! + end + + drop_table :user_org_roles + end +end