From 22f272c2c98887398eaf90ab0aac326f5e16d904 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 3 Mar 2022 16:31:42 +0100
Subject: [PATCH] new role website_manager - wip

---
 app/controllers/admin/users_controller.rb              |  2 +-
 app/models/ability.rb                                  | 10 ++++++++++
 app/models/user/with_roles.rb                          |  7 ++++++-
 app/views/admin/dashboard/index.html.erb               | 10 ++++++----
 app/views/admin/users/_form.html.erb                   |  6 ++++++
 app/views/admin/users/show.html.erb                    |  6 +++++-
 config/locales/en.yml                                  |  1 +
 config/locales/fr.yml                                  |  1 +
 ...0_create_join_table_communication_websites_users.rb |  8 ++++++++
 db/schema.rb                                           |  9 ++++++++-
 10 files changed, 52 insertions(+), 8 deletions(-)
 create mode 100644 db/migrate/20220303145900_create_join_table_communication_websites_users.rb

diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 36cb5ed0b..1cdadc2ae 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -78,7 +78,7 @@ class Admin::UsersController < Admin::ApplicationController
 
   def user_params
     params.require(:user)
-          .permit(:email, :first_name, :last_name, :role, :password, :language_id, :picture, :picture_delete, :picture_infos, :mobile_phone, programs_to_manage_ids: [])
+          .permit(:email, :first_name, :last_name, :role, :password, :language_id, :picture, :picture_delete, :picture_infos, :mobile_phone, programs_to_manage_ids: [], websites_to_manage_ids: [])
           .merge(university_id: current_university.id)
   end
 
diff --git a/app/models/ability.rb b/app/models/ability.rb
index b43d5ffcc..6c2f3d356 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -50,6 +50,16 @@ class Ability
     can :manage, Communication::Website::Post, university_id: @user.university_id
   end
 
+  def website_manager
+    managed_websites_ids = @user.websites_to_manage.pluck(:communication_website_id)
+    can :read, Communication::Website, university_id: @user.university_id, id: managed_websites_ids
+    can :manage, Communication::Website::Page, university_id: @user.university_id, communication_website_id: managed_websites_ids
+    can :manage, Communication::Website::Post, university_id: @user.university_id, communication_website_id: managed_websites_ids
+    can :manage, Communication::Website::Category, university_id: @user.university_id, communication_website_id: managed_websites_ids
+    can [:read, :update, :reorder], Communication::Website::Menu, university_id: @user.university_id, communication_website_id: managed_websites_ids
+    can :manage, Communication::Website::Menu::Item, university_id: @user.university_id, website_id: managed_websites_ids
+  end
+
   def admin
     can :read, Administration::Qualiopi::Criterion
     can :read, Administration::Qualiopi::Indicator
diff --git a/app/models/user/with_roles.rb b/app/models/user/with_roles.rb
index aeced21b8..057f89407 100644
--- a/app/models/user/with_roles.rb
+++ b/app/models/user/with_roles.rb
@@ -4,13 +4,18 @@ module User::WithRoles
   included do
     attr_accessor :modified_by
 
-    enum role: { visitor: 0, teacher: 10, program_manager: 12, admin: 20, server_admin: 30 }
+    enum role: { visitor: 0, teacher: 10, program_manager: 12, website_manager: 15, admin: 20, server_admin: 30 }
 
     has_and_belongs_to_many :programs_to_manage,
                             class_name: 'Education::Program',
                             join_table: 'education_programs_users',
                             association_foreign_key: 'education_program_id'
 
+    has_and_belongs_to_many :websites_to_manage,
+                            class_name: 'Communication::Website',
+                            join_table: 'communication_websites_users',
+                            association_foreign_key: 'communication_website_id'
+
     scope :for_role, -> (role) { where(role: role) }
 
     before_validation :set_default_role, on: :create
diff --git a/app/views/admin/dashboard/index.html.erb b/app/views/admin/dashboard/index.html.erb
index 06139d631..4df287ce4 100644
--- a/app/views/admin/dashboard/index.html.erb
+++ b/app/views/admin/dashboard/index.html.erb
@@ -1,10 +1,10 @@
-<% content_for :title, 'Tableau de bord' %>
+<% content_for :title, t('admin.dashboard') %>
 
 <div class="row">
   <div class="col-md-4">
     <div class="card">
       <div class="card-body">
-        <h4>Bonjour <%= current_user.first_name %>&nbsp;!</h4>
+        <h4><%= t('hello', name: current_user.first_name) %></h4>
         <% if current_university.logo.attached? %>
           <%= image_tag current_university.logo, width: 120, class: 'float-end' %>
         <% else %>
@@ -19,13 +19,14 @@
   <h2 class="h4 my-4"><%= Research::Journal.model_name.human(count: 2) %></h2>
   <div class="row">
     <% current_university.research_journals.each do |journal| %>
