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

- (Object) login

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'



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

def 
  @login
end

Class Method Details

+ (Object) find_for_database_authentication(conditions)



30
31
32
33
# File 'authentication/app/models/refinery/user.rb', line 30

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

Instance Method Details

- (Object) add_role(title)

Raises:

  • (ArgumentException)


64
65
66
67
# File 'authentication/app/models/refinery/user.rb', line 64

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

- (Object) authorized_plugins



46
47
48
# File 'authentication/app/models/refinery/user.rb', line 46

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

- (Boolean) can_delete?(user_to_delete = self)

Returns:

  • (Boolean)


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

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

- (Boolean) can_edit?(user_to_edit = self)

Returns:

  • (Boolean)


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

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

- (Object) create_first



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

def create_first
  if valid?
    # first we need to save user
    save
    # add refinery role
    add_role(:refinery)
    # add superuser role
    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

- (Boolean) has_role?(title)

Returns:

  • (Boolean)

Raises:

  • (ArgumentException)


69
70
71
72
# File 'authentication/app/models/refinery/user.rb', line 69

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

- (Object) plugins=(plugin_names)



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

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

- (Object) to_s



90
91
92
# File 'authentication/app/models/refinery/user.rb', line 90

def to_s
  username.to_s
end