Class: Skyline::User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authentication::User
Defined in:
app/models/skyline/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_passwordObject

This must be set to change the password (by the user himself)



11
12
13
# File 'app/models/skyline/user.rb', line 11

def current_password
  @current_password
end

#editing_myselfObject

This can be set by the admin to force checking of the current password etc



18
19
20
# File 'app/models/skyline/user.rb', line 18

def editing_myself
  @editing_myself
end

#force_passwordObject

This can be set by the admin to change the PW



14
15
16
# File 'app/models/skyline/user.rb', line 14

def force_password
  @force_password
end

#skip_email_validationObject

Returns the value of attribute skip_email_validation.



20
21
22
# File 'app/models/skyline/user.rb', line 20

def skip_email_validation
  @skip_email_validation
end

Class Method Details

.authenticate(email, password) ⇒ Object

Authenticates a user with email and password

Returns

User

The user if authentication passed



52
53
54
55
# File 'app/models/skyline/user.rb', line 52

def authenticate(email,password)
  user = self.first(:conditions => {:email => email.to_s.downcase, :password => encrypt(password.to_s), :is_destroyed => false})
  user && user || false
end

.encrypt(pw) ⇒ Object



61
62
63
# File 'app/models/skyline/user.rb', line 61

def encrypt(pw)
  Digest::SHA1.hexdigest(pw)
end

.extract_valid_email_address(email) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/skyline/user.rb', line 65

def extract_valid_email_address(email)
  if email.kind_of? Mail::Address
    return email.address
  else
    begin
      address = Mail::Address.new(email.to_s)
    rescue
      return false
    end
  end
  if address && address.respond_to?(:domain) && address.domain
    return address.address 
  else
    return false
  end
end

.find_by_identification(identification) ⇒ Object



57
58
59
# File 'app/models/skyline/user.rb', line 57

def find_by_identification(identification)
  self.find_by_id(identification)
end

.per_pageObject



82
# File 'app/models/skyline/user.rb', line 82

def per_page; 30; end

Instance Method Details

#allow?(right_or_class, suffix = nil) ⇒ Boolean

Check if a user has a specific right

Parameters

right_or_class<String,Symbol,~right_prefix>

Can be a string or a symbol to check a specific right, or you can pass an object that responds to #right_prefix, right_prefix must return a string

suffix<String,Symbol>,

A suffix to append to the right, mostly usefull in combination with an object

Returns

<Boolean>

true if the right exists in one of the roles fo the user

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
# File 'app/models/skyline/user.rb', line 102

def allow?(right_or_class,suffix=nil)
  if right_or_class.respond_to?(:right_prefix)
    right_or_class = right_or_class.right_prefix
  end
  
  right = [right_or_class,suffix].compact.map(&:to_s).join("_")
  
  @rights ||= self.rights.map{|r| r.name }.uniq
  @rights.include?(right)
end

#correct_password?(password) ⇒ Boolean

Check if this users password matches.

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'app/models/skyline/user.rb', line 114

def correct_password?(password)
  if self.password_changed?
    self.password == password.to_s
  else
    self.password == self.class.encrypt(password.to_s)
  end
end

#destroyObject

Don’t really destroy the object, just set the is_destroyed? flag.



157
158
159
160
161
162
163
164
# File 'app/models/skyline/user.rb', line 157

def destroy
  unless new_record?
    self.update_attributes(:is_destroyed => true)
    self.grants.collect {|g| g.destroy }
  end
  
  freeze
end

#display_nameObject

Display the name or e-mailaddress of the user for display purposes.



151
152
153
# File 'app/models/skyline/user.rb', line 151

def display_name
  self.name.present? ? self.name : self.email
end

#force_password!(pw) ⇒ Object

Forcefully set a password without any validations Directly saves the object. –



140
141
142
143
# File 'app/models/skyline/user.rb', line 140

def force_password!(pw)
  self.update_attribute(:password, pw)
  pw
end

#generate_new_password!Object

Generate a new password, set it and return it.

Returns

String

The newly set password



132
133
134
135
# File 'app/models/skyline/user.rb', line 132

def generate_new_password!
  pw = SimplePassword.new(8).to_s
  self.force_password!(pw)
end

#identificationObject



86
87
88
# File 'app/models/skyline/user.rb', line 86

def identification
  self.id
end

#reactivate(attributes) ⇒ Object

Reactivate user with these attributes, if they are valid



175
176
177
178
179
180
181
182
183
# File 'app/models/skyline/user.rb', line 175

def reactivate(attributes)
  temp_user = Skyline::User.new(attributes)
  temp_user.skip_email_validation = true
  if temp_user.valid?
    self.attributes = attributes
    self.force_password! attributes[:password]
    self.is_destroyed = false
  end
end

#viewable_rolesObject



166
167
168
169
170
171
172
# File 'app/models/skyline/user.rb', line 166

def viewable_roles
  if self.system?
    Skyline::Role.all
  else
    Skyline::Role.all(:conditions => {:system => false})
  end
end