pundit_implications 0.1.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pundit_implications.rb +58 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd92e7ad9a3331774840c4ddd3c3a3939f655d5977694123f4a5f0d765b662f6
4
+ data.tar.gz: 9d290841cd689a4fd54a03c6e7cc5e1708637eb7543b14e5d24eb719109bd085
5
+ SHA512:
6
+ metadata.gz: d455f65a62cc57337a8691681701d8eb05fca3659e29fe8affd2a25ee4bd4912fbde25914d7f80fb8662a6e2e802138e64fdc00d58093910f20294673a172b51
7
+ data.tar.gz: 692d7bb6ae43ff864c7f2c73a034e4736490367ad90ad0f53d31c5332141a62e4086b5f2e1f22ac063b38b9761411a2df357f316d7c31f93086ef24fda83aca0
@@ -0,0 +1,58 @@
1
+ # A small mixin for handling permission implications for Pundit policy classes
2
+ module PunditImplications
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ # Grants additional permissions with all implications
8
+ def grant(permission)
9
+ @_granted ||= []
10
+ @_granted |= implied_permissions(permission)
11
+ end
12
+
13
+ # Grants all possible permissions
14
+ def grant_all
15
+ @_granted = all_permissions
16
+ end
17
+
18
+ # List all granted permissions
19
+ def granted_list
20
+ defined?(@_granted) ? @_granted.sort : []
21
+ end
22
+
23
+ module ClassMethods
24
+ # Defines the implication graph and constructs query functions
25
+ # for all permissions by monkey-patching
26
+ def define_implications(implications)
27
+ @_implications ||= {}
28
+ @_implications.merge!(implications) {|k,v1,v2| v1 | v2}
29
+ transitive = transitive_hull(@_implications)
30
+ transitive.each_key do |key|
31
+ define_method(key.to_s + '?') {granted_list.include?(key)}
32
+ end
33
+
34
+ define_method('implied_permissions') {|initial|
35
+ transitive.has_key?(initial) ? transitive[initial] : []}
36
+
37
+ define_method('all_permissions') {transitive.keys.sort}
38
+ end
39
+
40
+ private
41
+
42
+ # Computes a transitive hull for the implication graph
43
+ def transitive_hull(implications)
44
+ transitive = {}
45
+ keys = implications.keys | implications.values.flatten
46
+ keys.each do |key|
47
+ weaker = []
48
+ queue = [key]
49
+ until queue.empty?
50
+ weaker |= queue
51
+ queue = queue.map{|k| implications[k]}.compact.flatten - weaker
52
+ end
53
+ transitive[key] = weaker.sort
54
+ end
55
+ transitive
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pundit_implications
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tamás Korodi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ description: A small mixin for handling permission implications in Pundit
28
+ email: tamaskorodi1988@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/pundit_implications.rb
34
+ homepage: https://github.com/Naturseptime/pundit_implications
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ source_code_uri: https://github.com/Naturseptime/pundit_implications
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.13
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Permission implications for Pundit
58
+ test_files: []