cancancan_js 0.0.2 → 1.0.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 +4 -4
- data/README.md +50 -3
- data/cancancan_js.gemspec +1 -1
- data/lib/cancan/ability/front_end_rules_extensions.rb +13 -0
- data/lib/cancan/ability/rules.rb +7 -0
- data/lib/cancancan_js/configuration.rb +13 -0
- data/lib/cancancan_js/export.rb +30 -3
- data/lib/cancancan_js.rb +19 -16
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41ca52e7ad7d0737fd146a795b7894f75043de5b1f4da76ef612e016822a1933
|
4
|
+
data.tar.gz: 61db88d3cc3d0bb7084d63c6b4a41cd85bbfca5f695ca4002f73132cca774b35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96a5681e3aa2da3587f7018ffa34e283922237f7e5191237d9e99ac602baaa4697f552a5861df6b041c05ebf0caa898dd0342d7ada0d970ebab4f2ede6044805
|
7
|
+
data.tar.gz: 6e9d196359bfaa8cfb350eddacffd037101dd22bd21e2c03f4069716aa1af26f42a75e99ff45dfbb9d3f3a4e25d5ef4ba30775b6673d60ad5d954b31c3f057c6
|
data/README.md
CHANGED
@@ -10,7 +10,18 @@ Depending on your implementation and rule-setup, you may not want to do this.
|
|
10
10
|
If you're using sensitive data as rule-conditions in your Ability#initialize, then you should NOT use this gem!
|
11
11
|
|
12
12
|
# Config
|
13
|
-
`
|
13
|
+
create init file: `config/initializers/cancancan_js.rb`
|
14
|
+
and populate it with the following:
|
15
|
+
```
|
16
|
+
require "cancancan_js"
|
17
|
+
|
18
|
+
# default values shown
|
19
|
+
CanCanCanJs.configure do |config|
|
20
|
+
# Option to export ALL rules (SQL-backed) to the front-end
|
21
|
+
# - false by default
|
22
|
+
config.export_all_back_end_rules = false
|
23
|
+
end
|
24
|
+
```
|
14
25
|
|
15
26
|
Add this to your class Ability:
|
16
27
|
`include CanCanCanJs::Export`
|
@@ -19,16 +30,52 @@ Add to your javascript application.js file:
|
|
19
30
|
`//= require cancancan_js`
|
20
31
|
|
21
32
|
# Implementation
|
33
|
+
## Ability class
|
34
|
+
You can either set the CanCanCanJs.configuration.export_all_back_end_rules config to true
|
35
|
+
Or use the `front_end` block we've added to Ability. Both rules are active, but only the `:read, Account` is exported to the front-end.
|
36
|
+
### ex:
|
37
|
+
```
|
38
|
+
class Ability
|
39
|
+
include CanCan::Ability
|
40
|
+
include CanCanCanJs::Export
|
41
|
+
def initialize(user = nil)
|
42
|
+
# not front-end visible
|
43
|
+
can :read, User
|
44
|
+
|
45
|
+
# front-end visible
|
46
|
+
front_end do
|
47
|
+
can :read, Account
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
22
53
|
## Back-end
|
23
54
|
We need to export the Ability rules to your front-end from your back-end. There are several ways to do this.
|
24
55
|
- Add a new method to your user model
|
25
56
|
- Add that method to a JSON serializer, export that user to the front-end
|
26
|
-
- Create a new action/route on your users controller.
|
57
|
+
- Create a new action/route on your users controller, application controller, or wherever.
|
27
58
|
- Use that action to render the following JSON data: `current_ability.export` or `Ability.export(current_user)`
|
59
|
+
### ex:
|
60
|
+
```
|
61
|
+
class ApplicationController < ActionController::Base
|
62
|
+
def abilities
|
63
|
+
render json: Ability.export(current_user).to_json
|
64
|
+
# or
|
65
|
+
# render json: current_ability.export.to_json
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
28
69
|
|
29
70
|
## Front-end
|
30
|
-
After you are able to pull the back-end cancancan export to the front-end, you then call this javascript method and pass it the cancancan export:
|
71
|
+
After you are able to pull the back-end cancancan export to the front-end via your created route, you then call this javascript method and pass it the cancancan export:
|
31
72
|
`set_abilities(<export_rules>)`
|
73
|
+
### ex:
|
74
|
+
```
|
75
|
+
$.get("<path_to_your_controller_and_action>", function(data, status){
|
76
|
+
set_abilities(data)
|
77
|
+
});
|
78
|
+
```
|
32
79
|
|
33
80
|
# Usage
|
34
81
|
You can now call the JS function `can`, and pass it similar CanCanCan values
|
data/cancancan_js.gemspec
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module CanCanCanJs
|
2
|
+
class Configuration
|
3
|
+
# CanCanCanJs.configuration.start_block_front_end_rules
|
4
|
+
attr_accessor :start_block_front_end_rules
|
5
|
+
# CanCanCanJs.configuration.export_all_back_end_rules
|
6
|
+
attr_accessor :export_all_back_end_rules
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@start_block_front_end_rules = false
|
10
|
+
@export_all_back_end_rules = false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/cancancan_js/export.rb
CHANGED
@@ -7,12 +7,39 @@ module CanCanCanJs
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def export
|
10
|
-
{class_abilities:
|
10
|
+
{class_abilities: front_end_permissions[:can], object_rules: export_rules}
|
11
|
+
end
|
12
|
+
|
13
|
+
def front_end &block
|
14
|
+
CanCanCanJs.configuration.start_block_front_end_rules = true
|
15
|
+
begin
|
16
|
+
block.call
|
17
|
+
ensure
|
18
|
+
CanCanCanJs.configuration.start_block_front_end_rules = false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# replicating Ability#permissions method, but for front-end export
|
23
|
+
def front_end_permissions
|
24
|
+
permissions_list = {
|
25
|
+
can: Hash.new { |actions, k1| actions[k1] = Hash.new { |subjects, k2| subjects[k2] = [] } },
|
26
|
+
cannot: Hash.new { |actions, k1| actions[k1] = Hash.new { |subjects, k2| subjects[k2] = [] } }
|
27
|
+
}
|
28
|
+
usable_rules_list = front_end_rules
|
29
|
+
if CanCanCanJs.configuration.export_all_back_end_rules
|
30
|
+
usable_rules_list = rules
|
31
|
+
end
|
32
|
+
usable_rules_list.each { |rule| extract_rule_in_permissions(permissions_list, rule) }
|
33
|
+
permissions_list
|
11
34
|
end
|
12
35
|
|
13
36
|
def export_rules
|
14
37
|
new_list = {}
|
15
|
-
|
38
|
+
usable_rules_list = front_end_rules
|
39
|
+
if CanCanCanJs.configuration.export_all_back_end_rules
|
40
|
+
usable_rules_list = rules
|
41
|
+
end
|
42
|
+
usable_rules_list.each do |rule|
|
16
43
|
# init subjects if necessary
|
17
44
|
rule.subjects.each do |subject|
|
18
45
|
# subject_key is Class name as sym.
|
@@ -45,7 +72,7 @@ module CanCanCanJs
|
|
45
72
|
def export user
|
46
73
|
local_ability = Ability.new(user)
|
47
74
|
# We don't care about the 'cannot' section
|
48
|
-
return {class_abilities: local_ability.
|
75
|
+
return {class_abilities: local_ability.front_end_permissions[:can], object_rules: local_ability.export_rules}
|
49
76
|
end
|
50
77
|
end
|
51
78
|
end
|
data/lib/cancancan_js.rb
CHANGED
@@ -1,22 +1,25 @@
|
|
1
|
-
|
1
|
+
require 'cancancan'
|
2
2
|
require_relative 'cancancan_js/export'
|
3
|
+
require_relative 'cancancan_js/configuration'
|
3
4
|
require_relative 'cancancan_js/engine'
|
5
|
+
require_relative 'cancan/ability/front_end_rules_extensions'
|
6
|
+
require_relative 'cancan/ability/rules'
|
4
7
|
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
# config src: http://lizabinante.com/blog/creating-a-configurable-ruby-gem/
|
9
|
+
module CanCanCanJs
|
10
|
+
class << self
|
11
|
+
attr_accessor :configuration
|
12
|
+
end
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
+
def self.reset
|
19
|
+
@configuration = Configuration.new
|
20
|
+
end
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
def self.configure
|
23
|
+
yield(configuration)
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cancancan_js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- benjamin.dana.software.dev@gmail.com
|
@@ -47,7 +47,10 @@ files:
|
|
47
47
|
- LICENSE
|
48
48
|
- README.md
|
49
49
|
- cancancan_js.gemspec
|
50
|
+
- lib/cancan/ability/front_end_rules_extensions.rb
|
51
|
+
- lib/cancan/ability/rules.rb
|
50
52
|
- lib/cancancan_js.rb
|
53
|
+
- lib/cancancan_js/configuration.rb
|
51
54
|
- lib/cancancan_js/engine.rb
|
52
55
|
- lib/cancancan_js/export.rb
|
53
56
|
- vendor/assets/javascripts/cancancan_js.js
|