diff --git a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/current_users_controller.rb b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/current_users_controller.rb index d235a59b6..7b6385f59 100644 --- a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/current_users_controller.rb +++ b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/current_users_controller.rb @@ -1,6 +1,8 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController extend ActiveSupport::Concern + INCLUDED_DEPENDENCIES = %i(deletion_requests organizations orga_relations dashboards teams orga_relations.user orga_relations.organization sub_tenant) + #================================================================== # Included methods #================================================================== @@ -17,55 +19,52 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController #================================================================== # GET /mnoe/jpi/v1/current_user def show - @user = current_user || MnoEnterprise::User.new(id: nil) + @user = current_user&.load_required(*INCLUDED_DEPENDENCIES) || MnoEnterprise::User.new(id: nil) end # PUT /mnoe/jpi/v1/current_user def update - @user = current_user - @user.attributes = user_params + current_user.attributes = user_params changed_attributes = @user.changed_attributes - @user.save! + current_user.save! + current_user.refresh_user_cache MnoEnterprise::EventLogger.info('user_update', current_user.id, 'User update', @user, changed_attributes) - @user = @user.load_required_dependencies + @user = current_user.load_required(*INCLUDED_DEPENDENCIES) render :show - current_user.refresh_user_cache end # PUT /mnoe/jpi/v1/current_user/register_developer def register_developer - @user = current_user - @user = @user.create_api_credentials! + current_user.create_api_credentials! MnoEnterprise::EventLogger.info('register_developer', current_user.id, 'Developer registration', @user) - @user = @user.load_required_dependencies + @user = current_user.load_required(*INCLUDED_DEPENDENCIES) render :show end # PUT /mnoe/jpi/v1/current_user/update_password def update_password - @user = current_user - @user = @user.update_password!(data: {attributes: password_params}) + current_user.update_password!(data: { attributes: password_params }) MnoEnterprise::EventLogger.info('user_update_password', current_user.id, 'User password change', @user) - @user = @user.load_required_dependencies + @user = current_user.load_required(*INCLUDED_DEPENDENCIES) sign_in @user, bypass: true render :show end private - def user_params - params.require(:user).permit( - :name, :surname, :email, :company, :phone, :website, :phone_country_code, :current_password, :password, :password_confirmation, - settings: [:locale] - ) - end + def user_params + params.require(:user).permit( + :name, :surname, :email, :company, :phone, :website, :phone_country_code, :current_password, :password, :password_confirmation, + settings: [:locale] + ) + end - def password_params - params.require(:user).permit(:current_password, :password, :password_confirmation) - end + def password_params + params.require(:user).permit(:current_password, :password, :password_confirmation) + end - def user_management_enabled? - return head :forbidden unless Settings.dashboard.user_management.enabled - end + def user_management_enabled? + return head :forbidden unless Settings.dashboard.user_management.enabled + end end diff --git a/core/app/models/mno_enterprise/base_resource.rb b/core/app/models/mno_enterprise/base_resource.rb index fa51b6f70..f974e861c 100644 --- a/core/app/models/mno_enterprise/base_resource.rb +++ b/core/app/models/mno_enterprise/base_resource.rb @@ -81,6 +81,11 @@ def relations @relations ||= ActiveSupport::HashWithIndifferentAccess.new end + # define if the relationship has been loaded (for example by calling include) + def relation_loaded?(relation) + relationships && relationships.has_attribute?(relation) && relationships[relation].key?('data') + end + def process_custom_result(result) collect_errors(result.errors) raise_if_errors diff --git a/core/app/models/mno_enterprise/user.rb b/core/app/models/mno_enterprise/user.rb index 3a1f72854..8dbf72bcb 100644 --- a/core/app/models/mno_enterprise/user.rb +++ b/core/app/models/mno_enterprise/user.rb @@ -7,8 +7,6 @@ class User < BaseResource include ActiveModel::AttributeMethods include ActiveModel::Validations - INCLUDED_DEPENDENCIES = %i(deletion_requests organizations orga_relations dashboards teams sub_tenant) - # ids property :id property :uid, type: :string @@ -125,7 +123,11 @@ def role_from_id(organization_id) end def orga_relation_from_id(organization_id) - MnoEnterprise::OrgaRelation.where('user.id': id, 'organization.id': organization_id).first + if relation_loaded?(:orga_relations) + self.orga_relations.find { |r| r.organization.id == organization_id } + else + MnoEnterprise::OrgaRelation.where('user.id': id, 'organization.id': organization_id).first + end end def create_deletion_request! diff --git a/core/lib/json_api_client_extension/json_api_client_orm_adapter.rb b/core/lib/json_api_client_extension/json_api_client_orm_adapter.rb index 8e08abc5f..3212d558d 100644 --- a/core/lib/json_api_client_extension/json_api_client_orm_adapter.rb +++ b/core/lib/json_api_client_extension/json_api_client_orm_adapter.rb @@ -15,14 +15,14 @@ def column_names # @see OrmAdapter::Base#get! def get!(id) - res = klass.includes(*klass::INCLUDED_DEPENDENCIES).find(wrap_key(id)).first + res = klass.find(wrap_key(id)).first raise JsonApiClient::Errors::ResourceNotFound, "resource not found" unless res res end # @see OrmAdapter::Base#get def get(id) - res = klass.includes(*klass::INCLUDED_DEPENDENCIES).find(wrap_key(id)) + res = klass.find(wrap_key(id)) error = res&.errors&.first if (error && error.code != '404') raise error.detail