Skip to content

Allow controller level authentication#175

Merged
mcelicalderon merged 4 commits intomasterfrom
allow-controller-level-authentication
May 8, 2021
Merged

Allow controller level authentication#175
mcelicalderon merged 4 commits intomasterfrom
allow-controller-level-authentication

Conversation

@mcelicalderon
Copy link
Copy Markdown
Member

Right now authentication happens during GQL query execution and there's no good way to authenticate at the controller level first.

This change will allow controllers so authenticate a resource before reaching the GQL schema. This could be useful in scenarios like the one discussed in #151

This way you will be able to send current_resource to the GQL context and I think we will probably deprecate not sending that one when you expect the resource to be authenticated.

@mcelicalderon mcelicalderon added the enhancement New feature or request label May 2, 2021
@mcelicalderon mcelicalderon force-pushed the allow-controller-level-authentication branch from a69a763 to 941cf68 Compare May 2, 2021 02:12
@mcelicalderon mcelicalderon force-pushed the allow-controller-level-authentication branch from e4f7e6a to 82155a8 Compare May 3, 2021 01:42
Comment thread lib/graphql_devise.rb
def self.mount_resource(mapping_name)
@mounted_resources << mapping_name
def self.mount_resource(model)
@mounted_resources << model
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the resource is not already there before adding?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do on the resource_loader

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would less error prone if the check is closer to the assignation, like with add_mapping bellow where the key is looked among the devise mapping before adding

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With add_mapping it is important to prevent adding a new Devise mapping as it has mnay implications, with this one, no real problem if we ever mount the same model more than once. I think it would just add unnecessary overhead to check the list each time we mount a resource. This array is only used to later check the model is or not already on the list.

Comment thread app/controllers/graphql_devise/concerns/set_user_by_token.rb Outdated
Comment thread lib/graphql_devise/resource_loader.rb
@mcelicalderon mcelicalderon marked this pull request as ready for review May 4, 2021 03:27
@mcelicalderon mcelicalderon force-pushed the allow-controller-level-authentication branch from 148e000 to 82155a8 Compare May 4, 2021 03:29
end

def resource_class(resource = nil)
return resource if resource.respond_to?(:find_by)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here indicating this is meant to assert an AR or MongoId instance?

Comment thread lib/graphql_devise.rb
def self.mount_resource(mapping_name)
@mounted_resources << mapping_name
def self.mount_resource(model)
@mounted_resources << model
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would less error prone if the check is closer to the assignation, like with add_mapping bellow where the key is looked among the devise mapping before adding

@mcelicalderon mcelicalderon requested a review from 00dav00 May 5, 2021 03:51
end

def resource_class(resource = nil)
# Return the resource class instead of looking for a Devise mapping if resource is already a resource class
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment should mention find_by is meant to use duck typing to indentify AR and Mongo ID models.


def resource_class(resource = nil)
# Return the resource class instead of looking for a Devise mapping if resource is already a resource class
return resource if resource.respond_to?(:find_by)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which cases will this not be an AR or Mongo Id model?

@mcelicalderon mcelicalderon force-pushed the allow-controller-level-authentication branch from 5c935cd to 2155f5d Compare May 8, 2021 14:51
@mcelicalderon mcelicalderon merged commit 3d4972c into master May 8, 2021
@mcelicalderon mcelicalderon deleted the allow-controller-level-authentication branch May 8, 2021 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants