Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNOE-688] Generate getter and setter for has_one relationship #560

Open
wants to merge 10 commits into
base: 4.0
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ImpersonateController < ApplicationController
# GET /impersonate/user/123
def create
session[:impersonator_redirect_path] = params[:redirect_path].presence
@user = MnoEnterprise::User.find_one(params[:user_id], :deletion_requests, :organizations, :orga_relations, :dashboards, :teams, :user_access_requests, :sub_tenant)
@user = MnoEnterprise::User.find_one(params[:user_id], :user_access_requests)
unless @user.present?
return redirect_with_error('User does not exist')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create

def app_answer_params
# for an admin, the organization does not matter
orga_relation = current_user.orga_relations.first
orga_relation = MnoEnterprise::OrgaRelation.where('user.id': current_user.id).first
params.require(:app_answer).permit(:description)
.merge(reviewer_id: orga_relation.id, reviewer_type: 'OrgaRelation',
parent_id: parent.id, reviewable_id: parent.reviewable_id, reviewable_type: 'App')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create

def app_comment_params
# for an admin, the organization does not matter
orga_relation = current_user.orga_relations.first
orga_relation = MnoEnterprise::OrgaRelation.where('user.id': current_user.id).first
params.require(:app_comment).permit(:description)
.merge(reviewer_id: orga_relation.id, reviewer_type: 'OrgaRelation',
parent_id: parent.id, reviewable_id: parent.reviewable_id, reviewable_type: 'App')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Jpi::V1::Admin::AppInstancesController < Jpi::V1::Admin::BaseResourceContr

# DELETE /mnoe/jpi/v1/app_instances/1
def destroy
app_instance = MnoEnterprise::AppInstance.find_one(params[:id])
app_instance = MnoEnterprise::AppInstance.find_one(params[:id], :owner)

if app_instance
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', app_instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Jpi::V1::Admin::Impac::DashboardTemplatesController < Jpi::V1::Admin::Base
#==================================================================
# GET /mnoe/jpi/v1/admin/impac/dashboard_templates
def index
dashboard_templates = MnoEnterprise::Dashboard.templates.includes(*DASHBOARD_DEPENDENCIES)
if params[:terms]
# For search mode
@dashboard_templates = []
Expand All @@ -19,61 +20,47 @@ def index
@dashboard_templates = query.to_a
response.headers['X-Total-Count'] = query.meta.record_count
end
load_organizations
end

# GET /mnoe/jpi/v1/admin/impac/dashboard_templates/1
def show
render json: { errors: { message: 'Dashboard template not found' } }, status: :not_found unless dashboard_template.present?
@dashboard_template = MnoEnterprise::Dashboard.find_one!(params[:id], *DASHBOARD_DEPENDENCIES)
load_organizations
end

# POST /mnoe/jpi/v1/admin/impac/dashboard_templates
def create
@dashboard_template = MnoEnterprise::Dashboard.new(dashboard_template_params.merge(dashboard_type: 'template'))

# Abort on failure
unless @dashboard_template.save
return render json: { errors: dashboard_template.errors }, status: :bad_request
end

MnoEnterprise::EventLogger.info('dashboard_template_create', current_user.id, 'Dashboard Template Creation', dashboard_template)
@dashboard_template.save!
MnoEnterprise::EventLogger.info('dashboard_template_create', current_user.id, 'Dashboard Template Creation', @dashboard_template)
@dashboard_template = @dashboard_template.load_required(*DASHBOARD_DEPENDENCIES)
load_organizations
render 'show'
end

# PATCH/PUT /mnoe/jpi/v1/admin/impac/dashboard_templates/1
def update
return render json: { errors: { message: 'Dashboard template not found' } }, status: :not_found unless dashboard_template

# Abort on failure
unless dashboard_template.update(dashboard_template_params)
return render json: { errors: dashboard_template.errors }, status: :bad_request
end

MnoEnterprise::EventLogger.info('dashboard_template_update', current_user.id, 'Dashboard Template Update', dashboard_template)
@dashboard_template = MnoEnterprise::Dashboard.find_one!(params[:id])
dashboard_template.update!(dashboard_template_params)
@dashboard_template = @dashboard_template.load_required(*DASHBOARD_DEPENDENCIES)
MnoEnterprise::EventLogger.info('dashboard_template_update', current_user.id, 'Dashboard Template Update', @dashboard_template)
load_organizations
render 'show'
end

# DELETE /mnoe/jpi/v1/admin/impac/dashboard_templates/1
def destroy
return render json: { errors: { message: 'Dashboard template not found' } }, status: :not_found unless dashboard_template

MnoEnterprise::EventLogger.info('dashboard_template_delete', current_user.id, 'Dashboard Template Deletion', dashboard_template)

# Abort on failure
unless dashboard_template.destroy
return render json: { errors: 'Cannot destroy dashboard template' }, status: :bad_request
end

@dashboard_template = MnoEnterprise::Dashboard.find_one!(params[:id])
MnoEnterprise::EventLogger.info('dashboard_template_delete', current_user.id, 'Dashboard Template Deletion', @dashboard_template)
@dashboard_template.destroy!
head status: :ok
end

private

def dashboard_templates
@dashboard_templates ||= MnoEnterprise::Dashboard.templates.includes(*DASHBOARD_DEPENDENCIES)
end

def dashboard_template
@dashboard_template ||= dashboard_templates.find(params[:id].to_i).first
def load_organizations
@organizations = MnoEnterprise::Organization.where('users.id': current_user.id)
end

def whitelisted_params
Expand All @@ -86,7 +73,7 @@ def dashboard_template_params
params.require(:dashboard).permit(*whitelisted_params).tap do |whitelisted|
whitelisted[:settings] = params[:dashboard][:metadata] || {}
end
.except(:metadata)
.except(:metadata)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ def update_app_list
if params[:organization].key?(:app_nids) && (desired_nids = Array(params[:organization][:app_nids]))
existing_apps = @organization.app_instances&.select(&:active?) || []
existing_apps.each { |app_instance| desired_nids.delete(app_instance.app.nid) || app_instance.terminate }
desired_nids.each { |nid| @organization.provision_app_instance!(nid) }
desired_nids.each do |nid|
MnoEnterprise::AppInstance.provision!(nid, @organization.id, 'Organization' )
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module MnoEnterprise
class Jpi::V1::Admin::UsersController < Jpi::V1::Admin::BaseResourceController

INCLUDED_FIELDS = [:id, :uid, :email, :phone, :name, :surname, :admin_role, :created_at, :updated_at, :confirmed_at, :last_sign_in_at, :sign_in_count, :user_access_requests, :organizations, :sub_tenant]

# GET /mnoe/jpi/v1/admin/users
def index
if params[:terms]
Expand All @@ -10,21 +12,24 @@ def index
@users = @users | MnoEnterprise::User
.apply_query_params(params.except(:terms))
.with_params(_metadata: { act_as_manager: current_user.id })
.select(INCLUDED_FIELDS)
.includes(:user_access_requests, :sub_tenant)
.where(Hash[*t])
end

# Ensure that no duplicates are returned as a result of multiple terms being applied to search query
# ex. user.name = "John" and user.email = "[email protected]" would return a duplicate when searching for "john"
@users.uniq!{ |u| u.id }
@users.uniq! { |u| u.id }

response.headers['X-Total-Count'] = @users.count
else
# Index mode
query = MnoEnterprise::User
.apply_query_params(params)
.with_params(_metadata: { act_as_manager: current_user.id })
.includes(:user_access_requests, :sub_tenant)
.apply_query_params(params)
.with_params(_metadata: { act_as_manager: current_user.id })
.select(INCLUDED_FIELDS)
.includes(:user_access_requests, :sub_tenant)

@users = query.to_a
response.headers['X-Total-Count'] = query.meta.record_count
end
Expand All @@ -33,9 +38,10 @@ def index
# GET /mnoe/jpi/v1/admin/users/1
def show
@user = MnoEnterprise::User.with_params(_metadata: { act_as_manager: current_user.id })
.includes(:orga_relations, :organizations, :user_access_requests, :sub_tenant)
.find(params[:id])
.first
.includes(:orga_relations, :organizations, :user_access_requests, :sub_tenant)
.select(INCLUDED_FIELDS)
.find(params[:id])
.first

@user_organizations = @user.organizations
end
Expand Down Expand Up @@ -73,7 +79,7 @@ def update_clients
@user = MnoEnterprise::User.with_params(_metadata: { act_as_manager: current_user.id }).find(params[:id]).first
return render_not_found('User') unless @user
attributes = params.require(:user).permit(add: [], remove: [])
@user.update_clients!({data: {attributes: attributes}})
@user.update_clients!({ data: { attributes: attributes } })
@user = @user.load_required(:sub_tenant)
render :show
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class Jpi::V1::AppInstancesSyncController < Jpi::V1::BaseResourceController

# GET /mnoe/jpi/v1/organization/org-fbba/app_instances_sync
def index
authorize! :check_apps_sync, @parent_organization
authorize! :check_apps_sync, orga_relation
connectors = parent_organization.app_instances_sync!
render json: results(connectors)
end


# POST /mnoe/jpi/v1/organizations/org-fbba/app_instances_sync
def create
authorize! :sync_apps, @parent_organization
authorize! :sync_apps, orga_relation
connectors = parent_organization.trigger_app_instances_sync!
render json: results(connectors)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def current_app
end

def orga_relation
@orga_relation ||= MnoEnterprise::OrgaRelation.where(organization_id: organization_id, user_id: current_user.id).first
@orga_relation ||= MnoEnterprise::OrgaRelation.where('organization.id': organization_id, 'user.id': current_user.id).first
end

def ensure_app_exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@ class Jpi::V1::BaseResourceController < ApplicationController

protected

def timestamp
@timestamp ||= (params[:timestamp] || 0).to_i
# test if the provided argument is a id or an uid
# @param [Object] id or uid
def is_id?(string)
string.to_i.to_s == string
end

def parent_organization_id
id_or_uid = params[:organization_id]
if is_id?(id_or_uid)
id_or_uid
else
parent_organization.id
end
end

def is_integer?(string)
string.to_i.to_s == string
def parent_organization
@parent_organization ||= begin
id_or_uid = params[:organization_id]
query = is_id?(id_or_uid) ? id_or_uid : { uid: id_or_uid }
MnoEnterprise::Organization.find(query).first
end
end

def parent_organization
@parent_organization ||= begin
id_or_uid = params[:organization_id]
query = is_integer?(id_or_uid) ? id_or_uid : {uid: id_or_uid}
o = MnoEnterprise::Organization.includes(:orga_relations, :users).find(query).first
## check that user is in the organization
o if o && o.orga_relation(current_user)
end
def orga_relation
@orga_relation ||= begin
id_or_uid = params[:organization_id]
organization_field = is_id?(id_or_uid) ? 'id' : 'uid'
MnoEnterprise::OrgaRelation.where('user.id' => current_user.id, "organization.#{organization_field}" => id_or_uid).first
end

# Check current user is logged in
# Check organization is valid if specified
def check_authorization
unless current_user
render nothing: true, status: :unauthorized
return false
end
if params[:organization_id] && !parent_organization
render nothing: true, status: :forbidden
return false
end
true
end

def render_not_found(resource, id = params[:id])
render json: { errors: {message: "#{resource.titleize} not found (id=#{id})", code: 404, params: params} }, status: :not_found
end

# Check current user is logged in
# Check organization is valid if specified
def check_authorization
unless current_user
render nothing: true, status: :unauthorized
return false
end

def render_bad_request(attempted_action, issue)
issue = issue.full_messages if issue.respond_to?(:full_messages)
render json: { errors: {message: "Error while trying to #{attempted_action}: #{issue}", code: 400, params: params} }, status: :bad_request
end

def render_forbidden_request(attempted_action)
render json: { errors: {message: "Error while trying to #{attempted_action}: you do not have permission", code: 403} }, status: :forbidden
if params[:organization_id] && !orga_relation
render nothing: true, status: :forbidden
return false
end
true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ json.extract! template, :id, :name, :full_name, :currency

json.metadata template.settings

json.data_sources template.organizations(current_user.organizations).compact.map do |org|
json.data_sources template.organizations(@organizations).compact.map do |org|
json.id org.id
json.uid org.uid
json.label org.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ json.extract! dashboard, :id, :name, :full_name, :currency

json.metadata dashboard.settings

json.data_sources dashboard.organizations(current_user.organizations).compact.map do |org|
json.data_sources dashboard.organizations(@organizations).compact.map do |org|
json.id org.id
json.uid org.uid
json.label org.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Admin::BaseResourceControl
end

protected

def timestamp
@timestamp ||= (params[:timestamp] || 0).to_i
end

# Check current user is logged in
# Check organization is valid if specified
def check_authorization
Expand All @@ -26,16 +21,4 @@ def check_authorization
render nothing: true, status: :unauthorized
false
end

def render_not_found(resource = controller_name.singularize, id = params[:id])
render json: { errors: {message: "#{resource.titleize} not found (id=#{id})", code: 404, params: params} }, status: :not_found
end

def render_bad_request(attempted_action, issue)
render json: { errors: {message: "Error while trying to #{attempted_action}: #{issue}", code: 400, params: params} }, status: :bad_request
end

def render_forbidden_request(attempted_action)
render json: { errors: {message: "Error while trying to #{attempted_action}: you do not have permission", code: 403} }, status: :forbidden
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::AppInstancesController
# GET /mnoe/jpi/v1/organization/1/app_instances
def index
statuses = MnoEnterprise::AppInstance::ACTIVE_STATUSES.join(',')
@app_instances = MnoEnterprise::AppInstance.includes(:app).where(owner_id: parent_organization.id, 'status.in': statuses, 'fulfilled_only': true).to_a.select do |i|
@app_instances = MnoEnterprise::AppInstance.includes(:app).where('owner.id': parent_organization_id, 'status.in': statuses, 'fulfilled_only': true).to_a.select do |i|
can?(:access,i)
end
end

# POST /mnoe/jpi/v1/organization/1/app_instances
def create
authorize! :manage_app_instances, parent_organization
app_instance = parent_organization.provision_app_instance!(params[:nid])
authorize! :manage_app_instances, orga_relation
app_instance = MnoEnterprise::AppInstance.provision!(params[:nid], parent_organization_id, 'Organization' )
app_instance = app_instance.load_required(:owner)
MnoEnterprise::EventLogger.info('app_add', current_user.id, 'App added', app_instance)
head :created
end

# DELETE /mnoe/jpi/v1/app_instances/1
def destroy
@app_instance = MnoEnterprise::AppInstance.find_one(params[:id])
@app_instance = MnoEnterprise::AppInstance.find_one(params[:id], :owner)
if @app_instance
organization = MnoEnterprise::Organization.find_one(@app_instance.owner_id)
authorize! :manage_app_instances, organization
authorize! :manage_app_instances, current_user.orga_relation(@app_instance.owner)
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', @app_instance)
@app_instance = @app_instance.terminate!
end
Expand Down
Loading