form_admin_fields.html.twig 6 KB
Newer Older
Sebastian Castro's avatar
Sebastian Castro committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}

{% block sonata_security_roles_widget %}
{% spaceless %}
    <div class="editable">
        {% set object = form.vars.sonata_admin.admin.subject %}

        <div class="role-group">
            <h4 class="role-group-title main-roles">Roles Principaux</h4>
            {% set mainRoles = {
                'ROLE_SUPER_ADMIN': 'SUPER ADMIN (tous les pouvoirs)',
                'ROLE_ADMIN': "MODERATEUR (Accès aux fonctions de modérations, à détailler dans ModèleDeDonnées/QuiPeutContribuer)",
                'ROLE_DIRECTMODERATION_ADD': "CONTRIBUTEUR + (peut ajouter des éléments comme un Admin depuis le site)",
                'ROLE_DIRECTMODERATION_EDIT_OWN_CONTRIB': "CONTRIBUTEUR + (peut éditer les éléments auquel il a contribué, même sans être propriétaire)",
                'ROLE_SONATA_ADMIN': "INTERFACE ADMINISTRATEUR"
            }%}
            {% for role, label in mainRoles %}
                <div class="checkbox role-checkbox {{ role }}">
                  <label>
                    <input type="checkbox" name="{{form.vars.full_name}}[]" value="{{role}}"
                            {{ role in object.roles ? 'checked' : '' }}
                            {{ is_granted(role) ? '' : 'disabled'}}>
                    <span class="control-label__text">{{ label }}</span>
                  </label>
               </div>
            {% endfor %}
        </div>

        <div class="admin-config">
            <p>Pour les utilisateurs ayant accès à l'interface administrateur, vous devez <b>configurer les permissions sur chaque object</b></br>(Note: les SUPER_ADMIN ont accès à tous les objects, pas besoin de configurer pour eux)</p>
            <div class="btn-check-all btn btn-primary btn-sm checked">Tout cocher/décocher</div>

            {% for roles_group in choices|batch(5) %}
                {% set firstRole = roles_group|first %}
                {% if 'ROLE_ADMIN_' in firstRole.label or 'ROLE_SONATA_USER_ADMIN_' in firstRole.label %}
                    {% set title = firstRole.label|split('ROLE_ADMIN_')|last|split('ROLE_SONATA_USER_ADMIN_')|last %}
46
                    {% set title = title|replace({'_': ' '})|slice(0,-5) %}
Sebastian Castro's avatar
Sebastian Castro committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
                    {% set hidden = 'HIDDEN' in title %}

                    {% if title in ["GENERAL CONFIG", "ELEMENTS", "PROJECT"]%}<hr>{% endif %}

                    {% if not hidden %}
                        <div class="role-group object">
                            <div class="checkbox role-checkbox group-checkbox inline">
                              <label>
                                <i class="fa fa-check-circle btn-check-all-for-object"></i>
                                <span class="role-group-title btn-check-all-for-object">{{ title|capitalize }}</span>
                              </label>
                           </div>

                            {% for role in roles_group %}
                               {% set action = role.label|split('_')|last %}
                               <div class="checkbox role-checkbox inline">
                                  <label>
                                    <input type="checkbox" name="{{form.vars.full_name}}[]" value="{{role.value}}"
                                          {% if role.value in object.roles %}checked{% endif %}>
                                    <span class="control-label__text">{{ action }}</span>
                                  </label>
                               </div>
                            {% endfor %}
                        </div>
                    {% endif %}
                {% endif %}
            {% endfor %}
        </div>
    </div>

    <script>
        $(document).ready(function() {
            $('.btn-check-all-for-object').click(function() {
                var checkboxes = $(this).closest('.role-group').find('input[type=checkbox]');
                var value = !checkboxes.first().is(':checked');
                $.each(checkboxes, function() {
                    $(this).prop('checked', value).trigger('change');
                    if (value) $(this).parent().addClass('checked');
                    else $(this).parent().removeClass('checked');
                });
            });

            $('.btn-check-all').click(function() {
                var value = $(this).hasClass('checked');
                console.log("vakye", value);
                $(this).toggleClass('checked', !value);
                var checkboxes = $('.role-group.object input[type=checkbox]');
                $.each(checkboxes, function() {
                    $(this).prop('checked', value).trigger('change');
                    if (value) $(this).parent().addClass('checked');
                    else $(this).parent().removeClass('checked');
                });
            });

            checkForChanges();
        });

        function checkForChanges()
        {
            var show = $('.ROLE_SONATA_ADMIN .icheckbox_square-blue').hasClass('checked') && !$('.ROLE_SUPER_ADMIN .icheckbox_square-blue').hasClass('checked');
            $('.admin-config').toggle(show);
            setTimeout(checkForChanges, 200);
        }

    </script>

    <style>
        .role-group { margin-bottom: 15px; }
        .role-group-title {
            font-size: 18px;
            color: #2b89c5;
            margin-right: 15px;
            font-weight: bold;
            width: 200px;
            display: inline-block;
        }
        .main-roles { margin-bottom: 20px; }
        .role-checkbox {
            margin-bottom: 15px;
            margin-right: 20px;
        }
        .role-checkbox.inline { display: inline; }
        .btn-check-all-for-object { font-size: 18px; margin-right: 10px;  }
        .btn-check-all { margin-bottom: 20px; }
        .icheckbox_square-blue.disabled ~ .control-label__text { color: grey; }
    </style>
{% endspaceless %}
{% endblock sonata_security_roles_widget %}