Commit 347d0ca2bc5c6517cd888b4afe05b298f2570942

Authored by Ábner Silva de Oliveira
2 parents 0c93a29d fedff2e4

Merge branch 'production' of gitlab.com:participa/noosfero into production

app/controllers/admin/environment_email_templates_controller.rb 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +class EnvironmentEmailTemplatesController < EmailTemplatesController
  2 +
  3 + protect 'manage_email_templates', :environment
  4 +
  5 + protected
  6 +
  7 + def owner
  8 + environment
  9 + end
  10 +
  11 + before_filter :only => :index do
  12 + @back_to = url_for(:controller => :admin_panel)
  13 + end
  14 +
  15 +end
app/controllers/email_templates_controller.rb 0 → 100644
@@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
  1 +class EmailTemplatesController < ApplicationController
  2 +
  3 + def index
  4 + @email_templates = owner.email_templates
  5 + end
  6 +
  7 + def show
  8 + @email_template = owner.email_templates.find(params[:id])
  9 +
  10 + respond_to do |format|
  11 + format.html # show.html.erb
  12 + format.json { render json: @email_template }
  13 + end
  14 + end
  15 +
  16 + def show_parsed
  17 + @email_template = owner.email_templates.find(params[:id])
  18 + template_params = {:profile => owner, :environment => environment}
  19 + render json: {:parsed_body => @email_template.parsed_body(template_params), :parsed_subject => @email_template.parsed_subject(template_params)}
  20 + end
  21 +
  22 + def new
  23 + @email_template = owner.email_templates.build(:owner => owner)
  24 + end
  25 +
  26 + def edit
  27 + @email_template = owner.email_templates.find(params[:id])
  28 + end
  29 +
  30 + def create
  31 + @email_template = owner.email_templates.build(params[:email_template])
  32 + @email_template.owner = owner
  33 +
  34 + if @email_template.save
  35 + session[:notice] = _('Email template was successfully created.')
  36 + redirect_to url_for(:action => :index)
  37 + else
  38 + render action: "new"
  39 + end
  40 + end
  41 +
  42 + def update
  43 + @email_template = owner.email_templates.find(params[:id])
  44 +
  45 + if @email_template.update_attributes(params[:email_template])
  46 + session[:notice] = _('Email template was successfully updated.')
  47 + redirect_to url_for(:action => :index)
  48 + else
  49 + render action: "edit"
  50 + end
  51 + end
  52 +
  53 + def destroy
  54 + @email_template = owner.email_templates.find(params[:id])
  55 + @email_template.destroy
  56 +
  57 + respond_to do |format|
  58 + format.html { redirect_to url_for(:action => :index)}
  59 + format.json { head :no_content }
  60 + end
  61 + end
  62 +end
app/controllers/my_profile/email_templates_controller.rb
@@ -1,63 +0,0 @@ @@ -1,63 +0,0 @@
1 -class EmailTemplatesController < MyProfileController  
2 -  
3 - protect 'manage_email_templates', :profile  
4 -  
5 - def index  
6 - @email_templates = profile.email_templates  
7 - end  
8 -  
9 - def show  
10 - @email_template = profile.email_templates.find(params[:id])  
11 -  
12 - respond_to do |format|  
13 - format.html # show.html.erb  
14 - format.json { render json: @email_template }  
15 - end  
16 - end  
17 -  
18 - def show_parsed  
19 - @email_template = profile.email_templates.find(params[:id])  
20 - template_params = {:profile => profile, :environment => environment}  
21 - render json: {:parsed_body => @email_template.parsed_body(template_params), :parsed_subject => @email_template.parsed_subject(template_params)}  
22 - end  
23 -  
24 - def new  
25 - @email_template = profile.email_templates.build  
26 - end  
27 -  
28 - def edit  
29 - @email_template = profile.email_templates.find(params[:id])  
30 - end  
31 -  
32 - def create  
33 - @email_template = profile.email_templates.build(params[:email_template], :owner => profile)  
34 -  
35 - if @email_template.save  
36 - session[:notice] = _('Email template was successfully created.')  
37 - redirect_to url_for(:action => :index)  
38 - else  
39 - render action: "new"  
40 - end  
41 - end  
42 -  
43 - def update  
44 - @email_template = profile.email_templates.find(params[:id])  
45 -  
46 - if @email_template.update_attributes(params[:email_template])  
47 - session[:notice] = _('Email template was successfully updated.')  
48 - redirect_to url_for(:action => :index)  
49 - else  
50 - render action: "edit"  
51 - end  
52 - end  
53 -  
54 - def destroy  
55 - @email_template = profile.email_templates.find(params[:id])  
56 - @email_template.destroy  
57 -  
58 - respond_to do |format|  
59 - format.html { redirect_to url_for(:action => :index)}  
60 - format.json { head :no_content }  
61 - end  
62 - end  
63 -end  
app/controllers/my_profile/profile_email_templates_controller.rb 0 → 100644
@@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
  1 +class ProfileEmailTemplatesController < EmailTemplatesController
  2 +
  3 + needs_profile
  4 + protect 'manage_email_templates', :profile
  5 +
  6 + protected
  7 +
  8 + def owner
  9 + profile
  10 + end
  11 +
  12 + before_filter :only => :index do
  13 + @back_to = url_for(:controller => :profile_editor)
  14 + end
  15 +
  16 +end
