change_password.rb
3.71 KB
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
class ChangePassword < Task
settings_items :field, :value
attr_accessor :password, :password_confirmation, :environment_id
include Noosfero::Plugin::HotSpot
def self.human_attribute_name(attrib)
case attrib.to_sym
when :field
_('Field')
when :value
_('Value')
when :password
_('Password')
when :password_confirmation
_('Password Confirmation')
else
_(self.superclass.human_attribute_name(attrib))
end
end
def plugins_fields
plugins.dispatch(:change_password_fields).inject({}) { |result, fields| result.merge!(fields)}
end
def environment
(requestor.environment if requestor) || Environment.find_by_id(environment_id)
end
def fields
%w[login email] + plugins_fields.map { |field, name| field.to_s }
end
def fields_choice
[
[_('Username'), 'login'],
[_('Email'), 'email'],
] + plugins_fields.map { |field, name| [name, field] }
end
###################################################
# validations for creating a ChangePassword task
validates_presence_of :field, :value, :environment_id, :on => :create, :message => _('must be filled in')
# TODO Only on rails3
# validates_inclusion_of :field, :in => lambda { |data| data.fields }
validates_each :field do |data, attr, value|
unless data.fields.include?(value)
data.errors.add(attr, _('is not in the list of valid fields.'))
end
end
validates_each :value, :on => :create do |data,attr,value|
unless data.field.blank? || data.value.blank?
user = data.user_find
if user.nil?
data.errors.add(:value, _('"%s" is not a valid %s.') % [value.to_s, human_attribute_name(data.field)])
end
end
end
before_validation do |change_password|
user = change_password.user_find
change_password.requestor = user.person if user
end
###################################################
# validations for updating a ChangePassword task
# only require the new password when actually changing it.
validates_presence_of :password, :on => :update, :if => lambda { |change| change.status != Task::Status::CANCELLED }
validates_presence_of :password_confirmation, :on => :update, :if => lambda { |change| change.status != Task::Status::CANCELLED }
validates_confirmation_of :password, :if => lambda { |change| change.status != Task::Status::CANCELLED }
def user_find
begin
method = "find_by_#{field}_and_environment_id"
user = nil
user = User.send(method, value, environment_id) if User.respond_to?(method)
user = Person.send(method, value, environment_id).user if user.nil? && Person.respond_to?(method)
rescue
end
user
end
def title
_("Change password")
end
def information
{:message => _('%{requestor} wants to change its password.')}
end
def icon
{:type => :profile_image, :profile => requestor, :url => requestor.url}
end
def perform
user = self.requestor.user
user.force_change_password!(self.password, self.password_confirmation)
end
def target_notification_description
_('%{requestor} wants to change its password.') % {:requestor => requestor.name}
end
# overriding messages
def task_cancelled_message
_('Your password change request was cancelled at %s.') % Time.now.to_s
end
def task_finished_message
_('Your password was changed successfully.')
end
include ActionController::UrlWriter
def task_created_message
hostname = self.requestor.environment.default_hostname
code = self.code
url = url_for(:host => hostname, :controller => 'account', :action => 'new_password', :code => code)
lambda do
_("In order to change your password, please visit the following address:\n\n%s") % url
end
end
end