Skip to content

Commit

Permalink
Merge pull request #532 from x4d3/feature/subtenant-v2
Browse files Browse the repository at this point in the history
[MNOE-689] API V2 - SubTenant replace mass assignment of relation ship by diff
  • Loading branch information
ouranos authored Nov 9, 2017
2 parents eb619b4 + 01c4352 commit 60239f0
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 120 deletions.
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)
@user = MnoEnterprise::User.find_one(params[:user_id], :deletion_requests, :organizations, :orga_relations, :dashboards, :teams, :user_access_requests, :sub_tenant)
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 @@ -7,16 +7,23 @@ class Jpi::V1::Admin::OrganizationsController < Jpi::V1::Admin::BaseResourceCont
:geo_country_code, :geo_state_code, :geo_city,
:geo_tz, :geo_currency, :metadata, :industry, :size,
:financial_year_end_month, :credit_card,
:financial_metrics, :created_at, :external_id]
:financial_metrics, :created_at, :external_id, :belong_to_sub_tenant, :belong_to_account_manager]

# GET /mnoe/jpi/v1/admin/organizations
def index
if params[:terms]
# Search mode
@organizations = []
JSON.parse(params[:terms]).map do |t|
@organizations = @organizations | MnoEnterprise::Organization.with_params(_metadata: { act_as_manager: current_user.id })
.select(INCLUDED_FIELDS)
.where(Hash[*t])

query = MnoEnterprise::Organization
.apply_query_params(params.except(:terms))
.select(INCLUDED_FIELDS)
.with_params(_metadata: { act_as_manager: current_user.id })
.where(Hash[*t])
query = query.with_params(sub_tenant_id: params[:sub_tenant_id]) if params[:sub_tenant_id]
query = query.with_params(account_manager_id: params[:account_manager_id]) if params[:account_manager_id]
@organizations = @organizations | query
end
response.headers['X-Total-Count'] = @organizations.count
else
Expand All @@ -26,7 +33,8 @@ def index
.apply_query_params(params)
.with_params(_metadata: { act_as_manager: current_user.id })
.select(INCLUDED_FIELDS)

query = query.with_params(sub_tenant_id: params[:sub_tenant_id]) if params[:sub_tenant_id]
query = query.with_params(account_manager_id: params[:account_manager_id]) if params[:account_manager_id]
@organizations = query.to_a
response.headers['X-Total-Count'] = query.meta.record_count
end
Expand Down Expand Up @@ -82,9 +90,9 @@ def update
return render_not_found('Organization') unless @organization

# Update organization
update_app_list
@organization.update!(organization_update_params)

update_app_list
@organization = @organization.load_required(*DEPENDENCIES)
@organization_active_apps = @organization.app_instances.select(&:active?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def index

# GET /mnoe/jpi/v1/admin/sub_tenants/1
def show
@sub_tenant = MnoEnterprise::SubTenant.find_one(params[:id], :clients, :account_managers)
@sub_tenant_clients = @sub_tenant.clients
@sub_tenant_account_managers = @sub_tenant.account_managers
@sub_tenant = MnoEnterprise::SubTenant.find_one(params[:id])
end

# POST /mnoe/jpi/v1/admin/sub_tenants
Expand All @@ -28,9 +26,22 @@ def create
def update
@sub_tenant = MnoEnterprise::SubTenant.find_one(params[:id])
@sub_tenant.update!(sub_tenant_params)
@sub_tenant = @sub_tenant.load_required(:clients, :account_managers)
@sub_tenant_clients = @sub_tenant.clients
@sub_tenant_account_managers = @sub_tenant.account_managers
render :show
end

# PATCH /mnoe/jpi/v1/admin/organizations/1/update_clients
def update_clients
@sub_tenant = MnoEnterprise::SubTenant.find_one(params[:id])
attributes = params.require(:sub_tenant).permit(add: [], remove: [])
@sub_tenant.update_clients!({data: {attributes: attributes}})
render :show
end

# PATCH /mnoe/jpi/v1/admin/organizations/1/update_account_managers
def update_account_managers
@sub_tenant = MnoEnterprise::SubTenant.find_one(params[:id])
attributes = params.require(:sub_tenant).permit(add: [], remove: [])
@sub_tenant.update_account_managers!({data: {attributes: attributes}})
render :show
end

Expand All @@ -47,11 +58,7 @@ def check_sub_tenant_authorization

private
def sub_tenant_params
sub_tenant_params = params.require(:sub_tenant)
allowed_params = sub_tenant_params.permit(:name, client_ids: [], account_manager_ids: [])
allowed_params[:client_ids] ||= [] if sub_tenant_params.has_key?(:client_ids)
allowed_params[:account_manager_ids] ||= [] if sub_tenant_params.has_key?(:account_manager_ids)
allowed_params
params.require(:sub_tenant).permit(:name)
end
end
end
60 changes: 36 additions & 24 deletions api/app/controllers/mno_enterprise/jpi/v1/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ def index
# Search mode
@users = []
JSON.parse(params[:terms]).map do |t|
@users = @users | MnoEnterprise::User.with_params(_metadata: { act_as_manager: current_user.id })
.includes(:user_access_requests)
.where(Hash[*t])
@users = @users | MnoEnterprise::User
.apply_query_params(params.except(:terms))
.with_params(_metadata: { act_as_manager: current_user.id })
.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
Expand All @@ -22,7 +24,7 @@ def index
query = MnoEnterprise::User
.apply_query_params(params)
.with_params(_metadata: { act_as_manager: current_user.id })
.includes(:user_access_requests)
.includes(:user_access_requests, :sub_tenant)
@users = query.to_a
response.headers['X-Total-Count'] = query.meta.record_count
end
Expand All @@ -31,18 +33,19 @@ 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, :clients)
.includes(:orga_relations, :organizations, :user_access_requests, :sub_tenant)
.find(params[:id])
.first

@user_organizations = @user.organizations
@user_clients = @user.clients
end

# POST /mnoe/jpi/v1/admin/users
def create
@user = MnoEnterprise::User.create!(user_create_params)
@user = @user.load_required(:clients)
@user = MnoEnterprise::User.new(user_create_params)
update_sub_tenant(@user)
@user.save!
@user = @user.load_required(:sub_tenant)
render :show
end

Expand All @@ -58,12 +61,20 @@ def update
# (the current_user may not have access to this record)
@user = MnoEnterprise::User.with_params(_metadata: { act_as_manager: current_user.id }).find(params[:id]).first
return render_not_found('User') unless @user
@user.attributes = user_update_params
update_sub_tenant(@user)
@user.save!
@user = @user.load_required(:sub_tenant)
render :show
end

# Update user
@user.update!(user_update_params)

@user = @user.load_required(:clients)
@user_clients = @user.clients
# PATCH /mnoe/jpi/v1/admin/organizations/1/update_clients
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 = @user.load_required(:sub_tenant)
render :show
end

Expand Down Expand Up @@ -107,25 +118,26 @@ def tenant_reporting
end

def user_update_params
attrs = [:name, :surname, :email, :phone, client_ids: []]
attrs = [:name, :surname, :email, :phone]
# TODO: replace with authorize/ability
if current_user.admin_role == 'admin'
attrs << :admin_role
attrs << :mnoe_sub_tenant_id
end
user_param = params.require(:user)
updated_params = user_param.permit(attrs)
updated_params[:sub_tenant_id] = updated_params.delete(:mnoe_sub_tenant_id)
updated_params[:client_ids] ||= [] if user_param.has_key?(:client_ids)
# if the user is updated to admin or division admin, his clients are cleared
if updated_params[:admin_role] && updated_params[:admin_role] != 'staff'
updated_params[:client_ids] = []
end
updated_params
params.require(:user).permit(attrs)
end

def user_create_params
user_update_params.merge(password: Devise.friendly_token.first(12))
end

def update_sub_tenant(user)
if current_user.admin_role == 'admin' && params.require(:user).has_key?(:sub_tenant_id)
if params.require(:user)[:sub_tenant_id]
user.relationships.sub_tenant = MnoEnterprise::SubTenant.new(id: params.require(:user)[:sub_tenant_id])
else
user.relationships.sub_tenant = nil
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
json.extract! organization, :id, :name, :uid, :soa_enabled, :created_at, :account_frozen, :financial_metrics, :billing_currency, :external_id
json.extract! organization, :id, :name, :uid, :soa_enabled, :created_at, :account_frozen, :financial_metrics, :billing_currency, :external_id, :belong_to_sub_tenant, :belong_to_account_manager

Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.extract! sub_tenant, :id, :name, :created_at, :updated_at, :client_ids, :account_manager_ids
json.extract! sub_tenant, :id, :name, :created_at, :updated_at
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
json.sub_tenants @sub_tenants, partial: 'sub_tenant', as: :sub_tenant
json.metadata @sub_tenants.metadata if @sub_tenants.respond_to?(:metadata)
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
json.sub_tenant do
json.partial! 'sub_tenant', sub_tenant: @sub_tenant

json.clients @sub_tenant_clients do |org|
json.extract! org, :id, :uid, :name, :created_at
end

json.account_managers @sub_tenant_account_managers do |user|
json.extract! user, :id, :uid, :name, :surname, :email, :created_at, :admin_role
end

end
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
json.extract! user, :id, :uid, :email, :phone, :name, :surname, :admin_role, :created_at, :updated_at, :confirmed_at, :last_sign_in_at, :sign_in_count, :client_ids
json.mnoe_sub_tenant_id user.sub_tenant_id
json.extract! user, :id, :uid, :email, :phone, :name, :surname, :admin_role, :created_at, :updated_at, :confirmed_at, :last_sign_in_at, :sign_in_count
json.sub_tenant_id user.sub_tenant&.id
json.access_request_status user.access_request_status(current_user)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ json.user do
json.organizations @user_organizations do |org|
json.extract! org, :id, :uid, :name, :account_frozen, :created_at
end

json.clients @user_clients do |org|
json.extract! org, :id, :uid, :name, :account_frozen, :created_at
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ json.cache! ['v2', @user.cache_key] do
json.admin_role @user.admin_role
json.avatar_url avatar_url(@user)
json.settings @user.settings
json.mnoe_sub_tenant_id @user.sub_tenant_id
json.sub_tenant_id @user.sub_tenant&.id

if current_impersonator
json.current_impersonator true
Expand Down
10 changes: 9 additions & 1 deletion api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@
post :signup_email
end
resource :user_access_requests, only: [:create]
member do
patch :update_clients
end
end

resources :products, only: [:index, :show]
Expand Down Expand Up @@ -266,7 +269,12 @@
end
end

resources :sub_tenants, only: [:index, :show, :destroy, :update, :create]
resources :sub_tenants, only: [:index, :show, :destroy, :update, :create] do
member do
patch :update_clients
patch :update_account_managers
end
end

resources :tenant_invoices, only: [:index, :show]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module MnoEnterprise
let(:user2) { build(:user) }
before do
stub_user(user)
stub_api_v2(:get, "/users/#{user2.id}", user2, %i(deletion_requests organizations orga_relations dashboards teams user_access_requests))
stub_api_v2(:get, "/users/#{user2.id}", user2, %i(deletion_requests organizations orga_relations dashboards teams user_access_requests sub_tenant))

stub_api_v2(:patch, "/users/#{user.id}")
stub_api_v2(:patch, "/users/#{user2.id}")
Expand Down Expand Up @@ -37,7 +37,7 @@ module MnoEnterprise
end

context 'when the user does not exist' do
before { stub_api_v2(:get, '/users/crappyId', [], %i(deletion_requests organizations orga_relations dashboards teams user_access_requests)) }
before { stub_api_v2(:get, '/users/crappyId', [], %i(deletion_requests organizations orga_relations dashboards teams user_access_requests sub_tenant)) }
subject { get :create, user_id: 'crappyId', dhbRefId: 10 }
it do
is_expected.to redirect_to('/admin/#!?flash=%7B%22msg%22%3A%22User+does+not+exist%22%2C%22type%22%3A%22error%22%7D')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ module MnoEnterprise
{
organizations: [
:uid, :name, :account_frozen, :soa_enabled, :mails, :logo, :latitude, :longitude, :geo_country_code, :geo_state_code,
:geo_city, :geo_tz, :geo_currency, :metadata, :industry, :size, :financial_year_end_month, :credit_card, :financial_metrics, :created_at, :external_id
:geo_city, :geo_tz, :geo_currency, :metadata, :industry, :size, :financial_year_end_month, :credit_card, :financial_metrics, :created_at, :external_id,
:belong_to_sub_tenant, :belong_to_account_manager
].join(',')
}
end
Expand Down
Loading

0 comments on commit 60239f0

Please sign in to comment.