app/helpers/email_template_helper.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +module EmailTemplateHelper
  2 +
  3 + def mail_with_template(params={})
  4 + if params[:email_template].present?
  5 + params[:body] = params[:email_template].parsed_body(params[:template_params])
  6 + params[:subject] = params[:email_template].parsed_subject(params[:template_params])
  7 + params[:content_type] = "text/html"
  8 + end
  9 + mail(params.except(:email_template))
  10 + end
  11 +
  12 +end
app/helpers/task_helper.rb
@@ -5,7 +5,7 @@ module TaskHelper @@ -5,7 +5,7 @@ module TaskHelper
5 5
6 content_tag( 6 content_tag(
7 :div, 7 :div,
8 - labelled_form_field(description, select_tag("tasks[#{task.id}][task][email_template_id]", options_from_collection_for_select(email_templates, :id, :name), :include_blank => include_blank, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed', :profile => profile.identifier))), 8 + labelled_form_field(description, select_tag("tasks[#{task.id}][task][email_template_id]", options_from_collection_for_select(email_templates, :id, :name), :include_blank => include_blank, 'data-url' => url_for(:controller => 'profile_email_templates', :action => 'show_parsed', :profile => profile.identifier))),
9 :class => 'template-selection' 9 :class => 'template-selection'
10 ) 10 )
11 end 11 end
app/mailers/task_mailer.rb
1 class TaskMailer < ActionMailer::Base 1 class TaskMailer < ActionMailer::Base
2 2
  3 + include EmailTemplateHelper
  4 +
3 def target_notification(task, message) 5 def target_notification(task, message)
4 @message = extract_message(message) 6 @message = extract_message(message)
5 @target = task.target.name 7 @target = task.target.name
@@ -33,15 +35,13 @@ class TaskMailer &lt; ActionMailer::Base @@ -33,15 +35,13 @@ class TaskMailer &lt; ActionMailer::Base
33 @requestor = task.requestor.name 35 @requestor = task.requestor.name
34 @environment = task.requestor.environment.name 36 @environment = task.requestor.environment.name
35 @url = url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') 37 @url = url_for(:host => task.requestor.environment.default_hostname, :controller => 'home')
36 - @email_template = task.email_template  
37 - template_params = {:environment => task.requestor.environment, :task => task}  
38 38
39 - mail( 39 + mail_with_template(
40 to: task.requestor.notification_emails, 40 to: task.requestor.notification_emails,
41 from: self.class.generate_from(task), 41 from: self.class.generate_from(task),
42 - subject: @email_template.present? ? @email_template.parsed_subject(template_params) : '[%s] %s' % [task.requestor.environment.name, task.target_notification_description],  
43 - body: @email_template.present? ? @email_template.parsed_body(template_params) : nil,  
44 - content_type: @email_template.present? ? "text/html" : nil 42 + subject: '[%s] %s' % [task.requestor.environment.name, task.target_notification_description],
  43 + email_template: task.email_template,
  44 + template_params: {:environment => task.requestor.environment, :task => task, :message => @message, :url => @url, :requestor => task.requestor}
45 ) 45 )
46 end 46 end
47 47
app/mailers/user_mailer.rb
1 class UserMailer < ActionMailer::Base 1 class UserMailer < ActionMailer::Base
  2 +
  3 + include EmailTemplateHelper
  4 +
2 def activation_email_notify(user) 5 def activation_email_notify(user)
3 user_email = "#{user.login}@#{user.email_domain}" 6 user_email = "#{user.login}@#{user.email_domain}"
4 @name = user.name 7 @name = user.name
@@ -22,10 +25,12 @@ class UserMailer &lt; ActionMailer::Base @@ -22,10 +25,12 @@ class UserMailer &lt; ActionMailer::Base
22 @redirection = (true if user.return_to) 25 @redirection = (true if user.return_to)
23 @join = (user.community_to_join if user.community_to_join) 26 @join = (user.community_to_join if user.community_to_join)
24 27
25 - mail( 28 + mail_with_template(
26 from: "#{user.environment.name} <#{user.environment.contact_email}>", 29 from: "#{user.environment.name} <#{user.environment.contact_email}>",
27 to: user.email, 30 to: user.email,
28 subject: _("[%s] Activate your account") % [user.environment.name], 31 subject: _("[%s] Activate your account") % [user.environment.name],
  32 + template_params: {:environment => user.environment, :activation_code => @activation_code, :redirection => @redirection, :join => @join},
  33 + email_template: user.environment.email_templates.find_by_template_type(:user_activation),
29 ) 34 )
30 end 35 end
31 36
app/models/change_password.rb
@@ -28,6 +28,13 @@ class ChangePassword &lt; Task @@ -28,6 +28,13 @@ class ChangePassword &lt; Task
28 validates_presence_of :password_confirmation, :on => :update, :if => lambda { |change| change.status != Task::Status::CANCELLED } 28 validates_presence_of :password_confirmation, :on => :update, :if => lambda { |change| change.status != Task::Status::CANCELLED }
29 validates_confirmation_of :password, :if => lambda { |change| change.status != Task::Status::CANCELLED } 29 validates_confirmation_of :password, :if => lambda { |change| change.status != Task::Status::CANCELLED }
30 30
  31 + before_save :set_email_template
  32 +
  33 + def set_email_template
  34 + template = environment.email_templates.find_by_template_type(:user_change_password)
  35 + data[:email_template_id] = template.id unless template.nil?
  36 + end
  37 +
31 def environment 38 def environment
32 requestor.environment unless requestor.nil? 39 requestor.environment unless requestor.nil?
33 end 40 end
app/models/email_template.rb
@@ -6,6 +6,10 @@ class EmailTemplate &lt; ActiveRecord::Base @@ -6,6 +6,10 @@ class EmailTemplate &lt; ActiveRecord::Base
6 6
7 validates_presence_of :name 7 validates_presence_of :name
8 8
  9 + validates :name, uniqueness: { scope: [:owner_type, :owner_id] }
  10 +
  11 + validates :template_type, uniqueness: { scope: [:owner_type, :owner_id] }, if: :unique_by_type?
  12 +
9 def parsed_body(params) 13 def parsed_body(params)
10 @parsed_body ||= parse(body, params) 14 @parsed_body ||= parse(body, params)
11 end 15 end
@@ -14,12 +18,26 @@ class EmailTemplate &lt; ActiveRecord::Base @@ -14,12 +18,26 @@ class EmailTemplate &lt; ActiveRecord::Base
14 @parsed_subject ||= parse(subject, params) 18 @parsed_subject ||= parse(subject, params)
15 end 19 end
16 20
  21 + def self.available_types
  22 + {
  23 + :task_rejection => {:description => _('Task Rejection'), :owner_type => Profile},
  24 + :task_acceptance => {:description => _('Task Acceptance'), :owner_type => Profile},
  25 + :organization_members => {:description => _('Organization Members'), :owner_type => Profile},
  26 + :user_activation => {:description => _('User Activation'), :unique => true, :owner_type => Environment},
  27 + :user_change_password => {:description => _('Change User Password'), :unique => true, :owner_type => Environment}
  28 + }
  29 + end
  30 +
17 def available_types 31 def available_types
18 - HashWithIndifferentAccess.new ({  
19 - :task_rejection => {:description => _('Task Rejection')},  
20 - :task_acceptance => {:description => _('Task Acceptance')},  
21 - :organization_members => {:description => _('Organization Members')}  
22 - }) 32 + HashWithIndifferentAccess.new EmailTemplate.available_types.select {|k, v| owner.kind_of?(v[:owner_type])}
  33 + end
  34 +
  35 + def type_description
  36 + available_types.fetch(template_type, {})[:description]
  37 + end
  38 +
  39 + def unique_by_type?
  40 + available_types.fetch(template_type, {})[:unique]
23 end 41 end
24 42
25 protected 43 protected
app/models/environment.rb
@@ -21,6 +21,7 @@ class Environment &lt; ActiveRecord::Base @@ -21,6 +21,7 @@ class Environment &lt; ActiveRecord::Base
21 21
22 has_many :tasks, :dependent => :destroy, :as => 'target' 22 has_many :tasks, :dependent => :destroy, :as => 'target'
23 has_many :search_terms, :as => :context 23 has_many :search_terms, :as => :context
  24 + has_many :email_templates, :foreign_key => :owner_id
24 25
25 IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/ 26 IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/
26 27
@@ -50,6 +51,7 @@ class Environment &lt; ActiveRecord::Base @@ -50,6 +51,7 @@ class Environment &lt; ActiveRecord::Base
50 'manage_environment_licenses' => N_('Manage environment licenses'), 51 'manage_environment_licenses' => N_('Manage environment licenses'),
51 'manage_environment_trusted_sites' => N_('Manage environment trusted sites'), 52 'manage_environment_trusted_sites' => N_('Manage environment trusted sites'),
52 'edit_appearance' => N_('Edit appearance'), 53 'edit_appearance' => N_('Edit appearance'),
  54 + 'manage_email_templates' => N_('Manage Email Templates'),
53 } 55 }
54 56
55 module Roles 57 module Roles
app/models/task.rb
@@ -305,7 +305,8 @@ class Task &lt; ActiveRecord::Base @@ -305,7 +305,8 @@ class Task &lt; ActiveRecord::Base
305 def to_liquid 305 def to_liquid
306 HashWithIndifferentAccess.new({ 306 HashWithIndifferentAccess.new({
307 :requestor => requestor, 307 :requestor => requestor,
308 - :reject_explanation => reject_explanation 308 + :reject_explanation => reject_explanation,
  309 + :code => code
309 }) 310 })
310 end 311 end
311 312
app/views/admin_panel/index.html.erb
@@ -12,6 +12,7 @@ @@ -12,6 +12,7 @@
12 <tr><td><%= link_to _('Licenses'), :controller =>'licenses' %></td></tr> 12 <tr><td><%= link_to _('Licenses'), :controller =>'licenses' %></td></tr>
13 <tr><td><%= link_to _('Trusted sites'), :controller =>'trusted_sites' %></td></tr> 13 <tr><td><%= link_to _('Trusted sites'), :controller =>'trusted_sites' %></td></tr>
14 <tr><td><%= link_to _('Blocks'), :controller => 'features', :action => 'manage_blocks' %></td></tr> 14 <tr><td><%= link_to _('Blocks'), :controller => 'features', :action => 'manage_blocks' %></td></tr>
  15 + <tr><td><%= link_to _('Email templates'), :controller =>'environment_email_templates' %></td></tr>
15 </table> 16 </table>
16 17
17 <h2><%= _('Profiles') %></h2> 18 <h2><%= _('Profiles') %></h2>
app/views/email_templates/_form.html.erb
1 -<%= form_for(@email_template, :url => {:controller => :email_templates, :action => @email_template.persisted? ? :update : :create, :id => @email_template.id}) do |f| %> 1 +<%= form_for(@email_template, :url => {:action => @email_template.persisted? ? :update : :create, :id => @email_template.id}) do |f| %>
2 2
3 <%= error_messages_for :email_template if @email_template.errors.any? %> 3 <%= error_messages_for :email_template if @email_template.errors.any? %>
4 4
@@ -25,7 +25,7 @@ @@ -25,7 +25,7 @@
25 25
26 <div class="actions"> 26 <div class="actions">
27 <%= submit_button(:save, _('Save')) %> 27 <%= submit_button(:save, _('Save')) %>
28 - <%= button(:back, _('Back'), :controller => :email_templates) %> 28 + <%= button(:back, _('Back'), :action => :index) %>
29 </div> 29 </div>
30 30
31 <% end %> 31 <% end %>
app/views/email_templates/index.html.erb
@@ -11,10 +11,10 @@ @@ -11,10 +11,10 @@
11 <% @email_templates.each do |email_template| %> 11 <% @email_templates.each do |email_template| %>
12 <tr> 12 <tr>
13 <td><%= email_template.name %></td> 13 <td><%= email_template.name %></td>
14 - <td><%= email_template.available_types[email_template.template_type][:description] if email_template.template_type.present? %></td> 14 + <td><%= email_template.type_description %></td>
15 <td> 15 <td>
16 - <%= button_without_text(:edit, _('Edit'), {:controller => :email_templates, :action => :edit, :id => email_template.id}) %>  
17 - <%= button_without_text(:remove, _('Remove'), {:controller => :email_templates, :action => :destroy, :id => email_template.id}, method: :delete, data: { confirm: 'Are you sure?' }) %> 16 + <%= button_without_text(:edit, _('Edit'), {:action => :edit, :id => email_template.id}) %>
  17 + <%= button_without_text(:remove, _('Remove'), {:action => :destroy, :id => email_template.id}, method: :delete, data: { confirm: 'Are you sure?' }) %>
