permission_forge 0.15.0 → 0.15.1

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -0
  3. data/app/controllers/concerns/permission_forge/api/response_renderer.rb +5 -0
  4. data/app/controllers/permission_forge/api/audit_events_controller.rb +9 -0
  5. data/app/controllers/permission_forge/audit_events_controller.rb +11 -0
  6. data/app/controllers/permission_forge/people_controller.rb +3 -1
  7. data/app/controllers/permission_forge/permission_set_editor.rb +2 -0
  8. data/app/controllers/permission_forge/permission_sets_controller.rb +8 -43
  9. data/app/frontend/permission_forge/pf_admin.css +56 -3
  10. data/app/frontend/permission_forge/pf_admin.js +73 -0
  11. data/app/helpers/permission_forge/application_helper.rb +1 -0
  12. data/app/helpers/permission_forge/set_summaries_helper.rb +144 -0
  13. data/app/helpers/permission_forge/ui_helper.rb +4 -3
  14. data/app/views/permission_forge/audit_events/unavailable.html.erb +7 -0
  15. data/app/views/permission_forge/people/_audit_tab.html.erb +4 -0
  16. data/app/views/permission_forge/permission_sets/_audit_tab.html.erb +4 -0
  17. data/app/views/permission_forge/permission_sets/_overview_body.html.erb +58 -0
  18. data/app/views/permission_forge/permission_sets/_set_summary_cells.html.erb +53 -0
  19. data/app/views/permission_forge/permission_sets/_set_summary_links.html.erb +14 -0
  20. data/app/views/permission_forge/permission_sets/index.html.erb +20 -14
  21. data/app/views/permission_forge/permission_sets/overview.html.erb +24 -0
  22. data/app/views/permission_forge/shared/_audit_unavailable.html.erb +2 -0
  23. data/config/locales/permission_forge.en.yml +114 -0
  24. data/config/locales/permission_forge.mk.yml +117 -0
  25. data/config/locales/permission_forge.sq.yml +117 -0
  26. data/config/routes.rb +1 -0
  27. data/lib/permission_forge/admin/last_changes.rb +1 -1
  28. data/lib/permission_forge/admin/permission_set_list.rb +62 -0
  29. data/lib/permission_forge/admin/set_overview.rb +100 -0
  30. data/lib/permission_forge/admin/set_summaries.rb +174 -0
  31. data/lib/permission_forge/admin/state_label.rb +3 -1
  32. data/lib/permission_forge/api/schema.rb +4 -2
  33. data/lib/permission_forge/audit/audited_views.rb +20 -2
  34. data/lib/permission_forge/cache/rails_cache.rb +23 -6
  35. data/lib/permission_forge/concerns/masked_attributes/alias_access.rb +28 -2
  36. data/lib/permission_forge/version.rb +1 -1
  37. data/lib/permission_forge.rb +3 -0
  38. metadata +11 -1
@@ -11,6 +11,12 @@ sq:
11
11
  refuzohen sa kohë që nuk është vendosur dhe config.api_require_authorization
12
12
  është aktiv
13
13
  assignment_not_authorized: Caktimi nuk është i autorizuar
14
+ audit_unavailable_missing: Regjistri i auditimit nuk është konfiguruar sepse
15
+ tabela e tij nuk ekziston. Ekzekuto bin/rails generate permission_forge:upgrade
16
+ dhe migrimet.
17
+ audit_unavailable_off: Regjistri i auditimit nuk është i disponueshëm sepse
18
+ ngjarjet e auditimit janë çaktivizuar (audit_events = false) dhe nuk ka
19
+ tabelë auditimi.
14
20
  body_unreadable: Përmbajtja e kërkesës nuk mund të lexohet. Dërgoni JSON të
15
21
  vlefshëm me Content-Type application/json.
16
22
  boolean_invalid: duhet të jetë true ose false
@@ -1191,9 +1197,12 @@ sq:
1191
1197
  name: Emri
1192
1198
  index:
1193
1199
  columns:
1200
+ access: Qasja
1194
1201
  actions: Veprimet
1195
1202
  group: Grupimi
1203
+ narrowed: Kufizohet nga
1196
1204
  people: Personat
1205
+ resources: Burimet
1197
1206
  set: Grup lejesh
1198
1207
  status: Statusi
