mikras_utils 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09748841bbccd2fc59e03f01fd240ac333963e7f6e8f35ed68d3d8268fdb4a97'
4
- data.tar.gz: b058155721495e702725d5cf1a68d37139e3c8abe82089939e3c018ac5b3e7b3
3
+ metadata.gz: 4b53fcf02009d498760ed46a4e2e01fb5418ed6a8ee19b37a66504d5c524b209
4
+ data.tar.gz: b4361d6d864f9582fe93462b9d8b2fac2d846e96ac04d5a18777dce4d16bddfb
5
5
  SHA512:
6
- metadata.gz: b25c043d64872f36c9b8d92e0f86d314164ef19695273255030d685e42497d0f7497395cf66718a41bf65e2ee6d4a4c1fad7db1b9202358ca6180e30df88eb76
7
- data.tar.gz: b66d7b04e85ff125b6f8b5dc143bdb7893db08686c036899ca37ac0aa6aa7dd58edda99b976962266ebae4e00343fd80d1e66aa9c719846bf327dedc42c89888
6
+ metadata.gz: 414f6394b7c34992986a859875d69c14931ac913af2739a80759157effb38e20a55ee2addc815d024210908c15cddbf41669ab87ba474c527eada2aa92695458
7
+ data.tar.gz: 8ef782e64378a14de544d0099534d952963a223573bfb833e710acf673b6d3058d4928cd6c2f3c00805371bf163c85a3fbe3c41a5ce83e589170dc9e14f2e8d1
data/exe/mkacl CHANGED
@@ -79,10 +79,12 @@ end
79
79
 
80
80
  spec = MkAcl::Parser.parse(file)
81
81
  MkAcl::Analyzer.analyze(spec, conn, warn: !opts.no_warn?)
82
+
82
83
  if opts.dump?
83
84
  spec.dump
84
85
  exit
85
86
  end
87
+
86
88
  MkAcl::Generator.generate(spec, conn, modules, interactive: opts.interactive?)
87
89
 
88
90
 
@@ -26,11 +26,15 @@ module MkAcl
26
26
  delete from acl_portal.acl_actions;
27
27
  delete from acl_portal.acl_tables;
28
28
  ).align
29
+ puts
29
30
  end
30
31
 
31
32
  def generate_seeds
32
33
  for table in spec.tables
33
34
  puts %(
35
+ --
36
+ -- #{table.name.gsub("_", " ").upcase}
37
+ --
34
38
  insert into acl_portal.acl_tables (
35
39
  schema_name, table_name, record_name, domain,
36
40
  parent_schema_name, parent_table_name, parent_link_field,
@@ -50,7 +54,8 @@ module MkAcl
50
54
 
51
55
  table.actions.values.each { |action|
52
56
  puts %(
53
- insert into acl_portal.acl_actions (table_id, kind)
57
+ -- #{action.name.upcase}
58
+ insert into acl_portal.acl_actions (acl_table_id, kind)
54
59
  values (:table_id, '#{action.name.upcase}')
55
60
  returning id as "action_id"
56
61
  \\gset
@@ -63,26 +68,32 @@ module MkAcl
63
68
  puts %(
64
69
  insert into acl_portal.acl_rules (
65
70
  acl_action_id, roles, filter, assert, function_name, fields, tables, ordinal)
66
- values (:action_id, #{values.join(', ')});
71
+ values (:action_id, #{values.join(', ')})
72
+ returning id as "rule_id"
73
+ \\gset
67
74
  ).align
68
75
  puts
69
- }
70
76
 
71
- action.stamps.each { |stamp|
72
- puts %(
73
- insert into acl_portal.acl_stamps (action_id, watch, stamp)
74
- values (
75
- :action_id,
76
- #{conn.quote_value(stamp.watch)},
77
- #{conn.quote_value(stamp.stamp)}
78
- );
79
- ).align
80
- puts
77
+ rule.stamps.each { |stamp|
78
+ puts %(
79
+ insert into acl_portal.acl_stamps (acl_rule_id, watch, stamp, value)
80
+ values (
81
+ :rule_id,
82
+ #{conn.quote_value(stamp.watch)},
83
+ #{conn.quote_value(stamp.stamp)},
84
+ #{conn.quote_value(stamp.value)}
85
+ );
86
+ ).align
87
+ }
81
88
  }
82
89
  }
83
90
  end
84
91
 
92
+ # Patch-up acl_tables
85
93
  puts %(
94
+ --
95
+ -- ASSIGN TABLE PARENT ID
96
+ --
86
97
  update acl_portal.acl_tables sub
87
98
  set parent_id = (
88
99
  select id
@@ -92,6 +103,33 @@ module MkAcl
92
103
  );
93
104
  ).align
94
105
  puts
106
+
107
+ # Patch-up acl_stamps
108
+ puts %(
109
+ --
110
+ -- ASSIGN STAMP TYPE AND VALUE
111
+ --
112
+ update acl_portal.acl_stamps s
113
+ set type = c.column_type,
114
+ value = coalesce(
115
+ s.value,
116
+ case c.column_type
117
+ when 'int' then 'public.current_role_id()'
118
+ when 'time' then 'now()'
119
+ end
120
+ )
121
+ from
122
+ acl_portal.acl_rules r
123
+ join acl_portal.acl_actions a on a.id = r.acl_action_id
124
+ join acl_portal.acl_tables t on t.id = a.acl_table_id
125
+ join meta.columns c
126
+ on c.schema_name = t.schema_name
127
+ and c.table_name = t.table_name
128
+ where
129
+ r.id = s.acl_rule_id
130
+ and c.column_name = s.stamp
131
+ ;
132
+ ).align
95
133
  end
96
134
  end
97
135
  end
@@ -103,12 +103,9 @@ module MkAcl
103
103
  def parse_stamps(rule, stamps)
104
104
  constrain rule, Rule
105
105
  constrain stamps, String, Hash, Array
106
- # constrain stamps, String, Hash[watch: NilClass | String | [String], stamp: String | [String]]
107
106
 
108
107
  # Normalize stamps
109
- stamps = Array(stamps)
110
-
111
- stamps.map! { |stamp|
108
+ stamps = Array(stamps).map! { |stamp|
112
109
  case stamp
113
110
  when Hash; stamp
114
111
  when String; { stamp: stamp }
@@ -120,32 +117,16 @@ module MkAcl
120
117
  # Check fields and create stamp
121
118
  stamps.each { |stamp|
122
119
  # Check fields
123
- diff = stamp.keys - [:watch, :stamp]
120
+ diff = stamp.keys - [:watch, :stamp, :value]
124
121
  diff.empty? or raise "Illegal field '#{diff.first}' in #{rule.name} rule in table #{rule.table.name}"
125
- Stamp.new(rule, stamp[:watch]&.split, stamp[:stamp].split)
122
+ (stamp[:watch]&.split || [nil]).map { |watch_field|
123
+ stamp[:stamp].split.map { |stamp_field|
124
+ Stamp.new(rule, watch_field, stamp_field, stamp[:value])
125
+ }
126
+ }
127
+ StampExpr.new(rule, stamp[:watch]&.split, stamp[:stamp].split, stamp[:value])
126
128
  }
127
129
  end
128
130
  end
129
131
  end
130
132
 
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
@@ -164,22 +164,22 @@ module MkAcl
164
164
 
165
165
  def to_s() name end
166
166
 
167
- def dump(header)
168
- header = "#{header || name}:"
167
+ def dump_body
169
168
  if rules.size == 1
170
- puts header
171
- indent { rules.first.dump }
169
+ rules.first.dump
172
170
  elsif rules.size > 1
173
- puts header
174
- indent {
175
- for rule in rules.sort_by(&:ordinal)
176
- print "- "
177
- indent(bol: false) { rule.dump }
178
- end
179
- }
171
+ for rule in rules.sort_by(&:ordinal)
172
+ print "- "
173
+ indent(bol: false) { rule.dump }
174
+ end
180
175
  end
181
176
  end
182
177
 
178
+ def dump(header = name)
179
+ puts "#{header || name}:"
180
+ indent { dump_body }
181
+ end
182
+
183
183
  private
184
184
  def attach_rule(rule)
185
185
  @rules << rule
@@ -198,6 +198,7 @@ module MkAcl
198
198
  attr_accessor :fields # Only used for insert and update, nil otherwise
199
199
  attr_accessor :tables # Only used for attach and detach, nil otherwise
200
200
  attr_accessor :stamps
201
+ attr_accessor :stamp_exprs
201
202
  attr_reader :ordinal
202
203
 
203
204
  # admin, internal, etc.
@@ -216,6 +217,7 @@ module MkAcl
216
217
  @fields = %w(insert update).include?(action.name) ? [] : nil
217
218
  @tables = %w(attach detach).include?(action.name) ? [] : nil
218
219
  @stamps = []
220
+ @stamp_exprs = []
219
221
 
220
222
  action.send :attach_rule, self
221
223
  end
@@ -228,47 +230,80 @@ module MkAcl
228
230
  puts "fields: #{fields.join(' ')}" if fields && !fields.empty?
229
231
  puts "tables: #{tables.join(' ')}" if tables && !tables.empty?
230
232
 
231
- if stamps.size == 1
232
- stamps.first.dump
233
- elsif stamps.size > 1
233
+ if stamp_exprs.size == 1
234
+ stamp_exprs.first.dump
235
+ elsif stamp_exprs.size > 1
234
236
  puts "stamps:"
235
237
  indent {
236
- for stamp in stamps
238
+ for stamp_expr in stamp_exprs
237
239
  print "- "
238
- indent(bol: false) { stamp.dump }
240
+ indent(bol: false) { stamp_expr.dump }
239
241
  end
240
242
  }
241
243
  end
242
244
 
243
245
  puts "ordinal: #{ordinal}"
244
246
  end
247
+
245
248
  private
246
249
  def attach_stamp(stamp)
247
250
  @stamps << stamp
248
251
  end
252
+
253
+ def attach_stamp_expr(stamp_expr)
254
+ @stamp_exprs << stamp_expr
255
+ end
249
256
  end
250
257
 
251
258
  class Stamp
252
259
  attr_reader :rule
260
+
253
261
  forward_to :rule, :table, :name
254
262
 
255
263
  attr_accessor :watch
256
264
  attr_accessor :stamp
265
+ attr_accessor :value
257
266
 
258
- def initialize(rule, watch = nil, stamp)
267
+ def initialize(rule, watch, stamp, value)
259
268
  constrain rule, Rule
269
+ constrain watch, String, nil
270
+ constrain stamp, String
260
271
  @rule = rule
261
- @watch, @stamp = watch, stamp
272
+ @watch, @stamp, @value = watch, stamp, value
262
273
  rule.send :attach_stamp, self
263
274
  end
264
275
 
276
+ def to_s() = watch ? "#{watch}->#{stamp}" : stamp
277
+
278
+ def dump
279
+ puts "watch: #{watch}" if watch
280
+ puts "stamp: #{stamp}" if stamp
281
+ puts "value: #{value}" if value
282
+ end
283
+ end
284
+
285
+ class StampExpr # For debug. Express the structure of stamp syntax
286
+ attr_reader :rule
287
+ forward_to :rule, :table, :name
288
+
289
+ attr_accessor :watch # [String]
290
+ attr_accessor :stamp # [String]
291
+ attr_accessor :value
292
+
293
+ def initialize(rule, watch, stamp, value)
294
+ constrain rule, Rule
295
+ @rule = rule
296
+ @watch, @stamp, @value = watch, stamp, value
297
+ rule.send :attach_stamp_expr, self
298
+ end
299
+
265
300
  def to_s() = watch
266
301
 
267
302
  def dump
268
303
  puts "watch: #{watch.join(" ")}" if watch
269
304
  puts "stamp: #{stamp.join(" ")}" if stamp
305
+ puts "value: #{value}" if value
270
306
  end
271
307
  end
272
-
273
308
  end
274
309
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MikrasUtils
4
- VERSION = "0.14.0"
4
+ VERSION = "0.15.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mikras_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-21 00:00:00.000000000 Z
11
+ date: 2025-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg_conn