qm-acts-as-generic-controller 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ begin
9
9
  gemspec.email = "marcin@saepia.net"
10
10
  gemspec.homepage = "http://q.saepia.net"
11
11
  gemspec.authors = ["Marcin Lewandowski"]
12
- gemspec.version = "0.1.5"
12
+ gemspec.version = "0.1.6"
13
13
  gemspec.files = Rake::FileList.new [ "MIT-LICENSE", "Rakefile", "lib/*", "app/views/generic_controller/*" ]
14
14
  gemspec.add_dependency "qui-common-helpers", ">= 0.0.8"
15
15
  gemspec.add_dependency "qui-index-table", ">= 0.0.8"
@@ -1,11 +1,18 @@
1
1
  <%- records ||= instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize}") -%>
2
2
  <%- class_name ||= @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
3
3
 
4
- <%- headers = class_name.generic_fields(:action => :index).collect{ |x| x[:options][:index_header] || ".#{x[:name]}".to_sym } -%>
4
+ <%- headers = class_name.generic_fields(:action => :index).collect{ |x| x[:options][:index_header] || ".#{x[:name]}".to_sym if not current_user.respond_to? "has_privileges?" or (class_name.generic_field_associations.has_key?(x[:name]) and current_user.has_privileges?(:class_name => class_name.generic_field_associations[x[:name]][:class_name])) or (current_user.has_privileges?(:class_name => class_name, :attribute => x[:name], :mode => :read)) }.compact -%>
5
+
5
6
  <%- controller_name = "#{defined?(section) ? "#{section.to_s.camelize}::" : ""}#{class_name.to_s.pluralize}".tableize %>
7
+
6
8
  <%- has_edit = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(controller_name, "edit") }.compact.size > 0 %>
7
9
  <%- has_delete = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(controller_name, "destroy") }.compact.size > 0 %>
8
10
 
11
+ <%- if current_user.respond_to? "has_privileges?" -%>
12
+ <%- has_edit = has_edit && current_user.has_privileges?(:class_name => class_name, :generic_action => :update_any) -%>
13
+ <%- has_delete = has_edit && current_user.has_privileges?(:class_name => class_name, :generic_action => :delete_any) -%>
14
+ <%- end -%>
15
+
9
16
  <%- if defined?(QM::ActsAsWorkflow) and class_name.is_workflow? -%>
10
17
  <%- headers << :"workflow.common.indexTableHeader" -%>
11
18
  <%- end -%>
@@ -18,13 +25,15 @@
18
25
 
19
26
  <%- index_table records, :headers => headers, :class_name => class_name do |r| %>
20
27
  <%- class_name.generic_fields(:action => :index).each do |field| -%>
21
- <td>
22
- <%- if field[:options].has_key? :renderer -%>
23
- <%= send(field[:options][:renderer], { :record => r, :field => field, :action => :index }) %>
24
- <%- else -%>
25
- <%= generic_renderer(:index, { :record => r, :field => field }) %>
26
- <%- end -%>
27
- </td>
28
+ <%- if not current_user.respond_to? "has_privileges?" or (class_name.generic_field_associations.has_key?(field[:name]) and current_user.has_privileges?(:class_name => class_name.generic_field_associations[field[:name]][:class_name])) or (current_user.has_privileges?(:class_name => class_name, :attribute => field[:name], :mode => :read)) -%>
29
+ <td>
30
+ <%- if field[:options].has_key? :renderer -%>
31
+ <%= send(field[:options][:renderer], { :record => r, :field => field, :action => :index }) %>
32
+ <%- else -%>
33
+ <%= generic_renderer(:index, { :record => r, :field => field }) %>
34
+ <%- end -%>
35
+ </td>
36
+ <%- end -%>
28
37
  <%- end -%>
29
38
 
30
39
  <%- if defined?(QM::ActsAsWorkflow) and class_name.is_workflow? -%>
@@ -32,7 +32,7 @@
32
32
  <%- end -%>
33
33
 
34
34
 
35
- <%- if is_accessible -%>
35
+ <%- if is_accessible and (not current_user.respond_to? "has_privileges?" or (current_user.respond_to? "has_privileges?" and current_user.has_privileges?(:class_name => klass, :attribute => field[:name]))) -%>
36
36
  <li>
37
37
  <%= f.label field[:name] %>
38
38
 
@@ -1,6 +1,8 @@
1
1
  <%- toolbar do |t| -%>
2
2
  <%= t.index %>
3
3
  <%= t.separator %>
4
+ <%= t.create %>
5
+ <%= t.separator %>
4
6
  <%= t.edit %>
5
7
  <%= t.delete %>
6
8
  <%- end -%>
@@ -14,24 +16,34 @@
14
16
  <%- description = capture do -%>
15
17
  <dl>
16
18
  <%- klass.generic_fields(:action => :show).each do |field| -%>
