cando 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -77,7 +77,7 @@ Using the CanDo in your code (working code with an empty database):
77
77
  # if passed, this will be executed if the user does not have the
78
78
  # asked-for capability (only applies if 'can' is passed a block)
79
79
  cannot_block do |user_urn, capability|
80
- raise "#{user_urn} can not #{capability}"
80
+ raise "#{user_urn} can not #{capability} .. user capabilities are: #{capabilities(user_urn)}"
81
81
  end
82
82
 
83
83
  connect "mysql://cando_user:cando_passwd@host:port/database"
@@ -86,9 +86,13 @@ Using the CanDo in your code (working code with an empty database):
86
86
  # if the role or a capability does not exist, it'll be created
87
87
  define_role("r1", ["capability1", "capability3"])
88
88
  define_role("r2", ["capability2"])
89
+ define_role("r3", ["capability3"])
89
90
 
90
91
  # if the user does not exist, he'll be created -- the roles must be available
91
- assign_roles("user1", ["r1", "r2"])
92
+ assign_roles("user1", ["r1"])
93
+ # add other roles by unifying arrays with '|'
94
+ assign_roles("user1", roles("user1") | ["r2","r3"])
95
+
92
96
  assign_roles("user2", ["r1"])
93
97
 
94
98
  # use 'can' block syntax
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
data/cando.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "cando"
8
- s.version = "0.1.3"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Bornkessel"]
12
- s.date = "2014-05-20"
12
+ s.date = "2014-05-21"
13
13
  s.description = "CanDo is a small gem to implement a simple user access system based on users, roles & capabilites, where:\n\n each user can have 0, 1 or many roles\n each role can have 0, 1 or many capabilites\n\nUsers have capabilities by getting roles assigned (role == collection of capabilities). Within the code, the can helper method can be used to test whether a user has a certain capability or not (see below for a working code example)."
14
14
  s.email = "daniel@soundcloud.com"
15
15
  s.extra_rdoc_files = [
data/lib/cando.rb CHANGED
@@ -85,4 +85,18 @@ EOF
85
85
  def assign_roles(user, roles)
86
86
  CanDo::User.find_or_create(:id => user).assign_roles(roles)
87
87
  end
88
+
89
+ def roles(user)
90
+ user = CanDo::User.first(:id => user)
91
+ return [] unless user
92
+
93
+ user.roles.map(&:id)
94
+ end
95
+
96
+ def capabilities(user)
97
+ user = CanDo::User.first(:id => user)
98
+ return [] unless user
99
+
100
+ user.capabilities.map{|x| x.to_sym }
101
+ end
88
102
  end
data/spec/cando_spec.rb CHANGED
@@ -15,6 +15,29 @@ describe "CanDo module methods" do
15
15
  it { expect{ CanDo.cannot_block{|x,y| x} }.to_not raise_error(CanDo::ConfigCannotBlockError) }
16
16
  end
17
17
 
18
+ context "CanDo.cannot_block executes in the current context" do
19
+
20
+ it "sees local methods" do
21
+ CanDo.cannot_block{|x,y| local_dummy_method }
22
+
23
+ class Dummy
24
+ include CanDo
25
+ class DummyException < Exception; end
26
+
27
+ def local_dummy_method
28
+ raise DummyException
29
+ end
30
+
31
+ def foo
32
+ can("erna",:superpowers) {}
33
+ end
34
+ end
35
+
36
+ foo = Dummy.new
37
+ expect{ foo.foo }.to raise_error Dummy::DummyException
38
+ end
39
+ end
40
+
18
41
  context "CanDo.connect" do
19
42
  it { expect{ CanDo.connect(nil) }.to raise_error(CanDo::ConfigMysqlConnectionError) }
20
43
  it { expect{ CanDo.connect("sqlite::memory:") }.to raise_error(CanDo::ConfigMysqlDBError) }
@@ -50,7 +73,7 @@ describe "CanDo module methods" do
50
73
 
51
74
  context "cleanup" do
52
75
  before(:each) do
53
- @role = define_role("role", [:capabilty])
76
+ @role = define_role("role", [:capability])
54
77
  end
55
78
 
56
79
  it { expect{ @role.destroy }.to change{CanDo::Role.count}.from(1).to(0) }
@@ -86,7 +109,7 @@ describe "CanDo module methods" do
86
109
 
87
110
  context "cleanup" do
88
111
  before(:each) do
89
- @role = define_role("role", [:capabilty])
112
+ @role = define_role("role", [:capability])
90
113
  @user = assign_roles("user", [@role])
91
114
  end
92
115
 
@@ -99,7 +122,7 @@ describe "CanDo module methods" do
99
122
  include CanDo
100
123
 
101
124
  before(:each) do
102
- @role = define_role("role", [:capability1, :capabilty2])
125
+ @role = define_role("role", [:capability1, :capability2])
103
126
  @user = assign_roles("user", [@role])
104
127
  end
105
128
 
@@ -112,7 +135,7 @@ describe "CanDo module methods" do
112
135
  context "CanDo.cannot_block" do
113
136
  class DummyException < RuntimeError; end
114
137
  before(:all) do
115
- CanDo.cannot_block do |user, capabilty|
138
+ CanDo.cannot_block do |user, capability|
116
139
  raise DummyException
117
140
  end
118
141
  end
@@ -122,5 +145,29 @@ describe "CanDo module methods" do
122
145
  end
123
146
  end
124
147
  end
148
+
149
+ context "CanDo#roles" do
150
+ include CanDo
151
+ before(:each) do
152
+ @role1 = define_role("role1", [:capability1, :capability2])
153
+ @role2 = define_role("role2", [:capability3, :capability4])
154
+ @user = assign_roles("user", [@role1,@role2])
155
+ end
156
+
157
+ it { roles("user").sort.should == ["role1","role2"] }
158
+ it { roles("non-existant-user").sort.should == [] }
159
+ end
160
+
161
+ context "CanDo#capabilities" do
162
+ include CanDo
163
+ before(:each) do
164
+ @role1 = define_role("role1", [:capability1, :capability2])
165
+ @role2 = define_role("role2", [:capability2, :capability4])
166
+ @user = assign_roles("user", [@role1,@role2])
167
+ end
168
+
169
+ it { capabilities("user").sort.should == [:capability1, :capability2,:capability4].sort }
170
+ it { capabilities("non-existant-user").sort.should == [] }
171
+ end
125
172
  end
126
173
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cando
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-20 00:00:00.000000000 Z
12
+ date: 2014-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel
@@ -169,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  segments:
171
171
  - 0
172
- hash: -2350834276738640713
172
+ hash: 1474018739418741316
173
173
  required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  none: false
175
175
  requirements: