-
Notifications
You must be signed in to change notification settings - Fork 386
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
Datamapper support #472
Merged
Merged
Datamapper support #472
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4d3f2f5
Add initial DataMapper support
3f61b62
Datamapper support improvements, passing tests
5a2b95f
Add DataMapper Support
a4148bb
Add datamapper to travis.yml
f342bd2
Update README, remove dm-types require from submodule
2ac93b4
Remove dm adapters redis, sqlite from gemfile and code cleanup
7197010
Overwrite save methods in DM adapter
83bf646
Use sorcery_save for saving
7145deb
Update DataMapper specs
fafdf93
Add exclude filter for datamapper
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
module Sorcery | ||
module Model | ||
module Adapters | ||
module DataMapper | ||
def self.included(klass) | ||
klass.extend ClassMethods | ||
klass.send(:include, InstanceMethods) | ||
end | ||
|
||
module InstanceMethods | ||
def increment(attr) | ||
self[attr] ||= 0 | ||
self[attr] += 1 | ||
self | ||
end | ||
|
||
def update_many_attributes(attrs) | ||
attrs.each do |name, value| | ||
value = value.utc if value.is_a?(ActiveSupport::TimeWithZone) | ||
self.send(:"#{name}=", value) | ||
end | ||
self.class.get(self.id).update(attrs) | ||
end | ||
|
||
def update_single_attribute(name, value) | ||
update_many_attributes(name => value) | ||
end | ||
|
||
def sorcery_save(options = {}) | ||
if options.key?(:validate) && ! options[:validate] | ||
save! | ||
else | ||
save | ||
end | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def find(id) | ||
get(id) | ||
end | ||
|
||
def delete_all | ||
destroy | ||
end | ||
|
||
# NOTE | ||
# DM Adapter dependent | ||
# DM creates MySQL tables case insensitive by default | ||
# http://datamapper.lighthouseapp.com/projects/20609-datamapper/tickets/1105 | ||
def find_by_credentials(credentials) | ||
credential = credentials[0].dup | ||
credential.downcase! if @sorcery_config.downcase_username_before_authenticating | ||
@sorcery_config.username_attribute_names.each do |name| | ||
@user = first(name => credential) | ||
break if @user | ||
end | ||
!!@user ? get(@user.id) : nil | ||
end | ||
|
||
def find_by_provider_and_uid(provider, uid) | ||
@user_klass ||= ::Sorcery::Controller::Config.user_class.to_s.constantize | ||
user = first(@user_klass.sorcery_config.provider_attribute_name => provider, @user_klass.sorcery_config.provider_uid_attribute_name => uid) | ||
!!user ? get(user.id) : nil | ||
end | ||
|
||
def find_by_id(id) | ||
find(id) | ||
rescue ::DataMapper::ObjectNotFoundError | ||
nil | ||
end | ||
|
||
def find_by_activation_token(token) | ||
user = first(sorcery_config.activation_token_attribute_name => token) | ||
!!user ? get(user.id) : nil | ||
end | ||
|
||
def find_by_remember_me_token(token) | ||
user = first(sorcery_config.remember_me_token_attribute_name => token) | ||
!!user ? get(user.id) : nil | ||
end | ||
|
||
def find_by_username(username) | ||
user = nil | ||
sorcery_config.username_attribute_names.each do |name| | ||
user = first(name => username) | ||
break if user | ||
end | ||
!!user ? get(user.id) : nil | ||
end | ||
|
||
def transaction(&blk) | ||
tap(&blk) | ||
end | ||
|
||
def find_by_sorcery_token(token_attr_name, token) | ||
user = first(token_attr_name => token) | ||
!!user ? get(user.id) : nil | ||
end | ||
|
||
def find_by_email(email) | ||
user = first(sorcery_config.email_attribute_name => email) | ||
!!user ? get(user.id) : nil | ||
end | ||
|
||
# NOTE | ||
# DM Adapter dependent | ||
def get_current_users | ||
unless self.repository.adapter.is_a?(::DataMapper::Adapters::MysqlAdapter) | ||
raise 'Unsupported DataMapper Adapter' | ||
end | ||
config = sorcery_config | ||
ret = all(config.last_logout_at_attribute_name => nil) | | ||
all(config.last_activity_at_attribute_name.gt => config.last_logout_at_attribute_name) | ||
ret = ret.all(config.last_activity_at_attribute_name.not => nil) | ||
ret = ret.all(config.last_activity_at_attribute_name.gt => config.activity_timeout.seconds.ago.utc) | ||
ret | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
README (https://github.com/NoamB/sorcery/pull/472/files#diff-cb3e0f2c76a671c083e8f001970f4631R232) says to add user properties manually, but here I see that these properties are declared automatically. Did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it works similar to the method that defines fields for mongoid, the idea was to inform users that they should declare the 'id' property and take care of adding validators, going to change the wording a bit.