From f8f70e0ebec5ef25525509801874c4e8e5b67978 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 5 Aug 2016 20:00:34 +0800 Subject: [PATCH] added settings for approver election and also for sending emails --- .../admin/announcements_controller.rb | 16 ++- app/controllers/announcements_controller.rb | 4 +- app/helpers/admin/announcements_helper.rb | 45 ++++-- app/models/announcement_setting.rb | 2 + app/views/admin/announcements/_index.html.erb | 6 +- app/views/admin/announcements/index.html.erb | 2 +- .../admin/announcements/settings.html.erb | 133 +++++++++++++++++- 7 files changed, 185 insertions(+), 23 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 6f1b36e..3f682c5 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -4,7 +4,7 @@ class Admin::AnnouncementsController < OrbitAdminController include Admin::AnnouncementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_bulletin, only: [:edit, :destroy] - before_action :load_access_level + before_action :load_access_level, :load_settings def initialize super @@ -38,10 +38,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def settings - @setting = AnnouncementSetting.first rescue nil - if @setting.nil? - @setting = AnnouncementSetting.new - end + @setting = @announcement_setting end def import @@ -86,7 +83,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def updatesettings - setting = AnnouncementSetting.first + setting = @announcement_setting setting.update_attributes(settings_params) setting.save redirect_to admin_announcement_settings_path @@ -339,6 +336,13 @@ class Admin::AnnouncementsController < OrbitAdminController private + def load_settings + @announcement_setting = AnnouncementSetting.first rescue nil + if @announcement_setting.nil? + @announcement_setting = AnnouncementSetting.create + end + end + def set_bulletin @bulletin = Bulletin.find(params[:id]) end diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 5ea0170..4342852 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -255,7 +255,9 @@ class AnnouncementsController < ApplicationController access_level = OrbitHelper.user_access_level? if !announcement.approved && (access_level != "manager" && access_level != "admin") - return {} + if !(access_level == "sub_manager" && AnnouncementSetting.first.approvers.include?(OrbitHelper.current_user.id.to_s)) + return {} + end end return {} if announcement.category.disable diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index cc0357e..4fc728f 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -133,14 +133,23 @@ module Admin::AnnouncementsHelper end def send_notification_mail_to_managers(announcement, type) - authorizations = Authorization.where(:module_app_id => @module_app.id) - users = authorizations.collect do |auth| - auth.user + users = [] + if @announcement_setting.email_to.include?("managers") + authorizations = Authorization.where(:module_app_id => @module_app.id) + users = authorizations.collect do |auth| + auth.user + end + end + if @announcement_setting.email_to.include?("admins") + wg = Workgroup.where(:key => "admin").first + admins = User.where(:workgroup_id => wg.id) + users.delete(nil) + users = users.concat(admins.to_a) + end + if @announcement_setting.email_to.include?("approvers") + approvers = User.find(@announcement_setting.approvers) rescue [] + users = users.concat(approvers.to_a) end - wg = Workgroup.where(:key => "admin").first - admins = User.where(:workgroup_id => wg.id) - users.delete(nil) - users = users.concat(admins.to_a) users.each do |user| email = user.member_profile.email if !email.nil? && email != "" @@ -210,15 +219,29 @@ module Admin::AnnouncementsHelper end end - def user_can_approve? + def user_can_approve?(anns=nil) + can_approve = false + setting = AnnouncementSetting.first case @access_level when "admin" - return true + can_approve = true when "manager" - return true + can_approve = true else - return false + can_approve = false end + if !can_approve + if !anns.nil? + if setting.approvers.include?(current_user.id.to_s) + if (current_user.approved_categories_for_module(@module_app).include?(anns.category) rescue false) + can_approve = true + end + end + else + can_approve = setting.approvers.include?(current_user.id.to_s) + end + end + can_approve end end diff --git a/app/models/announcement_setting.rb b/app/models/announcement_setting.rb index 041f100..112c486 100644 --- a/app/models/announcement_setting.rb +++ b/app/models/announcement_setting.rb @@ -4,6 +4,8 @@ class AnnouncementSetting field :top_limit, type: Integer, :default => 0 field :pro_enabled, type: Boolean, :default => false + field :approvers, type: Array, :default => [] + field :email_to, type: Array, :default => ["admins","managers","approvers"] def self.check_limit_for_user(user_id, b_id = nil) limit = self.first.top_limit rescue 0 diff --git a/app/views/admin/announcements/_index.html.erb b/app/views/admin/announcements/_index.html.erb index d352938..3510c4c 100644 --- a/app/views/admin/announcements/_index.html.erb +++ b/app/views/admin/announcements/_index.html.erb @@ -48,9 +48,9 @@ <% if can_edit_or_delete?(b) %>
  • <%= t(:edit) %>
  • <%= t(:delete_) %>
  • - <% if (!b.approved || b.reapproval) && user_can_approve? %> -
  • <%= t("announcement.approval_waiting") %>
  • - <% end %> + <% end %> + <% if ((!b.approved && !b.rejected && !b.reapproval) || (b.rejected && b.reapproval)) && user_can_approve?(b) %> +
  • <%= t("announcement.approval_waiting") %>
  • <% end %> diff --git a/app/views/admin/announcements/index.html.erb b/app/views/admin/announcements/index.html.erb index f6074c5..da6f8e4 100644 --- a/app/views/admin/announcements/index.html.erb +++ b/app/views/admin/announcements/index.html.erb @@ -5,7 +5,7 @@ <%= render 'layouts/delete_modal', delete_options: @delete_options %> -<% if AnnouncementSetting.is_pro? && (@access_level == "admin" || @access_level == "manager") %> +<% if AnnouncementSetting.is_pro? && user_can_approve? %> <%= render :partial=> "approval_modal" %> + + + + + + + + +