Commit 3a167ceb authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

BDD : nouvelles procédures utilitaires privilege__* pour accorder/retirer un privilège à un profil

parent 22ae714f
Loading
Loading
Loading
Loading
+67 −0
Changes for doc/release-notes/v5.2.11.md: 67 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -346,4 +346,71 @@ FROM tmp_these tmp
         LEFT JOIN unite_rech ur ON ur.source_code::text = tmp.unite_rech_id::text
         LEFT JOIN version_corrigee_deposee vcd ON vcd.source_code::text = tmp.source_code::text;


create function privilege__update_role_privilege() returns void
  language plpgsql
as $$
BEGIN
  -- création des 'role_privilege' manquants d'après le contenu de 'profil_to_role' et de 'profil_privilege'
  insert into role_privilege (role_id, privilege_id)
  select p2r.role_id, pp.privilege_id
  from profil_to_role p2r
         join profil pr on pr.id = p2r.profil_id
         join profil_privilege pp on pp.profil_id = pr.id
  where not exists(
          select * from role_privilege where role_id = p2r.role_id and privilege_id = pp.privilege_id
    );

  -- suppression des 'role_privilege' en trop d'après le contenu de 'profil_to_role' et de 'profil_privilege'
  delete from role_privilege rp
  where not exists (
          select *
          from profil_to_role p2r
                 join profil_privilege pp on pp.profil_id = p2r.profil_id
          where rp.role_id = p2r.role_id and rp.privilege_id = pp.privilege_id
    );
END;
$$;

create function privilege__grant_privilege_to_profile(categoryCode varchar, privilegeCode varchar, profileRoleId varchar) returns void
  language plpgsql
as $$
BEGIN
  -- insertion dans 'profil_privilege' (si pas déjà fait)
  insert into profil_privilege (privilege_id, profil_id)
  select p.id as privilege_id, profil.id as profil_id
  from profil
         join categorie_privilege cp on cp.code = categoryCode
         join privilege p on p.categorie_id = cp.id and p.code = privilegeCode
  where profil.role_id = profileroleid
    and not exists(
          select * from profil_privilege where privilege_id = p.id and profil_id = profil.id
    );

  perform privilege__update_role_privilege();
END;
$$;

create function privilege__revoke_privilege_to_profile(categoryCode varchar, privilegeCode varchar, profileRoleId varchar) returns void
  language plpgsql
as $$
BEGIN
  delete
  from profil_privilege pp1
  where exists(
                select *
                from profil_privilege pp
                       join profil on pp.profil_id = profil.id and role_id = profileRoleId
                       join privilege p on pp.privilege_id = p.id
                       join categorie_privilege cp on p.categorie_id = cp.id
                where p.code = privilegeCode
                  and cp.code = categoryCode
                  and pp.profil_id = pp1.profil_id
                  and pp.privilege_id = pp1.privilege_id
          );

  perform privilege__update_role_privilege();
END;
$$;

```
 No newline at end of file