create admin API call

tags the user as an admin in the account. the membership type may be
specified by a parameter, defaulting to AccountAdmin.

refs #6176

test-plan:
 - create user through API; user should not be an admin
 - POST to accounts/<account_id>/admins with that user_id; user should
   now be an admin with membership type AccountAdmin
 - create another user; POST to the same URL with that new user_id and a
   membership_type specified; user should be an admin with membership
   type matching the specified value.

Change-Id: I003c23324298d8c4214a31be0276067de4b9630b
Reviewed-on: https://gerrit.instructure.com/7443
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Jacob Fugal 2011-12-12 14:19:43 -07:00
parent edd59601ee
commit a577fcb033
7 changed files with 218 additions and 10 deletions

View File

@ -381,23 +381,18 @@ class AccountsController < ApplicationController
end
end
# TODO Refactor add_account_user and remove_account_user actions into
# AdminsController. see https://redmine.instructure.com/issues/6634
def add_account_user
if authorized_action(@context, @current_user, :manage_account_memberships)
list = UserList.new(params[:user_list], @context, params[:only_search_existing_users] ? false : @context.open_registration_for?(@current_user, session))
users = list.users
account_users = users.map do |user|
account_user = @context.add_user(user, params[:membership_type])
if user.registered?
account_user.account_user_notification!
else
account_user.account_user_registration!
end
admin = user.flag_as_admin(@context, params[:membership_type])
{ :enrollment => {
:id => account_user.id,
:id => admin.id,
:name => user.name,
:membership_type => account_user.membership_type,
:membership_type => admin.membership_type,
:workflow_state => 'active',
:user_id => user.id,
:type => 'admin',

View File

@ -0,0 +1,41 @@
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# @API Admins
class AdminsController < ApplicationController
before_filter :require_user
before_filter :get_context
include Api::V1::Admin
# @API
# Flag an existing user as an admin within the account.
#
# @argument user_id The id of the user to promote.
#
# @argument membership_type [Optional] The user's admin relationship with the
# account will be created with the given membership type. Defaults to
# 'AccountAdmin'.
def create
if authorized_action(@context, @current_user, :manage_account_memberships)
user = api_find(User, params[:user_id])
admin = user.flag_as_admin(@context, params[:membership_type])
render :json => admin_json(admin, @current_user, session)
end
end
end

View File

@ -2167,4 +2167,14 @@ class User < ActiveRecord::Base
end
pseudonym
end
def flag_as_admin(account, membership_type=nil)
admin = account.add_user(self, membership_type)
if self.registered?
admin.account_user_notification!
else
admin.account_user_registration!
end
admin
end
end

View File

@ -724,6 +724,10 @@ ActionController::Routing::Routes.draw do |map|
accounts.get 'accounts/:account_id/courses', :action => :courses_api, :path_name => 'account_courses'
end
api.with_options(:controller => :admins) do |admins|
admins.post 'accounts/:account_id/admins', :action => :create
end
api.with_options(:controller => :account_authorization_configs) do |authorization_configs|
authorization_configs.post 'accounts/:account_id/account_authorization_configs', :action => 'update_all'
end

32
lib/api/v1/admin.rb Normal file
View File

@ -0,0 +1,32 @@
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module Api::V1::Admin
include Api::V1::Json
include Api::V1::User
def admin_json(admin, current_user, session)
# admin is an AccountUser
{
:id => admin.id,
:membership_type => admin.membership_type,
:user => user_json(admin.user, current_user, session)
}
end
end

View File

@ -0,0 +1,80 @@
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../api_spec_helper')
describe "Admins API", :type => :integration do
before do
@admin = account_admin_user
user_with_pseudonym(:user => @admin)
end
describe "create" do
before :each do
@new_user = user(:name => 'new guy')
@user = @admin
end
it "should flag the user as an admin for the account" do
json = api_call(:post, "/api/v1/accounts/#{@admin.account.id}/admins",
{ :controller => 'admins', :action => 'create', :format => 'json', :account_id => @admin.account.id.to_s },
{ :user_id => @new_user.id })
@new_user.reload
@new_user.account_users.size.should == 1
admin = @new_user.account_users.first
admin.account.should == @admin.account
end
it "should default the membership type of the admin association to AccountAdmin" do
json = api_call(:post, "/api/v1/accounts/#{@admin.account.id}/admins",
{ :controller => 'admins', :action => 'create', :format => 'json', :account_id => @admin.account.id.to_s },
{ :user_id => @new_user.id })
@new_user.reload
admin = @new_user.account_users.first
admin.membership_type.should == 'AccountAdmin'
end
it "should respect the provided membership type, if any" do
json = api_call(:post, "/api/v1/accounts/#{@admin.account.id}/admins",
{ :controller => 'admins', :action => 'create', :format => 'json', :account_id => @admin.account.id.to_s },
{ :user_id => @new_user.id, :membership_type => "CustomAccountUser" })
@new_user.reload
admin = @new_user.account_users.first
admin.membership_type.should == 'CustomAccountUser'
end
it "should return json of the new admin association" do
json = api_call(:post, "/api/v1/accounts/#{@admin.account.id}/admins",
{ :controller => 'admins', :action => 'create', :format => 'json', :account_id => @admin.account.id.to_s },
{ :user_id => @new_user.id })
@new_user.reload
admin = @new_user.account_users.first
json.should == {
"id" => admin.id,
"membership_type" => admin.membership_type,
"user" => {
"id" => @new_user.id,
"name" => @new_user.name,
"short_name" => @new_user.short_name,
"sortable_name" => @new_user.sortable_name
}
}
end
end
end

View File

@ -1178,4 +1178,50 @@ describe User do
(lambda {User.create!.sis_pseudonym_for(context)}).should raise_error("could not resolve root account")
end
end
describe "flag_as_admin" do
it "should add an AccountUser" do
@account = account_model
u = User.create!
u.account_users.should be_empty
u.flag_as_admin(@account)
u.reload
u.account_users.size.should == 1
admin = u.account_users.first
admin.account.should == @account
end
it "should default to the AccountAdmin membership type" do
@account = account_model
u = User.create!
u.flag_as_admin(@account)
u.reload
admin = u.account_users.first
admin.membership_type.should == 'AccountAdmin'
end
it "should respect a provided membership type" do
@account = account_model
u = User.create!
u.flag_as_admin(@account, "CustomAccountUser")
u.reload
admin = u.account_users.first
admin.membership_type.should == 'CustomAccountUser'
end
it "should send an account registration email for users that haven't registered yet" do
AccountUser.any_instance.expects(:account_user_registration!)
@account = account_model
u = User.create!
u.flag_as_admin(@account)
end
it "should send the pre-registered account registration email for users the have already registered" do
AccountUser.any_instance.expects(:account_user_notification!)
@account = account_model
u = User.create!
u.register
u.flag_as_admin(@account)
end
end
end