From a7cb020815c79eabe8427de67d4abfe7459eed00 Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Wed, 24 Apr 2019 18:47:21 +0200 Subject: [PATCH] suite --- app/controllers/admin/admins_controller.rb | 3 +- app/helpers/mail_helper.rb | 55 +++++++++++++++++++ app/mailers/general_mailer.rb | 23 ++++++++ app/models/admin.rb | 28 ++++++++-- app/models/mail_content.rb | 45 +++++++++++++++ app/models/mail_template.rb | 2 + app/models/mail_type.rb | 17 ++++++ app/views/admin/admins/_form.html.haml | 13 +++-- .../general_mailer/send_qi_mail.html.haml | 1 + app/views/layouts/admin.html.haml | 3 +- .../20180104181313_create_mail_types.rb | 11 ++++ .../20180104181352_create_mail_contents.rb | 22 ++++++++ ...180104181353_add_infos_to_mail_contents.rb | 7 +++ .../20180501135206_create_mail_templates.rb | 10 ++++ ...5303_add_mail_template_to_mail_contents.rb | 5 ++ ...0190424164427_add_super_admin_to_admins.rb | 5 ++ db/schema.rb | 38 +++++++++++-- 17 files changed, 273 insertions(+), 15 deletions(-) create mode 100644 app/helpers/mail_helper.rb create mode 100755 app/mailers/general_mailer.rb create mode 100644 app/models/mail_content.rb create mode 100644 app/models/mail_template.rb create mode 100644 app/models/mail_type.rb create mode 100644 app/views/general_mailer/send_qi_mail.html.haml create mode 100644 db/migrate/20180104181313_create_mail_types.rb create mode 100644 db/migrate/20180104181352_create_mail_contents.rb create mode 100644 db/migrate/20180104181353_add_infos_to_mail_contents.rb create mode 100644 db/migrate/20180501135206_create_mail_templates.rb create mode 100644 db/migrate/20180501135303_add_mail_template_to_mail_contents.rb create mode 100644 db/migrate/20190424164427_add_super_admin_to_admins.rb diff --git a/app/controllers/admin/admins_controller.rb b/app/controllers/admin/admins_controller.rb index ddcaf59..b4b0609 100644 --- a/app/controllers/admin/admins_controller.rb +++ b/app/controllers/admin/admins_controller.rb @@ -30,6 +30,7 @@ class Admin::AdminsController < ApplicationController if @admin.save @admins = Admin.all + @admin.generate_mdp_now if @admin.generate_mdp and @admin.generate_mdp != "0" else render :action => "new" @@ -40,7 +41,7 @@ class Admin::AdminsController < ApplicationController @admin = Admin.find(params[:id]) if @admin.update_attributes(admin_params) - + @admin.generate_mdp_now if @admin.generate_mdp and @admin.generate_mdp != "0" else render :action => "edit" diff --git a/app/helpers/mail_helper.rb b/app/helpers/mail_helper.rb new file mode 100644 index 0000000..e76bf1a --- /dev/null +++ b/app/helpers/mail_helper.rb @@ -0,0 +1,55 @@ +# -*- encoding : utf-8 -*- +module MailHelper + + def mail_content(mail_content, lang_slug, arguments = {}) + + lang_site = LangSite.find_by_slug(lang_slug) + + + + if mail_content.content_type == "blocs" and mail_content.block + r = "
"+render(:partial => "public/blocks/block", :locals => {:block => mail_content.block})+render(:partial => "public/shared/render_block.html.haml")+"
" + + + else + + if mail_content.message? + r= mail_content.message + elsif mail_content.mail_type.mail_type_reference + r= mail_content.mail_type.mail_type_reference.mail_contents.find_by_lang_site_id(lang_site.id).message.to_s + else + r= "" + end + + r = simple_format(r) + + end + + + arguments.each_pair do |key, value| + r = r.gsub(/\[#{key}\]/, value.to_s) + + end + + if mail_content.mail_template and mail_content.mail_template.template_html? + template = mail_content.mail_template.template_html + + + r = template.gsub(/\[contenu\]/, r) + + + + + raw r + + + + else + raw r + end + + + + end + +end \ No newline at end of file diff --git a/app/mailers/general_mailer.rb b/app/mailers/general_mailer.rb new file mode 100755 index 0000000..675606c --- /dev/null +++ b/app/mailers/general_mailer.rb @@ -0,0 +1,23 @@ +class GeneralMailer < ActionMailer::Base + default from: "n.bally@jipe.fr" + + add_template_helper(MailHelper) + + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.question.deliver.subject + # + def send_qi_mail(lang_slug, slug, to, options) + + @mail_content = MailContent.find_key(LangSite.find_by_slug(lang_slug), slug) + + @lang_slug = lang_slug + + @options = options + + mail to: to, :subject => "[Jipé] "+@mail_content.subject.to_s + end + + +end diff --git a/app/models/admin.rb b/app/models/admin.rb index 857aef7..1156fc7 100644 --- a/app/models/admin.rb +++ b/app/models/admin.rb @@ -4,19 +4,19 @@ class Admin < ActiveRecord::Base has_secure_password - attr_accessor :login + attr_accessor :login, :generate_mdp validates :password, :presence => true, :confirmation => true, :length => {:within => 6..40}, :on => :create - validates :password, :confirmation => true, + validates :password, :length => {:within => 6..40}, :allow_blank => true, :on => :update - validates :password_confirmation, :presence => true, - :unless => Proc.new { |a| a.password.blank? } + #validates :password_confirmation, :presence => true, + # :unless => Proc.new { |a| a.password.blank? } validates :email, :presence => true, :uniqueness => true @@ -26,6 +26,26 @@ class Admin < ActiveRecord::Base before_create { generate_token(:remember_token) } + def generate_mdp_now + ps = SecureRandom.hex(4) + self.password = ps + self.password_confirmation = ps + + if !self.email? + errors.add(:email, "Doit être présent") + + end + + + if self.save + GeneralMailer.send_qi_mail("fr", "generation_mdp", self.email, {"mdp" => ps}).deliver + else + fgfdggdf = dfgdgf + end + + + end + def send_password_reset generate_token(:reset_password_token) self.reset_password_sent_at = Time.now diff --git a/app/models/mail_content.rb b/app/models/mail_content.rb new file mode 100644 index 0000000..bbcd432 --- /dev/null +++ b/app/models/mail_content.rb @@ -0,0 +1,45 @@ +class MailContent < ActiveRecord::Base + belongs_to :lang_site + belongs_to :mail_type + + belongs_to :mail_template + + has_one :block, :as => :blockable + + after_create do + generate_block + + end + + def generate_block + @block = Block.new(:block_name => "general", :lang_site => self.lang_site) + @block.blockable = self + @block.save + end + + + def alloweds_types + + { + TitleContent: "Titre", + TextContent: "Texte", + ImageContent: "Image", + LinkContent:"Lien", + BreakContent: "Séparation", + HtmlContent: "Code HTML", + DownloadContent: "Téléchargement", + + BlockContent: "Bloc", + + ShareContent: "Ligne partage réseaux", + + } + end + + + def self.find_key(lang, slug ) + + MailContent.joins(:mail_type).where(:lang_site_id =>lang, :mail_types => { :slug => slug}).first + end + +end diff --git a/app/models/mail_template.rb b/app/models/mail_template.rb new file mode 100644 index 0000000..c37b2a2 --- /dev/null +++ b/app/models/mail_template.rb @@ -0,0 +1,2 @@ +class MailTemplate < ActiveRecord::Base +end diff --git a/app/models/mail_type.rb b/app/models/mail_type.rb new file mode 100644 index 0000000..d49dc94 --- /dev/null +++ b/app/models/mail_type.rb @@ -0,0 +1,17 @@ +class MailType < ActiveRecord::Base + has_many :mail_contents + belongs_to :mail_type_reference, :class_name => "MailType" + + + + def self.generate_mail_content(slug) + mc = MailType.new(:slug => slug) + LangSite.all.each do |ls| + + mc.mail_contents.new(:lang_site_id => ls.id, :content_type => "text") + end + mc.save + end + + +end diff --git a/app/views/admin/admins/_form.html.haml b/app/views/admin/admins/_form.html.haml index 387bc3d..eb7758a 100644 --- a/app/views/admin/admins/_form.html.haml +++ b/app/views/admin/admins/_form.html.haml @@ -10,7 +10,9 @@ Ajouter un admin = f.inputs do - + + = f.input :super_admin, :label => "Super admin ?" + = f.input :email, :label => "Email :" @@ -24,10 +26,11 @@ = f.input :firstname, :label => "Prénom :" - = f.input :password, :label => "Mot de passe :" - - - = f.input :password_confirmation, :label => "Confirmation :" + = f.input :generate_mdp, :label => "Forcer la génération d'un nouveau mot de passe ? (un mail sera envoyé à l'utilisateur)", :as => :boolean + + = f.input :password, :label => "Mot de passe :", :input_html => { :autocomplete => "new-password"} + + = f.input :color, :label => "Couleur pour identification dans contacts" diff --git a/app/views/general_mailer/send_qi_mail.html.haml b/app/views/general_mailer/send_qi_mail.html.haml new file mode 100644 index 0000000..de4a378 --- /dev/null +++ b/app/views/general_mailer/send_qi_mail.html.haml @@ -0,0 +1 @@ +=mail_content(@mail_content, @lang_slug, @options) \ No newline at end of file diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index 1d12944..04969d6 100644 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -96,7 +96,8 @@ %b.caret %ul.dropdown-menu - %li= link_to "Gestion des admins", admin_admins_path + -if current_admin.super_admin + %li= link_to "Gestion des admins", admin_admins_path %li.divider %li=link_to "Se déconnecter", admin_admin_auth_path(1), method: :delete %li diff --git a/db/migrate/20180104181313_create_mail_types.rb b/db/migrate/20180104181313_create_mail_types.rb new file mode 100644 index 0000000..7125b68 --- /dev/null +++ b/db/migrate/20180104181313_create_mail_types.rb @@ -0,0 +1,11 @@ +class CreateMailTypes < ActiveRecord::Migration + def change + create_table :mail_types do |t| + t.string :slug + t.string :default_title + t.text :default_message + t.integer :mail_type_reference_id + t.timestamps null: false + end + end +end diff --git a/db/migrate/20180104181352_create_mail_contents.rb b/db/migrate/20180104181352_create_mail_contents.rb new file mode 100644 index 0000000..2b6efdb --- /dev/null +++ b/db/migrate/20180104181352_create_mail_contents.rb @@ -0,0 +1,22 @@ +class CreateMailContents < ActiveRecord::Migration + def change + create_table :mail_contents do |t| + t.references :lang_site + t.references :mail_type + t.string :subject + t.text :message + + + t.timestamps null: false + + + + + + end + + + + + end +end diff --git a/db/migrate/20180104181353_add_infos_to_mail_contents.rb b/db/migrate/20180104181353_add_infos_to_mail_contents.rb new file mode 100644 index 0000000..72a7cb0 --- /dev/null +++ b/db/migrate/20180104181353_add_infos_to_mail_contents.rb @@ -0,0 +1,7 @@ +class AddInfosToMailContents < ActiveRecord::Migration + def change + add_column :mail_contents, :enabled, :boolean + add_column :mail_contents, :content_type, :string + add_column :mail_contents, :tags, :text + end +end diff --git a/db/migrate/20180501135206_create_mail_templates.rb b/db/migrate/20180501135206_create_mail_templates.rb new file mode 100644 index 0000000..b493591 --- /dev/null +++ b/db/migrate/20180501135206_create_mail_templates.rb @@ -0,0 +1,10 @@ +class CreateMailTemplates < ActiveRecord::Migration + def change + create_table :mail_templates do |t| + t.string :title + t.text :template_html + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20180501135303_add_mail_template_to_mail_contents.rb b/db/migrate/20180501135303_add_mail_template_to_mail_contents.rb new file mode 100644 index 0000000..0228fa7 --- /dev/null +++ b/db/migrate/20180501135303_add_mail_template_to_mail_contents.rb @@ -0,0 +1,5 @@ +class AddMailTemplateToMailContents < ActiveRecord::Migration + def change + add_reference :mail_contents, :mail_template + end +end diff --git a/db/migrate/20190424164427_add_super_admin_to_admins.rb b/db/migrate/20190424164427_add_super_admin_to_admins.rb new file mode 100644 index 0000000..9b6e4fe --- /dev/null +++ b/db/migrate/20190424164427_add_super_admin_to_admins.rb @@ -0,0 +1,5 @@ +class AddSuperAdminToAdmins < ActiveRecord::Migration + def change + add_column :admins, :super_admin, :boolean, :default => false + end +end diff --git a/db/schema.rb b/db/schema.rb index 592dfa9..d9b1788 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,15 +11,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180628225938) do +ActiveRecord::Schema.define(version: 20190424164427) do create_table "admins", force: :cascade do |t| t.string "name", limit: 255 t.string "firstname", limit: 255 t.string "avatar", limit: 255 - t.string "username", limit: 255, default: "", null: false - t.string "email", limit: 255, default: "", null: false - t.string "password_digest", limit: 255, default: "", null: false + t.string "username", limit: 255, default: "", null: false + t.string "email", limit: 255, default: "", null: false + t.string "password_digest", limit: 255, default: "", null: false t.string "reset_password_token", limit: 255 t.datetime "reset_password_sent_at" t.datetime "remember_created_at" @@ -41,6 +41,7 @@ ActiveRecord::Schema.define(version: 20180628225938) do t.boolean "label_role" t.string "color", limit: 255 t.boolean "contact_role" + t.boolean "super_admin", default: false end create_table "albums", force: :cascade do |t| @@ -546,6 +547,19 @@ ActiveRecord::Schema.define(version: 20180628225938) do t.datetime "updated_at" end + create_table "mail_contents", force: :cascade do |t| + t.integer "lang_site_id", limit: 4 + t.integer "mail_type_id", limit: 4 + t.string "subject", limit: 255 + t.text "message", limit: 65535 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "enabled" + t.string "content_type", limit: 255 + t.text "tags", limit: 65535 + t.integer "mail_template_id", limit: 4 + end + create_table "mail_profiles", force: :cascade do |t| t.string "civilite", limit: 255 t.string "firstname", limit: 255 @@ -565,6 +579,22 @@ ActiveRecord::Schema.define(version: 20180628225938) do t.datetime "updated_at", null: false end + create_table "mail_templates", force: :cascade do |t| + t.string "title", limit: 255 + t.text "template_html", limit: 65535 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "mail_types", force: :cascade do |t| + t.string "slug", limit: 255 + t.string "default_title", limit: 255 + t.text "default_message", limit: 65535 + t.integer "mail_type_reference_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "map_contents", force: :cascade do |t| t.string "address", limit: 255 t.string "name", limit: 255