17
- <dt><%= h klass.human_attribute_name field[:name] %></dt>
19
+ <%- if not current_user.respond_to? "has_privileges?" or (current_user.respond_to? "has_privileges?" and current_user.has_privileges?(:class_name => klass, :attribute => field[:name])) -%>
20
+ <dt><%= h klass.human_attribute_name field[:name] %></dt>
18
21
 
19
- <dd>
20
- <%- if field[:options].has_key? :renderer -%>
21
- <%= send(field[:options][:renderer], { :record => record, :field => field, :action => :show }) %>
22
- <%- else -%>
23
- <%= generic_renderer(:show, { :record => record, :field => field }) %>
24
- <%- end -%>
25
- </dd>
22
+ <dd>
23
+ <%- if field[:options].has_key? :renderer -%>
24
+ <%= send(field[:options][:renderer], { :record => record, :field => field, :action => :show }) %>
25
+ <%- else -%>
26
+ <%= generic_renderer(:show, { :record => record, :field => field }) %>
27
+ <%- end -%>
28
+ </dd>
29
+ <%- end -%>
26
30
  <%- end -%>
27
31
  </dl>
28
32
  <%- end -%>
29
33
 
30
34
  <%- tab_contents = {} -%>
31
35
  <%- record.class.generic_field_associations.each do |k,v| -%>
32
- <%- if (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and (v[:generic_create] or not v[:through]) -%>
36
+ <%- if current_user.respond_to? "has_privileges?" -%>
37
+ <%- has_privileges = current_user.has_privileges?(:class_name => v[:class_name], :generic_action => :index_any) -%>
38
+ <%- has_privileges_to_generic_create = current_user.has_privileges?(:class_name => v[:class_name], :generic_action => :create) -%>
39
+ <%- else -%>
40
+ <%- has_privileges = true -%>
41
+ <%- has_privileges_to_generic_create = true -%>
42
+ <%- end -%>
43
+
44
+ <%- if has_privileges and (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and (v[:generic_create] or not v[:through]) -%>
33
45
  <%- tab_contents[k] = capture do -%>
34
- <%- if v[:generic_create] -%>
46
+ <%- if v[:generic_create] and has_privileges_to_generic_create -%>
35
47
  <%= link_to t(:"toolbar.create"), url_for([ "new", section, v[:class_name].table_name.singularize ]) + "?#{v[:class_name].table_name.singularize}[#{table_name}_id]=#{record.id}" %>
36
48
  <%- end -%>
37
49
 
@@ -40,6 +52,6 @@
40
52
  <%- end -%>
41
53
  <%- end -%>
42
54
 
43
- <%= tabs :tabs => [ { :header => { :text => :"#{section_prefix}tabs.common.description" }, :body => { :text => description } } ] + record.class.generic_field_associations.collect{|k,v| { :header => { :text => t(:"#{section_prefix}tabs.#{table_name}.#{k}") + " (#{record.send(k).count})" }, :body => { :text => tab_contents[k] } } if (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and v[:through] == false and (record.send(k).count > 0 or v[:generic_create]) }.compact -%>
55
+ <%= tabs :tabs => [ { :header => { :text => :"#{section_prefix}tabs.common.description" }, :body => { :text => description } } ] + record.class.generic_field_associations.collect{|k,v| { :header => { :text => t(:"#{section_prefix}tabs.#{table_name}.#{k}") + " (#{record.send(k).count})" }, :body => { :text => tab_contents[k] } } if tab_contents.has_key?(k) and (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and v[:through] == false and (record.send(k).count > 0 or v[:generic_create]) }.compact -%>
44
56
 
45
57
 
@@ -25,6 +25,9 @@ module QM
25
25
  raise exception
26
26
  end
27
27
  end
28
+
29
+ before_filter :check_generic_privileges
30
+ before_filter :check_limit_for_user, :only => [ :show, :edit, :update, :destroy ]
28
31
  END
29
32
 
30
33
  end
@@ -33,33 +36,59 @@ module QM
33
36
  module InstanceMethods
34
37
  def index
35
38
  unless instance_variable_defined?(plural_variable)
36
-
37
39
  if params[:scopes] and params[:scopes].is_a?(Array)
38
40
  valid_scopes = params[:scopes].uniq.each{ |scope| scope if model.generic_named_scopes.has_key? scope.to_sym }.compact
39
41
  if valid_scopes.size > 0
40
- data = eval "model.#{valid_scopes.join(".")}"
42
+ if defined?(current_user) and model.respond_to? :limit_for_user
43
+ data = eval "model.#{valid_scopes.join(".")}.limit_for_user(#{current_user})"
44
+ else
45
+ data = eval "model.#{valid_scopes.join(".")}"
46
+ end
41
47
  else
42
- data = model.all
48
+ if defined?(current_user) and model.respond_to? :limit_for_user
49
+ data = model.limit_for_user(current_user)
50
+ else
51
+ data = model.all
52
+ end
43
53
  end
44
54
  else
45
- data = model.all
55
+ if defined?(current_user) and model.respond_to? :limit_for_user
56
+ data = model.limit_for_user(current_user)
57
+ else
58
+ data = model.all
59
+ end
46
60
  end
61
+
47
62
 
48
63
  instance_variable_set(plural_variable, data)
49
64
  end
50
65
 
51
66
  respond_to do |format|
52
67
  format.html
53
- format.xml { render :xml => instance_variable_get(plural_variable) }
68
+ format.xml {
69
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
70
+ render :xml => instance_variable_get(plural_variable).to_xml(:only => current_user.privileged_attributes(model, :read))
71
+ else
72
+ render :xml => instance_variable_get(plural_variable)
73
+ end
74
+ }
54
75
  end
55
76
  end
56
77
 
57
78
  def show
79
+
58
80
  instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)
59
81
 
60
82
  respond_to do |format|
61
83
  format.html
62
- format.xml { render :xml => instance_variable_get(singular_variable) }
84
+
85
+ format.xml {
86
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
87
+ render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user.privileged_attributes(model, :read))
88
+ else
89
+ render :xml => instance_variable_get(singular_variable)
90
+ end
91
+ }
63
92
  end
64
93
  end
65
94
 
@@ -72,7 +101,14 @@ module QM
72
101
 
73
102
  respond_to do |format|
74
103
  format.html
75
- format.xml { render :xml => instance_variable_get(singular_variable) }
104
+ format.xml {
105
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
106
+ render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user.privileged_attributes(model, :read))
107
+ else
108
+ render :xml => instance_variable_get(singular_variable)
109
+ end
110
+ }
111
+
76
112
  end
77
113
  end
78
114
 
@@ -87,7 +123,13 @@ module QM
87
123
  format.xml { render :xml => instance_variable_get(singular_variable), :status => :created, :location => [ section, instance_variable_get(singular_variable) ] }
88
124
  else
89
125
  format.html { redirect_to instance_variable_get(singular_variable) }
90
- format.xml { render :xml => instance_variable_get(singular_variable), :status => :created, :location => instance_variable_get(singular_variable) }
126
+ format.xml {
127
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
128
+ render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user.privileged_attributes(model, :read)), :status => :created, :location => instance_variable_get(singular_variable)
129
+ else
130
+ render :xml => instance_variable_get(singular_variable), :status => :created, :location => instance_variable_get(singular_variable)
131
+ end
132
+ }
91
133
  end
