permissionable 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -4
- data/lib/permissionable.rb +10 -2
- data/lib/permissionable/exceptions.rb +11 -0
- data/lib/permissionable/permissions.rb +17 -7
- data/lib/permissionable/version.rb +1 -1
- data/spec/dummy.rb +12 -0
- data/spec/permissionable_spec.rb +9 -3
- data/spec/permissions_spec.rb +20 -11
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f3fae2297371971e0aad5ccd33ad1c829e13138
|
4
|
+
data.tar.gz: 08999968d5e6a368127b7bed81b07825eb47c138
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ca28cb7275e551aa82f4ee0c0149ca278f5ccf94d746915d574b2f4143c217dce794da58fe4b825b3e827632a1fcac7e49a1343b2e91984dd9e0f0ea59f2708
|
7
|
+
data.tar.gz: 5c70cbd690a1237cc9ff520a72c0d6b936ebe0b3384ee9155797a46206e7c4066db44c90ef329eb1312ac31201cb1933bd9da829fc2288c688e595bec8cfc994
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
## Installation
|
5
5
|
|
6
6
|
Permissionable is distributed as a gem. To install, just add this line to your Gemfile:
|
7
|
-
`gem 'permissionable', '~> 0.1.
|
7
|
+
`gem 'permissionable', '~> 0.1.3'`
|
8
8
|
|
9
9
|
|
10
10
|
## Usage
|
@@ -17,16 +17,16 @@ permissions read: 1,
|
|
17
17
|
delete: 3
|
18
18
|
```
|
19
19
|
|
20
|
-
Your model doesn't have to be an ActiveRecord (AR) model, but Permissionable works out of the box with AR to persist the permissions. Just throw in a `
|
20
|
+
Your model doesn't have to be an ActiveRecord (AR) model, but Permissionable works out of the box with AR to persist the permissions. Just throw in a `permissions_integer` column in the mix.
|
21
21
|
```
|
22
22
|
class AddPermissionsToResource < ActiveRecord::Migration
|
23
23
|
def change
|
24
|
-
add_column :resources, :
|
24
|
+
add_column :resources, :permissions_integer, :integer
|
25
25
|
end
|
26
26
|
end
|
27
27
|
```
|
28
28
|
|
29
|
-
If your're not using AR can also implement your own `#read_attribute` and `#update_column` methods to handle `read_attribute(:
|
29
|
+
If your're not using AR can also implement your own `#read_attribute` and `#update_column` methods to handle `read_attribute(:permissions_integer)` and `update_column(:permissions_integer, permissions_integer)`
|
30
30
|
|
31
31
|
### Working with permissions
|
32
32
|
#### Check for permissions
|
@@ -55,6 +55,9 @@ You can also add an array of permissions yo your array of permissions:
|
|
55
55
|
Removing permissions from your resource is pretty straight forward:
|
56
56
|
`user.permissions.remove(:delete_books)`
|
57
57
|
|
58
|
+
Or, if you would like to remove all permissions:
|
59
|
+
`user.permissions.clear!`
|
60
|
+
|
58
61
|
### Examples
|
59
62
|
```
|
60
63
|
class User < ActiveRecord::Base
|
data/lib/permissionable.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "permissionable/version"
|
2
|
+
require "permissionable/exceptions"
|
2
3
|
require "permissionable/permissions"
|
3
4
|
|
4
5
|
module Permissionable
|
@@ -6,14 +7,21 @@ module Permissionable
|
|
6
7
|
def permissions
|
7
8
|
@permissions ||= Permissions.new(self)
|
8
9
|
end
|
10
|
+
|
11
|
+
def permissions=(*permissions)
|
12
|
+
permissions.flatten!
|
13
|
+
self.permissions.clear!
|
14
|
+
self.permissions << permissions
|
15
|
+
self.permissions.send(:sync_with_owner)
|
16
|
+
end
|
9
17
|
|
10
18
|
# Class methods from here on
|
11
19
|
module ClassMethods
|
20
|
+
|
12
21
|
def permissions(permission_definitions)
|
13
22
|
@permissions = {}
|
14
23
|
permission_definitions.each do |permission,i|
|
15
|
-
raise
|
16
|
-
raise "#{i} is not an integer" unless i.is_a?(Fixnum)
|
24
|
+
raise InvalidPermissionAssignment.new(i) if i == 0 || !i.is_a?(Fixnum)
|
17
25
|
if i == 1
|
18
26
|
@permissions[permission] = 1
|
19
27
|
else
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Permissionable
|
2
|
+
class InvalidPermissionAssignment < StandardError
|
3
|
+
def initialize(object)
|
4
|
+
if object == 0
|
5
|
+
super "Can not assign a permission the value 0"
|
6
|
+
elsif !object.is_a?(Fixnum)
|
7
|
+
super "Can not assign permissions to values of type #{object.class.name} (only Fixnum allowed)"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -3,14 +3,14 @@ module Permissionable
|
|
3
3
|
def initialize(owner)
|
4
4
|
@owner = owner
|
5
5
|
if @owner.respond_to?(:read_attribute)
|
6
|
-
@
|
6
|
+
@permissions_integer = @owner.read_attribute(:permissions_integer)
|
7
7
|
else
|
8
|
-
@
|
8
|
+
@permissions_integer = 0
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_i
|
13
|
-
@
|
13
|
+
@permissions_integer
|
14
14
|
end
|
15
15
|
|
16
16
|
# Add new permissions
|
@@ -23,7 +23,7 @@ module Permissionable
|
|
23
23
|
control_definitions(permissions)
|
24
24
|
permissions.each do |permission|
|
25
25
|
new_permission = defined_permissions[permission]
|
26
|
-
@
|
26
|
+
@permissions_integer += new_permission unless include?(permission)
|
27
27
|
end
|
28
28
|
sync_with_owner
|
29
29
|
end
|
@@ -38,7 +38,7 @@ module Permissionable
|
|
38
38
|
permissions.flatten!
|
39
39
|
control_definitions(permissions)
|
40
40
|
permissions.each do |permission|
|
41
|
-
@
|
41
|
+
@permissions_integer -= defined_permissions[permission] if include?(permission)
|
42
42
|
end
|
43
43
|
sync_with_owner
|
44
44
|
end
|
@@ -55,10 +55,15 @@ module Permissionable
|
|
55
55
|
# Sum the corresponding int value of all
|
56
56
|
# permissions provided in the argument list
|
57
57
|
asserted = permissions.inject(0){ |mem, permission| mem + defined_permissions[permission] }
|
58
|
-
@
|
58
|
+
@permissions_integer & asserted == asserted
|
59
59
|
end
|
60
60
|
alias_method :[], :include?
|
61
61
|
|
62
|
+
def clear!
|
63
|
+
@permissions_integer = 0
|
64
|
+
sync_with_owner
|
65
|
+
end
|
66
|
+
|
62
67
|
private
|
63
68
|
|
64
69
|
def control_definitions(permissions)
|
@@ -68,8 +73,13 @@ module Permissionable
|
|
68
73
|
end
|
69
74
|
|
70
75
|
def sync_with_owner
|
76
|
+
if @owner.respond_to?(:permissions_integer=)
|
77
|
+
@owner.permissions_integer = @permissions_integer
|
78
|
+
end
|
71
79
|
if @owner.respond_to?(:update_column)
|
72
|
-
@owner.
|
80
|
+
if @owner.respond_to?(:persisted?) && @owner.persisted?
|
81
|
+
@owner.update_column(:permissions_integer, @permissions_integer)
|
82
|
+
end
|
73
83
|
end
|
74
84
|
end
|
75
85
|
|
data/spec/dummy.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'pry'
|
1
2
|
class Dummy
|
2
3
|
include Permissionable
|
3
4
|
|
@@ -7,4 +8,15 @@ class Dummy
|
|
7
8
|
play: 4,
|
8
9
|
eat: 5
|
9
10
|
|
11
|
+
def persisted?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(attributes={})
|
16
|
+
attributes.each do |attribute,value|
|
17
|
+
setter_method = "#{attribute}=".to_sym
|
18
|
+
self.send(setter_method, value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
10
22
|
end
|
data/spec/permissionable_spec.rb
CHANGED
@@ -2,6 +2,8 @@ require 'permissionable'
|
|
2
2
|
require 'dummy'
|
3
3
|
|
4
4
|
|
5
|
+
# This class is a bit unreliable because
|
6
|
+
# it has its permissions redefined in the tests
|
5
7
|
class FlakyDummy
|
6
8
|
include Permissionable
|
7
9
|
end
|
@@ -31,13 +33,13 @@ describe Permissionable do
|
|
31
33
|
it 'can not assign 0' do
|
32
34
|
expect{
|
33
35
|
FlakyDummy.permissions(nope: 0)
|
34
|
-
}.to raise_exception
|
36
|
+
}.to raise_exception(Permissionable::InvalidPermissionAssignment)
|
35
37
|
end
|
36
38
|
|
37
|
-
it 'can
|
39
|
+
it 'can not assign strings' do
|
38
40
|
expect{
|
39
41
|
FlakyDummy.permissions(nope: '1')
|
40
|
-
}.to raise_exception
|
42
|
+
}.to raise_exception(Permissionable::InvalidPermissionAssignment)
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
@@ -49,6 +51,10 @@ describe Permissionable do
|
|
49
51
|
expect(dummy.permissions).to be_an_instance_of(Permissionable::Permissions)
|
50
52
|
end
|
51
53
|
|
54
|
+
it 'can assign permissions on initializing' do
|
55
|
+
dummy = Dummy.new(permissions: [:sit, :eat])
|
56
|
+
expect(dummy.permissions[:sit, :eat]).to be true
|
57
|
+
end
|
52
58
|
end
|
53
59
|
|
54
60
|
end
|
data/spec/permissions_spec.rb
CHANGED
@@ -6,7 +6,7 @@ class DummyWithPermissions
|
|
6
6
|
permissions walk: 1
|
7
7
|
|
8
8
|
def read_attribute(attribute)
|
9
|
-
1 if attribute == :
|
9
|
+
1 if attribute == :permissions_integer
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -53,7 +53,7 @@ describe Permissionable::Permissions do
|
|
53
53
|
expect(@permissions.include?(:run, :walk)).to be true
|
54
54
|
end
|
55
55
|
|
56
|
-
it '
|
56
|
+
it 'is false when one of asserted is missing' do
|
57
57
|
@permissions << :run
|
58
58
|
@permissions << :walk
|
59
59
|
expect(@permissions.include?(:run, :play)).to be false
|
@@ -68,18 +68,27 @@ describe Permissionable::Permissions do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
it 'updates column when adding permission' do
|
71
|
-
|
72
|
-
|
73
|
-
expect(
|
74
|
-
|
71
|
+
owner = Dummy.new
|
72
|
+
permissions = Permissionable::Permissions.new(owner)
|
73
|
+
expect(owner).to receive(:update_column).with(:permissions_integer, 2)
|
74
|
+
permissions << :walk
|
75
75
|
end
|
76
76
|
|
77
77
|
it 'updates column when removing permission' do
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
expect(
|
82
|
-
|
78
|
+
owner = Dummy.new
|
79
|
+
permissions = Permissionable::Permissions.new(owner)
|
80
|
+
permissions << :run
|
81
|
+
expect(owner).to receive(:update_column).with(:permissions_integer, 0)
|
82
|
+
permissions.remove :run
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'can clear permissions' do
|
86
|
+
owner = Dummy.new
|
87
|
+
permissions = Permissionable::Permissions.new(owner)
|
88
|
+
permissions << :run
|
89
|
+
expect(permissions[:run]).to be true
|
90
|
+
permissions.clear!
|
91
|
+
expect(permissions.to_i).to be_zero
|
83
92
|
end
|
84
93
|
|
85
94
|
it 'can read permissions from owner' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permissionable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Falkén
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
68
|
- lib/permissionable.rb
|
69
|
+
- lib/permissionable/exceptions.rb
|
69
70
|
- lib/permissionable/permissions.rb
|
70
71
|
- lib/permissionable/version.rb
|
71
72
|
- permissionable.gemspec
|