18 </td> 18 </td>
19 </tr> 19 </tr>
20 <% end %> 20 <% end %>
@@ -22,6 +22,6 @@ @@ -22,6 +22,6 @@
22 22
23 <br /> 23 <br />
24 24
25 - <%= button(:new, _('New template'), :controller => :email_templates, :action => :new) %>  
26 - <%= button(:back, _('Back to control panel'), :controller => :profile_editor) %> 25 + <%= button(:new, _('New template'), :action => :new) %>
  26 + <%= button(:back, _('Back'), @back_to) %>
27 </div> 27 </div>
app/views/email_templates/show.html.erb
1 <p id="notice"><%= notice %></p> 1 <p id="notice"><%= notice %></p>
2 2
3 3
4 -<%= link_to 'Edit', url_for(:controller => :email_templates, :action => :edit, :id => @email_template.id) %> |  
5 -<%= link_to 'Back', url_for(:controller => :email_templates) %> 4 +<%= link_to 'Edit', url_for(:action => :edit, :id => @email_template.id) %> |
  5 +<%= link_to 'Back', url_for(:action => :index) %>
app/views/profile/send_mail.html.erb
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 6
7 <% if @email_templates.present? %> 7 <% if @email_templates.present? %>
8 <div class="template-selection"> 8 <div class="template-selection">
9 - <%= labelled_form_field(_('Select a template:'), select_tag(:template, options_from_collection_for_select(@email_templates, :id, :name), :include_blank => true, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed'))) %> 9 + <%= labelled_form_field(_('Select a template:'), select_tag(:template, options_from_collection_for_select(@email_templates, :id, :name), :include_blank => true, 'data-url' => url_for(:controller => 'profile_email_templates', :action => 'show_parsed'))) %>
10 </div> 10 </div>
11 <% end %> 11 <% end %>
12 12
app/views/profile_editor/index.html.erb
@@ -72,7 +72,7 @@ @@ -72,7 +72,7 @@
72 72
73 <%= control_panel_button(_('Edit welcome page'), 'welcome-page', :action => 'welcome_page') if has_welcome_page %> 73 <%= control_panel_button(_('Edit welcome page'), 'welcome-page', :action => 'welcome_page') if has_welcome_page %>
74 74
75 - <%= control_panel_button(_('Email Templates'), 'email-templates', :controller => :email_templates) if profile.organization? %> 75 + <%= control_panel_button(_('Email Templates'), 'email-templates', :controller => :profile_email_templates) if profile.organization? %>
76 76
77 <% @plugins.dispatch(:control_panel_buttons).each do |button| %> 77 <% @plugins.dispatch(:control_panel_buttons).each do |button| %>
78 <%= control_panel_button(button[:title], button[:icon], button[:url], button[:html_options]) %> 78 <%= control_panel_button(button[:title], button[:icon], button[:url], button[:html_options]) %>
config/initializers/eager_load.rb
1 -Rails.application.eager_load! 1 +Rails.application.eager_load! if ActiveRecord::Base.connection.table_exists? 'categories'
lib/noosfero/api/helpers.rb
@@ -57,7 +57,7 @@ @@ -57,7 +57,7 @@
57 57
58 def find_article(articles, id) 58 def find_article(articles, id)
59 article = articles.find(id) 59 article = articles.find(id)
60 - article.display_to?(current_user) ? article : forbidden! 60 + article.display_to?(current_person) ? article : forbidden!
61 end 61 end
62 62
63 def post_article(asset, params) 63 def post_article(asset, params)
@@ -350,6 +350,7 @@ @@ -350,6 +350,7 @@
350 begin 350 begin
351 body = https.request(request).body 351 body = https.request(request).body
352 rescue Exception => e 352 rescue Exception => e
  353 + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log"))
353 logger.error e 354 logger.error e
354 return _("Google recaptcha error: #{e.message}") 355 return _("Google recaptcha error: #{e.message}")
355 end 356 end
@@ -375,6 +376,7 @@ @@ -375,6 +376,7 @@
375 begin 376 begin
376 body = https.request(request).body 377 body = https.request(request).body
377 rescue Exception => e 378 rescue Exception => e
  379 + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log"))
378 logger.error e 380 logger.error e
379 return _("Google recaptcha error: #{e.message}") 381 return _("Google recaptcha error: #{e.message}")
380 end 382 end
@@ -393,6 +395,7 @@ @@ -393,6 +395,7 @@
393 begin 395 begin
394 body = http.request(request).body 396 body = http.request(request).body
395 rescue Exception => e 397 rescue Exception => e
  398 + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log"))
396 logger.error e 399 logger.error e
397 return _("Serpro captcha error: #{e.message}") 400 return _("Serpro captcha error: #{e.message}")
398 end 401 end
lib/noosfero/api/session.rb
@@ -40,8 +40,9 @@ module Noosfero @@ -40,8 +40,9 @@ module Noosfero
40 attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields 40 attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields
41 remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR']) 41 remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR'])
42 42
43 - unless test_captcha(remote_ip, params, environment) == true  
44 - render_api_error!(_('Please solve the test in order to register.'), 401) 43 + result = test_captcha(remote_ip, params, environment)
  44 + unless result == true
  45 + render_api_error!(result, 401)