1199
1208
  duplicate_aria: Dublikoje %{name}
@@ -1240,6 +1249,30 @@ sq:
1240
1249
  start_from_muting: Fillo nga
1241
1250
  title: Grup i ri lejesh
1242
1251
  type: Çfarë duhet të bëjë ky grup?
1252
+ overview:
1253
+ no_family: Modele të tjera
1254
+ subtitle: Çfarë lejon ky grup për çdo burim, sipas familjes së modelit.
1255
+ title:
1256
+ groups: Familjet në %{name}
1257
+ resources: Burimet në %{name}
1258
+ views:
1259
+ groups: Familjet
1260
+ resources: Burimet
1261
+ views_label: Shfaq
1262
+ overview_body:
1263
+ empty: Ky grup nuk ka ende burime.
1264
+ groups_hint: Familjet e modeleve në këtë grup. Zgjidh njërën për të parë burimet
1265
+ e saj.
1266
+ legend: Një pikë (·) është një leje që nuk jepet.
1267
+ legend_muting: Një shkronjë e theksuar është qasje që ky grup e heq.
1268
+ none_match: Asnjë burim i këtij grupi nuk përputhet me këtë pamje.
1269
+ only_family: Po shfaqet vetëm %{family}.
1270
+ only_narrowed: Po shfaqen burimet me rregulla regjistrimesh ose fusha të maskuara.
1271
+ only_view_all: 'Po shfaqen burimet me “Shiko të gjitha”: rregullat e tyre
1272
+ të regjistrimeve anashkalohen.'
1273
+ show_all:
1274
+ one: Shfaq %{count} burimin
1275
+ other: Shfaq të %{count} burimet
1243
1276
  records_summary:
1244
1277
  ignored:
1245
1278
  one: "%{count} rregull i injoruar"
@@ -1276,6 +1309,50 @@ sq:
1276
1309
  title:
1277
1310
  one: Rishiko %{count} ndryshim
1278
1311
  other: Rishiko %{count} ndryshime
1312
+ set_summary_cells:
1313
+ every_row: Sheh çdo regjistrim
1314
+ force_masked:
1315
+ one: "•• %{count} fushë e maskuar me detyrim"
1316
+ other: "•• %{count} fusha të maskuara me detyrim"
1317
+ hidden:
1318
+ one: "⊘ %{count} fushë e fshehur"
1319
+ other: "⊘ %{count} fusha të fshehura"
1320
+ ignored:
1321
+ one: "⚠ %{count} rregull i injoruar nga “Shiko të gjitha”"
1322
+ other: "⚠ %{count} rregulla të injoruara nga “Shiko të gjitha”"
1323
+ inactive:
1324
+ one: "+ %{count} i çaktivizuar"
1325
+ other: "+ %{count} të çaktivizuar"
1326
+ masked:
1327
+ one: "•• %{count} fushë e maskuar"
1328
+ other: "•• %{count} fusha të maskuara"
1329
+ modify_all:
1330
+ one: Ndrysho të gjitha në %{count} burim
1331
+ other: Ndrysho të gjitha në %{count} burime
1332
+ of_models:
1333
+ one: nga %{count} model
1334
+ other: nga %{count} modele
1335
+ rules:
1336
+ one: "⛉ %{count} rregull regjistrimesh"
1337
+ other: "⛉ %{count} rregulla regjistrimesh"
1338
+ view_all:
1339
+ one: Shiko të gjitha në %{count} burim
1340
+ other: Shiko të gjitha në %{count} burime
1341
+ view_modify_all_every: Shiko të gjitha dhe Ndrysho të gjitha në çdo burim
1342
+ set_summary_links:
1343
+ hide_groups: Fshih familjet
1344
+ hide_resources: Fshih burimet
1345
+ includes: Përfshin %{resources}
1346
+ more:
1347
+ one: "%{count} tjetër"
1348
+ other: "%{count} të tjera"
1349
+ no_resources: Ende pa burime
1350
+ show_groups:
1351
+ one: Shfaq %{count} familje
1352
+ other: Shfaq %{count} familje
1353
+ show_resources:
1354
+ one: Shfaq %{count} burim
1355
+ other: Shfaq %{count} burime
1279
1356
  show:
1280
1357
  and_more:
1281
1358
  one: "%{count} tjetër"
@@ -1292,6 +1369,41 @@ sq:
1292
1369
  conditions: Kushtet
1293
1370
  resources: Burimet
1294
1371
  tabs_label: Seksionet e Grupit të lejeve
1372
+ summary:
1373
+ flag_count: "%{flag}: %{count} nga %{total}"
1374
+ initials:
1375
+ create_access: K
1376
+ delete_access: F
1377
+ edit_access: "N"
1378
+ modify_all: NT
1379
+ read_access: L
1380
+ view_all: ShT
1381
+ mark_titles:
1382
+ hidden:
1383
+ one: "%{count} fushë e fshehur"
1384
+ other: "%{count} fusha të fshehura"
1385
+ ignored:
1386
+ one: "%{count} rregull që “Shiko të gjitha” e anashkalon"
1387
+ other: "%{count} rregulla që “Shiko të gjitha” i anashkalon"
1388
+ masked:
1389
+ one: "%{count} fushë e maskuar"
1390
+ other: "%{count} fusha të maskuara"
1391
+ rules:
1392
+ one: "%{count} rregull regjistrimesh"
1393
+ other: "%{count} rregulla regjistrimesh"
1394
+ view_all:
1395
+ one: Shiko të gjitha në %{count} burim
1396
+ other: Shiko të gjitha në %{count} burime
1397
+ marks:
1398
+ hidden: "⊘ %{count}"
1399
+ ignored: "⛉ %{count} të injoruara"
1400
+ masked: "•• %{count}"
1401
+ rules: "⛉ %{count}"
1402
+ view_all: ShT %{count}
1403
+ no_flags: Asgjë
1404
+ on_resources:
1405
+ one: në %{count} burim
1406
+ other: në %{count} burime
1295
1407
  resource_actions:
1296
1408
  add_select:
1297
1409
  block: Shto dhe blloko
@@ -2453,6 +2565,11 @@ sq:
2453
2565
  search_people: Emër ose email
2454
2566
  setting: Cilësimi
2455
2567
  states:
2568
+ access_profile:
2569
+ full: Qasje e plotë
2570
+ mixed: Disa ndryshime
2571
+ none: Pa qasje
2572
+ takes_away: Heq qasje
2456
2573
  actor:
2457
2574
  api: Klient API
2458
2575
  basic_auth: Dritarja e fjalëkalimit
data/config/routes.rb CHANGED
@@ -53,6 +53,7 @@ PermissionForge::Engine.routes.draw do
53
53
  get :switch_mode, action: :confirm_switch_mode
54
54
  patch :switch_mode
55
55
  patch :conditions
56
+ get :overview
56
57
  end
57
58
 
58
59
  resource :bulk_assignment, only: %i[new create] do
@@ -75,7 +75,7 @@ module PermissionForge
75
75
  end
76
76
 
77
77
  def available?
78
- @records.any? && PermissionForge::Audit.enabled?
78
+ @records.any? && PermissionForge::Audit.enabled? && PermissionForge::Admin::AuditLog.table?
79
79
  rescue StandardError
80
80
  false
81
81
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PermissionForge
4
+ module Admin
5
+ # The Permission Sets list: the sets a filter and a search select, how
6
+ # many sets each filter has, and what the list says about each set
7
+ # (SetSummaries) and who holds it. The search matches a set's name, its
8
+ # description, or the stored name of one of its active Resources.
9
+ class PermissionSetList
10
+ FILTERS = %w[all granting muting conditional off].freeze
11
+
12
+ # Filter => predicate on a set ('all' matches everything).
13
+ MATCHERS = {
14
+ 'all' => ->(_set) { true },
15
+ 'granting' => ->(set) { !set.is_muting },
16
+ 'muting' => lambda(&:is_muting),
17
+ 'conditional' => lambda(&:conditional?),
18
+ 'off' => ->(set) { !set.is_active }
19
+ }.freeze
20
+
21
+ attr_reader :query, :filter
22
+
23
+ # @param query [String] the search, as typed
24
+ # @param filter [String] one of FILTERS (anything else: 'all')
25
+ def initialize(query:, filter:)
26
+ @query = query.to_s
27
+ @filter = FILTERS.include?(filter) ? filter : 'all'
28
+ @all = PermissionForge::PermissionSet.includes(:permission_set_group).order(:name).to_a
29
+ @summaries = SetSummaries.call(@all)
30
+ end
31
+
32
+ # @return [Array<PermissionSet>] the sets the filter and the search select
33
+ def sets = @all.select { |set| MATCHERS.fetch(filter).call(set) && found?(set) }
34
+
35
+ def total = @all.size
36
+ def counts = FILTERS.index_with { |key| @all.count(&MATCHERS.fetch(key)) }
37
+
38
+ # @return [SetSummaries::Summary]
39
+ def summary(set) = @summaries.fetch(set.id)
40
+
41
+ # The set's active Resources whose name the search matches (none without a search).
42
+ def matching(set) = query.empty? ? [] : summary(set).matching(needle)
43
+
44
+ # @return [Hash{Integer => Integer}] people holding each set (active assignments)
45
+ def people_counts
46
+ @people_counts ||= PermissionForge::PermissionSetUser.active_assignments.group(:permission_set_id).count
47
+ end
48
+
49
+ # @return [Integer] the application's models (the bar of a set's Resources)
50
+ def model_count = @model_count ||= ModelCatalog.new(verify: false).models.size
51
+
52
+ private
53
+
54
+ def needle = query.downcase
55
+
56
+ def found?(set)
57
+ query.empty? || [set.name, set.description].compact.any? { |text| text.downcase.include?(needle) } ||
58
+ matching(set).any?
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PermissionForge
4
+ module Admin
5
+ # One Permission Set's Resources as the Permission Sets list opens them
6
+ # in place: by model family (`groups`) or one by one (`resources`), all
7
+ # of them or `only` the ones narrowed (a row rule that applies, a masked
8
+ # field), the ones with View All, or one family. The entries and their
9
+ # counts are SetSummaries' (one definition of what a row grants).
10
+ #
11
+ # A family is a model's namespace (`Billing::Invoice` → `Billing`); a
12
+ # model outside any namespace goes with the namespace that is its name
13
+ # plus a plural ending (`Invoice` → `Invoices::`), else with the models
14
+ # of no family. A family is a grouping for reading, never a decision.
15
+ class SetOverview
16
+ VIEWS = %w[groups resources].freeze
17
+ NARROWED = 'narrowed'
18
+ VIEW_ALL = 'view_all'
19
+ # `only` for the models of no family (no namespace is named `-`).
20
+ NO_FAMILY = '-'
21
+ # The entries `only` keeps, by its value; a family's key keeps that family.
22
+ FILTERS = { nil => ->(_entry) { true }, NARROWED => :narrowed?.to_proc, VIEW_ALL => :view_all?.to_proc }.freeze
23
+ # The endings a namespace may add to a model's name to be its family.
24
+ PLURAL_ENDINGS = %w[s es].freeze
25
+
26
+ # A family of the set's Resources; `name` nil for the models of no family.
27
+ Family = Data.define(:name, :entries) do
28
+ # The value of `only` that shows this family.
29
+ def key = name || NO_FAMILY
30
+ def sort_key = [name.nil? ? 1 : 0, -entries.size, name.to_s]
31
+
32
+ # The family with only the entries `only` keeps, or nil when none is left.
33
+ def kept(only)
34
+ filter = FILTERS.fetch(only) { ->(_entry) { key == only } }
35
+ kept = entries.select(&filter)
36
+ with(entries: kept) if kept.any?
37
+ end
38
+
39
+ # { kind => count } of the marks its entries carry, View All first
40
+ # (a muting set's View All is a flag it takes away, not a mark).
41
+ def marks
42
+ totals = SetSummaries::MARKS.index_with { |kind| entries.sum { |entry| entry.marks.fetch(kind) } }
43
+ view_all = entries.any?(&:muting) ? 0 : entries.count(&:view_all?)
44
+ { 'view_all' => view_all }.merge(totals)
45
+ end
46
+ end
47
+
48
+ # @param entries [Array<SetSummaries::Entry>]
49
+ # @return [Array<Family>] largest first, the models of no family last
50
+ def self.families(entries)
51
+ namespaces = entries.filter_map { |entry| namespace(entry.name) }.uniq
52
+ grouped = entries.group_by { |entry| family_of(entry.name, namespaces) }
53
+ grouped.map { |name, members| Family.new(name: name, entries: members.sort_by(&:name)) }.sort_by(&:sort_key)
54
+ end
55
+
56
+ def self.namespace(name)
57
+ head, rest = name.split('::', 2)
58
+ head if rest
59
+ end
60
+
61
+ def self.family_of(name, namespaces)
62
+ namespace(name) || namespaces.find { |candidate| own_namespace?(name, candidate) }
63
+ end
64
+
65
+ # `Invoice` and `Invoices`, `Address` and `Addresses`, `Category` and
66
+ # `Categories`, compared without case (`RFQ` and `Rfqs`).
67
+ def self.own_namespace?(name, namespace)
68
+ model = name.downcase
69
+ candidates = PLURAL_ENDINGS.map { |ending| model + ending }
70
+ candidates << "#{model.delete_suffix('y')}ies" if model.end_with?('y')
71
+ candidates.include?(namespace.downcase)
72
+ end
73
+ private_class_method :namespace, :family_of, :own_namespace?
74
+
75
+ attr_reader :permission_set, :view, :only
76
+
77
+ # @param permission_set [PermissionSet]
78
+ # @param view [String] `groups` or `resources` (anything else: `resources`)
79
+ # @param only [String] `narrowed`, `view_all`, a family's key, or blank
80
+ def initialize(permission_set, view:, only:)
81
+ @permission_set = permission_set
82
+ @view = VIEWS.include?(view) ? view : 'resources'
83
+ @all = self.class.families(SetSummaries.entries([permission_set]))
84
+ @only = only if FILTERS.key?(only) || @all.any? { |family| family.key == only }
85
+ end
86
+
87
+ def muting? = permission_set.is_muting == true
88
+ def groups? = view == 'groups'
89
+
90
+ # @return [Array<Family>] the families shown (every family in `groups`)
91
+ def families
92
+ groups? ? @all : @all.filter_map { |family| family.kept(only) }
93
+ end
94
+
95
+ # @return [Integer] every Resource of the set
96
+ def total = @all.sum { |family| family.entries.size }
97
+ def filtered? = !only.nil? && !groups?
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/enumerable'
4
+
5
+ module PermissionForge
6
+ module Admin
7
+ # What the Permission Sets list says about each set without building its
8
+ # Resource records: how many Resources it has, what they allow (each flag
9
+ # counted) and what narrows them (row rules, masked fields). Read from
10
+ # tuples in three queries, whatever the number of sets or rows. Every count
11
+ # asks the one definition of what it counts, on a tuple shaped like the
12
+ # row: PermissionHashBuilder.granted_flags for what a granting Resource
13
+ # row grants (edit and delete only with read, Modify All only with View
14
+ # All), FieldLevel.level_of for a field's level. Rules on a Resource that
15
+ # View All bypasses are counted apart, as the Resources tab does
16
+ # ("rules ignored"). A Muting Permission Set adds no rules
17
+ # (PermissionHashBuilder): its counts are what it takes away.
18
+ # SetOverview reads the same entries for one set.
19
+ class SetSummaries
20
+ FLAGS = %i[read_access create_access edit_access delete_access view_all modify_all].freeze
21
+ CRUD = FLAGS.first(4).freeze
22
+ COLUMNS = (%i[id permission_set_id name is_active] + FLAGS).freeze
23
+ # The kinds of mark a Resource carries, in the order they are shown.
24
+ MARKS = %w[rules ignored masked hidden].freeze
25
+
26
+ # The flags of a Resource row, for PermissionHashBuilder.granted_flags.
27
+ FlagRow = Struct.new(*FLAGS, keyword_init: true)
28
+ # The flags of a field row, for FieldLevel.level_of.
29
+ FieldRow = Struct.new(:read_access, :edit_access, :mask_access, keyword_init: true)
30
+
31
+ # One Resource row of a set. `flags`: what a granting row grants
32
+ # (granted_flags) or what a muting row takes away (as stored); `rules`:
33
+ # its active row rules (none on a muting set); `masked`: its active
34
+ # fields shown masked (granting) or force-masked (muting); `hidden`: the
35
+ # fields a muting row hides.
36
+ Entry = Data.define(:id, :set_id, :name, :active, :muting, :flags, :rules, :masked, :hidden) do
37
+ # Rules View All bypasses (the Resources tab: "rules ignored").
38
+ def ignored_rules = view_all? ? rules : 0
39
+ def applied_rules = rules - ignored_rules
40
+ # A turned-off Resource grants nothing (PermissionHashBuilder skips it).
41
+ def view_all? = active && flags[:view_all] == true
42
+
43
+ # It narrows what the set grants on this Resource (a rule that applies,
44
+ # a masked field) or, on a muting set, takes fields away.
45
+ def narrowed? = (applied_rules + masked + hidden).positive?
46
+
47
+ # An active Resource whose stored name contains `needle` (downcased).
48
+ def matches?(needle) = active && name.downcase.include?(needle)
49
+
50
+ # { kind => count } of the marks it carries (MARKS).
51
+ def marks = MARKS.zip([applied_rules, ignored_rules, masked, hidden]).to_h
52
+ end
53
+
54
+ # A set's counts over its active Resources (`inactive` counts the others).
55
+ Summary = Data.define(:muting, :entries) do
56
+ def active = entries.select(&:active)
57
+ def resources = active.size
58
+ def inactive = entries.size - resources
59
+ def flags = FLAGS.index_with { |flag| active.count { |entry| entry.flags[flag] } }
60
+ def rules = total(:applied_rules)
61
+ def rule_resources = on(:applied_rules)
62
+ def ignored_rules = total(:ignored_rules)
63
+ def masked = total(:masked)
64
+ def masked_resources = on(:masked)
65
+ def hidden = total(:hidden)
66
+ def hidden_resources = on(:hidden)
67
+
68
+ # How much the set allows, in one state (Admin::StateLabel kind
69
+ # :access_profile): `takes_away` for a muting set with Resources,
70
+ # `none` when nothing is granted, `full` when every Resource allows
71
+ # Read, Create, Edit and Delete, `read_only` when none allows more
72
+ # than Read, else `mixed`.
73
+ def profile
74
+ return 'none' if resources.zero?
75
+ return 'takes_away' if muting
76
+
77
+ counts = flags.values_at(*CRUD)
78
+ return 'full' if counts.all?(resources)
79
+
80
+ changes = counts.drop(1).sum
81
+ return 'mixed' if changes.positive?
82
+
83
+ counts.first.positive? ? 'read_only' : 'none'
84
+ end
85
+
86
+ # Every active Resource bypasses its rules: the set sees every row.
87
+ def every_row? = !muting && resources.positive? && flags[:view_all] == resources
88
+
89
+ def groups = SetOverview.families(entries).size
90
+
91
+ # The active Resources whose stored name contains `needle` (downcased).
92
+ def matching(needle) = entries.select { |entry| entry.matches?(needle) }
93
+
94
+ private
95
+
96
+ def total(count) = active.sum(&count)
97
+ def on(count) = active.count { |entry| entry.public_send(count).positive? }
98
+ end
99
+
100
+ # Builds the entries of one read: the rules and field levels of the
101
+ # active Resources, and which sets are muting.
102
+ Reader = Struct.new(:muting, :rules, :levels) do
103
+ def entry(row)
104
+ id, set_id, name, active, *values = row
105
+ muting_set = muting.fetch(set_id, false)
106
+ levels_of = levels.fetch(id, {})
107
+ Entry.new(id: id, set_id: set_id, name: name, active: active == true, muting: muting_set,
108
+ flags: flags(values, muting_set), rules: muting_set ? 0 : rules.fetch(id, 0),
109
+ masked: levels_of.fetch(muting_set ? 'mask' : 'masked', 0),
110
+ hidden: muting_set ? levels_of.fetch('hide', 0) : 0)
111
+ end
112
+
113
+ private
114
+
115
+ def flags(values, muting_set)
116
+ stored = FLAGS.zip(values).to_h { |flag, value| [flag, value == true] }
117
+ muting_set ? stored : PermissionForge::Services::PermissionHashBuilder.granted_flags(FlagRow.new(**stored))
118
+ end
119
+ end
120
+
121
+ # @param sets [Enumerable<PermissionSet>]
122
+ # @return [Hash{Integer => Summary}] by set id, one for every set given
123
+ def self.call(sets)
124
+ sets = sets.to_a
125
+ by_set = entries(sets).group_by(&:set_id)
126
+ sets.to_h { |set| [set.id, Summary.new(muting: set.is_muting == true, entries: by_set.fetch(set.id, []))] }
127
+ end
128
+
129
+ # @param sets [Enumerable<PermissionSet>]
130
+ # @return [Array<Entry>] every Resource row of the sets, in name order
131
+ def self.entries(sets)
132
+ muting = sets.to_h { |set| [set.id, set.is_muting == true] }
133
+ rows = resource_rows(muting.keys)
134
+ active_ids = rows.filter_map { |row| row.first if row[3] == true }
135
+ reader = Reader.new(muting, rule_counts(active_ids), field_levels(active_ids))
136
+ rows.map { |row| reader.entry(row) }
137
+ end
138
+
139
+ def self.resource_rows(set_ids)
140
+ PermissionForge::Resource.where(permission_set_id: set_ids).order(:name).pluck(*COLUMNS)
141
+ end
142
+ private_class_method :resource_rows
143
+
144
+ # Active rules per Resource id.
145
+ def self.rule_counts(resource_ids)
146
+ return {} if resource_ids.empty?
147
+
148
+ PermissionForge::PermissionSetRule.where(resource_id: resource_ids, is_active: true).group(:resource_id).count
149
+ end
150
+ private_class_method :rule_counts
151
+
152
+ # Active fields per Resource id and level ({ id => { level => count } }),
153
+ # each flag combination's level read by FieldLevel.level_of.
154
+ def self.field_levels(resource_ids)
155
+ return {} if resource_ids.empty?
156
+
157
+ counts = PermissionForge::ResourceField.where(resource_id: resource_ids, is_active: true)
158
+ .group(:resource_id, :read_access, :edit_access, :mask_access).count
159
+ counts.each_with_object(Hash.new { |hash, key| hash[key] = Hash.new(0) }) do |((id, *values), count), levels|
160
+ levels_for(values).each { |level| levels[id][level] += count }
161
+ end
162
+ end
163
+ private_class_method :field_levels
164
+
165
+ # The levels a field with these flags has: on a granting set, and on a muting one.
166
+ def self.levels_for(values)
167
+ read, edit, mask = values.map { |value| value == true }
168
+ row = FieldRow.new(read_access: read, edit_access: edit, mask_access: mask)
169
+ [PermissionForge::FieldLevel.level_of(row, muting: false), PermissionForge::FieldLevel.level_of(row, muting: true)]
170
+ end
171
+ private_class_method :levels_for
172
+ end
173
+ end
174
+ end
@@ -33,6 +33,7 @@ module PermissionForge
33
33
  crud: { 'allowed' => '✓', 'no' => '–', 'muted' => '⊘', 'unchanged' => '–', 'needs_read' => '–' },
34
34
  action: { 'permitted' => '✓', 'not_permitted' => '–', 'blocked' => '⊘', 'unchanged' => '–', 'none' => '–' },
35
35
  lock: { 'locked' => nil },
36
+ access_profile: { 'full' => '●', 'read_only' => '◉', 'mixed' => '◐', 'none' => '–', 'takes_away' => '⊘' },
36
37
  actor: { 'api' => nil, 'basic_auth' => nil, 'system' => nil, 'unknown' => nil, 'user' => nil }
37
38
  }.freeze
38
39
 
@@ -43,7 +44,8 @@ module PermissionForge
43
44
  KEY_NAMES = { [:crud, 'needs_read'] => 'needs_read_short' }.freeze
44
45
  # A state worded by another kind's key: an action no set configures is
45
46
  # "Not configured", the field level's word.
46
- SHARED = { [:action, 'none'] => [:field_level, 'none'] }.freeze
47
+ # A set that allows only Read is "Read-only", the field level's word.
48
+ SHARED = { [:action, 'none'] => [:field_level, 'none'], [:access_profile, 'read_only'] => [:field_level, 'read'] }.freeze
47
49
 
48
50
  # One state: its glyph (nil when the kind has none) and its word.
49
51
  Label = Data.define(:kind, :state, :glyph, :word) do
@@ -146,8 +146,10 @@ module PermissionForge
146
146
  }.freeze
147
147
 
148
148
  # What `messages` is under each error status. A 422 maps fields to
149
- # their messages; the others carry one sentence.
150
- ERRORS = { 400 => :string, 401 => :string, 403 => :string, 404 => :string, 422 => :field_messages }.freeze
149
+ # their messages; the others carry one sentence. 503 is the audit
150
+ # events endpoints while the audit table does not exist.
151
+ ERRORS = { 400 => :string, 401 => :string, 403 => :string, 404 => :string, 422 => :field_messages,
152
+ 503 => :string }.freeze
151
153
 
152
154
  # The answers without a body, and when: a delete (204), the 401 while
153
155
  # config.api_auth is not set, the 404 while config.api_enabled is off
@@ -31,6 +31,10 @@ module PermissionForge
31
31
  end
32
32
  end
33
33
 
34
+ # The event of a screen that shows events of the log; without the audit
35
+ # table such a screen shows a notice instead (AuditedViews.record).
36
+ LOG_VIEW = 'audit.viewed'
37
+
34
38
  @entries = {}
35
39
 
36
40
  class << self
@@ -72,14 +76,28 @@ module PermissionForge
72
76
  end
73
77
 
74
78
  # Records the view when the response succeeded and an entry applies.
75
- # A HEAD request shows nothing and is not recorded.
79
+ # A HEAD request shows nothing and is not recorded, nor is a view of
80
+ # the log (LOG_VIEW) while the audit table does not exist: the screen
81
+ # showed a notice, no event, and with audit_events = true recording
82
+ # it would turn that notice into a 500. Everything else (an export,
83
+ # a view of someone's effective access) showed its data and is
84
+ # recorded as ever, so under audit_events = true it still fails
85
+ # rather than go through unrecorded.
76
86
  #
77
87
  # @return [AuditEvent, nil]
78
88
  def record(controller)
79
89
  return if controller.request.head? || !controller.response.successful?
80
90
 
81
91
  view = match(controller)
82
- Audit.record(view.event, **view.payload_for(controller)) if view
92
+ return if view.nil? || log_unread?(view)
93
+
94
+ Audit.record(view.event, **view.payload_for(controller))
95
+ end
96
+
97
+ private
98
+
99
+ def log_unread?(view)
100
+ view.event == LOG_VIEW && !PermissionForge::DatabaseReach.table_exists?(PermissionForge::AuditEvent)
83
101
  end
84
102
  end
85
103
 
@@ -22,13 +22,30 @@ module PermissionForge
22
22
  cache_store.read_multi(*keys.map { |key| prefixed_key(key) }).transform_values { |value| deep_symbolize(value) }
23
23
  end
24
24
 
25
+ # Classes and modules whose #write_multi_entries is no batch whose
26
+ # answer says the entries were written, by name (none is loaded for the
27
+ # asking). ActiveSupport's LocalCache strategy (Rails 8.2) only forgets
28
+ # the request's copies and hands the entries on. Rails 8.2's Memcached
29
+ # store does send one batch, but answers nil whether it was written or
30
+ # not and hands its errors to no handler: it is written key by key, as
31
+ # on earlier versions, where each #write answers.
32
+ UNJUDGED_BATCHES = %w[
33
+ ActiveSupport::Cache::Strategy::LocalCache
34
+ ActiveSupport::Cache::MemCacheStore
35
+ ].freeze
36
+
25
37
  # Whether the store writes many entries in one call of its own, rather
26
- # than ActiveSupport's default loop (which ignores each write's answer).
27
- # Anything that is not an ActiveSupport store (a wrapper) is written key
28
- # by key through its #write.
29
- def self.multi_write?(store)
30
- store.is_a?(ActiveSupport::Cache::Store) &&
31
- store.class.instance_method(:write_multi_entries).owner != ActiveSupport::Cache::Store
38
+ # than ActiveSupport's default loop (which ignores each write's answer):
39
+ # the first #write_multi_entries along its ancestors that is not one of
40
+ # `unjudged` is not ActiveSupport's own. Anything that is not an
41
+ # ActiveSupport store (a wrapper) is written key by key through its
42
+ # #write.
43
+ def self.multi_write?(store, unjudged: UNJUDGED_BATCHES)
44
+ return false unless store.is_a?(ActiveSupport::Cache::Store)
45
+
46
+ writer = store.class.instance_method(:write_multi_entries)
47
+ writer = writer.super_method while writer && unjudged.include?(writer.owner.name)
48
+ !writer.nil? && writer.owner != ActiveSupport::Cache::Store
32
49
  end
33
50
 
34
51
  def initialize(prefix: 'pf', store: nil)