Module: MegaBar::AuthorizationHelper

Included in:
ApplicationController
Defined in:
app/helpers/mega_bar/authorization_helper.rb

Instance Method Summary collapse

Instance Method Details

#can_create?(subject) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/helpers/mega_bar/authorization_helper.rb', line 67

def can_create?(subject)
  can_perform_action?(:create, subject)
end

#can_destroy?(subject) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/helpers/mega_bar/authorization_helper.rb', line 79

def can_destroy?(subject)
  can_perform_action?(:destroy, subject)
end

#can_edit?(subject) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/helpers/mega_bar/authorization_helper.rb', line 71

def can_edit?(subject)
  can_perform_action?(:edit, subject)
end

#can_index?(subject) ⇒ Boolean

Permission check helpers

Returns:

  • (Boolean)


59
60
61
# File 'app/helpers/mega_bar/authorization_helper.rb', line 59

def can_index?(subject)
  can_perform_action?(:index, subject)
end

#can_perform_action?(action, subject) ⇒ Boolean

Check if user can perform action - delegates to CCCUX if available, otherwise allows

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'app/helpers/mega_bar/authorization_helper.rb', line 18

def can_perform_action?(action, subject)
  if defined?(Cccux::AuthorizationHelper)
    # CCCUX is available, use its authorization
    can?(action, subject)
  else
    # CCCUX not available, allow all actions
    true
  end
end

#can_show?(subject) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/helpers/mega_bar/authorization_helper.rb', line 63

def can_show?(subject)
  can_perform_action?(:show, subject)
end

#can_update?(subject) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/helpers/mega_bar/authorization_helper.rb', line 75

def can_update?(subject)
  can_perform_action?(:update, subject)
end

#has_mega_role?Boolean

Check if user has Mega Role for admin access

Returns:

  • (Boolean)


84
85
86
87
# File 'app/helpers/mega_bar/authorization_helper.rb', line 84

def has_mega_role?
  return false unless defined?(Devise) && user_signed_in?
  current_user.has_role?('Mega Role')
end

Generic action helper



54
55
56
# File 'app/helpers/mega_bar/authorization_helper.rb', line 54

def link_if_can(action, subject, text, path, **opts)
  link_if_can_with_wrapper(action, subject, text, path, **opts)
end


37
38
39
# File 'app/helpers/mega_bar/authorization_helper.rb', line 37

def link_if_can_create(subject, text, path, **opts)
  link_if_can_with_wrapper(:create, subject, text, path, **opts)
end


49
50
51
# File 'app/helpers/mega_bar/authorization_helper.rb', line 49

def link_if_can_destroy(subject, text, path, **opts)
  link_if_can_with_wrapper(:destroy, subject, text, path, **opts)
end


41
42
43
# File 'app/helpers/mega_bar/authorization_helper.rb', line 41

def link_if_can_edit(subject, text, path, **opts)
  link_if_can_with_wrapper(:edit, subject, text, path, **opts)
end

Link helpers that match CCCUX interface



29
30
31
# File 'app/helpers/mega_bar/authorization_helper.rb', line 29

def link_if_can_index(subject, text, path, **opts)
  link_if_can_with_wrapper(:index, subject, text, path, **opts)
end


33
34
35
# File 'app/helpers/mega_bar/authorization_helper.rb', line 33

def link_if_can_show(subject, text, path, **opts)
  link_if_can_with_wrapper(:show, subject, text, path, **opts)
end


45
46
47
# File 'app/helpers/mega_bar/authorization_helper.rb', line 45

def link_if_can_update(subject, text, path, **opts)
  link_if_can_with_wrapper(:update, subject, text, path, **opts)
end

Base method that handles both prepend and append options



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/mega_bar/authorization_helper.rb', line 4

def link_if_can_with_wrapper(action, subject, text, path, **opts)
  prepend = opts.delete(:prepend)
  append = opts.delete(:append)
  
  if can_perform_action?(action, subject)
    "#{prepend}#{link_to(text, path, **opts)}#{append}".html_safe
  elsif opts.delete(:show_text)
    text
  else
    ""
  end
end