stffn / declarative_authorization
- Source
- Commits
- Network (41)
- Issues (7)
- Downloads (4)
- Wiki (2)
- Graphs
-
Branch:
master
-
Hi,
could you tell me plz - how to write access rules for following scheme. I have 2 models - Post and Comment. Post has many comments and Comment belongs to Post. In posts_controller i have methods add_comment and remove_comment. But rules likehas_permission_on :posts, :to => :comments_manage do if_attribute :user_id => is {current_user} endnot working and
has_permission_on :comments, :to => :comments_manage do if_attribute :user_id => is {current_user} endnot working too :(
additional information on privileges:privilege :comments_manage do includes :add_comment, :remove_comment endComments
-
I receive the following error when trying to call a show action in my users controller
Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:535:in
object_attribute_value'<br/> /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:428:invalidate?' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2036:inall?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:427:ineach' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:427:inall?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:427:invalidate?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:374:invalidate?' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2036:inany?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:372:ineach' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:372:inany?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:372:insend' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:372:invalidate?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:174:inpermit!' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2036:inany?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:174:ineach' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:174:inany?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:174:inpermit!' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:184:inpermit?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:70:inpermitted_to!' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:46:inpermitted_to?' /Library/Ruby/Gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/helper.rb:32:inpermitted_to?' /Users/kevin/Development/web/osr/app/views/users/show.html.erb:8:in_run_erb_app47views47users47show46html46erb' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:insend' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:306:inwith_template' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:30:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/template.rb:205:inrender_template' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:265:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:348:in_render_with_layout' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:262:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1250:inrender_for_file' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:951:inrender_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:51:inrender' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:51:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:135:insend' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:135:incustom' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:179:incall' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:179:inrespond' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:ineach' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:inrespond' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:107:inrespond_to' /Users/kevin/Development/web/osr/app/controllers/users_controller.rb:37:inshow' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:insend' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:inperform_action_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:incall_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:inperform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:inperform_action_without_flash' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:inperform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:insend' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:inprocess_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:inprocess' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:inprocess' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:incall' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:incall'This is how my role is set up:
role :user doincludes :guest has_permission_on :reviews, :to => [:new, :create] has_permission_on :reviews, :to => [:edit, :update] do if_attribute :user => is { user } end has_permission_on :users, :to => [:edit, :update] do if_attribute :user => is { user } endend
And this is the problem permission call
<%= link_to 'Edit profile', edit_user_path(@user), :id => 'edit_profile' if permitted_to? :edit, @user %>Comments
-
attribute check is done on array instead of the instances in the array
1 comment Created 26 days ago by xtremeIf you have a has_many :through relationship as follows:
(taken from http://github.com/stffn/decl_auth_demo_app)conference <1------*> conference_attendees <*--------1> user
class Conference < ActiveRecord::Base has_many :conference_attendees has_many :attendees, :through => :conference_attendees, :source => :userIf you want to give read for a conference to all users attending that conference you would type
has_permission_on :conferences, :to => :read do if_attribute :attendees => contains {user} endThat works fine. But what if you have a attribute on the conference_attendees model that you need to consider as well. Perhaps you wan't to give read right to all users attending a conference and that have paid (paid beeing a boolean in the conference_attendees model).
has_permission_on :conferences, :to => :read do if_attribute :conference_attendees => {:user => is {user}, :paid => true} endThis will work when using
Conference.with_permissions_to(:read)but will fail if:attribute_check => trueTo replicate this error:
- In your decl_auth_demo_app change authorization_rules.rb line 4 from
if_attribute :published => true
to
if_attribute :conference_attendees => {:user => is{user}}
- Go to /conferences as presenter_2
- You will see conference Emerging Technologies 2009
Click on the conference and you will get "You are not allowed to access this action."
Permission denied: Error when calling user on [#<ConferenceAttendee id: 15, user_id: 3, conference_id: 2>] for validating attribute: undefined method `user' for [#<ConferenceAttendee id: 15, user_id: 3, conference_id: 2>]:Array Filter chain halted as [:filter_access_filter] rendered_or_redirected.
Comments
I now see that you have fixed this in your commit http://github.com/stffn/declarative_authorization/commit/907255af13a710b4683ee8ce86acf95977a53860.
> - In your decl_auth_demo_app change authorization_rules.rb line 4 from
-
Hi,
i've created following rulesrole :guest do has_permission_on :news_articles, :to => :news_articles_view do if_attribute :status => is {'approved'} end endand
privilege :news_articles_view do includes :index, :show, :tag, :category endbut NewsArticle instances with other that approved status still visible :( could you tell me plz - where i'm wrong?
PS: other roles/permissions works fine
PPS: thanks a lot for perfect plugin :)Comments
-
authorization_rules/change line(35) undefined method `to_sym' for nil:NilClass
8 comments Created about 1 month ago by bublikShowing /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/_change.erb where line #35 raised:
undefined method `to_sym' for nil:NilClass
Extracted source (around line #35):
32:
33:
34: <% @users.each do |user| %>
35:
36:<%=h user.id %>
Comments
/usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in
method_missing' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:142:inpermit!' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:184:inpermit?' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/_change.erb:35:inrun_erb_47usr47lib47ruby45enterprise45146846747lib47ruby47gems47146847gems47declarative_authorization45046447app47views47authorization_rules47_change46erb_locals_change_object' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/change.erb:34:ineach' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/_change.erb:34:in_run_erb_47usr47lib47ruby45enterprise45146846747lib47ruby47gems47146847gems47declarative_authorization45046447app47views47authorization_rules47_change46erb_locals_change_object' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:insend' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:inrender' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:306:inwith_template' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:30:inrender' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable_partial.rb:20:inrender' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:26:inbenchmark' /usr/lib/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'do you possible have any simple instructions for reproducing this. or as easily a project to create and hub it up or gist it
Crete users with privilegies
['admin', 'moderator', 'user'].each do |priv|u = User.create(:login => priv, :name => "User #{priv}", :email => "#{priv}@#{APP_CONFIG['domain']}", :password => priv, :password_confirm => priv) u.activate! u.roles.create(:name => priv) endUser model
has_many :videos has_many :assignments has_many :roles, :through => :assignmentsdef role_symbols
(roles || []).map {|r| r.name.underscore.to_sym}end
Next step open
http://localhost:3000/authorization_rules/change
And have errorundefined method `to_sym' for nil:NilClass
In line
My authorization rules
authorization do
role :guest dohas_permission_on [:videos, :thumbnails, :categories], :to => [:index, :show, :search, :tag] has_permission_on [:users], :to => [:create, :new, :activate] # has_permission_on :comments, :to => [:new, :create] # has_permission_on :comments, :to => [:edit, :update] do # if_attribute :user => is { user } # endend
role :user do
includes :guest has_permission_on :videos, :to => [:edit, :update] do if_attribute :user => is { user } endend
role :moderator do
includes :guest includes :user has_permission_on [:videos, :categories], :to => [:edit, :update]end
role :admin do
includes :moderator has_permission_on [:authorization_rules, :authorization_usages], :to => :read has_permission_on [:videos, :categories], :to => [:new, :create, :destroy, :delete]end
end
Back trace
/usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/controllers/./../lib/declarative_authorization/authorization.rb:142:in `permit!' /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/controllers/./../lib/declarative_authorization/authorization.rb:184:in `permit?' /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/_change.erb:35 /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/_change.erb:34:in `each' /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/_change.erb:34 /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/views/authorization_rules/change.html.erb:168 /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/app/controllers/authorization_rules_controller.rb:47:in `change'You don't seem to have any rules in your project's authorization rules. Still, this shouldn't result in an exception. I will fix that.
Oh, actually, it is about not having any privilege hierarchies in the rules file.
Fixes exception in change support for rules file without privilege hierarchy (Closed by aac2336)
Update permissions are only checked the first time
4 comments Created about 1 month ago by rkistnerUpdate permissions are only checked on the first save. If this is the intended behavior, it should be clear in the documentation.
post = Post.first post.owner_id = 2 post.save # Invalid update, exception is thrown post = Post.first post.contents = "..." post.save # Valid update, no exception thrown post.owner_id = 2 post.save # Invalid update, but no exception is thrownComments
Why would this be an intended behavior, sounds like a serious issue.
I just tried to reproduce this behavior, but couldn't. First changing an unrelated attribute doesn't let me change the critical one. I push the reorganized tests in a second to github.
I'll run the tests when I'm back at my office (next year probably)
Again: Unnessecary db query in conjunction with named_scope
1 comment Created about 1 month ago by ledermannWith version 0.4 we have a bug like in issue #7 again:
MyModel.with_permissions_to(:read).foo_scope
=> One query, all is fineMyModel.foo_scope.with_permissions_to(:read)
=> Two queries: The first for "MyModel.foo.find :all" (unnessecary), the second is the right one.If you can't reproduce this, I will try to build a failing test or something.
Comments
Why cant use Hash as an attribute value for Array attributes?
3 comments Created 2 months ago by UVSoftauthorization.rb, line 412:
if value.is_a?(Hash) if attr_value.is_a?(Array) raise AuthorizationUsageError, "Unable evaluate multiple attributes " + "on a collection. Cannot use '=>' operator on #{attr.inspect} " + "(#{attr_value.inspect}) for attributes #{value.inspect}."What if i want to check length of some association?
has_permission_on :pages, :to => :publish do
if_attribute :paragraphs => { :length => is_not { 0 } }
end
Comments
You won't be able to use with_permissions_to for that model then. If we'd allow it, the error in with_permissions_to would be hard to debug. What about :paragraphs => is_not {[]}?
0 was just for example...
ok, so i have to make a helper method in the model... (There is a critical issue in d_a which fires up unneeded database queries. I stumbled upon the issue while profiling my own application, which makes heavy use of d_a AND uses a lot of named scopes. Some requests in my app are very slow and I discovered that there are many SQL queries in the log which are not needed. This queries are generated by d_a. Because I don't know how to fix it I will give you a detailed description to reproduce the issue based on the d_a_demo_app:
1) Install the official demo app from http://github.com/stffn/decl_auth_demo_app on your development machine
2) The demo app does not use any named scope, so add this example scope to talk.rb:
named_scope :with_abstract, :conditions => "talks.abstract IS NOT NULL"3) Start the console and type in this example query:
Conference.first.talks.with_abstract.with_permissions_to(:read).find :all, :limit => 24) In the development.log you find this:
SELECT * FROM "conferences" LIMIT 1 SELECT * FROM "talks" WHERE ("talks".conference_id = 1) AND ((talks.abstract IS NOT NULL) AND ("talks".conference_id = 1)) SELECT "talks".* FROM "talks" INNER JOIN "conferences" ON "conferences".id = "talks".conference_id WHERE ("talks".conference_id = 1) AND (((("conferences"."published" = 't')) AND (talks.abstract IS NOT NULL)) AND ("talks".conference_id = 1)) LIMIT 2Please have a look at the second query. It is not needed at all and should not be there! So far as I see, this time consuming query is only generated if the scope "with_permissions_to" is combined with an existing named scope.
You also see a second issue (duplicate condition the WHERE clause), but this is another story (not as important).
Comments
Let's start with the second issue: as long as there is no measurement showing any performance issues with the duplicate conditions, I believe in the DBM's query optimizer to handle them.
To the second issue: Full agree.
But to the main issue: It seems to me that your commit does not fix the bug. Do you have tested it with my description above? I have checked it with your commit and the bug still occurs for me.
Sorry for not providing a failing test, I don't know how to build it in this case.One more hint: I found that the bug was introduced by 824b11b
I tested as you described and could reproduce it before e90082c, but it works for me now. If there is a similar bug because of 824b11b, it isn't reproduced in your described test.
Strange. Did you notice that the submodule in the current d_a_demo is not up to date? Here a detailed bash script to reproduce the bug (based on my description above):
git clone git://github.com/stffn/decl_auth_demo_app.git cd decl_auth_demo_app/ cp config/database.yml.example config/database.yml cp db/development.sqlite3.example db/development.sqlite3 git submodule init git submodule update cd vendor/plugins/declarative_authorization/ git checkout master cd - script/console Talk.named_scope :with_abstract, :conditions => "talks.abstract IS NOT NULL" Conference.first.talks.with_abstract.with_permissions_to(:read).find :all, :limit => 2 exit cat log/development.logJust run this script on your local (OS X) machine and you will see the unnecessary query:
SELECT * FROM "talks" WHERE ("talks".conference_id = 1)Hope this helps.
Do not raise AuthorizationUsageError when object does not have required attribute
10 comments Created 4 months ago by UVSoftobject_attribute_value raises AuthorizationUsageError exception when the send method fails with NoMethodError. It would be better if in this case the method returned just nil flunking the rule. In my application I have polymorphic association: Paragraph belongs_to(:owner, :polymorphic => true), [ ManPage, WomanPage ] has_many(:paragraphs, :as => :owner). Owners have different attributes. Rules look like this:
has_permissions_on :paragraphs, :to => :manage do
if_attribute :owner => { :man => is { user } }
if_attribute :owner => { :woman => is { user } }
end
So WomanPage does not have :man, and we get NoMethodError.
Comments
The most common case still is a typo. That is why there currently is this fail-fast approach to raise. That's why I'd consider a flag on has_permission_on to allow missing attributes.
For your specific case: is there a reason not to just have man/woman methods returning nil on the other class?
yes, there is a reason, it's just a simplified example, in my app those classes have a lot of different attrifutes, and there are many classes and many polymorphic association, and defining a lot of fake attributes is not the way to go for me, please add this [allow missing attributes] option, it'll be very useful! thanks! now i just patch the plugin myself returning nil from object_attribute_value...
By the way, what is the common case? What idea is behind this exception raising?
There is another solution. According to the documentation conditions in if_attributes are ORed, but they can't be ordered, so when the first one is false, the rest are still been checked. But if we could order them, it wouldn't happen. Some thing like this:
if_attribute [ { :owner_type => 'Man' }, { :owner => { :man => is { user } } } ]
if_attribute [ { :owner_type => 'Woman' }, { :owner => { :woman => is { user } } } ]
What do you think?
The common case is that all attributes are known beforehand. Any non-existant attribute thus is a typo which should lead to a failure as soon as possible (fail-fast).
So how about ordering? Please implement this feature and additional option to has_permissions_on =) Thanks!
This really is an edge case. I think the best approach would be an application-wide switch, e.g. setting Authorization.raise_on_missing_attribute = false. If you provide such a patch, I'm happy to include it.
Mmm.... and what about
has_permissions_on :paragraphs, :to => :manage, :join_by => :and do
if_attribute :owner_type => 'Man'
if_attribute :owner => { :man => is { user } }
end
has_permission_on :paragraphs, :to => :manage, :join_by => :and do
if_attribute :owner_type => 'Woman'
if_attribute :owner => { :woman => is { user } }
end
?
That might work. The if_attribute statements are held in an array and checked in order. Please test it.
As described in this thread in the google group:
http://groups.google.com/group/declarative_authorization/browse_thread/thread/2029d99b9d7df2cb#Comments
Authorization::Maintenance::without_access_control can not be nested
2 comments Created 6 months ago by ledermannIt's not possible to do nesting like this:
Authorization::Maintenance::without_access_control do do_something Authorization::Maintenance::without_access_control do do_something end do_something # => here the access control takes place again! endComments
This feature is coming soon. Sit tight!





Please use the issue tracker for feature requests and bug reports only. For usage questions, please post on the mailing list.