Skip to content

Commit

Permalink
[MNOE-428] Migrate mno-enterprise to MnoHub API V2
Browse files Browse the repository at this point in the history
- Add missing methods on user and organization

User
- Add create_api_credentials
Organization
- add role
- add payment_restriction
- add orga_relation
- Add KPI and Alert
- Implement Invite and Delete Organization Members
- Implement Create Organization
- Implement Update member
- Implement Team management
- Add star_ready? connec_ready? and responsive
- Implement app instance provisioning
- Implement Team Update
- Implement Dashboard management
- Implement widget controller
- Update Dashboard
- Correct issues with Dashboard's widgets not being sorted
- Implement App Synchronisation
- Apply Code Review Remark
- Implement Audit Logging
- Start Spec Writing
- Adapt organization_spec.rb
- Adapt Team Controller
- Adapt Specs
  • Loading branch information
x4d3 committed Jun 7, 2017
1 parent 3efdd04 commit 094bbb3
Show file tree
Hide file tree
Showing 137 changed files with 1,997 additions and 1,264 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(params[:user_id])
@user = Mno::User.find_one(params[:user_id], :deletion_requests, :organizations, :orga_relations, :dashboards)
if @user.present?
impersonate(@user)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def create
# Invite for unconfirmed users are automatically accepted
def find_org_invite(organization, user)
if user.confirmed?
status_scope = { 'status.in' => %w(staged pending) }
status_scope = { status: %w(staged pending) }
else
status_scope = { status: 'accepted' }
end
organization.org_invites.where(status_scope.merge(user_id: user.id)).first
Mno::OrgInvite.includes(:user).where(status_scope.merge(user_id: user.id, organization_id: organization.id)).first
end

# Send the org invite and update the status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,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
# find method is overriden in the mnoe interface to call organization.check_sync_apps_progress
connectors = @parent_organization.app_instances_sync.find('anything').connectors
connectors = parent_organization.app_instances_sync.first.connectors
render json: results(connectors)
end


# POST /mnoe/jpi/v1/organizations/org-fbba/app_instances_sync
def create
authorize! :sync_apps, @parent_organization

# Some weird behaviour with Her and has_one. If app_instances_sync.find is called somewhere before the create,
# Her won't detect the organization_id as dirty and won't submit it.
sync = @parent_organization.app_instances_sync.build(mode: params[:mode])
sync.organization_id_will_change!
sync.save

connectors = sync.connectors

connectors = parent_organization.trigger_app_instances_sync.first.connectors
render json: results(connectors)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ class Jpi::V1::AuditEventsController < Jpi::V1::BaseResourceController

# GET /mnoe/jpi/v1/admin/audit_events
def index
@organization = MnoEnterprise::Organization.find(params.require(:organization_id))
@organization = Mno::Organization.find_one(params.require(:organization_id))

authorize! :administrate, @organization

@audit_events = MnoEnterprise::AuditEvent.where(organization_id: @organization.id)
@audit_events = @audit_events.limit(params[:limit]) if params[:limit]
@audit_events = @audit_events.skip(params[:offset]) if params[:offset]
@audit_events = @audit_events.order_by(params[:order_by]) if params[:order_by]
@audit_events = @audit_events.where(params[:where]) if params[:where]
@audit_events = @audit_events.all.fetch

response.headers['X-Total-Count'] = @audit_events.metadata[:pagination][:count]
query = Mno::AuditEvent.where(organization_id: @organization.id)
query = Mno::AuditEvent.apply_query_params(query, params)

response.headers['X-Total-Count'] = query.meta.record_count
@audit_events = query.to_a
respond_to do |format|
format.json
format.csv do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ def timestamp
@timestamp ||= (params[:timestamp] || 0).to_i
end

def is_integer?(string)
string.to_i.to_s == string
end

def parent_organization
@parent_organization ||= current_user.organizations.to_a.find do |o|
key = (params[:organization_id].to_i == 0) ? o.uid : o.id.to_s
key == params[:organization_id].to_s
@parent_organization ||= begin
id_or_uid = params[:organization_id]
query = is_integer?(id_or_uid) ? id_or_uid : {uid: id_or_uid}
o = Mno::Organization.includes(:orga_relations, :users).find(query).first
## check that user is in the organization
o if o && o.orga_relation(current_user)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@ json.stack app_instance.stack
json.name app_instance.name
json.status app_instance.status
json.oauth_keys_valid app_instance.oauth_keys_valid
#json.http_url app_instance.http_url
#json.microsoft_trial_url app_instance.microsoft_trial_url
json.created_at app_instance.created_at

json.per_user_licence app_instance.per_user_licence
json.licences_count app_instance.active_licences_count if app_instance.per_user_licence?

if app_instance.under_free_trial?
json.free_trial_end_at app_instance.free_trial_end_at
end

if app_instance.oauth_company
json.oauth_company_name app_instance.oauth_company
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
json.audit_events do
json.array! @audit_events, partial: 'audit_event', as: :audit_event
end
json.metadata @audit_events.metadata
# TODO: Port previous pagination metadata information ?
# json.metadata @audit_events.metadata

Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ json.cache! ['v1', @user.cache_key] do
# Embed association if user is persisted
if @user.id
json.organizations do
json.array! (@user.organizations.active || []) do |o|
json.array! (@user.organizations.select(&:active?) || []) do |o|
json.id o.id
json.uid o.uid
json.name o.name
json.currency o.billing_currency
json.current_user_role o.role
json.has_myob_essentials_only o.has_myob_essentials_only?
json.current_user_role @user.role(o)
json.has_myob_essentials_only o.has_myob_essentials_only
json.financial_year_end_month o.financial_year_end_month
end
end

if @user.deletion_request.present?
if @user.current_deletion_request.present?
json.deletion_request do
json.extract! @user.deletion_request, :id, :token
json.extract! @user.current_deletion_request, :id, :token
end
end

json.kpi_enabled !!@user.kpi_enabled
end
end
end


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
json.ignore_nil!
json.extract! alert, :id, :title, :webhook, :service, :sent
json.metadata alert.settings
json.kpi_id alert.impac_kpi_id
json.kpi_id alert.kpi_id
json.recipients alert.recipients.map do |recipient|
json.extract! recipient, :id, :email
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ end

json.kpis dashboard.kpis, partial: 'mno_enterprise/jpi/v1/impac/kpis/kpi', as: :kpi

json.widgets dashboard.widgets, partial: 'mno_enterprise/jpi/v1/impac/widgets/widget', as: :widget
json.widgets dashboard.sorted_widgets, partial: 'mno_enterprise/jpi/v1/impac/widgets/widget', as: :widget

json.widgets_templates dashboard.filtered_widgets_templates
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ json.name widget.name
json.endpoint (widget.endpoint || widget.widget_category)
json.width widget.width
json.metadata widget.settings
json.owner widget.owner
json.owner widget.dashboard_owner_uid

json.kpis widget.kpis, partial: 'mno_enterprise/jpi/v1/impac/kpis/kpi', as: :kpi
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ json.id user.id
json.name user.name
json.surname user.surname
json.email user.email
json.role organization.members.to_a.find { |e| e.id == user.id }.role
json.role user.role(organization)
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
json.invoices do
# TODO: Introduce pagination
json.array! organization.invoices.order_by('ended_at.desc').limit(12) do |invoice|
json.period invoice.period_label
json.amount AccountingjsSerializer.serialize(invoice.total_due)
json.paid invoice.paid?
json.link mno_enterprise.invoice_path(invoice.slug)
end
json.array! []
# TODO [MIGRATION_V2] remove and replace by a call to /organizations/id/invoices

end
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
if member.is_a?(MnoEnterprise::User)
if member.is_a?(Mno::User)
json.id member.id
json.entity 'User'
json.name member.name
json.surname member.surname
json.email member.email
json.role member.role
elsif member.is_a?(MnoEnterprise::OrgInvite)
json.role organization.role(member)
elsif member.is_a?(Mno::OrgaInvite)
json.id member.id
json.entity 'OrgInvite'
json.email member.user_email
Expand Down
8 changes: 2 additions & 6 deletions api/app/views/mno_enterprise/jpi/v1/teams/_team.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
org = @parent_organization || team.organization
@all_apps ||= MnoEnterprise::App.all.to_a
@all_apps ||= Mno::App.all.to_a

json.id team.id
json.name team.name

json.users do
json.array! team.users do |user|
json.extract! user, :id, :name, :surname, :email

# Retrieve role from cached version (org user list)
org_user = org.users.to_a.find { |e| e.id == user.id }
json.role org_user ? org_user.role : nil
json.role @parent_organization.role(user)
end
end