45 return 46 return
46 end 47 end
47 48
@@ -54,6 +55,42 @@ module Noosfero @@ -54,6 +55,42 @@ module Noosfero
54 render_api_error!(message, 400) 55 render_api_error!(message, 400)
55 end 56 end
56 end 57 end
  58 +
  59 + params do
  60 + requires :activation_code, type: String, desc: _("Activation token")
  61 + end
  62 +
  63 + # Activate a user.
  64 + #
  65 + # Parameter:
  66 + # activation_code (required) - Activation token
  67 + # Example Request:
  68 + # PATCH /activate?activation_code=28259abd12cc6a64ef9399cf3286cb998b96aeaf
  69 + patch "/activate" do
  70 + user = User.find_by_activation_code(params[:activation_code])
  71 + if user
  72 + unless user.environment.enabled?('admin_must_approve_new_users')
  73 + if user.activate
  74 + user.generate_private_token!
  75 + present user, :with => Entities::UserLogin
  76 + end
  77 + else
  78 + if user.create_moderate_task
  79 + user.activation_code = nil
  80 + user.save!
  81 +
  82 + # Waiting for admin moderate user registration
  83 + status 202
  84 + body({
  85 + :message => 'Waiting for admin moderate user registration'
  86 + })
  87 + end
  88 + end
  89 + else
  90 + # Token not found in database
  91 + render_api_error!(_('Token is invalid'), 412)
  92 + end
  93 + end
57 end 94 end
58 end 95 end
59 end 96 end
lib/noosfero/api/v1/articles.rb
@@ -96,7 +96,7 @@ module Noosfero @@ -96,7 +96,7 @@ module Noosfero
96 #TODO make tests for this situation 96 #TODO make tests for this situation
97 votes_order = params.delete(:order) if params[:order]=='votes_score' 97 votes_order = params.delete(:order) if params[:order]=='votes_score'
98 articles = select_filtered_collection_of(article, 'children', params) 98 articles = select_filtered_collection_of(article, 'children', params)
99 - articles = articles.display_filter(current_person, nil) 99 + articles = articles.display_filter(current_person, article.profile)
100 100
101 101
102 #TODO make tests for this situation 102 #TODO make tests for this situation
plugins/proposals_discussion
1 -Subproject commit bba03dc8febb8d44564339bc08fb71eb65349d8a 1 +Subproject commit da9cae0554eb4d16a7a910b13bf425c0fa9debda
test/fixtures/roles.yml
@@ -38,6 +38,7 @@ four: @@ -38,6 +38,7 @@ four:
38 - manage_environment_organizations 38 - manage_environment_organizations
39 - manage_environment_templates 39 - manage_environment_templates
40 - manage_environment_licenses 40 - manage_environment_licenses
  41 + - manage_email_templates
41 profile_admin: 42 profile_admin:
42 id: 5 43 id: 5
43 environment_id: 1 44 environment_id: 1
@@ -60,6 +61,7 @@ profile_admin: @@ -60,6 +61,7 @@ profile_admin:
60 - manage_friends 61 - manage_friends
61 - validate_enterprise 62 - validate_enterprise
62 - publish_content 63 - publish_content
  64 + - manage_email_templates
63 profile_member: 65 profile_member:
64 id: 6 66 id: 6
65 environment_id: 1 67 environment_id: 1
@@ -100,3 +102,4 @@ environment_administrator: @@ -100,3 +102,4 @@ environment_administrator:
100 - destroy_profile 102 - destroy_profile
101 - manage_environment_templates 103 - manage_environment_templates
102 - manage_environment_licenses 104 - manage_environment_licenses
  105 + - manage_email_templates
test/functional/email_templates_controller_test.rb
@@ -1,68 +0,0 @@ @@ -1,68 +0,0 @@
1 -require 'test_helper'  
2 -  
3 -class EmailTemplatesControllerTest < ActionController::TestCase  
4 -  
5 - setup do  
6 - @profile = fast_create(Community)  
7 - @email_template = EmailTemplate.create!(:name => 'template', :owner => @profile)  
8 - @person = create_user_with_permission('templatemanager', 'manage_email_templates', @profile)  
9 - login_as(@person.user.login)  
10 - end  
11 -  
12 - attr_accessor :profile, :person  
13 -  
14 - test "should get index" do  
15 - get :index, :profile => profile.identifier  
16 - assert_response :success  
17 - assert_not_nil assigns(:email_templates)  
18 - end  
19 -  
20 - test "should get new" do  
21 - get :new, :profile => profile.identifier  
22 - assert_response :success  
23 - end  
24 -  
25 - test "should create email_template" do  
26 - assert_difference('EmailTemplate.count') do  
27 - post :create, email_template: { :name => 'test' }, :profile => profile.identifier  
28 - end  
29 -  
30 - assert_redirected_to url_for(:action => :index)  
31 - end  
32 -  
33 - test "should show email_template" do  
34 - get :show, id: @email_template, :profile => profile.identifier  
35 - assert_response :success  
36 - end  
37 -  
38 - test "should get edit" do  
39 - get :edit, id: @email_template, :profile => profile.identifier  
40 - assert_response :success  
41 - end  
42 -  
43 - test "should update email_template" do  
44 - put :update, id: @email_template, email_template: { }, :profile => profile.identifier  
45 - assert_redirected_to url_for(:action => :index)  
46 - end  
47 -  
48 - test "should destroy email_template" do  
49 - assert_difference('EmailTemplate.count', -1) do  
50 - delete :destroy, id: @email_template, :profile => profile.identifier  
51 - end  
52 -  
53 - assert_redirected_to url_for(:action => :index)  
54 - end  
55 -  
56 - test "should get parsed template" do  
57 - environment = Environment.default  
58 - @email_template.subject = '{{profile.name}} - {{profile.identifier}}'  
59 - @email_template.body = '{{profile.name}} - {{profile.identifier}} - {{environment.name}}'  
60 - @email_template.save!  
61 - get :show_parsed, id: @email_template, :profile => profile.identifier  
62 - assert_response :success  
63 - json_response = ActiveSupport::JSON.decode(@response.body)  
64 - assert_equal "#{profile.name} - #{profile.identifier}", json_response['parsed_subject']  
65 - assert_equal "#{profile.name} - #{profile.identifier} - #{environment.name}", json_response['parsed_body']  
66 - end  
67 -  
68 -end  
test/functional/environment_email_templates_controller_test.rb 0 → 100644
@@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
  1 +require 'test_helper'
  2 +
  3 +class EnvironmentEmailTemplatesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @email_template = EmailTemplate.create!(:name => 'template', :owner => Environment.default)
  7 + person = create_user_with_permission('template_manager', 'manage_email_templates', Environment.default)
  8 + login_as(person.user.login)
  9 + end
  10 +
  11 + test "should get index" do
  12 + get :index
  13 + assert_response :success
  14 + assert_not_nil assigns(:email_templates)
  15 + end
  16 +
  17 + test "should get new" do
  18 + get :new
  19 + assert_response :success
  20 + end
  21 +
  22 + test "should create email_template" do
  23 + assert_difference('EmailTemplate.count') do
  24 + post :create, email_template: { :name => 'test' }
  25 + end
  26 +
  27 + assert_redirected_to url_for(:action => :index)
  28 + end
  29 +
  30 + test "should show email_template" do
  31 + get :show, id: @email_template
  32 + assert_response :success
  33 + end
  34 +
  35 + test "should get edit" do
  36 + get :edit, id: @email_template
  37 + assert_response :success
  38 + end
  39 +
  40 + test "should update email_template" do
  41 + put :update, id: @email_template, email_template: { }
  42 + assert_redirected_to url_for(:action => :index)
  43 + end
  44 +
  45 + test "should destroy email_template" do
  46 + assert_difference('EmailTemplate.count', -1) do
  47 + delete :destroy, id: @email_template
  48 + end
  49 +
  50 + assert_redirected_to url_for(:action => :index)
  51 + end
  52 +
  53 + test "should get parsed template" do
  54 + environment = Environment.default
  55 + @email_template.subject = '{{environment.name}}'
  56 + @email_template.body = '{{environment.name}}'
  57 + @email_template.save!
  58 + get :show_parsed, id: @email_template
  59 + assert_response :success
  60 + json_response = ActiveSupport::JSON.decode(@response.body)
  61 + assert_equal "#{environment.name}", json_response['parsed_subject']
  62 + assert_equal "#{environment.name}", json_response['parsed_body']
  63 + end
  64 +
  65 +end
test/functional/profile_editor_controller_test.rb
@@ -629,7 +629,7 @@ class ProfileEditorControllerTest &lt; ActionController::TestCase @@ -629,7 +629,7 @@ class ProfileEditorControllerTest &lt; ActionController::TestCase
629 should 'display email template link for organizations in control panel' do 629 should 'display email template link for organizations in control panel' do
630 profile = fast_create(Organization) 630 profile = fast_create(Organization)
631 get :index, :profile => profile.identifier 631 get :index, :profile => profile.identifier
632 - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/email_templates" } 632 + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_email_templates" }
633 end 633 end
634 634
635 should 'not display email template link in control panel for person' do 635 should 'not display email template link in control panel for person' do
test/functional/profile_email_templates_controller_test.rb 0 → 100644
@@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
  1 +require 'test_helper'
  2 +
  3 +class ProfileEmailTemplatesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @profile = fast_create(Community)
  7 + @email_template = EmailTemplate.create!(:name => 'template', :owner => @profile)
  8 + @person = create_user_with_permission('templatemanager', 'manage_email_templates', @profile)
  9 + login_as(@person.user.login)
  10 + end
  11 +
  12 + attr_accessor :profile, :person
  13 +
  14 + test "should get index" do
  15 + get :index, :profile => profile.identifier
  16 + assert_response :success
  17 + assert_not_nil assigns(:email_templates)
  18 + end
  19 +
  20 + test "should get new" do
  21 + get :new, :profile => profile.identifier
  22 + assert_response :success
  23 + end
  24 +
  25 + test "should create email_template" do
  26 + assert_difference('EmailTemplate.count') do
  27 + post :create, email_template: { :name => 'test' }, :profile => profile.identifier
  28 + end
  29 +
  30 + assert_redirected_to url_for(:action => :index)
  31 + end
  32 +
  33 + test "should show email_template" do
  34 + get :show, id: @email_template, :profile => profile.identifier
  35 + assert_response :success
  36 + end
  37 +
  38 + test "should get edit" do
  39 + get :edit, id: @email_template, :profile => profile.identifier
  40 + assert_response :success
  41 + end
  42 +
  43 + test "should update email_template" do
  44 + put :update, id: @email_template, email_template: { }, :profile => profile.identifier
  45 + assert_redirected_to url_for(:action => :index)
  46 + end
  47 +
  48 + test "should destroy email_template" do
  49 + assert_difference('EmailTemplate.count', -1) do
  50 + delete :destroy, id: @email_template, :profile => profile.identifier
  51 + end
  52 +
  53 + assert_redirected_to url_for(:action => :index)
  54 + end
  55 +
  56 + test "should get parsed template" do
  57 + environment = Environment.default
  58 + @email_template.subject = '{{profile.name}} - {{profile.identifier}}'
  59 + @email_template.body = '{{profile.name}} - {{profile.identifier}} - {{environment.name}}'
  60 + @email_template.save!
  61 + get :show_parsed, id: @email_template, :profile => profile.identifier
  62 + assert_response :success
  63 + json_response = ActiveSupport::JSON.decode(@response.body)
  64 + assert_equal "#{profile.name} - #{profile.identifier}", json_response['parsed_subject']
  65 + assert_equal "#{profile.name} - #{profile.identifier} - #{environment.name}", json_response['parsed_body']
  66 + end
  67 +
  68 +end
test/unit/api/articles_test.rb
@@ -48,6 +48,15 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -48,6 +48,15 @@ class ArticlesTest &lt; ActiveSupport::TestCase
48 assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] } 48 assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] }
49 end 49 end
50 50
  51 + should 'list public article children for not logged in access' do
  52 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  53 + child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing")
  54 + child2 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing")
  55 + get "/api/v1/articles/#{article.id}/children"
  56 + json = JSON.parse(last_response.body)
  57 + assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] }
  58 + end
  59 +
