ixtlan-guard 0.1.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/lib/generators/active_record/templates/flavor_migration.rb +13 -0
- data/lib/generators/active_record/templates/flavor_model.rb +8 -0
- data/lib/generators/active_record/templates/group_model.rb +43 -0
- data/lib/generators/active_record/templates/group_user_migration.rb +13 -0
- data/lib/generators/active_record/templates/user_model.rb +124 -0
- data/lib/generators/active_record/user_management_models_generator.rb +202 -0
- data/lib/generators/erb/user_management_controller_generator.rb +10 -0
- data/lib/generators/{ixtlan → guard}/controller/USAGE +0 -0
- data/lib/generators/{ixtlan → guard}/controller/controller_generator.rb +2 -2
- data/lib/generators/{ixtlan → guard}/scaffold/USAGE +0 -0
- data/lib/generators/{ixtlan → guard}/scaffold/scaffold_generator.rb +4 -6
- data/lib/generators/guard/scaffold/templates/guard.rb +20 -0
- data/lib/generators/guard/templates/guard.rb +20 -0
- data/lib/generators/ixtlan/maintenance_scaffold/USAGE +8 -0
- data/lib/generators/ixtlan/maintenance_scaffold/maintenance_scaffold_generator.rb +40 -0
- data/lib/generators/ixtlan/permissions_scaffold/USAGE +8 -0
- data/lib/generators/ixtlan/permissions_scaffold/permissions_scaffold_generator.rb +33 -0
- data/lib/generators/ixtlan/user_management_controller/USAGE +8 -0
- data/lib/generators/ixtlan/user_management_controller/user_management_controller_generator.rb +23 -0
- data/lib/generators/ixtlan/user_management_models/USAGE +8 -0
- data/lib/generators/ixtlan/user_management_models/user_management_models_generator.rb +19 -0
- data/lib/generators/ixtlan/user_management_scaffold/user_management_scaffold_generator.rb +13 -0
- data/lib/ixtlan-guard.rb +2 -2
- data/lib/ixtlan/guard.rb +1 -159
- data/lib/ixtlan/guard/controllers/maintenance_controller.rb +45 -0
- data/lib/ixtlan/guard/controllers/permissions_controller.rb +41 -0
- data/lib/ixtlan/guard/guard.rb +245 -0
- data/lib/ixtlan/guard/models/maintenance.rb +55 -0
- data/lib/ixtlan/guard/models/user_update_manager.rb +95 -0
- data/lib/ixtlan/guard/rails_integration.rb +88 -0
- data/lib/ixtlan/guard/railtie.rb +33 -0
- data/lib/ixtlan/guard/spec/user_management_models_spec.rb +193 -0
- data/spec/guard_spec.rb +48 -12
- data/spec/railtie_spec.rb +1 -1
- metadata +75 -52
- data/lib/generators/ixtlan/templates/edit.html.erb +0 -10
- data/lib/generators/ixtlan/templates/guard.rb +0 -13
- data/lib/generators/ixtlan/templates/index.html.erb +0 -35
- data/lib/generators/ixtlan/templates/new.html.erb +0 -7
- data/lib/generators/ixtlan/templates/show.html.erb +0 -16
- data/lib/generators/scaffold/scaffold/scaffold_generator.rb +0 -23
- data/lib/ixtlan/guard_railtie.rb +0 -43
- data/lib/ixtlan/rails_integration.rb +0 -55
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Kristian Meier
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Create<%= association_name.camelize %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= association_name %>, :id => false, :force => true do |t|
|
4
|
+
<% [file_name, group_name, user_name].sort.each do |name| -%>
|
5
|
+
t.integer :<%= name %>_id
|
6
|
+
<% end -%>
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.down
|
11
|
+
drop_table :<%= association_name %>
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class <%= association_class_name(plural_name) %> < <%= parent_class_name.classify %>
|
2
|
+
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
3
|
+
belongs_to :<%= attribute.name %>
|
4
|
+
<% end -%>
|
5
|
+
<% [name, group_name, user_name].sort.each do |ref_name| -%>
|
6
|
+
belongs_to :<%= ref_name %>
|
7
|
+
<% end -%>
|
8
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class <%= group_class_name %> < <%= parent_class_name.classify %>
|
2
|
+
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
3
|
+
belongs_to :<%= attribute.name %>
|
4
|
+
<% end -%>
|
5
|
+
|
6
|
+
has_and_belongs_to_many :<%= plural_user_name %>
|
7
|
+
|
8
|
+
ROOT = 'root'
|
9
|
+
ADMIN = 'admin'
|
10
|
+
|
11
|
+
def self.admin_group
|
12
|
+
find_by_<%= attributes.first.name %>(ADMIN)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.root_group
|
16
|
+
find_by_<%= attributes.first.name %>(ROOT)
|
17
|
+
end
|
18
|
+
|
19
|
+
def admin?
|
20
|
+
<%= attributes.first.name %> == ADMIN
|
21
|
+
end
|
22
|
+
|
23
|
+
def root?
|
24
|
+
<%= attributes.first.name %> == ROOT
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.get(id_or_<%= attributes.first.name %>_or_<%= file_name %>)
|
28
|
+
case id_or_<%= attributes.first.name %>_or_<%= file_name %>
|
29
|
+
when Fixnum
|
30
|
+
find(id_or_<%= attributes.first.name %>_or_<%= file_name %>)
|
31
|
+
when String
|
32
|
+
find_by_<%= attributes.first.name %>(id_or_<%= attributes.first.name %>_or_<%= file_name %>)
|
33
|
+
when Symbol
|
34
|
+
find_by_<%= attributes.first.name %>(id_or_<%= attributes.first.name %>_or_<%= file_name %>.to_s)
|
35
|
+
else
|
36
|
+
id_or_<%= attributes.first.name %>_or_<%= file_name %>
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_name
|
41
|
+
<%= group_field_name %>
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Create<%= group_user_name.camelize %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= group_user_name %>, :id => false, :force => true do |t|
|
4
|
+
<% [group_name, user_name].sort.each do |name| -%>
|
5
|
+
t.integer :<%= name %>_id
|
6
|
+
<% end -%>
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.down
|
11
|
+
drop_table :<%= group_user_name %>
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'ixtlan/guard/models/user_update_manager'
|
2
|
+
class <%= user_class_name %> < <%= parent_class_name.classify %>
|
3
|
+
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
4
|
+
belongs_to :<%= attribute.name %>
|
5
|
+
<% end -%>
|
6
|
+
|
7
|
+
has_and_belongs_to_many :<%= plural_group_name %>
|
8
|
+
|
9
|
+
attr_accessor :id
|
10
|
+
|
11
|
+
def self.update_manager
|
12
|
+
@manager ||= Ixtlan::Guard::Models::UserUpdateManager.new( :group_model => <%= group_class_name %>, :user_id => :<%= user_name %>_id, :group_id => :<%= group_name %>_id, :plural_group_name => :<%= plural_group_name %> )
|
13
|
+
end
|
14
|
+
|
15
|
+
alias :create! :create
|
16
|
+
def self.create(params = {})
|
17
|
+
u = self.new
|
18
|
+
u.current_user = params.delete("current_user") || params.delete(:current_user)
|
19
|
+
u.update_attributes(params)
|
20
|
+
u
|
21
|
+
end
|
22
|
+
|
23
|
+
def update_attributes(params)
|
24
|
+
result = []
|
25
|
+
<% flavors.each do |flavor| %>
|
26
|
+
# update <%= flavor.pluralize %>
|
27
|
+
if result.all?{|a| a}
|
28
|
+
result << self.class.update_manager.update(self, params, :flavor_id => :<%= flavor %>_id, :flavors_for_group => :<%= flavor.pluralize %>_for_<%= group_name %>, :association_model => <%= association_class_name(flavor.pluralize) %>)
|
29
|
+
end
|
30
|
+
<% end -%>
|
31
|
+
|
32
|
+
# update <%= plural_group_name %>
|
33
|
+
self.class.update_manager.update_groups(self, params) if result.all?{|a| a}
|
34
|
+
end
|
35
|
+
|
36
|
+
unless respond_to? :current_user
|
37
|
+
# do not use attr_accessor to allow them to be used
|
38
|
+
# for mass_assignmnet_protection
|
39
|
+
def current_user=(u)
|
40
|
+
@current_user = u
|
41
|
+
end
|
42
|
+
|
43
|
+
def current_user
|
44
|
+
@current_user
|
45
|
+
end
|
46
|
+
|
47
|
+
after_save do
|
48
|
+
@current_user = nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def root?
|
53
|
+
<%= plural_group_name %>.detect {|g| g.root? } != nil
|
54
|
+
end
|
55
|
+
|
56
|
+
<% flavors.each do |flavor|
|
57
|
+
plural_flavor = flavor.pluralize %>
|
58
|
+
def managed_<%= plural_flavor %>_for_<%= group_name %>(gid)
|
59
|
+
<%= group_name %> = <%= group_class_name %>.get(gid)
|
60
|
+
if <%= group_name %>.root?
|
61
|
+
[]
|
62
|
+
else
|
63
|
+
existing = <%= plural_flavor %>_for_<%= group_name %>(<%= group_name %>)
|
64
|
+
managed = <%= plural_flavor %>_for_<%= group_name %>(<%= group_class_name %>.admin_group)
|
65
|
+
(managed - (managed - existing))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def <%= plural_flavor %>_for_<%= group_name %>(group_or_id)
|
70
|
+
if root?
|
71
|
+
<%= flavor.camelize %>.all
|
72
|
+
else
|
73
|
+
<%= group_name %> = <%= group_class_name %>.get(group_or_id)
|
74
|
+
<%= association_class_name(plural_flavor) %>.all(:conditions => ["<%= user_name %>_id=? and <%= group_name %>_id=?", id, <%= group_name %>.id]).collect { |dgu| dgu.<%= flavor %> }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def <%= flavor %>_<%= group_name %>?(<%= flavor %>, <%= group_name %>)
|
79
|
+
<%= association_class_name(plural_flavor) %>.count(:conditions => ["<%= user_name %>_id=? and <%= group_name %>_id=? and <%= flavor %>_id=?", id, <%= group_name %>.id, <%= flavor %>.id]) == 1
|
80
|
+
end
|
81
|
+
|
82
|
+
<% end -%>
|
83
|
+
|
84
|
+
def all_<%= plural_group_name %>
|
85
|
+
if root?
|
86
|
+
<%= group_class_name %>.all
|
87
|
+
else
|
88
|
+
<%= plural_group_name %>
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_xml
|
93
|
+
to_hash.to_xml(:root => "<%= file_name %>")
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_json
|
97
|
+
{ :<%= file_name %> => to_hash }.to_json
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_hash
|
101
|
+
map = attributes.dup
|
102
|
+
g = (map[:<%= plural_group_name %>] = [])
|
103
|
+
<%= plural_group_name %>.each do |<%= group_name %>|
|
104
|
+
gg = <%= group_name %>.attributes.dup
|
105
|
+
gg.delete("<%= group_name %>_id")
|
106
|
+
gg.delete("<%= file_name %>_id")
|
107
|
+
<% flavors.each do |flavor|
|
108
|
+
plural_flavor = flavor.pluralize %>
|
109
|
+
<%= plural_flavor %> = <%= plural_flavor %>_for_<%= group_name %>(<%= group_name %>)
|
110
|
+
if <%= plural_flavor %>.size > 0
|
111
|
+
d = (gg[:<%= plural_flavor %>] = [])
|
112
|
+
<%= plural_flavor %>.each do |<%= flavor %>|
|
113
|
+
dd = <%= flavor %>.attributes.dup
|
114
|
+
dd.delete("created_at")
|
115
|
+
dd.delete("updated_at")
|
116
|
+
d << dd
|
117
|
+
end
|
118
|
+
end
|
119
|
+
<% end -%>
|
120
|
+
g << gg
|
121
|
+
end
|
122
|
+
map
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
require 'rails/generators/active_record/model/model_generator'
|
2
|
+
module ActiveRecord
|
3
|
+
class UserManagementModelsGenerator < ::ActiveRecord::Generators::ModelGenerator
|
4
|
+
|
5
|
+
source_paths << ActiveRecord::Generators::ModelGenerator.source_root
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
arguments.clear # clear name argument from NamedBase
|
10
|
+
|
11
|
+
argument :params, :type => :array, :required => true
|
12
|
+
|
13
|
+
remove_hook_for :test_framework
|
14
|
+
|
15
|
+
def name
|
16
|
+
@name ||= user_params[0]
|
17
|
+
end
|
18
|
+
|
19
|
+
def attributes
|
20
|
+
@attributes ||= user_params[1,10000]
|
21
|
+
end
|
22
|
+
|
23
|
+
def attributes=(a = nil)
|
24
|
+
@attributes = a
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
def user_params
|
29
|
+
retrieve_params(@params)
|
30
|
+
end
|
31
|
+
|
32
|
+
def group_params
|
33
|
+
result = retrieve_params(@params[user_params.size, 10000])
|
34
|
+
case result.size
|
35
|
+
when 0
|
36
|
+
["group", "name:string"]
|
37
|
+
when 1
|
38
|
+
result << "name:string"
|
39
|
+
else
|
40
|
+
result
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def flavor_params(index, count = 0)
|
45
|
+
count = user_params.size + retrieve_params(@params[user_params.size, 10000]).size if count == 0
|
46
|
+
if(index > 0)
|
47
|
+
count += retrieve_params(@params[count, 10000]).size
|
48
|
+
if count != @params.size
|
49
|
+
flavor_params(index - 1, count)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
retrieve_params(@params[count, 10000])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def retrieve_params(params)
|
57
|
+
done = nil
|
58
|
+
params.select do |para|
|
59
|
+
if done.nil?
|
60
|
+
done = false
|
61
|
+
true
|
62
|
+
elsif done
|
63
|
+
false
|
64
|
+
else
|
65
|
+
done = (para =~ /[^:]+\:[^:]+/).nil?
|
66
|
+
!done
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
public
|
72
|
+
|
73
|
+
check_class_collision
|
74
|
+
|
75
|
+
class_option :migration, :type => :boolean
|
76
|
+
class_option :timestamps, :type => :boolean
|
77
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
78
|
+
|
79
|
+
def user_name
|
80
|
+
@user_name ||= user_params[0].singularize
|
81
|
+
end
|
82
|
+
|
83
|
+
def group_name
|
84
|
+
@group_name ||= group_params[0].singularize
|
85
|
+
end
|
86
|
+
|
87
|
+
def group_field_name
|
88
|
+
@group_field_name ||= (group_params[1] || "name:").sub(/\:.*/, '')
|
89
|
+
end
|
90
|
+
|
91
|
+
def plural_user_name
|
92
|
+
@plural_user_name ||= user_name.pluralize
|
93
|
+
end
|
94
|
+
|
95
|
+
def plural_group_name
|
96
|
+
@plural_group_name ||= group_name.pluralize
|
97
|
+
end
|
98
|
+
|
99
|
+
def user_class_name
|
100
|
+
@user_class_name ||= user_name.camelize
|
101
|
+
end
|
102
|
+
|
103
|
+
def group_class_name
|
104
|
+
@group_class_name ||= group_name.camelize
|
105
|
+
end
|
106
|
+
|
107
|
+
def association_name
|
108
|
+
@association_name ||= [plural_name, plural_group_name, plural_user_name].sort.join("_")
|
109
|
+
end
|
110
|
+
|
111
|
+
def group_user_name
|
112
|
+
@group_user_name ||= [plural_group_name, plural_user_name].sort.join("_")
|
113
|
+
end
|
114
|
+
|
115
|
+
def flavors
|
116
|
+
@flavors ||= params_array[2,10000].collect {|c| c[0].to_s }
|
117
|
+
end
|
118
|
+
|
119
|
+
def create_migration_file
|
120
|
+
return unless options[:migration] && options[:parent].nil?
|
121
|
+
|
122
|
+
index = 0
|
123
|
+
params_array.each do |params|
|
124
|
+
setup(params)
|
125
|
+
migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
|
126
|
+
index += 1
|
127
|
+
if(index > 2)
|
128
|
+
migration_template "flavor_migration.rb", "db/migrate/create_#{association_name}.rb"
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
migration_template "group_user_migration.rb", "db/migrate/create_#{group_user_name}.rb"
|
133
|
+
end
|
134
|
+
|
135
|
+
def create_model_file
|
136
|
+
setup(user_params)
|
137
|
+
template 'user_model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
138
|
+
|
139
|
+
setup(group_params)
|
140
|
+
template 'group_model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
141
|
+
|
142
|
+
if params_array.size > 2
|
143
|
+
params_array[2,10000].each do |params|
|
144
|
+
setup(params)
|
145
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
146
|
+
template 'flavor_model.rb', File.join('app/models', class_path, "#{association_name.singularize}.rb")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def create_module_file
|
152
|
+
params_array.each do |params|
|
153
|
+
assign_names!(params[0])
|
154
|
+
unless class_path.empty?
|
155
|
+
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
protected
|
161
|
+
|
162
|
+
def association_class_name(plural_flavor)
|
163
|
+
@association_class_name ||= {}
|
164
|
+
@association_class_name[plural_flavor] ||= [plural_flavor, plural_group_name, plural_user_name].sort.join("_").singularize.camelize
|
165
|
+
end
|
166
|
+
|
167
|
+
def setup(params)
|
168
|
+
@name = params[0]
|
169
|
+
assign_names!(params[0])
|
170
|
+
self.attributes = params[1, 10000]
|
171
|
+
parse_attributes!
|
172
|
+
@file_path = nil
|
173
|
+
@class_name = nil
|
174
|
+
@human_name = nil
|
175
|
+
@plural_name = nil
|
176
|
+
@i18n_scope = nil
|
177
|
+
@table_name = nil
|
178
|
+
@singular_table_name = nil
|
179
|
+
@plural_table_name = nil
|
180
|
+
@plural_file_name = nil
|
181
|
+
@route_url = nil
|
182
|
+
@association_name = nil
|
183
|
+
end
|
184
|
+
|
185
|
+
def params_array
|
186
|
+
@array ||= begin
|
187
|
+
result = [user_params, group_params]
|
188
|
+
index = 0
|
189
|
+
while( flavor = flavor_params(index) ) do
|
190
|
+
result << flavor if flavor.size > 0
|
191
|
+
index += 1
|
192
|
+
end
|
193
|
+
result
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def parent_class_name
|
198
|
+
options[:parent] || "ActiveRecord::Base"
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
end
|
File without changes
|