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-106] Admin Panel Improvments #50

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ def create

# PATCH /mnoe/jpi/v1/admin/users/:id
def update
@user = MnoEnterprise::User.find(params[:id])
@user.update(user_params)
if current_user.admin_role == "admin"
@user = MnoEnterprise::User.find(params[:id])
@user.update(user_params)

render :show
render :show
else
render :index, status: :unauthorized
end
end

# DELETE /mnoe/jpi/v1/admin/users/1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,31 @@ def hash_for_user(user)

describe 'PUT #update' do
subject { put :update, id: user.id, user: {admin_role: 'staff'} }
let(:current_user) { build(:user, :admin) }

before do
api_stub_for(put: "/users/#{user.id}", response: ->{ user.admin_role = 'staff'; from_api(user) })
api_stub_for(get: "/users/#{current_user.id}", response: from_api(current_user))
sign_in current_user

user.admin_role = nil
api_stub_for(put: "/users/#{user.id}", response: -> { user.admin_role = 'staff'; from_api(user) })
subject
end

it { expect(response).to be_success }
context 'when admin' do
it { expect(response).to be_success }

# Test that the user is updated by testing the api endpoint was called
it { expect(user.admin_role).to eq('staff') }
end

# Test that the user is updated by testing the api endpoint was called
it { expect(user.admin_role).to eq('staff') }
context 'when staff' do
let(:current_user) { build(:user, :staff) }

it { expect(response).to have_http_status(:unauthorized) }

it { expect(user.admin_role).to be_nil }
end
end

describe 'DELETE #destroy' do
Expand Down
8 changes: 6 additions & 2 deletions core/app/controllers/mno_enterprise/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ def return_to_url(resource)
# Redirect to previous url and reset it
def after_sign_in_path_for(resource)
previous_url = session.delete(:previous_url)
url = MnoEnterprise.router.dashboard_path || main_app.root_url
return (return_to_url(resource) || previous_url || url)
default_url = if resource.respond_to?(:admin_role) && resource.admin_role.present?
MnoEnterprise.router.admin_path
else
MnoEnterprise.router.dashboard_path || main_app.root_url
end
return (return_to_url(resource) || previous_url || default_url)
end

# Some controllers needs to redirect to 'MySpace' which breaks if you dont use mnoe-frontend
Expand Down
4 changes: 4 additions & 0 deletions core/lib/mno_enterprise/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def terms_url
@terms_url || '#'
end

def admin_path
@admin_path || '/admin/'
end

def launch_url(id,opts = {})
host_url("/launch/#{id}",opts)
end
Expand Down
16 changes: 10 additions & 6 deletions core/lib/mno_enterprise/testing_support/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Use as such: build(:api_user)
# See http://stackoverflow.com/questions/10032760/how-to-define-an-array-hash-in-factory-girl
FactoryGirl.define do

factory :user, class: MnoEnterprise::User do
sequence(:id)
sequence(:uid) { |n| "usr-fda9#{n}" }
Expand All @@ -20,28 +20,32 @@
created_at 2.days.ago
updated_at 2.days.ago
sso_session "1fdd5sf5a73D7sd1as2a4sd541"
admin_role false
admin_role nil

confirmation_sent_at 2.days.ago
confirmation_token "wky763pGjtzWR7dP44PD"
confirmed_at 1.days.ago

trait :unconfirmed do
confirmed_at nil
end

trait :admin do
admin_role true
admin_role 'admin'
end

trait :staff do
admin_role 'staff'
end

trait :with_deletion_request do
deletion_request { build(:deletion_request).attributes }
end

trait :with_organizations do
organizations { [build(:organization).attributes] }
end

# Properly build the resource with Her
initialize_with { new(attributes).tap { |e| e.clear_attribute_changes! } }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@

module MnoEnterprise
describe ApplicationController, type: :controller do
# create an anonymous subclass of ApplicationController to expose protected methods
controller(MnoEnterprise::ApplicationController) do
def after_sign_in_path_for(resource)
super
end
def add_param_to_fragment(url, param_name, param_value)
super
end
end

describe '#add_param_to_fragment' do
let(:controller) { MnoEnterprise::ApplicationController.new }
it { expect(controller.add_param_to_fragment('/#/platform/accounts', 'foo', 'bar')).to eq('/#/platform/accounts?foo=bar') }
it { expect(controller.add_param_to_fragment('/', 'foo', 'bar')).to eq('/#?foo=bar') }
it { expect(controller.add_param_to_fragment('/#/platform/dashboard/he/43?en=690', 'foo', 'bar')).to eq('/#/platform/dashboard/he/43?en=690&foo=bar') }
it { expect(controller.add_param_to_fragment('/#/platform/dashboard/he/43?en=690', 'foo', [{msg: 'yolo'}])).to eq('/#/platform/dashboard/he/43?en=690&foo=%7B%3Amsg%3D%3E%22yolo%22%7D') }
end

it { expect(subject.send(:add_param_to_fragment, '/#/platform/accounts', 'foo', 'bar')).to eq('/#/platform/accounts?foo=bar') }
it { expect(subject.send(:add_param_to_fragment, '/', 'foo', 'bar')).to eq('/#?foo=bar') }
it { expect(subject.send(:add_param_to_fragment, '/#/platform/dashboard/he/43?en=690', 'foo', 'bar')).to eq('/#/platform/dashboard/he/43?en=690&foo=bar') }
it { expect(subject.send(:add_param_to_fragment, '/#/platform/dashboard/he/43?en=690', 'foo', [{msg: 'yolo'}])).to eq('/#/platform/dashboard/he/43?en=690&foo=%7B%3Amsg%3D%3E%22yolo%22%7D') }
describe '#after_sign_in_path_for' do
before { @request.env["devise.mapping"] = Devise.mappings[:user] }

it { expect(controller.after_sign_in_path_for(User.new())).to eq('/dashboard/') }
it { expect(controller.after_sign_in_path_for(User.new(admin_role: "staff"))).to eq('/admin/') }
it { expect(controller.after_sign_in_path_for(User.new(admin_role: ""))).to eq('/dashboard/') }
it { expect(controller.after_sign_in_path_for(User.new(admin_role: "admin"))).to eq('/admin/') }
end
end

end
2 changes: 1 addition & 1 deletion core/spec/controllers/mno_enterprise/i18n_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails_helper'

module MnoEnterprise
describe ApplicationController, type: :controller do
describe 'ApplicationController I18n', type: :controller do
# Anonymous controller
controller do
include MnoEnterprise::Concerns::Controllers::I18n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# fork of the upstream library


@App.service 'MnoeCurrentUser', (MnoeApiSvc, $window, $state) ->
@App.service 'MnoeCurrentUser', (MnoeApiSvc, $window, $state, $q) ->
_self = @

# Store the current_user promise
Expand All @@ -27,4 +27,14 @@
response
)

@skipIfNotAdmin = () ->
if _self.user.admin_role? && _self.user.admin_role == 'admin'
return $q.resolve()
else
$timeout(->
# Runs after the authentication promise has been rejected.
$state.go('dashboard.home')
)
$q.reject()

return @
2 changes: 2 additions & 0 deletions frontend-admin/src/app/index.route.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
controllerAs: 'vm'
ncyBreadcrumb:
label: 'Staff'
resolve:
skip: (MnoeCurrentUser) -> MnoeCurrentUser.skipIfNotAdmin()
.state 'dashboard.customers',
url: '/customers'
templateUrl: 'app/views/customers/customers.html'
Expand Down