Class: Refinery::Authentication::Devise::User
- Inherits:
-
Core::BaseModel
- Object
- Core::BaseModel
- Refinery::Authentication::Devise::User
- Extended by:
- FriendlyId
- Defined in:
- app/models/refinery/authentication/devise/user.rb
Instance Attribute Summary collapse
-
#login ⇒ Object
Setup accessible (or protected) attributes for your model :login is a virtual attribute for authenticating by either username or email This is in addition to a real persisted field like ‘username’.
Class Method Summary collapse
-
.find_for_database_authentication(conditions) ⇒ Object
Find user by email or username.
- .find_or_initialize_with_error_by_reset_password_token(original_token) ⇒ Object
Instance Method Summary collapse
- #active_plugins ⇒ Object
- #add_role(title) ⇒ Object
- #authorised_plugins ⇒ Object (also: #authorized_plugins)
- #can_delete?(user_to_delete = self) ⇒ Boolean
- #can_edit?(user_to_edit = self) ⇒ Boolean
- #create_first ⇒ Object
-
#generate_reset_password_token! ⇒ Object
Call devise reset function, taken from github.com/plataformatec/devise/blob/v3.2.4/lib/devise/models/recoverable.rb#L45-L56.
- #has_plugin?(name) ⇒ Boolean
- #has_role?(title) ⇒ Boolean
-
#landing_url ⇒ Object
Returns a URL to the first plugin with a URL in the menu.
- #plugins=(plugin_names) ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#login ⇒ Object
Setup accessible (or protected) attributes for your model :login is a virtual attribute for authenticating by either username or email This is in addition to a real persisted field like ‘username’
29 30 31 |
# File 'app/models/refinery/authentication/devise/user.rb', line 29 def login @login end |
Class Method Details
.find_for_database_authentication(conditions) ⇒ Object
Find user by email or username. github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address
37 38 39 40 |
# File 'app/models/refinery/authentication/devise/user.rb', line 37 def find_for_database_authentication(conditions) value = conditions[authentication_keys.first] where(["username = :value OR email = :value", { value: value }]).first end |
.find_or_initialize_with_error_by_reset_password_token(original_token) ⇒ Object
42 43 44 45 |
# File 'app/models/refinery/authentication/devise/user.rb', line 42 def find_or_initialize_with_error_by_reset_password_token(original_token) find_or_initialize_with_error_by :reset_password_token, ::Devise.token_generator.digest(self, :reset_password_token, original_token) end |
Instance Method Details
#active_plugins ⇒ Object
66 67 68 69 70 71 72 |
# File 'app/models/refinery/authentication/devise/user.rb', line 66 def active_plugins @active_plugins ||= Refinery::Plugins.new( Refinery::Plugins.registered.select do |plugin| has_role?(:superuser) || .include?(plugin.name) end ) end |
#add_role(title) ⇒ Object
101 102 103 104 |
# File 'app/models/refinery/authentication/devise/user.rb', line 101 def add_role(title) raise ::ArgumentError, "Role should be the title of the role not a role object." if title.is_a?(::Refinery::Authentication::Devise::Role) roles << ::Refinery::Authentication::Devise::Role[title] unless has_role?(title) end |
#authorised_plugins ⇒ Object Also known as:
78 79 80 |
# File 'app/models/refinery/authentication/devise/user.rb', line 78 def plugins.collect(&:name) | ::Refinery::Plugins.always_allowed.names end |
#can_delete?(user_to_delete = self) ⇒ Boolean
90 91 92 93 94 95 |
# File 'app/models/refinery/authentication/devise/user.rb', line 90 def can_delete?(user_to_delete = self) user_to_delete.persisted? && !user_to_delete.has_role?(:superuser) && ::Refinery::Authentication::Devise::Role[:refinery].users.any? && id != user_to_delete.id end |
#can_edit?(user_to_edit = self) ⇒ Boolean
97 98 99 |
# File 'app/models/refinery/authentication/devise/user.rb', line 97 def can_edit?(user_to_edit = self) user_to_edit.persisted? && (user_to_edit == self || self.has_role?(:superuser)) end |
#create_first ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/refinery/authentication/devise/user.rb', line 111 def create_first if valid? # first we need to save user save # add refinery role add_role(:refinery) # add superuser role if there are no other users add_role(:superuser) if ::Refinery::Authentication::Devise::Role[:refinery].users.count == 1 # add plugins self.plugins = Refinery::Plugins.registered..names end # return true/false based on validations valid? end |
#generate_reset_password_token! ⇒ Object
Call devise reset function, taken from github.com/plataformatec/devise/blob/v3.2.4/lib/devise/models/recoverable.rb#L45-L56
50 51 52 53 54 55 56 57 |
# File 'app/models/refinery/authentication/devise/user.rb', line 50 def generate_reset_password_token! raw, enc = ::Devise.token_generator.generate(self.class, :reset_password_token) update_attributes( :reset_password_token => enc, :reset_password_sent_at => Time.now.utc ) raw end |
#has_plugin?(name) ⇒ Boolean
74 75 76 |
# File 'app/models/refinery/authentication/devise/user.rb', line 74 def has_plugin?(name) active_plugins.names.include?(name) end |
#has_role?(title) ⇒ Boolean
106 107 108 109 |
# File 'app/models/refinery/authentication/devise/user.rb', line 106 def has_role?(title) raise ::ArgumentError, "Role should be the title of the role not a role object." if title.is_a?(::Refinery::Authentication::Devise::Role) roles.any?{ |r| r.title == title.to_s.camelize} end |
#landing_url ⇒ Object
Returns a URL to the first plugin with a URL in the menu. Used for admin user’s root admin url. See Refinery::Core::NilUser#landing_url.
86 87 88 |
# File 'app/models/refinery/authentication/devise/user.rb', line 86 def landing_url active_plugins.. end |
#plugins=(plugin_names) ⇒ Object
59 60 61 62 63 64 |
# File 'app/models/refinery/authentication/devise/user.rb', line 59 def plugins=(plugin_names) return :can_not_set_plugins_when_not_persisted unless persisted? filtered_names = filter_existing_plugins_for(string_plugin_names(plugin_names)) create_plugins_for(filtered_names) end |
#to_s ⇒ Object
127 128 129 |
# File 'app/models/refinery/authentication/devise/user.rb', line 127 def to_s (full_name.presence || username).to_s end |