Expand Down
2 changes: 1 addition & 1 deletion api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
skipped_devise_modules = [:omniauth_callbacks]
skipped_devise_modules << :registrations if Settings.try(:devise).try(:registration).try(:disabled)
devise_for :users, {
class_name: "MnoEnterprise::User",
class_name: "Mno::User",
module: :devise,
path_prefix: 'auth',
skip: skipped_devise_modules,
Expand Down
30 changes: 11 additions & 19 deletions api/lib/mno_enterprise/audit_events_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,23 @@

module MnoEnterprise
class AuditEventsListener
include HTTParty
base_uri "#{MnoEnterprise.mno_api_private_host || MnoEnterprise.mno_api_host}/api/mnoe/v1/audit_events"
read_timeout 0.1
basic_auth MnoEnterprise.tenant_id, MnoEnterprise.tenant_key

def info(key, current_user_id, description, subject_type, subject_id, metadata)
organization_id = if (subject_type == 'MnoEnterprise::Organization') then
data = {
key: key,
user_id: current_user_id,
description: description,
metadata: metadata,
subject_type: subject_type,
subject_id: subject_id,
}
organization_id = if (subject_type == 'Mno::Organization') then
subject_id
elsif metadata.is_a?(Hash)
metadata[:organization_id].presence
end
body = {
data: {
key: key,
user_id: current_user_id,
description: description,
metadata: metadata,
subject_type: subject_type,
subject_id: subject_id,
}
}
body[:data][:organization_id] = organization_id if organization_id
self.class.post('', body: body)
rescue Net::ReadTimeout
# Meant to fail
data[:organization_id] = organization_id if organization_id
Mno::AuditEvent.create(data)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module MnoEnterprise::Concerns::Controllers::DeletionRequestsController
before_filter :set_meta

def set_meta
@meta[:title] = "Account Termination"
@meta[:description] = "Account Termination"
@meta[:title] = 'Account Termination'
@meta[:description] = 'Account Termination'
end
end

Expand All @@ -33,7 +33,7 @@ module ClassMethods
# GET /deletion_requests/1
def show
# authorize! :manage_billing, current_user.organizations.find(@invoice.organization_id)
@deletion_request = current_user.deletion_request
@deletion_request = current_user.current_deletion_request

respond_to do |format|
# Check that the user has a deletion_request in progress
Expand All @@ -58,7 +58,7 @@ def show

# PATCH /deletion_requests/1/freeze_account
def freeze_account
@deletion_request = current_user.deletion_request
@deletion_request = current_user.current_deletion_request

respond_to do |format|
# Check that the user has a deletion_request in progress
Expand All @@ -68,9 +68,9 @@ def freeze_account
# Check that the deletion_request has the right status
if @deletion_request.status == 'pending'
@deletion_request.freeze_account!
format.html { redirect_to @deletion_request, notice: 'Your account has been frozen' }
format.html { redirect_to({action: :show, id: @deletion_request.id}, notice: 'Your account has been frozen') }
else
format.html { redirect_to @deletion_request, alert: 'Invalid action' }
format.html { redirect_to({action: :show, id: @deletion_request.id}, alert: 'Invalid action')}
end
else
format.html { redirect_to main_app.root_path, alert: 'This deletion request is invalid or expired' }
Expand All @@ -81,7 +81,7 @@ def freeze_account

# PATCH /deletion_requests/1/checkout
def checkout
@deletion_request = current_user.deletion_request
@deletion_request = current_user.current_deletion_request

respond_to do |format|
# Check that the user has a deletion_request in progress
Expand All @@ -95,9 +95,9 @@ def checkout
# Finally Perform the checkout
@deletion_request.status = 'account_checked_out'
@deletion_request.save
format.html { redirect_to @deletion_request, notice: 'Checkout has been performed successfully' }
format.html { redirect_to({action: :show, id: @deletion_request.id}, notice: 'Checkout has been performed successfully')}
else
format.html { redirect_to @deletion_request, alert: 'Invalid action' }
format.html { redirect_to({action: :show, id: @deletion_request.id}, alert: 'Invalid action') }
end
else
format.html { redirect_to main_app.root_path, alert: 'This deletion request is invalid or expired' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,33 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::AppInstancesController
#==================================================================
# Instance methods
#==================================================================
# GET /mnoe/jpi/v1/organization/1/apps.json?timestamp=151452452345
# GET /mnoe/jpi/v1/organization/1/apps.json?tg=151452452345
def index
@app_instances = parent_organization.app_instances.active.where("updated_at.gt" => Time.at(timestamp)).select do |i|
# force owner assignment to avoid a refetch in ability can?(:access,i)
i.owner = parent_organization
@app_instances = Mno::AppInstance.includes(:app, :owner).where(owner_id: parent_organization.id, status: Mno::AppInstance::ACTIVE_STATUSES).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.app_instances.create(product: params[:nid])
MnoEnterprise::EventLogger.info('app_add', current_user.id, 'App added', app_instance)
head :created
app_instance = parent_organization.provision_app_instance(params[:nid])
if app_instance.errors.any?
render json: app_instance.errors, status: :bad_request
else
MnoEnterprise::EventLogger.info('app_add', current_user.id, 'App added', app_instance.first)
head :created
end
end

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

if app_instance
authorize! :manage_app_instances, app_instance.owner
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', app_instance)
app_instance.terminate
if @app_instance
authorize! :manage_app_instances, @app_instance.owner
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', @app_instance)
@app_instance = @app_instance.terminate.first
end

head :accepted
Expand Down
Loading

0 comments on commit 094bbb3

Please sign in to comment.