Class: Refinery::Authentication::Devise::User

Inherits:
Core::BaseModel
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/refinery/authentication/devise/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loginObject

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
end

Class Method Details

.find_for_database_authentication(conditions) ⇒ Object



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_pluginsObject



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) || authorised_plugins.include?(plugin.name)
    end
  )
end

#add_role(title) ⇒ Object

Raises:

  • (::ArgumentError)


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_pluginsObject Also known as: authorized_plugins



78
79
80
# File 'app/models/refinery/authentication/devise/user.rb', line 78

def authorised_plugins
  plugins.collect(&:name) | ::Refinery::Plugins.always_allowed.names
end

#can_delete?(user_to_delete = self) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_firstObject



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.in_menu.names
  end

  # return true/false based on validations
  valid?
end

#generate_reset_password_token!Object



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

Returns:

  • (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

Returns:

  • (Boolean)

Raises:

  • (::ArgumentError)


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_urlObject

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.in_menu.first_url_in_menu
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_sObject



127
128
129
# File 'app/models/refinery/authentication/devise/user.rb', line 127

def to_s
  (full_name.presence || username).to_s
end