Class: Refinery::User

Inherits:
Core::BaseModel show all
Extended by:
FriendlyId
Defined in:
authentication/app/models/refinery/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'



23
24
25
# File 'authentication/app/models/refinery/user.rb', line 23

def 
  @login
end

Class Method Details

.find_for_database_authentication(conditions) ⇒ Object



32
33
34
35
# File 'authentication/app/models/refinery/user.rb', line 32

def find_for_database_authentication(conditions)
  value = conditions[authentication_keys.first]
  where(["username = :value OR email = :value", { :value => value }]).first
end

Instance Method Details

#add_role(title) ⇒ Object

Raises:

  • (ArgumentException)


66
67
68
69
# File 'authentication/app/models/refinery/user.rb', line 66

def add_role(title)
  raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(::Refinery::Role)
  roles << ::Refinery::Role[title] unless has_role?(title)
end

#authorized_pluginsObject



48
49
50
# File 'authentication/app/models/refinery/user.rb', line 48

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

#can_delete?(user_to_delete = self) ⇒ Boolean



52
53
54
55
56
57
# File 'authentication/app/models/refinery/user.rb', line 52

def can_delete?(user_to_delete = self)
  user_to_delete.persisted? &&
    !user_to_delete.has_role?(:superuser) &&
    ::Refinery::Role[:refinery].users.any? &&
    id != user_to_delete.id
end

#can_edit?(user_to_edit = self) ⇒ Boolean



59
60
61
62
63
64
# File 'authentication/app/models/refinery/user.rb', line 59

def can_edit?(user_to_edit = self)
  user_to_edit.persisted? && (
    user_to_edit == self ||
    self.has_role?(:superuser)
  )
end

#create_firstObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'authentication/app/models/refinery/user.rb', line 76

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::Role[:refinery].users.count == 1
    # add plugins
    self.plugins = Refinery::Plugins.registered.in_menu.names
  end

  # return true/false based on validations
  valid?
end

#has_role?(title) ⇒ Boolean

Raises:

  • (ArgumentException)


71
72
73
74
# File 'authentication/app/models/refinery/user.rb', line 71

def has_role?(title)
  raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(::Refinery::Role)
  roles.any?{|r| r.title == title.to_s.camelize}
end

#plugins=(plugin_names) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'authentication/app/models/refinery/user.rb', line 38

def plugins=(plugin_names)
  if persisted? # don't add plugins when the user_id is nil.
    UserPlugin.delete_all(:user_id => id)

    plugin_names.each_with_index do |plugin_name, index|
      plugins.create(:name => plugin_name, :position => index) if plugin_name.is_a?(String)
    end
  end
end

#to_sObject



92
93
94
# File 'authentication/app/models/refinery/user.rb', line 92

def to_s
  username.to_s
end