+      <% next unless can?(:read, journal) %>
       <div class="col-md-4">
         <div class="card">
           <div class="card-body">
             <span class="float-end"><i class="fas fa-newspaper fa-2x"></i></span>
             <h4><%= journal %></h4>
             <p>&nbsp;</p>
-            <%= link_to 'Voir', [:admin, journal], class: button_classes('stretched-link') %>
+            <%= link_to t('show'), [:admin, journal], class: button_classes('stretched-link') %>
           </div>
         </div>
       </div>
@@ -37,13 +38,14 @@
   <h2 class="h4 my-4"><%= Communication::Website.model_name.human(count: 2) %></h2>
   <div class="row">
     <% current_university.communication_websites.each do |website| %>
+      <% next unless can?(:read, website) %>
       <div class="col-md-4">
         <div class="card">
           <div class="card-body">
             <span class="float-end"><i class="fas fa-sitemap fa-2x"></i></span>
             <h4><%= website %></h4>
             <p class="small"><%= website.url %></p>
-            <%= link_to 'Voir', [:admin, website], class: button_classes('stretched-link') %>
+            <%= link_to t('show'), [:admin, website], class: button_classes('stretched-link') %>
           </div>
         </div>
       </div>
diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb
index 6123085cb..98e997371 100644
--- a/app/views/admin/users/_form.html.erb
+++ b/app/views/admin/users/_form.html.erb
@@ -37,6 +37,12 @@
                               label_method: ->(p) { sanitize p[:label] },
                               value_method: ->(p) { p[:id] } %>
           </div>
+          <div data-show-for-roles="website_manager">
+            <%= f.association :websites_to_manage,
+                              as: :check_boxes,
+                              collection: current_university.communication_websites.ordered
+                               %>
+          </div>
         </div>
       </div>
     </div>
diff --git a/app/views/admin/users/show.html.erb b/app/views/admin/users/show.html.erb
index 921500972..462cb9547 100644
--- a/app/views/admin/users/show.html.erb
+++ b/app/views/admin/users/show.html.erb
@@ -13,12 +13,16 @@
           <td><%= User.human_attribute_name('email') %></td>
           <td class="text-end"><%= link_to @user.email, "mailto:#{@user.email}" %></td>
         </tr>
-        <% ['first_name', 'last_name', 'role', 'mobile_phone'].each do |variable| %>
+        <% ['first_name', 'last_name', 'mobile_phone'].each do |variable| %>
           <tr>
             <td><%= User.human_attribute_name(variable) %></td>
             <td class="text-end"><%= @user.public_send variable %></td>
           </tr>
         <% end %>
+        <tr>
+          <td><%= User.human_attribute_name('role') %></td>
+          <td class="text-end"><%= t("activerecord.attributes.user.roles.#{@user.role.to_s}") %></td>
+        </tr>
         <tr>
           <td><%= User.human_attribute_name('language') %></td>
           <td class="text-end"><%= t("languages.#{@user.language.iso_code.to_s}") %></td>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8538d27eb..6234469eb 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -20,6 +20,7 @@ en:
           server_admin: Server admin
           teacher: Teacher
           visitor: Visitor
+          website_manager: Website manager
     errors:
       models:
         user:
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 9cd2fafc6..5d3348026 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -20,6 +20,7 @@ fr:
           server_admin: Administrateur du serveur
           teacher: Enseignant·e
           visitor: Visiteur
+          website_manager: Responsable de site web
     errors:
       models:
         user:
diff --git a/db/migrate/20220303145900_create_join_table_communication_websites_users.rb b/db/migrate/20220303145900_create_join_table_communication_websites_users.rb
new file mode 100644
index 000000000..742d66025
--- /dev/null
+++ b/db/migrate/20220303145900_create_join_table_communication_websites_users.rb
@@ -0,0 +1,8 @@
+class CreateJoinTableCommunicationWebsitesUsers < ActiveRecord::Migration[6.1]
+  def change
+    create_join_table :communication_websites, :users, column_options: {type: :uuid} do |t|
+      t.index [:communication_website_id, :user_id], name: 'website_user'
+      t.index [:user_id, :communication_website_id], name: 'user_website'
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 0c898ee22..3853ceb63 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2022_03_03_125819) do
+ActiveRecord::Schema.define(version: 2022_03_03_145900) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -362,6 +362,13 @@ ActiveRecord::Schema.define(version: 2022_03_03_125819) do
     t.index ["university_id"], name: "index_communication_websites_on_university_id"
   end
 
+  create_table "communication_websites_users", id: false, force: :cascade do |t|
+    t.uuid "communication_website_id", null: false
+    t.uuid "user_id", null: false
+    t.index ["communication_website_id", "user_id"], name: "website_user"
+    t.index ["user_id", "communication_website_id"], name: "user_website"
+  end
+
   create_table "delayed_jobs", force: :cascade do |t|
     t.integer "priority", default: 0, null: false
     t.integer "attempts", default: 0, null: false
-- 
GitLab