diff --git a/plugins/work_assignment/controllers/work_assignment_plugin_myprofile_controller.rb b/plugins/work_assignment/controllers/work_assignment_plugin_myprofile_controller.rb index 8035770..1b8cc5f 100644 --- a/plugins/work_assignment/controllers/work_assignment_plugin_myprofile_controller.rb +++ b/plugins/work_assignment/controllers/work_assignment_plugin_myprofile_controller.rb @@ -5,13 +5,6 @@ helper CmsHelper before_filter :protect_if, :only => [:edit_visibility] -def protect_if - article = environment.articles.find_by_id(params[:article_id]) - render_access_denied unless (user && !article.nil? && (user.is_member_of? article.profile) && - article.parent.allow_visibility_edition && article.folder? && - (article.author == user || user.has_permission?('view_private_content', profile))) -end - def edit_visibility unless params[:article_id].blank? folder = profile.environment.articles.find_by_id(params[:article_id]) @@ -32,4 +25,14 @@ def edit_visibility result = profile.members.find(:all, :conditions => ['LOWER(name) LIKE ?', "%#{arg}%"]) render :text => prepare_to_token_input(result).to_json end + + protected + + def protect_if + article = environment.articles.find_by_id(params[:article_id]) + render_access_denied unless (user && !article.nil? && (user.is_member_of? article.profile) && + article.parent.allow_visibility_edition && article.folder? && + (article.author == user || user.has_permission?('view_private_content', profile))) + end + end diff --git a/plugins/work_assignment/lib/ext/article.rb b/plugins/work_assignment/lib/ext/article.rb new file mode 100644 index 0000000..edb5ab3 --- /dev/null +++ b/plugins/work_assignment/lib/ext/article.rb @@ -0,0 +1,20 @@ +require_dependency 'article' + +class Article + before_validation :work_assignment_save_into_author_folder + after_validation :work_assignment_change_visibility + + def work_assignment_save_into_author_folder + if not self.is_a? Folder and self.parent.kind_of? WorkAssignmentPlugin::WorkAssignment + author_folder = self.parent.find_or_create_author_folder(self.author) + self.name = WorkAssignmentPlugin::WorkAssignment.versioned_name(self, author_folder) + self.parent = author_folder + end + end + + def work_assignment_change_visibility + if self.parent && self.parent.parent && self.parent.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment) + self.published = self.parent.published + end + end +end \ No newline at end of file diff --git a/plugins/work_assignment/lib/ext/email_contact.rb b/plugins/work_assignment/lib/ext/email_contact.rb deleted file mode 100644 index 605b2be..0000000 --- a/plugins/work_assignment/lib/ext/email_contact.rb +++ /dev/null @@ -1,65 +0,0 @@ -class EmailContact - - include ActiveModel::Validations - - def initialize(attributes = nil) - if attributes - attributes.each do |attr,value| - self.send("#{attr}=", value) - end - end - end - - attr_accessor :name - attr_accessor :subject - attr_accessor :message - attr_accessor :email - attr_accessor :receive_a_copy - attr_accessor :sender - attr_accessor :receiver - - N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name') - - validates_presence_of :receiver, :subject, :message, :sender - validates_format_of :receiver, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) - - def deliver - return false unless self.valid? - EmailContact::EmailSender.notification(self).deliver - end - - class EmailSender < ActionMailer::Base - - def notification(email_contact) - name = email_contact.sender.name - email = email_contact.sender.email - message = email_contact.message - target = email_contact.receiver - - options = { - content_type: 'text/html', - to: target, - reply_to: email, - subject: email_contact.subject, - body: message, - from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>", - } - - mail(options) - end - end - - def build_mail_message!(environment, uploaded_files, parent_id) - article = environment.articles.find_by_id(parent_id) - message = "" - if !article.nil? && article.kind_of?(WorkAssignmentPlugin::WorkAssignment) - message = article.default_email + "
" - end - uploaded_files.each do |file| - file_url = "http://#{file.url[:host]}:#{file.url[:port]}/#{file.url[:profile]}/#{file.path}" - message += "
#{file_url}" - end - self.message = message - end - -end diff --git a/plugins/work_assignment/lib/ext/uploaded_file.rb b/plugins/work_assignment/lib/ext/uploaded_file.rb deleted file mode 100644 index a4876e9..0000000 --- a/plugins/work_assignment/lib/ext/uploaded_file.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_dependency 'article' -require_dependency 'uploaded_file' - -class UploadedFile < Article - before_validation do |uploaded_file| - if uploaded_file.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment) - author_folder = uploaded_file.parent.find_or_create_author_folder(uploaded_file.author) - uploaded_file.name = WorkAssignmentPlugin::WorkAssignment.versioned_name(uploaded_file, author_folder) - uploaded_file.parent = author_folder - end - end - - after_validation do |uploaded_file| - if uploaded_file.parent && uploaded_file.parent.parent && uploaded_file.parent.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment) - uploaded_file.published = uploaded_file.parent.published - end - end -end diff --git a/plugins/work_assignment/lib/work_assignment_plugin.rb b/plugins/work_assignment/lib/work_assignment_plugin.rb index 1fb0903..5f23616 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin.rb @@ -56,8 +56,8 @@ class WorkAssignmentPlugin < Noosfero::Plugin if request.post? && params[:uploaded_files] email_notification = params[:article_email_notification] unless !email_notification || email_notification.empty? - email_contact = EmailContact.new(:subject => @parent.name, :receiver => email_notification, :sender => user) - email_contact.build_mail_message!(environment, @uploaded_files, @parent.id) + email_contact = WorkAssignmentPlugin::EmailContact.new(:subject => @parent.name, :receiver => email_notification, :sender => user) + WorkAssignmentPlugin::EmailContact::EmailSender.build_mail_message(email_contact, @uploaded_files) if email_contact.deliver session[:notice] = _('Notification successfully sent') else diff --git a/plugins/work_assignment/lib/work_assignment_plugin/email_contact.rb b/plugins/work_assignment/lib/work_assignment_plugin/email_contact.rb new file mode 100644 index 0000000..e3f6716 --- /dev/null +++ b/plugins/work_assignment/lib/work_assignment_plugin/email_contact.rb @@ -0,0 +1,64 @@ +class WorkAssignmentPlugin::EmailContact + + include ActiveModel::Validations + + def initialize(attributes = nil) + if attributes + attributes.each do |attr,value| + self.send("#{attr}=", value) + end + end + end + + attr_accessor :name + attr_accessor :subject + attr_accessor :message + attr_accessor :email + attr_accessor :receive_a_copy + attr_accessor :sender + attr_accessor :receiver + + N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name') + + validates_presence_of :receiver, :subject, :message, :sender + validates_format_of :receiver, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) + + def deliver + return false unless self.valid? + WorkAssignmentPlugin::EmailContact::EmailSender.notification(self).deliver + end + + class EmailSender < ActionMailer::Base + + def notification(email_contact) + name = email_contact.sender.name + email = email_contact.sender.email + message = email_contact.message + target = email_contact.receiver + + options = { + content_type: 'text/html', + to: target, + reply_to: email, + subject: email_contact.subject, + body: message, + from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>", + } + + mail(options) + end + + def build_mail_message(email_contact, uploaded_files) + message = "" + if uploaded_files && uploaded_files.first && uploaded_files.first.parent && uploaded_files.first.parent.parent + article = uploaded_files.first.parent.parent + message = article.default_email + "
" + uploaded_files.each do |file| + url = url_for(file.url) + message += "
#{url}" + end + end + email_contact.message = message + end + end +end -- libgit2 0.21.2