Admin
extends AbstractBaseApi
in package
Admin Class
Represents an admin user in the Canvas LMS. Admins are users with specific permissions to manage accounts and their resources. This class provides methods to create, manage, and remove admin privileges for users at the account level.
Admin operations always require an account context, as admins are assigned permissions within specific accounts in the Canvas hierarchy.
Usage:
// Creating a new admin for an account
$adminData = [
'userId' => 123,
'role' => 'AccountAdmin',
'sendConfirmation' => true
];
$admin = Admin::create($adminData, 1); // Account ID 1
// Finding all admins for an account
$admins = Admin::get(['account_id' => 1]);
// Get all admins from all pages
$allAdmins = Admin::all(['account_id' => 1]);
// Removing admin privileges
$admin = Admin::find(123, ['account_id' => 1]); // User ID 123, Account ID 1
$admin->delete();
// Getting available admin roles
$roles = Admin::getSelfAdminRoles(1);
Table of Contents
Properties
- $accountId : int|null
- The account ID this admin is associated with
- $email : string|null
- The admin's email address
- $id : int|null
- The ID of the User object who is the admin
- $loginId : string|null
- The admin's login ID
- $name : string|null
- The admin's user name
- $role : string|null
- The role of the admin (e.g., 'AccountAdmin', 'SubAccountAdmin')
- $roleId : int|null
- The ID of the role
- $user : array<string, mixed>|null
- Additional user details (optional, based on include parameters)
- $workflowState : string|null
- The workflow state of the admin
- $methodAliases : array<string, array<string|int, string>>
- Define method aliases
Methods
- __callStatic() : mixed
- Magic method to handle function aliases
- __construct() : mixed
- BaseApi constructor.
- all() : array<int, static>
- Get all admins from all pages
- create() : self
- Create a new admin for an account
- delete() : self
- Remove admin privileges (delete admin)
- find() : static
- Find an admin by user ID in a specific account Note: This implementation differs from the interface as it requires account context
- get() : array<int, static>
- Get first page of admins
- getAccount() : Account|null
- Get the account this admin belongs to
- getAccountId() : int|null
- Get account ID
- getEmail() : string|null
- Get admin email
- getId() : int|null
- Get admin user ID
- getName() : string|null
- Get admin name
- getRole() : string|null
- Get admin role
- getRoleId() : int|null
- Get admin role ID
- getSelfAdminRoles() : array<int, array<string, mixed>>
- Get a list of roles that can be assigned to users in the current account
- overrideApiClient() : void
- Set an API client for this class only, leaving other resources on the shared default.
- paginate() : PaginationResult
- Get paginated admins
- resetApiClients() : void
- Clear the shared default client and all per-class overrides.
- setAccountId() : self
- Set account ID
- setApiClient() : void
- Set the shared default API client used by ALL resource classes.
- setEmail() : self
- Set admin email
- setId() : self
- Set admin user ID
- setName() : self
- Set admin name
- setRole() : self
- Set admin role
- setRoleId() : self
- Set admin role ID
- stream() : Generator<int, static>
- Stream all items across all pages one at a time.
- castValue() : DateTime|mixed
- Cast a value to the correct type
- checkApiClient() : void
- Check if the API client is set, if not, instantiate a new one
- convertPaginatedResponseToModels() : array<string|int, static>
- Helper method to convert paginated response data to model instances
- createConfiguredHttpClient() : HttpClient
- Create an HttpClient with configured middleware
- createPaginationResult() : PaginationResult
- Helper method to create PaginationResult from paginated response
- getAliasMap() : array<string, string>
- Build a flat alias-to-method lookup from $methodAliases.
- getApiClient() : HttpClientInterface
- Get the API client, initializing if necessary
- getEndpoint() : string
- Get the API endpoint for this resource
- getPaginatedResponse() : PaginatedResponse
- Helper method to get paginated response from API endpoint
- hydrate() : void
- Assign API response data to properties with type coercion.
- parseJsonResponse() : array<string|int, mixed>
- Parse JSON response from API safely handling StreamInterface
- populate() : void
- Populate the object with new data
- toDtoArray() : array<string|int, mixed>
- Convert the object to an array
- validateContext() : void
- Validate a context type path segment against an allowlist.
Properties
$accountId
The account ID this admin is associated with
public
int|null
$accountId
= null
The admin's email address
public
string|null
$email
= null
$id
The ID of the User object who is the admin
public
int|null
$id
= null
$loginId
The admin's login ID
public
string|null
$loginId
= null
$name
The admin's user name
public
string|null
$name
= null
$role
The role of the admin (e.g., 'AccountAdmin', 'SubAccountAdmin')
public
string|null
$role
= null
$roleId
The ID of the role
public
int|null
$roleId
= null
$user
Additional user details (optional, based on include parameters)
public
array<string, mixed>|null
$user
= null
$workflowState
The workflow state of the admin
public
string|null
$workflowState
= null
$methodAliases
Define method aliases
protected
static array<string, array<string|int, string>>
$methodAliases
= ['get' => ['fetch', 'list'], 'all' => ['fetchAllPages', 'getAll', 'fetchAll'], 'paginate' => ['getPaginated', 'withPagination'], 'find' => ['one', 'getOne']]
Methods
__callStatic()
Magic method to handle function aliases
public
static __callStatic(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
- $name : string
- $arguments : array<string|int, mixed>
Tags
__construct()
BaseApi constructor.
public
__construct(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
all()
Get all admins from all pages
public
static all([array<string, mixed> $params = [] ]) : array<int, static>
Parameters
- $params : array<string, mixed> = []
-
Query parameters (can include 'account_id')
Tags
Return values
array<int, static>create()
Create a new admin for an account
public
static create(array<string, mixed>|CreateAdminDTO $data[, int|null $accountId = null ]) : self
Parameters
- $data : array<string, mixed>|CreateAdminDTO
-
Admin data
- $accountId : int|null = null
-
Account ID (required)
Tags
Return values
selfdelete()
Remove admin privileges (delete admin)
public
delete() : self
Tags
Return values
selffind()
Find an admin by user ID in a specific account Note: This implementation differs from the interface as it requires account context
public
static find(int $userId[, array<string, mixed> $params = [] ]) : static
Parameters
- $userId : int
-
User ID of the admin
- $params : array<string, mixed> = []
-
Optional parameters including account_id
Tags
Return values
staticget()
Get first page of admins
public
static get([array<string, mixed> $params = [] ]) : array<int, static>
Parameters
- $params : array<string, mixed> = []
-
Query parameters (can include 'account_id')
Tags
Return values
array<int, static>getAccount()
Get the account this admin belongs to
public
getAccount() : Account|null
Tags
Return values
Account|nullgetAccountId()
Get account ID
public
getAccountId() : int|null
Return values
int|nullgetEmail()
Get admin email
public
getEmail() : string|null
Return values
string|nullgetId()
Get admin user ID
public
getId() : int|null
Return values
int|nullgetName()
Get admin name
public
getName() : string|null
Return values
string|nullgetRole()
Get admin role
public
getRole() : string|null
Return values
string|nullgetRoleId()
Get admin role ID
public
getRoleId() : int|null
Return values
int|nullgetSelfAdminRoles()
Get a list of roles that can be assigned to users in the current account
public
static getSelfAdminRoles([int|null $accountId = null ]) : array<int, array<string, mixed>>
Parameters
- $accountId : int|null = null
-
Account ID
Tags
Return values
array<int, array<string, mixed>>overrideApiClient()
Set an API client for this class only, leaving other resources on the shared default.
public
static overrideApiClient(HttpClientInterface $apiClient) : void
Parameters
- $apiClient : HttpClientInterface
paginate()
Get paginated admins
public
static paginate([array<string, mixed> $params = [] ]) : PaginationResult
Parameters
- $params : array<string, mixed> = []
-
Query parameters (can include 'account_id')
Tags
Return values
PaginationResultresetApiClients()
Clear the shared default client and all per-class overrides.
public
static resetApiClients() : void
setAccountId()
Set account ID
public
setAccountId(int $accountId) : self
Parameters
- $accountId : int
Return values
selfsetApiClient()
Set the shared default API client used by ALL resource classes.
public
static setApiClient(HttpClientInterface $apiClient) : void
Calling this on any resource (e.g. Course::setApiClient()) replaces the client for every resource, because relationship methods cross class boundaries ($course->enrollments() calls Enrollment internally). Use overrideApiClient() to scope a client to a single class, and resetApiClients() in test teardown to avoid state leaking.
Parameters
- $apiClient : HttpClientInterface
setEmail()
Set admin email
public
setEmail(string $email) : self
Parameters
- $email : string
Return values
selfsetId()
Set admin user ID
public
setId(int $id) : self
Parameters
- $id : int
Return values
selfsetName()
Set admin name
public
setName(string $name) : self
Parameters
- $name : string
Return values
selfsetRole()
Set admin role
public
setRole(string $role) : self
Parameters
- $role : string
Return values
selfsetRoleId()
Set admin role ID
public
setRoleId(int $roleId) : self
Parameters
- $roleId : int
Return values
selfstream()
Stream all items across all pages one at a time.
public
static stream([array<string|int, mixed> $params = [] ]) : Generator<int, static>
Unlike all(), only one page of raw data is held in memory at a time, making this safe for very large datasets (e.g. tens of thousands of enrollments):
foreach (User::stream(['per_page' => 100]) as $user) {
processUser($user);
}
Parameters
- $params : array<string|int, mixed> = []
-
Query parameters for the request
Tags
Return values
Generator<int, static>castValue()
Cast a value to the correct type
protected
castValue(string $key, mixed $value) : DateTime|mixed
Parameters
- $key : string
- $value : mixed
Tags
Return values
DateTime|mixedcheckApiClient()
Check if the API client is set, if not, instantiate a new one
protected
static checkApiClient() : void
convertPaginatedResponseToModels()
Helper method to convert paginated response data to model instances
protected
static convertPaginatedResponseToModels(PaginatedResponse $paginatedResponse) : array<string|int, static>
Parameters
- $paginatedResponse : PaginatedResponse
Return values
array<string|int, static>createConfiguredHttpClient()
Create an HttpClient with configured middleware
protected
static createConfiguredHttpClient() : HttpClient
Return values
HttpClientcreatePaginationResult()
Helper method to create PaginationResult from paginated response
protected
static createPaginationResult(PaginatedResponse $paginatedResponse) : PaginationResult
Parameters
- $paginatedResponse : PaginatedResponse
Return values
PaginationResultgetAliasMap()
Build a flat alias-to-method lookup from $methodAliases.
protected
static getAliasMap() : array<string, string>
Return values
array<string, string>getApiClient()
Get the API client, initializing if necessary
protected
static getApiClient() : HttpClientInterface
Return values
HttpClientInterfacegetEndpoint()
Get the API endpoint for this resource
protected
static getEndpoint() : string
Return values
stringgetPaginatedResponse()
Helper method to get paginated response from API endpoint
protected
static getPaginatedResponse(string $endpoint[, array<string|int, mixed> $params = [] ]) : PaginatedResponse
Parameters
- $endpoint : string
-
The API endpoint path
- $params : array<string|int, mixed> = []
-
Query parameters for the request
Return values
PaginatedResponsehydrate()
Assign API response data to properties with type coercion.
protected
hydrate(array<string|int, mixed> $data) : void
Shared by the constructor and populate() so objects keep the same type guarantees after save()/update() round-trips as on creation.
Parameters
- $data : array<string|int, mixed>
parseJsonResponse()
Parse JSON response from API safely handling StreamInterface
protected
static parseJsonResponse(ResponseInterface $response) : array<string|int, mixed>
Parameters
- $response : ResponseInterface
Return values
array<string|int, mixed>populate()
Populate the object with new data
protected
populate(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
Tags
toDtoArray()
Convert the object to an array
protected
toDtoArray() : array<string|int, mixed>
Return values
array<string|int, mixed>validateContext()
Validate a context type path segment against an allowlist.
protected
static validateContext(string|null $contextType, array<int, string> $allowed) : void
Context types are interpolated into URL paths; validating against the contexts Canvas actually supports prevents crafted values from injecting extra path segments or query parameters.
Parameters
- $contextType : string|null
-
The context type (plural, e.g. 'courses'); null is ignored
- $allowed : array<int, string>
-
Allowed context types