allow viewing inherited roles through roles API index

test plan:
* using the argument "show_inherited" with the
 roles API index should also return roles
 inherited from parent accounts

closes #CNVS-19404

Change-Id: I7a08af2c523fbe3c549d4179c2acda97091ff974
Reviewed-on: https://gerrit.instructure.com/50624
Tested-by: Jenkins
Reviewed-by: Jeremy Stanley <jeremy@instructure.com>
QA-Review: Jahnavi Yetukuri <jyetukuri@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2015-03-19 07:51:43 -06:00
parent 6e625f57f6
commit 1fff1f83c0
4 changed files with 42 additions and 9 deletions

View File

@ -107,16 +107,25 @@ class RoleOverridesController < ApplicationController
# Filter by role state. If this argument is omitted, only 'active' roles are
# returned.
#
# @argument show_inherited [Boolean]
# If this argument is true, all roles inherited from parent accounts will
# be included.
#
# @returns [Role]
def api_index
if authorized_action(@context, @current_user, :manage_role_overrides)
route = polymorphic_url([:api, :v1, @context, :roles])
states = params[:state].to_a.reject{ |s| %w(active inactive).exclude?(s) }
states = %w(active) if states.empty?
roles = []
roles += Role.visible_built_in_roles if states.include?('active')
roles += @context.roles.where(:workflow_state => states).order(:id).all
scope = value_to_boolean(params[:show_inherited]) ? @context.available_custom_roles(true) : @context.roles
roles += scope.where(:workflow_state => states).order(:id).all
roles = Api.paginate(roles, self, route)
ActiveRecord::Associations::Preloader.new(roles, :account).run
render :json => roles.collect{|role| role_json(@context, role, @current_user, session)}
end
end

View File

@ -715,9 +715,7 @@ class Account < ActiveRecord::Base
end
def available_custom_account_roles(include_inactive=false)
account_roles = include_inactive ? self.roles.for_accounts.not_deleted : self.roles.for_accounts.active
account_roles += self.parent_account.available_custom_account_roles(include_inactive) if self.parent_account
account_roles
available_custom_roles(include_inactive).for_accounts
end
def available_account_roles(include_inactive=false, user = nil)
@ -730,9 +728,7 @@ class Account < ActiveRecord::Base
end
def available_custom_course_roles(include_inactive=false)
course_roles = include_inactive ? self.roles.for_courses.not_deleted : self.roles.for_courses.active
course_roles += self.parent_account.available_custom_course_roles(include_inactive) if self.parent_account
course_roles
available_custom_roles(include_inactive).for_courses
end
def available_course_roles(include_inactive=false)
@ -741,6 +737,13 @@ class Account < ActiveRecord::Base
course_roles
end
def available_custom_roles(include_inactive=false)
@role_chain_ids ||= self.account_chain.map(&:id)
scope = Role.where(:account_id => @role_chain_ids)
scope = include_inactive ? scope.not_deleted : scope.active
scope
end
def available_roles(include_inactive=false)
available_account_roles(include_inactive) + available_course_roles(include_inactive)
end

View File

@ -22,7 +22,6 @@ module Api::V1::Role
def role_json(account, role, current_user, session, opts={})
json = {
:account => account_json(account, current_user, session, []),
:id => role.id,
:role => role.name,
:label => role.label,
@ -31,6 +30,8 @@ module Api::V1::Role
:permissions => {}
}
json[:account] = account_json(role.account, current_user, session, []) if role.account
RoleOverride.manageable_permissions(account).keys.each do |permission|
perm = RoleOverride.permission_for(account, permission, role, account)
json[:permissions][permission] = permission_json(perm, current_user, session) if perm[:account_allows]

View File

@ -74,6 +74,27 @@ describe "Roles API", type: :request do
expect(json.find{|role| role['role'] == "NewRole"}['workflow_state']).to eq 'active'
end
it "should include inherited roles if requested" do
role = @account.roles.new(:name => 'inherited role')
role.base_role_type = 'StudentEnrollment'
role.save!
sub_account = @account.sub_accounts.create!
account_admin_user(:account => sub_account, :active_user => true)
json = api_call(:get, "/api/v1/accounts/#{sub_account.id}/roles?show_inherited=1",
{ :controller => 'role_overrides', :action => 'api_index', :format => 'json',
:account_id => sub_account.id.to_param, :show_inherited => '1' })
expect(json.map{|r| r['id']}).to match_array ([role.id] + Role.visible_built_in_roles.map(&:id))
expect(json.detect{|r| r['id'] == role.id}['account']['id']).to eq @account.id
json2 = api_call(:get, "/api/v1/accounts/#{sub_account.id}/roles",
{ :controller => 'role_overrides', :action => 'api_index', :format => 'json',
:account_id => sub_account.id.to_param })
expect(json2.map{|r| r['id']}).to match_array (Role.visible_built_in_roles.map(&:id))
end
it "should paginate" do
api_call_with_settings(:explicit => '1', :enabled => '1')
json = api_call(:get, "/api/v1/accounts/#{@account.id}/roles?per_page=5",
@ -424,7 +445,6 @@ describe "Roles API", type: :request do
'explicit' => true })
expect(json['id']).to eql teacher_role.id
expect(json['role']).to eql 'TeacherEnrollment'
expect(json['account']['id']).to eq Account.default.id
end
it "should not be able to edit read-only permissions" do