51 should 'not list children of forbidden article' do 60 should 'not list children of forbidden article' do
52 person = fast_create(Person, :environment_id => environment.id) 61 person = fast_create(Person, :environment_id => environment.id)
53 article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) 62 article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false)
test/unit/api/helpers_test.rb
@@ -236,7 +236,7 @@ class APIHelpersTest &lt; ActiveSupport::TestCase @@ -236,7 +236,7 @@ class APIHelpersTest &lt; ActiveSupport::TestCase
236 236
237 end 237 end
238 238
239 - should 'captcha serpro say Name or service not known' do 239 + should 'captcha serpro say name or service not known' do
240 environment = Environment.new 240 environment = Environment.new
241 environment.api_captcha_settings = { 241 environment.api_captcha_settings = {
242 enabled: true, 242 enabled: true,
test/unit/api/session_test.rb
@@ -40,4 +40,69 @@ class SessionTest &lt; ActiveSupport::TestCase @@ -40,4 +40,69 @@ class SessionTest &lt; ActiveSupport::TestCase
40 json = JSON.parse(last_response.body) 40 json = JSON.parse(last_response.body)
41 end 41 end
42 42
  43 + should 'detected error, Name or service not known, for Serpro Captcha communication' do
  44 + environment = Environment.default
  45 + environment.api_captcha_settings = {
  46 + enabled: true,
  47 + provider: 'serpro',
  48 + serpro_client_id: '0000000000000000',
  49 + verify_uri: 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate',
  50 + }
  51 + environment.save!
  52 + params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com",
  53 + :txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'}
  54 + post "/api/v1/register?#{params.to_query}"
  55 + assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", JSON.parse(last_response.body)["message"]
  56 + end
  57 +
  58 + # TODO: Add another test cases to check register situations
  59 + should 'activate a user' do
  60 + params = {
  61 + :login => "newuserapi",
  62 + :password => "newuserapi",
  63 + :password_confirmation => "newuserapi",
  64 + :email => "newuserapi@email.com"
  65 + }
  66 + user = User.new(params)
  67 + user.save!
  68 +
  69 + params = { activation_code: user.activation_code}
  70 + patch "/api/v1/activate?#{params.to_query}"
  71 + assert_equal 200, last_response.status
  72 + end
  73 +
  74 + should 'do not activate a user if admin must approve him' do
  75 + params = {
  76 + :login => "newuserapi",
  77 + :password => "newuserapi",
  78 + :password_confirmation => "newuserapi",
  79 + :email => "newuserapi@email.com",
  80 + :environment => Environment.default
  81 + }
  82 + user = User.new(params)
  83 + user.environment.enable('admin_must_approve_new_users')
  84 + user.save!
  85 +
  86 + params = { activation_code: user.activation_code}
  87 + patch "/api/v1/activate?#{params.to_query}"
  88 + assert_equal 202, last_response.status
  89 + assert_equal 'Waiting for admin moderate user registration', JSON.parse(last_response.body)["message"]
  90 + end
  91 +
  92 + should 'do not activate a user if the token is invalid' do
  93 + params = {
  94 + :login => "newuserapi",
  95 + :password => "newuserapi",
  96 + :password_confirmation => "newuserapi",
  97 + :email => "newuserapi@email.com",
  98 + :environment => Environment.default
  99 + }
  100 + user = User.new(params)
  101 + user.save!
  102 +
  103 + params = { activation_code: '70250abe20cc6a67ef9399cf3286cb998b96aeaf'}
  104 + patch "/api/v1/activate?#{params.to_query}"
  105 + assert_equal 412, last_response.status
  106 + end
  107 +
43 end 108 end
test/unit/change_password_test.rb
@@ -71,4 +71,10 @@ class ChangePasswordTest &lt; ActiveSupport::TestCase @@ -71,4 +71,10 @@ class ChangePasswordTest &lt; ActiveSupport::TestCase
71 assert_match(/#{task.requestor.name} wants to change its password/, email.subject) 71 assert_match(/#{task.requestor.name} wants to change its password/, email.subject)
72 end 72 end
73 73
  74 + should 'set email template when it exists' do
  75 + template = EmailTemplate.create!(:template_type => :user_change_password, :name => 'template1', :owner => Environment.default)
  76 + task = ChangePassword.create!(:requestor => person)
  77 + assert_equal template.id, task.email_template_id
  78 + end
  79 +
74 end 80 end
test/unit/email_template_helper_test.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class EmailTemplateHelperTest < ActionView::TestCase
  4 +
  5 + should 'replace body and subject with parsed values from template' do
  6 + template = mock
  7 + template.expects(:parsed_body).returns('parsed body')
  8 + template.expects(:parsed_subject).returns('parsed subject')
  9 + params = {:subject => 'subject', :body => 'body', :email_template => template}
  10 + expects(:mail).with({:subject => 'parsed subject', :body => 'parsed body', :content_type => 'text/html'})
  11 + mail_with_template(params)
  12 + end
  13 +
  14 + should 'do not change params if there is no email template' do
  15 + params = {:subject => 'subject', :body => 'body'}
  16 + expects(:mail).with(params)
  17 + mail_with_template(params)
  18 + end
  19 +
  20 +end
test/unit/email_template_test.rb
@@ -19,4 +19,35 @@ class EmailTemplateTest &lt; ActiveSupport::TestCase @@ -19,4 +19,35 @@ class EmailTemplateTest &lt; ActiveSupport::TestCase
19 assert_equal 'Hi John', template.parsed_subject({:person => 'John'}) 19 assert_equal 'Hi John', template.parsed_subject({:person => 'John'})
20 end 20 end
21 21
  22 + should 'not create template with the same name of other' do
  23 + template1 = EmailTemplate.new(:template_type => :type1, :name => 'template', :owner => Environment.default)
  24 + template2 = EmailTemplate.new(:template_type => :type1, :name => 'template', :owner => Environment.default)
  25 + assert template1.save
  26 + assert !template2.save
  27 + end
  28 +
  29 + should 'not create duplicated template when template type is unique' do
  30 + template1 = EmailTemplate.new(:template_type => :user_activation, :name => 'template1', :owner => Environment.default)
  31 + template2 = EmailTemplate.new(:template_type => :user_activation, :name => 'template2', :owner => Environment.default)
  32 + assert template1.save
  33 + assert !template2.save
  34 + end
  35 +
  36 + should 'create duplicated template when template type is not unique' do
  37 + template1 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template1', :owner => Environment.default)
  38 + template2 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template2', :owner => Environment.default)
  39 + assert template1.save
  40 + assert template2.save
  41 + end
  42 +
  43 + should 'return available types when the owner is an environment' do
  44 + template = EmailTemplate.new(:owner => Environment.default)
  45 + assert_equal [:user_activation, :user_change_password], template.available_types.symbolize_keys.keys
  46 + end
  47 +
  48 + should 'return available types when the owner is a profile' do
  49 + template = EmailTemplate.new(:owner => Profile.new)
  50 + assert_equal [:task_rejection, :task_acceptance, :organization_members], template.available_types.symbolize_keys.keys
  51 + end
  52 +
22 end 53 end
test/unit/user_mailer_test.rb
@@ -26,6 +26,24 @@ fast_create(Person)) @@ -26,6 +26,24 @@ fast_create(Person))
26 assert_match /profile\/some-user\/friends\/suggest/, email.body.to_s 26 assert_match /profile\/some-user\/friends\/suggest/, email.body.to_s
27 end 27 end
28 28
  29 + should 'deliver activation code email' do
  30 + assert_difference 'ActionMailer::Base.deliveries.size' do
  31 + u = create_user('some-user')
  32 + UserMailer.activation_code(u).deliver
  33 + end
  34 + end
  35 +
  36 + should 'deliver activation code email with template' do
  37 + EmailTemplate.create!(:template_type => :user_activation, :name => 'template1', :subject => 'activation template subject', :body => 'activation template body', :owner => Environment.default)
  38 + assert_difference 'ActionMailer::Base.deliveries.size' do
  39 + u = create_user('some-user')
  40 + UserMailer.activation_code(u).deliver
  41 + end
  42 + mail = ActionMailer::Base.deliveries.last
  43 + assert_equal 'activation template subject', mail.subject.to_s
  44 + assert_equal 'activation template body', mail.body.to_s
  45 + end
  46 +
29 private 47 private
30 48
31 def read_fixture(action) 49 def read_fixture(action)