/
reader.rb
321 lines (273 loc) · 8.93 KB
/
reader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require 'authlogic'
require 'digest/sha1'
require 'snail'
require 'vcard'
require "fastercsv"
class Reader < ActiveRecord::Base
@@user_columns = %w{name email created_at password password_confirmation notes}
cattr_accessor :user_columns
cattr_accessor :current
attr_accessor :email_field, :newly_activated, :skip_user_update
acts_as_authentic do |config|
config.validations_scope = :site_id if defined? Site
config.transition_from_restful_authentication = true
config.validate_email_field = false
config.validate_password_field = false
end
belongs_to :user
before_validation :combine_names
before_update :update_user
belongs_to :created_by, :class_name => 'User'
belongs_to :updated_by, :class_name => 'User'
has_many :message_readers
has_many :messages, :through => :message_readers
has_many :memberships
has_many :groups, :through => :memberships, :uniq => true
accepts_nested_attributes_for :memberships
validates_presence_of :name, :forename, :surname, :email
validates_uniqueness_of :nickname, :allow_blank => true
validates_length_of :name, :forename, :surname, :maximum => 100, :allow_nil => false
validates_length_of :password, :minimum => 5, :allow_nil => false, :unless => :existing_reader_keeping_password?
# validates_format_of :password, :with => /[^A-Za-z]/, :unless => :existing_reader_keeping_password? # we have to match radiant so that users can log in both ways
validates_confirmation_of :password, :unless => :existing_reader_keeping_password?
validate :email_must_not_be_in_use
include RFC822
validates_format_of :email, :with => RFC822_valid
default_scope :order => 'name ASC'
named_scope :any
named_scope :none, { :conditions => "1 = 0" } # nasty! but doesn't break chains
named_scope :active, :conditions => "activated_at IS NOT NULL"
named_scope :inactive, :conditions => "activated_at IS NULL"
named_scope :imported, :conditions => "old_id IS NOT NULL"
named_scope :disabled, :conditions => "disabled = 1"
named_scope :enabled, :conditions => "disabled = 0"
named_scope :except, lambda { |readers|
readers = [readers].flatten.compact
if readers.any?
{ :conditions => ["NOT readers.id IN (#{readers.map{"?"}.join(',')})", *readers.map(&:id)] }
else
{ }
end
}
named_scope :in_groups, lambda { |groups|
{
:select => "readers.*",
:joins => "INNER JOIN memberships as mm on mm.reader_id = readers.id",
:conditions => ["mm.group_id IN (#{groups.map{'?'}.join(',')})", *groups.map{|g| g.is_a?(Group) ? g.id : g}],
:group => "mm.reader_id"
}
}
def self.find_by_nickname_or_email(nickname_or_email)
reader = find(:first, :conditions => ["nickname = ? OR email = ?", nickname_or_email, nickname_or_email])
end
def self.for_user(user)
if user.respond_to?(:site) && site = Page.current_site
reader = self.find_or_create_by_site_id_and_user_id(site.id, user.id)
else
reader = self.find_or_create_by_user_id(user.id)
end
if reader.new_record?
user_columns.each { |att| reader.send("#{att.to_s}=", user.send(att)) }
reader.crypted_password = user.password
reader.password_salt = user.salt
reader.activated_at = reader.created_at
reader.save(false)
end
reader
end
def self.visible_to(reader=nil)
case Radiant.config['reader.directory_visibility']
when 'public'
self.all
when 'private'
reader ? self.all : self.none
when 'grouped'
reader ? self.in_groups(reader.group_ids) : self.none
else
self.none
end
end
def visible_to?(reader=nil)
(reader && (reader == self)) || self.class.visible_to(reader).map(&:id).include?(self.id)
end
def can_see? (this)
this.groups.empty? or in_any_of_these_groups?(this.groups)
end
def in_any_of_these_groups? (grouplist)
(grouplist & groups).any?
end
def membership_of(group)
memberships.of(group).first
end
def has_group? (group)
!!membership_of(group)
end
alias :is_in? :has_group?
def is_grouped?
groups.any?
end
def preferred_name
nickname? ? nickname : name
end
def preferred_informal_name
nickname? ? nickname : forename
end
def postal_address?
!post_line1.blank? && !post_city.blank?
end
def postal_address
Snail.new(
:line_1 => post_line1,
:line_2 => post_line2,
:city => post_city,
:region => post_province,
:postal_code => postcode,
:country => post_country
)
end
def vcard
@vcard ||= Vpim::Vcard::Maker.make2 do |maker|
maker.add_name do |n|
n.prefix = honorific || ""
n.given = forename || ""
n.family = surname || ""
end
maker.add_addr {|a|
a.location = 'home' # until we do this properly with multiple contact sets
a.country = post_country || ""
a.region = post_province || ""
a.locality = post_city || ""
a.street = [post_line1, post_line2].compact.join("\n")
a.postalcode = postcode || ""
}
maker.add_tel phone { |t| t.location = 'home' } unless phone.blank?
maker.add_tel mobile { |t| t.location = 'cell' } unless mobile.blank?
maker.add_email email { |e| t.location = 'home' }
end
end
def filename
name.downcase.gsub(/\W/, '_')
end
def activate!
self.activated_at = Time.now.utc
self.newly_activated = true
self.save!
send_welcome_message
send_group_welcomes
end
def activated?
!inactive?
end
def newly_activated?
!!newly_activated
end
def inactive?
self.activated_at.nil?
end
def disable_perishable_token_maintenance?
inactive? && !new_record?
end
[:activation, :invitation, :welcome, :password_reset].each do |function|
define_method("send_#{function}_message".intern) {
send_functional_message(function)
}
end
def send_functional_message(function, group=nil)
reset_perishable_token!
message = Message.functional(function, group) # returns the standard functional message if no group is supplied, or no group message exists
raise StandardError, "No #{function} message could be found" unless message
message.deliver_to(self)
end
def generate_email_field_name
self.email_field = Authlogic::Random.friendly_token
end
def is_user?
!!self.user
end
def is_admin?
is_user? && self.user.admin?
end
def create_password!
self.clear_password = self.password_confirmation = self.randomize_password # randomize_password is provided by authlogic
self.save! unless self.new_record?
self.clear_password
end
def find_homepage
if homegroup = groups.with_home_page.first
homegroup.homepage
end
end
def home_url
if homepage = self.find_homepage
homepage.url
else
nil
end
end
# Generates a csv file listing the supplied group of readers.
# No access checks are performed here.
#
def self.csv_for(readers=[])
columns = %w{forename surname email phone mobile postal_address}
FasterCSV.generate do |csv|
csv << columns.map { |f| I18n.t("activerecord.attributes.reader.#{f}") }
readers.each { |r| csv << columns.map{ |f| r.send(f.to_sym) } }
end
end
# Generates a vcard file containing the supplied group of readers.
# No access checks are performed here.
#
def self.vcards_for(readers=[])
readers.map(&:vcard).join("\n")
end
def send_group_invitation_message(group=nil)
send_functional_message('group_invitation', group)
end
private
def combine_names
if self.forename_changed? || self.surname_changed?
self.name = "#{self.forename} #{self.surname}"
elsif self.name_changed?
self.forename = self.name.split(/\s+/).first
self.surname = self.name.split(/\s+/).last
end
self.forename = self.name.split(/\s+/).first unless self.forename?
self.surname = self.name.split(/\s+/).last unless self.surname?
self.name = "#{self.forename} #{self.surname}" unless self.name?
end
def email_must_not_be_in_use
reader = Reader.find_by_email(self.email)
user = User.find_by_email(self.email)
if user && user != self.user
errors.add :email, :taken_by_author
elsif reader && reader != self
errors.add :email, :taken
else
return true
end
return false
end
def existing_reader_keeping_password?
!new_record? && !password_changed?
end
def update_user
if self.user && !self.skip_user_update
changed_columns = Reader.user_columns & self.changed
att = self.attributes.slice(*changed_columns)
att['password'] = self.password if self.crypted_password_changed?
self.user.send :update_with, att if att.any?
end
true
end
def update_with(att)
self.skip_user_update = true
if att['password']
att["clear_password"] = att["password_confirmation"] = att["password"]
end
self.update_attributes(att)
self.skip_user_update = false
end
def send_group_welcomes
groups.each { |g| g.send_welcome_to(self) }
end
end