Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cancan
Manage
Activity
Members
Plan
External wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Github Mirror
coopdevs
cancan
Commits
d4405e60
Commit
d4405e60
authored
15 years ago
by
Ryan Bates
Browse files
Options
Downloads
Patches
Plain Diff
adding cannot method to define which abilities cannot be done - closes #7
parent
e6036550
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.rdoc
+2
-0
2 additions, 0 deletions
CHANGELOG.rdoc
README.rdoc
+19
-10
19 additions, 10 deletions
README.rdoc
lib/cancan/ability.rb
+21
-4
21 additions, 4 deletions
lib/cancan/ability.rb
spec/cancan/ability_spec.rb
+17
-0
17 additions, 0 deletions
spec/cancan/ability_spec.rb
with
59 additions
and
14 deletions
CHANGELOG.rdoc
+
2
−
0
View file @
d4405e60
* adding "cannot" method to define which abilities cannot be done - see issue #7
* support custom objects (usually symbols) in can definition - see issue #8
0.2.0 (Nov 17, 2009)
...
...
This diff is collapsed.
Click to expand it.
README.rdoc
+
19
−
10
View file @
d4405e60
...
...
@@ -111,17 +111,11 @@ You can also pass :manage as the action which will match any action. In this cas
can :manage, Comment do |action, comment|
action != :destroy
end
Finally, the "cannot" method works similar to "can" but defines which abilities cannot be done.
Finally, you can use the "alias_action" method to alias one or more actions into one.
alias_action :update, :destroy, :to => :modify
can :modify, Comment
The following aliases are added by default for conveniently mapping common controller actions.
alias_action :index, :show, :to => :read
alias_action :new, :to => :create
alias_action :edit, :to => :update
can :read, :all
cannot :read, Product
== Checking Abilities
...
...
@@ -141,6 +135,21 @@ The "cannot?" method is for convenience and performs the opposite check of "can?
cannot? :destroy, @project
== Aliasing Actions
You can use the "alias_action" method to alias one or more actions into one.
alias_action :update, :destroy, :to => :modify
can :modify, Comment
can? :update, Comment # => true
The following aliases are added by default for conveniently mapping common controller actions.
alias_action :index, :show, :to => :read
alias_action :new, :to => :create
alias_action :edit, :to => :update
== Assumptions & Configuring
CanCan makes two assumptions about your application.
...
...
This diff is collapsed.
Click to expand it.
lib/cancan/ability.rb
+
21
−
4
View file @
d4405e60
...
...
@@ -41,19 +41,19 @@ module CanCan
# end
#
def
can?
(
original_action
,
target
)
# TODO this could use some refactoring
(
@can_history
||
[]).
reverse
.
each
do
|
can_action
,
can_target
,
can_block
|
(
@can_history
||
[]).
reverse
.
each
do
|
base_behavior
,
can_action
,
can_target
,
can_block
|
can_actions
=
[
can_action
].
flatten
can_targets
=
[
can_target
].
flatten
possible_actions_for
(
original_action
).
each
do
|
action
|
if
(
can_actions
.
include?
(
:manage
)
||
can_actions
.
include?
(
action
))
&&
(
can_targets
.
include?
(
:all
)
||
can_targets
.
include?
(
target
)
||
can_targets
.
any?
{
|
c
|
c
.
kind_of?
(
Class
)
&&
target
.
kind_of?
(
c
)
})
if
can_block
.
nil?
return
true
return
base_behavior
else
block_args
=
[]
block_args
<<
action
if
can_actions
.
include?
(
:manage
)
block_args
<<
(
target
.
class
==
Class
?
target
:
target
.
class
)
if
can_targets
.
include?
(
:all
)
block_args
<<
(
target
.
class
==
Class
?
nil
:
target
)
return
can_block
.
call
(
*
block_args
)
return
base_behavior
?
can_block
.
call
(
*
block_args
)
:
!
can_block
.
call
(
*
block_args
)
end
end
end
...
...
@@ -114,7 +114,24 @@ module CanCan
#
def
can
(
action
,
target
,
&
block
)
@can_history
||=
[]
@can_history
<<
[
action
,
target
,
block
]
@can_history
<<
[
true
,
action
,
target
,
block
]
end
# Define an ability which cannot be done. Accepts the same arguments as "can".
#
# can :read, :all
# cannot :read, Comment
#
# A block can be passed just like "can", however if the logic is complex it is recommended
# to use the "can" method.
#
# cannot :read, Product do |product|
# product.invisible?
# end
#
def
cannot
(
action
,
target
,
&
block
)
@can_history
||=
[]
@can_history
<<
[
false
,
action
,
target
,
block
]
end
# Alias one or more actions into another one.
...
...
This diff is collapsed.
Click to expand it.
spec/cancan/ability_spec.rb
+
17
−
0
View file @
d4405e60
...
...
@@ -106,4 +106,21 @@ describe CanCan::Ability do
@ability
.
can?
(
:update
,
:stats
).
should
be_false
@ability
.
can?
(
:read
,
:nonstats
).
should
be_false
end
it
"should support 'cannot' method to define what user cannot do"
do
@ability
.
can
:read
,
:all
@ability
.
cannot
:read
,
Integer
@ability
.
can?
(
:read
,
"foo"
).
should
be_true
@ability
.
can?
(
:read
,
123
).
should
be_false
end
it
"should support block on 'cannot' method"
do
@ability
.
can
:read
,
:all
@ability
.
cannot
:read
,
Integer
do
|
int
|
int
>
5
end
@ability
.
can?
(
:read
,
"foo"
).
should
be_true
@ability
.
can?
(
:read
,
3
).
should
be_true
@ability
.
can?
(
:read
,
123
).
should
be_false
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment