Commit f62c4121 authored by Jean-Noël Rouchon's avatar Jean-Noël Rouchon

Possibilité d'ajouter des droits pour les types d'intervention et les sites aux utilisateurs

parent 9c2edecb
...@@ -21,14 +21,54 @@ class Technician < ActiveRecord::Base ...@@ -21,14 +21,54 @@ class Technician < ActiveRecord::Base
validates_presence_of :name, :login validates_presence_of :name, :login
has_many :tasks has_many :tasks
has_many :technicians_rights has_many :technicians_rights
def get_types
return Technicians_type.includes(:type).where(:technician_id => self.id)
end
def get_types_ids
ids = []
Technicians_type.includes(:type).where(:technician_id => self.id).each do |type|
ids << type.type_id
end
return ids
end
def add_types ids
Technicians_type.where(:technician_id => self.id).destroy_all
ids.uniq.each do |id|
Technicians_type.create :technician_id => self.id, :type_id => id
end
end
def get_sites
return Technicians_site.includes(:site).where(:technician_id => self.id)
end
def get_sites_ids
ids = []
Technicians_site.includes(:site).where(:technician_id => self.id).each do |site|
ids << site.site_id
end
return ids
end
def add_sites ids
Technicians_site.where(:technician_id => self.id).destroy_all
ids.uniq.each do |id|
Technicians_site.create :technician_id => self.id, :site_id => id
end
end
end end
class Technicians_type < ActiveRecord::Base class Technicians_type < ActiveRecord::Base
belongs_to :technician belongs_to :technician
belongs_to :type
end end
class Technicians_site < ActiveRecord::Base class Technicians_site < ActiveRecord::Base
belongs_to :technician belongs_to :technician
belongs_to :site
end end
class State < ActiveRecord::Base class State < ActiveRecord::Base
......
...@@ -249,5 +249,7 @@ post '/users' do ...@@ -249,5 +249,7 @@ post '/users' do
user.passwordhash = passwordhash user.passwordhash = passwordhash
end end
user.save user.save
user.add_types params["typeDroits"].split(",")
user.add_sites params["siteDroits"].split(",")
redirect "/users" redirect "/users"
end end
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
%td %td
= (user.actif ? "oui" : "non") = (user.actif ? "oui" : "non")
%td %td
%button.btn.btn-success.btn-xs{type: "button", "data-id"=>"#{user.id}", "data-toggle"=>"modal", "data-target"=>"#myModal", "data-name" => "#{user.name}", "data-login" => "#{user.login}", "data-title" => "Utilisateur n°#{user.id}", "data-email" => "#{user.email}", "data-admin" => "#{user.admin}", "data-actif" => "#{user.actif}"} %button.btn.btn-success.btn-xs{type: "button", "data-id"=>"#{user.id}", "data-toggle"=>"modal", "data-target"=>"#myModal", "data-name" => "#{user.name}", "data-login" => "#{user.login}", "data-title" => "Utilisateur n°#{user.id}", "data-email" => "#{user.email}", "data-admin" => "#{user.admin}", "data-actif" => "#{user.actif}", "data-typedroit" => "#{user.get_types_ids.join(',')}", "data-sitedroit" => "#{user.get_sites_ids.join(',')}"}
Modifier Modifier
%button.btn.btn-primary{type: "button", "data-id"=>"0", "data-toggle"=>"modal", "data-target"=>"#myModal", "data-admin" => "false", "data-actif" => "true"} %button.btn.btn-primary{type: "button", "data-id"=>"0", "data-toggle"=>"modal", "data-target"=>"#myModal", "data-admin" => "false", "data-actif" => "true"}
Nouvel utilisateur Nouvel utilisateur
...@@ -70,6 +70,37 @@ ...@@ -70,6 +70,37 @@
Mot de passe Mot de passe
.col-sm-10 .col-sm-10
%input{id: "userPassword", name: "userPassword", type: "password"} %input{id: "userPassword", name: "userPassword", type: "password"}
%input{type: "text", id: 'typeDroits', name: 'typeDroits', hidden: true}
%input{type: "text", id: 'siteDroits', name: 'siteDroits', hidden: true}
.form-group
.col-md-6
%table.table.table-hover{id: "table_type"}
%tr
%th Types de travaux
%th
- Type.all.each do |type|
%tr{id: "ligne_type_#{type.id}", class: "ligne_type"}
%td
= type.description
%td{align: "right"}
%button.btn.btn-success.btn-xs{type: "button", onclick:"type_autorise(#{type.id})"}
Autoriser
%button.btn.btn-danger.btn-xs{type: "button", onclick:"type_interdit(#{type.id})"}
Interdire
.col-md-6
%table.table.table-hover{id: "table_site"}
%tr
%th Sites
%th
- Site.all.each do |site|
%tr{id: "ligne_site_#{site.id}", class: "ligne_site"}
%td
= site.name
%td{align: "right"}
%button.btn.btn-success.btn-xs{type: "button", onclick:"site_autorise(#{site.id})"}
Autoriser
%button.btn.btn-danger.btn-xs{type: "button", onclick:"site_interdit(#{site.id})"}
Interdire
.modal-footer .modal-footer
%button.btn.btn-primary{type: "submit", form: "formAction"} %button.btn.btn-primary{type: "submit", form: "formAction"}
Enregistrer Enregistrer
...@@ -79,6 +110,9 @@ ...@@ -79,6 +110,9 @@
:coffeescript :coffeescript
$ -> $ ->
type_droit = []
site_droit = []
$('#myModal').on('show.bs.modal', (event) -> $('#myModal').on('show.bs.modal', (event) ->
button = $(event.relatedTarget) button = $(event.relatedTarget)
modal = $(this) modal = $(this)
...@@ -90,4 +124,57 @@ ...@@ -90,4 +124,57 @@
$('#userAdmin').prop('checked',button.data('admin')) $('#userAdmin').prop('checked',button.data('admin'))
$('#userActif').prop('checked',button.data('actif')) $('#userActif').prop('checked',button.data('actif'))
$('#userPassword').val("") $('#userPassword').val("")
type_droit = button.data('typedroit').split(",")
$('.ligne_type').removeClass('success')
for type in type_droit
$('#ligne_type_' + type).addClass('success')
changeTypeDroit()
site_droit = button.data('sitedroit').split(",")
$('.ligne_site').removeClass('success')
for site in site_droit
$('#ligne_site_' + site).addClass('success')
changeSiteDroit()
) )
removeTermFromArray = (array, term) ->
newArray = array.filter (word) -> word isnt term.toString()
return(newArray)
addTermToArray = (array, term) ->
addBool = true
for id in array
addBool = addBool and id!=term.toString()
newArray = array
newArray = array.push(term.toString()) if addBool
return(newArray)
changeTypeDroit = () ->
$('#typeDroits').val(type_droit.join())
changeSiteDroit = () ->
$('#siteDroits').val(site_droit.join())
type_autorise = (id) ->
$('#ligne_type_' + id).addClass('success')
addTermToArray(type_droit, id)
changeTypeDroit()
type_interdit = (id) ->
$('#ligne_type_' + id).removeClass('success')
type_droit = removeTermFromArray(type_droit, id)
changeTypeDroit()
site_autorise = (id) ->
$('#ligne_site_' + id).addClass('success')
addTermToArray(site_droit, id)
changeSiteDroit()
site_interdit = (id) ->
$('#ligne_site_' + id).removeClass('success')
site_droit = removeTermFromArray(site_droit, id)
changeSiteDroit()
window["type_autorise"] = type_autorise
window["type_interdit"] = type_interdit
window["site_autorise"] = site_autorise
window["site_interdit"] = site_interdit
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment