Get admin roles through API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all, I'm hoping someone might be able to help me out, as I'm running in to some brick walls on a project...
I'm trying to create a javascript function to be used for customizations in our theme that will return what I refer to as a users roles in context... What I mean by that is that if the user is on a page in course X which belongs in subaccount Z, my function would ideally return that user's role in course X, along with their roles in subaccount Z and any parents accounts of that, but not subccounts in different branches of the account structure. This is similar to the ENV.current_user_roles variable, but my my function would include the actual roles (not just the types) and would be in context, unlike the ENV variable which essentially says the user is a teacher somewhere in the Canvas instance, but maybe not on the page that's loaded right now. I hope this makes sense...
I believe I have this working for course roles, but I'm running into a problem trying to list out admin roles. I am currently using the admins api to call 'https://<instance>.instructure.com/api/v1/accounts/134/admins?user_id[]=ENV.current_user_id' if ENV.current_user_roles indicates the user has admin access somewhere. This all works great using my account, which is a full root admin. What I've discovered in testing is that that API call only seems to work if the user's admin role includes the "Admins add / remove" permission. I want my function to work somewhat universally, and for many reasons, I can't give every one of our account roles the ability to add other admins.
Anyone have any suggestions on a different API call I could use for this purpose, or a different way to approach it? I know enough javascript (and how to google things) to be dangerous, but I'm definitely not a full expert. I know I could somehow use a root admin token to make the API call, but we definitely can't put a key like that in our javascript file that anyone can access. This is my first time i'm trying to do API calls within the javascript file at all, so I could be approaching this completely incorrectly or inefficiently. I'm hoping theres an easy solution for this that I just don't know the term to search and find though.
If anyone is interested, I put my rough code on my GitHub at https://github.com/cmcasey79/canvas-customizations-by-chris/blob/master/get_user_context_roles.js.
Thanks in advance for any help someone might be able to provide!
-Chris