92
134
  else
93
135
  format.html {
@@ -98,13 +140,25 @@ module QM
98
140
  render :template => "generic_controller/form"
99
141
  end
100
142
  }
101
- format.xml { render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity }
143
+ format.xml {
144
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
145
+ render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user.privileged_attributes(model, :read)), :status => :unprocessable_entity
146
+ else
147
+ render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity
148
+ end
149
+ }
102
150
  end
103
151
  end
104
152
  end
105
153
 
106
154
  def update
107
155
  instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)
156
+
157
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
158
+ params[singular_variable(true)].keys.each do |key|
159
+ params[singular_variable(true)].delete key unless current_user.privileged_attributes(model, :write).include? key
160
+ end
161
+ end
108
162
  instance_variable_get(singular_variable).update_attributes params[singular_variable(true)]
109
163
 
110
164
  respond_to do |format|
@@ -126,7 +180,13 @@ module QM
126
180
  render :template => "generic_controller/form"
127
181
  end
128
182
  }
129
- format.xml { render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity }
183
+ format.xml {
184
+ if defined?(current_user) and current_user.respond_to? :privileged_attributes
185
+ render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user.privileged_attributes(model, :read)), :status => :unprocessable_entity
186
+ else
187
+ render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity
188
+ end
189
+ }
130
190
  end
131
191
  end
132
192
  end
@@ -155,7 +215,38 @@ module QM
155
215
  def singular_variable(local = false)
156
216
  :"#{(local ? "" : "@")}#{self.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}"
157
217
  end
218
+
219
+
220
+ def check_generic_privileges
221
+ return true unless defined?(current_user)
222
+ return true unless current_user.respond_to? "has_privileges?"
158
223
 
224
+ action = params[:action].to_sym
225
+ action = case action
226
+ when :index
227
+ :index_any
228
+ when :show
229
+ :show_any
230
+ when :update
231
+ :update_any
232
+ when :create
233
+ :create
234
+ when :destroy
235
+ :delete_any
236
+ end
237
+
238
+ render_generic_forbidden unless current_user.has_privileges? :class_name => model, :action => action
239
+ end
240
+
241
+ def check_limit_for_user
242
+ render_generic_forbidden unless model.limit_for_user(current_user).include? model.find(params[:id]) if model.respond_to? :limit_for_user if defined?(current_user)
243
+ end
244
+
245
+
246
+ def render_generic_forbidden
247
+ # FIXME TODO render something more nice to user
248
+ render :status => 403, :text => "Access denied."
249
+ end
159
250
  end
160
251
  end
161
252
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qm-acts-as-generic-controller
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marcin Lewandowski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-28 00:00:00 +02:00
18
+ date: 2010-11-09 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency