Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

admin.py 6.85 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
chirac's avatar
chirac committed
23 24 25 26
"""
Definition des vues pour les admin. Classique, sauf pour users,
où on fait appel à UserChange et ServiceUserChange, forms custom
"""
27

28 29
from __future__ import unicode_literals

lhark's avatar
lhark committed
30
from django.contrib import admin
31 32
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
33
from reversion.admin import VersionAdmin
lhark's avatar
lhark committed
34

35 36
from .models import (
    User,
37
    EMailAddress,
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    ServiceUser,
    School,
    ListRight,
    ListShell,
    Adherent,
    Club,
    Ban,
    Whitelist,
    Request,
    LdapUser,
    LdapServiceUser,
    LdapServiceUserGroup,
    LdapUserGroup
)
from .forms import (
    UserChangeForm,
    UserCreationForm,
    ServiceUserChangeForm,
    ServiceUserCreationForm
)
58

59

60
class LdapUserAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
61 62 63
    """Administration du ldapuser"""
    list_display = ('name', 'uidNumber', 'login_shell')
    exclude = ('user_password', 'sambat_nt_password')
64 65
    search_fields = ('name',)

chirac's avatar
chirac committed
66

chirac's avatar
chirac committed
67
class LdapServiceUserAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
68
    """Administration du ldapserviceuser"""
chirac's avatar
chirac committed
69 70 71 72
    list_display = ('name',)
    exclude = ('user_password',)
    search_fields = ('name',)

chirac's avatar
chirac committed
73

74
class LdapUserGroupAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
75 76
    """Administration du ldapusergroupe"""
    list_display = ('name', 'members', 'gid')
77 78
    search_fields = ('name',)

chirac's avatar
chirac committed
79

80
class LdapServiceUserGroupAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
81
    """Administration du ldap serviceusergroup"""
82 83 84
    list_display = ('name',)
    search_fields = ('name',)

chirac's avatar
chirac committed
85

86
class SchoolAdmin(VersionAdmin):
chirac's avatar
chirac committed
87 88 89
    """Administration, gestion des écoles"""
    pass

90

91
class ListRightAdmin(VersionAdmin):
chirac's avatar
chirac committed
92 93
    """Gestion de la liste des droits existants
    Ne permet pas l'edition du gid (primarykey pour ldap)"""
94
    list_display = ('unix_name',)
95

chirac's avatar
chirac committed
96

97
class ListShellAdmin(VersionAdmin):
chirac's avatar
chirac committed
98 99 100
    """Gestion de la liste des shells coté admin"""
    pass

101

102
class RequestAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
103
    """Gestion des request objet, ticket pour lien de reinit mot de passe"""
104
    list_display = ('user', 'type', 'created_at', 'expires_at')
105

chirac's avatar
chirac committed
106

107
class BanAdmin(VersionAdmin):
chirac's avatar
chirac committed
108 109
    """Gestion des bannissements"""
    pass
chirac's avatar
chirac committed
110

111

112
class EMailAddressAdmin(VersionAdmin):
grisel-davy's avatar
grisel-davy committed
113 114 115 116
    """Gestion des alias mail"""
    pass


117
class WhitelistAdmin(VersionAdmin):
chirac's avatar
chirac committed
118 119
    """Gestion des whitelist"""
    pass
chirac's avatar
chirac committed
120

121

122
class UserAdmin(VersionAdmin, BaseUserAdmin):
chirac's avatar
chirac committed
123
    """Gestion d'un user : modification des champs perso, mot de passe, etc"""
124 125 126 127 128 129 130
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
chirac's avatar
chirac committed
131 132 133
    list_display = (
        'pseudo',
        'surname',
134 135 136
        'email',
        'local_email_redirect',
        'local_email_enabled',
chirac's avatar
chirac committed
137 138 139 140
        'school',
        'is_admin',
        'shell'
    )
141 142
    # Need to reset the settings from BaseUserAdmin
    # They are using fields we don't use like 'is_staff'
143
    list_filter = ()
144 145
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
chirac's avatar
chirac committed
146 147 148 149
        (
            'Personal info',
            {
                'fields':
150
                ('surname', 'email', 'school', 'shell', 'uid_number')
chirac's avatar
chirac committed
151 152
            }
        ),
153 154 155 156 157
        ('Permissions', {'fields': ('is_admin', )}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
chirac's avatar
chirac committed
158 159 160 161 162 163 164
        (
            None,
            {
                'classes': ('wide',),
                'fields': (
                    'pseudo',
                    'surname',
165
                    'email',
chirac's avatar
chirac committed
166 167 168 169 170 171
                    'school',
                    'is_admin',
                    'password1',
                    'password2'
                    )
            }
172 173
        ),
    )
174
    search_fields = ('pseudo', 'surname')
175 176 177
    ordering = ('pseudo',)
    filter_horizontal = ()

chirac's avatar
chirac committed
178

chirac's avatar
chirac committed
179
class ServiceUserAdmin(VersionAdmin, BaseUserAdmin):
chirac's avatar
chirac committed
180 181
    """Gestion d'un service user admin : champs personnels,
    mot de passe; etc"""
chirac's avatar
chirac committed
182 183 184 185 186 187 188
    # The forms to add and change user instances
    form = ServiceUserChangeForm
    add_form = ServiceUserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
189
    list_display = ('pseudo', 'access_group')
chirac's avatar
chirac committed
190 191
    list_filter = ()
    fieldsets = (
192
        (None, {'fields': ('pseudo', 'password', 'access_group')}),
chirac's avatar
chirac committed
193 194 195 196
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
chirac's avatar
chirac committed
197 198 199 200 201 202
        (
            None,
            {
                'classes': ('wide',),
                'fields': ('pseudo', 'password1', 'password2')
            }
chirac's avatar
chirac committed
203 204 205 206 207 208
        ),
    )
    search_fields = ('pseudo',)
    ordering = ('pseudo',)
    filter_horizontal = ()

chirac's avatar
chirac committed
209

210
admin.site.register(User, UserAdmin)
211 212
admin.site.register(Adherent, UserAdmin)
admin.site.register(Club, UserAdmin)
chirac's avatar
chirac committed
213
admin.site.register(ServiceUser, ServiceUserAdmin)
214 215
admin.site.register(LdapUser, LdapUserAdmin)
admin.site.register(LdapUserGroup, LdapUserGroupAdmin)
chirac's avatar
chirac committed
216
admin.site.register(LdapServiceUser, LdapServiceUserAdmin)
217
admin.site.register(LdapServiceUserGroup, LdapServiceUserGroupAdmin)
218
admin.site.register(School, SchoolAdmin)
219
admin.site.register(ListRight, ListRightAdmin)
220
admin.site.register(ListShell, ListShellAdmin)
chirac's avatar
chirac committed
221
admin.site.register(Ban, BanAdmin)
222
admin.site.register(EMailAddress, EMailAddressAdmin)
chirac's avatar
chirac committed
223
admin.site.register(Whitelist, WhitelistAdmin)
224
admin.site.register(Request, RequestAdmin)
225 226
# Now register the new UserAdmin...
admin.site.unregister(User)
chirac's avatar
chirac committed
227
admin.site.unregister(ServiceUser)
228
admin.site.register(User, UserAdmin)
chirac's avatar
chirac committed
229
admin.site.register(ServiceUser, ServiceUserAdmin)
230 231 232
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)