diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 488debf..d073907 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -8,7 +8,7 @@ has_many :guidance_groups has_many :dmptemplates has_many :sections - has_many :users, through: :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 6921a36..c81f3bd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,11 +13,11 @@ has_many :answers has_many :user_org_roles has_many :project_groups, :dependent => :destroy - has_many :organisations , through: :user_org_roles + #has_many :organisations , through: :user_org_roles has_many :user_role_types, through: :user_org_roles has_one :language - + belongs_to :organisation has_many :projects, through: :project_groups do def filter(query) @@ -48,7 +48,7 @@ :firstname, :last_login,:login_count, :orcid_id, :password, :shibboleth_id, :user_status_id, :surname, :user_type_id, :organisation_id, :skip_invitation, :other_organisation, :accept_terms, :role_ids, :dmponline3, :api_token, - :language_id + :language_id, :organisation # FIXME: The duplication in the block is to set defaults. It might be better if # they could be set in Settings::PlanList itself, if possible. @@ -79,31 +79,34 @@ # @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 !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 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 +# else # set role to first role - user_org_role = self.user_org_roles.first +# user_org_role = self.user_org_roles.first # change org_id to new org_id - user_org_role.organisation_id = new_organisation_id +# user_org_role.organisation_id = new_organisation_id # save modified role - user_org_role.save +# 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? +# 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.roles.delete_all +# self.save! +# end +# end +# end +# end + + self.organisation = Organisation.find(new_organisation_id) + # rip api_token from user self.remove_token! end @@ -113,24 +116,25 @@ # # @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 +# 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 +# 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 @@ -138,20 +142,21 @@ # # @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 +# 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 = organisation.id +# end ## # checks if the user is a super admin