/
mongoid.rb
82 lines (69 loc) · 2.74 KB
/
mongoid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module Sorcery
module Model
module Adapters
module Mongoid
def self.included(klass)
klass.extend ClassMethods
klass.send(:include, InstanceMethods)
end
module InstanceMethods
def increment(attr)
self.inc(attr,1)
end
def update_single_attribute(name, value)
value = value.utc if value.is_a?(ActiveSupport::TimeWithZone)
self.send(:"#{name}=", value)
self.class.where(:_id => self.id).update_all(name => value)
end
end
module ClassMethods
def credential_regex(credential)
return { :$regex => /^#{credential}$/i } if (@sorcery_config.downcase_username_before_authenticating)
return credential
end
def find_by_credentials(credentials)
@sorcery_config.username_attribute_names.each do |attribute|
@user = where(attribute => credential_regex(credentials[0])).first
break if @user
end
@user
end
def find_by_provider_and_uid(provider, uid)
@user_klass ||= ::Sorcery::Controller::Config.user_class.to_s.constantize
where(@user_klass.sorcery_config.provider_attribute_name => provider, @user_klass.sorcery_config.provider_uid_attribute_name => uid).first
end
def find_by_id(id)
find(id)
rescue ::Mongoid::Errors::DocumentNotFound
nil
end
def find_by_activation_token(token)
where(sorcery_config.activation_token_attribute_name => token).first
end
def find_by_remember_me_token(token)
where(sorcery_config.remember_me_token_attribute_name => token).first
end
def find_by_username(username)
query = sorcery_config.username_attribute_names.map {|name| {name => username}}
any_of(*query).first
end
def transaction(&blk)
tap(&blk)
end
def find_by_sorcery_token(token_attr_name, token)
where(token_attr_name => token).first
end
def find_by_email(email)
where(sorcery_config.email_attribute_name => email).first
end
def get_current_users
config = sorcery_config
where(config.last_activity_at_attribute_name.ne => nil) \
.where("this.#{config.last_logout_at_attribute_name} == null || this.#{config.last_activity_at_attribute_name} > this.#{config.last_logout_at_attribute_name}") \
.where(config.last_activity_at_attribute_name.gt => config.activity_timeout.seconds.ago.utc).order_by([:_id,:asc])
end
end
end
end
end
end