Module: CanvasSync::Concerns::AbilityHelper
- Extended by:
- ActiveSupport::Concern
- Includes:
- PandaPal::Concerns::AbilityHelper
- Defined in:
- lib/canvas_sync/concerns/ability_helper.rb
Constant Summary collapse
- ACCOUNT_ADMIN_ROLE_LABELS =
["AccountAdmin", "Account Admin"].freeze
Instance Method Summary collapse
-
#canvas_account_admin?(account = launch_account) ⇒ Boolean
True when the User administers
account, INCLUDING by way of an Admin role held on an Account above it: in Canvas an Account Admin administers their Account and everything beneath it, so checking only the exact Account under-reports. - #canvas_account_roles ⇒ Object
- #canvas_course_roles ⇒ Object
- #canvas_permissions ⇒ Object
- #canvas_roles ⇒ Object
- #canvas_root_account_roles ⇒ Object
-
#canvas_super_user? ⇒ Boolean
True for an Admin of the Account this Tool is installed on.
- #canvas_user_id ⇒ Object
- #launch_account ⇒ Object
- #launch_context ⇒ Object
-
#load_canvas_model(model, canvas_id) ⇒ Object
CanvasSync Domain.
- #panda_pal_session ⇒ Object
-
#user ⇒ Object
Middle Domain.
Instance Method Details
#canvas_account_admin?(account = launch_account) ⇒ Boolean
True when the User administers account, INCLUDING by way of an Admin
role held on an Account above it: in Canvas an Account Admin administers
their Account and everything beneath it, so checking only the exact
Account under-reports. Where a Tool is installed on a sub-account, that
under-reporting inverted this check both ways — a genuine higher-level
admin was rejected while a lower-scoped one was accepted.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 116 def canvas_account_admin?(account = launch_account) # The `:inherited` cache-key segment is load-bearing, not decoration: # #cache persists into the Session row, so sessions created before the # traversal existed hold a cached `false` under the old key. Keying the # new semantics separately retires those answers instead of serving # stale denials to already-launched sessions. panda_pal_session.cache([:canvas_account_admin?, account, :inherited]) do panda_pal_session.canvas_site_admin? || (account_role_labels(account) & ACCOUNT_ADMIN_ROLE_LABELS).present? end end |
#canvas_account_roles ⇒ Object
93 94 95 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 93 def canvas_account_roles canvas_roles.where(base_role_type: 'AccountMembership') end |
#canvas_course_roles ⇒ Object
97 98 99 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 97 def canvas_course_roles canvas_roles.where.not(base_role_type: 'AccountMembership') end |
#canvas_permissions ⇒ Object
80 81 82 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 80 def panda_pal_session[:canvas_permissions] ||= ::Role.(canvas_roles) end |
#canvas_roles ⇒ Object
84 85 86 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 84 def canvas_roles @canvas_roles ||= ::Role.for_labels(panda_pal_session.canvas_role_labels, launch_account) end |
#canvas_root_account_roles ⇒ Object
88 89 90 91 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 88 def canvas_root_account_roles role_labels = panda_pal_session.canvas_account_role_labels('self') ::Role.for_labels(role_labels, ::Account.find_by(canvas_parent_account_id: nil)) end |
#canvas_super_user? ⇒ Boolean
True for an Admin of the Account this Tool is installed on. Note that
:root is PandaPal's install-Account sense of "root" — the top of what
this Tool can see — and not necessarily Canvas's own root Account.
106 107 108 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 106 def canvas_super_user? canvas_account_admin?(:root) end |
#canvas_user_id ⇒ Object
128 129 130 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 128 def canvas_user_id user&.canvas_id || panda_pal_session.get_lti_cust_param('canvas_user_id') end |
#launch_account ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 51 def launch_account return @launch_account if defined?(@launch_account) if launch_context.respond_to?(:account) @launch_account = launch_context.account @launch_account ||= load_canvas_model(::Account, launch_context.canvas_account_id) if launch_context.respond_to?(:canvas_account_id) end @launch_account ||= load_canvas_model(::Account, panda_pal_session.get_lti_cust_param('custom_canvas_account_id')) @launch_account ||= ::Account.find_by(canvas_parent_account_id: nil) @launch_account end |
#launch_context ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 36 def launch_context return nil unless panda_pal_session.present? return @launch_context if defined?(@launch_context) @launch_context ||= begin if panda_pal_session.lti_launch_placement == "global_navigation" :global elsif panda_pal_session.get_lti_cust_param('custom_canvas_course_id').present? load_canvas_model(::Course, panda_pal_session.get_lti_cust_param('custom_canvas_course_id')) else load_canvas_model(::Account, panda_pal_session.get_lti_cust_param('custom_canvas_account_id')) end end end |
#load_canvas_model(model, canvas_id) ⇒ Object
CanvasSync Domain
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 68 def load_canvas_model(model, canvas_id) return nil unless model < CanvasSync::Record return nil unless canvas_id.present? return nil if canvas_id.is_a?(String) && canvas_id.include?('$') if model.respond_to?(:find_or_jit_sync) model.find_or_jit_sync(canvas_id) else model.find_by(canvas_id: canvas_id) end end |
#panda_pal_session ⇒ Object
14 15 16 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 14 def panda_pal_session raise "This feature was moved to PandaPal as of CanvasSync 0.20.0/PandaPal 5.9.9. You should update to PandaPal >= 5.9.9." end |
#user ⇒ Object
Middle Domain
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/canvas_sync/concerns/ability_helper.rb', line 21 def user return @user if defined?(@user) canvas_user_id = panda_pal_session.get_lti_cust_param('custom_canvas_user_id') if canvas_user_id.present? begin @user = load_canvas_model(::User, canvas_user_id) rescue Footrest::HttpError::NotFound => err # Mainly catches SiteAdmin users not found via the API @user = nil end end @user end |