Module: LogicalAuthz::Helper

Included in:
Application
Defined in:
app/helpers/logical_authz_helper.rb

Instance Method Summary collapse

Instance Method Details

#authorized?(criteria = nil) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/logical_authz_helper.rb', line 22

def authorized?(criteria=nil)
  criteria ||= {}

  laz_debug{"Helper authorizing: #{LogicalAuthz.inspect_criteria(criteria)}"}

  criteria = {
    :controller => controller_path, 
    :action => action_name, 
    :id => params[:id] 
  }.merge(criteria)
  criteria[:params] = criteria.dup

  unless criteria.has_key?(:group) or criteria.has_key?(:user)
    controller = case self
                 when ActionView::Base
                   self.controller
                 else
                   self #XXX ???
                 end
    criteria[:user] = AuthnFacade.current_user(controller)
  end

  result = LogicalAuthz.is_authorized?(criteria)

  return result
end

#authorized_menu(*items) {|items| ... } ⇒ Object

Yields:

  • (items)


98
99
100
101
102
# File 'app/helpers/logical_authz_helper.rb', line 98

def authorized_menu(*items)
  yield(items) if items.all? do |item|
    authorized_url? [*item].last
  end
end

#authorized_url?(options, html_options = nil) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/logical_authz_helper.rb', line 82

def authorized_url?(options, html_options = nil)
  html_options ||= {}
  params = {}
  if Hash === options
    params = options
  else
    params = criteria_from_url(options)
  end
  if params.nil?
    true #We can't work out where it is, so we have no opinion
    #XXX: Shouldn't this be false?
  else
    authorized?(params)
  end
end

#button_to_if_authorized(name, options = {}, html_options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/logical_authz_helper.rb', line 119

def button_to_if_authorized(name, options = {}, html_options = {})
  url = options
  if(authorized_url?(url, html_options))
    button_to(name, options, html_options)
  else
    if block_given?
      yield
    else
      ""
    end
  end
end

#button_to_remote_if_authorized(name, options = {}, html_options = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/helpers/logical_authz_helper.rb', line 145

def button_to_remote_if_authorized(name, options = {}, html_options = nil)
  url = options[:url]
  if(authorized_url?(url, html_options))
    button_to_remote(name, options, html_options)
  else
    if block_given?
      yield
    else
      ""
    end
  end
end

#controller_pairsObject



61
62
63
64
65
# File 'app/helpers/logical_authz_helper.rb', line 61

def controller_pairs
  controllers = ActionController::Routing::possible_controllers
  controllers -= %w{rails/info application authz rails_info}
  controllers.map{|c| [c.classify, c]}
end

#criteria_from_url(url, html_options = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/logical_authz_helper.rb', line 67

def criteria_from_url(url, html_options = nil)
  return nil if url.nil?
  uri = URI.parse(url_for(url))
  path = uri.path
  querystring = uri.query
  http_method = (html_options.nil? ? nil : html_options[:method]) || :get
  begin
    params = Rails.application.routes.recognize_path(path, :method => http_method)
  rescue ActionController::RoutingError => ex
    Rails.logger.info{"Asked to authorize url: #{html_options.inspect} - couldn't route: #{ex.class.name}: #{ex.message}"}
    return nil
  end
  querystring.blank? ? params : params.merge(Rack::Utils.parse_query(querystring).symbolize_keys!)
end

#groupsObject



55
56
57
58
59
# File 'app/helpers/logical_authz_helper.rb', line 55

def groups
  LogicalAuthz::group_model.all.map do |group|
    [group.name, group.id ]
  end
end

#laz_debugObject



16
17
18
19
20
# File 'app/helpers/logical_authz_helper.rb', line 16

def laz_debug
  if block_given?
    LogicalAuthz::laz_debug{yield}
  end
end


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/logical_authz_helper.rb', line 104

def link_to_if_authorized(name, options = nil, html_options = nil)
  options ||= {}
  html_options ||= {}
  url = options
  if(authorized_url?(url, html_options))
    link_to(name, options, html_options)
  else
    if block_given?
      yield
    else
      ""
    end
  end
end


132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/helpers/logical_authz_helper.rb', line 132

def link_to_remote_if_authorized(name, options = {}, html_options = nil)
  url = options[:url]
  if(authorized_url?(url, html_options))
    link_to_remote(name, options, html_options)
  else
    if block_given?
      yield
    else
      ""
    end
  end
end

#nonmembered_groups(user) ⇒ Object

returns an array of group names and ids (suitable for select_tag) for which <user> is not a member



51
52
53
# File 'app/helpers/logical_authz_helper.rb', line 51

def nonmembered_groups(user)
  (LogicalAuthz::group_model.all - user.groups).map { |g| [ g.name, g.id ] }
end