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.64 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
from .models import (
    User,
    ServiceUser,
    School,
    ListRight,
    ListShell,
    Adherent,
    Club,
    Ban,
    Whitelist,
    Request,
    LdapUser,
    LdapServiceUser,
    LdapServiceUserGroup,
    LdapUserGroup
)
from .forms import (
    UserChangeForm,
    UserCreationForm,
    ServiceUserChangeForm,
    ServiceUserCreationForm
)
57

58

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

chirac's avatar
chirac committed
65

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

chirac's avatar
chirac committed
72

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

chirac's avatar
chirac committed
78

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

chirac's avatar
chirac committed
84

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

89

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

chirac's avatar
chirac committed
95

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

100

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

chirac's avatar
chirac committed
105

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

110

111
class WhitelistAdmin(VersionAdmin):
chirac's avatar
chirac committed
112 113
    """Gestion des whitelist"""
    pass
chirac's avatar
chirac committed
114

115

116
class UserAdmin(VersionAdmin, BaseUserAdmin):
chirac's avatar
chirac committed
117
    """Gestion d'un user : modification des champs perso, mot de passe, etc"""
118 119 120 121 122 123 124
    # 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
125 126 127 128 129 130 131 132
    list_display = (
        'pseudo',
        'surname',
        'email',
        'school',
        'is_admin',
        'shell'
    )
133 134
    # Need to reset the settings from BaseUserAdmin
    # They are using fields we don't use like 'is_staff'
135
    list_filter = ()
136 137
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
chirac's avatar
chirac committed
138 139 140 141
        (
            'Personal info',
            {
                'fields':
142
                ('surname', 'email', 'school', 'shell', 'uid_number')
chirac's avatar
chirac committed
143 144
            }
        ),
145 146 147 148 149
        ('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
150 151 152 153 154 155 156 157 158 159 160 161 162 163
        (
            None,
            {
                'classes': ('wide',),
                'fields': (
                    'pseudo',
                    'surname',
                    'email',
                    'school',
                    'is_admin',
                    'password1',
                    'password2'
                    )
            }
164 165
        ),
    )
166
    search_fields = ('pseudo', 'surname')
167 168 169
    ordering = ('pseudo',)
    filter_horizontal = ()

chirac's avatar
chirac committed
170

chirac's avatar
chirac committed
171
class ServiceUserAdmin(VersionAdmin, BaseUserAdmin):
chirac's avatar
chirac committed
172 173
    """Gestion d'un service user admin : champs personnels,
    mot de passe; etc"""
chirac's avatar
chirac committed
174 175 176 177 178 179 180
    # 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.
181
    list_display = ('pseudo', 'access_group')
chirac's avatar
chirac committed
182 183
    list_filter = ()
    fieldsets = (
184
        (None, {'fields': ('pseudo', 'password', 'access_group')}),
chirac's avatar
chirac committed
185 186 187 188
    )
    # 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
189 190 191 192 193 194
        (
            None,
            {
                'classes': ('wide',),
                'fields': ('pseudo', 'password1', 'password2')
            }
chirac's avatar
chirac committed
195 196 197 198 199 200
        ),
    )
    search_fields = ('pseudo',)
    ordering = ('pseudo',)
    filter_horizontal = ()

chirac's avatar
chirac committed
201

202
admin.site.register(User, UserAdmin)
203 204
admin.site.register(Adherent, UserAdmin)
admin.site.register(Club, UserAdmin)
chirac's avatar
chirac committed
205
admin.site.register(ServiceUser, ServiceUserAdmin)
206 207
admin.site.register(LdapUser, LdapUserAdmin)
admin.site.register(LdapUserGroup, LdapUserGroupAdmin)
chirac's avatar
chirac committed
208
admin.site.register(LdapServiceUser, LdapServiceUserAdmin)
209
admin.site.register(LdapServiceUserGroup, LdapServiceUserGroupAdmin)
210
admin.site.register(School, SchoolAdmin)
211
admin.site.register(ListRight, ListRightAdmin)
212
admin.site.register(ListShell, ListShellAdmin)
chirac's avatar
chirac committed
213
admin.site.register(Ban, BanAdmin)
chirac's avatar
chirac committed
214
admin.site.register(Whitelist, WhitelistAdmin)
215
admin.site.register(Request, RequestAdmin)
216 217
# Now register the new UserAdmin...
admin.site.unregister(User)
chirac's avatar
chirac committed
218
admin.site.unregister(ServiceUser)
219
admin.site.register(User, UserAdmin)
chirac's avatar
chirac committed
220
admin.site.register(ServiceUser, ServiceUserAdmin)
221 222 223
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)