Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attributes rewrite loaded parent relation #472

Closed
Able1991 opened this issue Dec 12, 2017 · 5 comments
Closed

Initial attributes rewrite loaded parent relation #472

Able1991 opened this issue Dec 12, 2017 · 5 comments
Assignees
Labels
with gist Gist has been provided

Comments

@Able1991
Copy link

If I have a rule related to the parent resource, I have something that the value of the rule erases the realation with the parent resource
My rules:

user.related_objects.each do |relatet_object|
 can :manage, ChildObject, { :user_id => user.id, :object_id => relatet_object.object_id}
end

Controller code:

load_and_authorize_resource :object
load_and_authorize_resource :child_object, :through => :object

I have two objects with ids 1 and 2, at the time of build the resource, the correct reference to the parent is first specified, but it is replaced by the initial attributes, which according to the created rules will be:

{ :user_id => 1, :object_id => 1}.merge({ :user_id => 1, :object_id => 2}) => { :user_id => 1, :object_id => 2}

def attributes_for(action, subject)
attributes = {}
relevant_rules(action, subject).map do |rule|
attributes.merge!(rule.attributes_from_conditions) if rule.base_behavior
end
attributes
end

def assign_attributes(resource)
resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
initial_attributes.each do |attr_name, value|
resource.send("#{attr_name}=", value)
end
resource
end
def initial_attributes
current_ability.attributes_for(@params[:action].to_sym, resource_class).delete_if do |key, _value|
resource_params && resource_params.include?(key)
end
end

Perhaps it is worth not to consider the initial attribute if it has already been set?

@coorasse
Copy link
Member

Please write a gist to reproduce the issue. https://gist.github.com/coorasse/3f00f536563249125a37e15a1652648c

@Able1991
Copy link
Author

@coorasse coorasse self-assigned this Feb 21, 2019
@coorasse coorasse added the with gist Gist has been provided label Feb 21, 2019
@coorasse
Copy link
Member

Hi, I analysed your gist and the definition of the abilities was the issue. If you define the abilities like that:

can :read, Library, id: user.library_ids
can :read, Book, library_id: user.library_ids
can :manage, Book, library: { library_users: {user_id: user.id, manage: true} }

the tests pass. Please feel free to re-open providing another non-working test. Thank you!

@Able1991
Copy link
Author

Hi, I analysed your gist and the definition of the abilities was the issue. If you define the abilities like that:

can :read, Library, id: user.library_ids
can :read, Book, library_id: user.library_ids
can :manage, Book, library: { library_users: {user_id: user.id, manage: true} }

the tests pass. Please feel free to re-open providing another non-working test. Thank you!

I know that you can pass an array, but this is not an explicit behavior, therefore a ticket was created

@Able1991
Copy link
Author

Able1991 commented May 8, 2021

Hi, I analysed your gist and the definition of the abilities was the issue. If you define the abilities like that:

can :read, Library, id: user.library_ids
can :read, Book, library_id: user.library_ids
can :manage, Book, library: { library_users: {user_id: user.id, manage: true} }

the tests pass. Please feel free to re-open providing another non-working test. Thank you!

can :manage, ShareReport, :owner_id => rightholder_id, :owner_type => "Rightholder", :user => user
can :manage, ShareReport, :owner_id => customer_id, :owner_type => "Customer", :user => user

Will there also be proposals to transfer an array? reopen issue pls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
with gist Gist has been provided
Projects
None yet
Development

No branches or pull requests

2 participants