Menu

[r8607]: / trunk / gui / account / views.py  Maximize  Restore  History

Download this file

220 lines (190 with data), 8.5 kB

  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
 46
 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#+
# Copyright 2010 iXsystems, Inc.
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# $FreeBSD$
#####################################################################
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, render
from django.utils import simplejson
from django.utils.translation import ugettext as _
from django.contrib.auth.views import login
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.forms import AuthenticationForm
from freenasUI.account import forms, models
from freenasUI.common.system import get_sw_login_version, get_sw_name
from freenasUI.freeadmin.views import JsonResponse
def home(request):
focus_form = request.GET.get('tab', 'passform')
return render(request, 'account/index.html', {
'focus_form': focus_form,
})
def bsduser(request):
bsduser_list = models.bsdUsers.objects.order_by("bsdusr_uid").select_related().filter(bsdusr_builtin=False)
bsduser_list_builtin = models.bsdUsers.objects.order_by("bsdusr_uid").select_related().filter(bsdusr_builtin=True)
return render(request, 'account/bsdusers.html', {
'bsduser_list': bsduser_list,
'bsduser_list_builtin': bsduser_list_builtin,
})
def bsdgroup(request):
bsdgroup_list = models.bsdGroups.objects.order_by("bsdgrp_gid").filter(bsdgrp_builtin=False)
bsdgroup_list_builtin = models.bsdGroups.objects.order_by("bsdgrp_gid").filter(bsdgrp_builtin=True)
return render_to_response('account/bsdgroups.html', {
'bsdgroup_list': bsdgroup_list,
'bsdgroup_list_builtin': bsdgroup_list_builtin,
})
def password_change(request):
extra_context = {}
password_change_form=forms.PasswordChangeForm
passform = password_change_form(user=request.user)
if request.method == 'POST':
passform = password_change_form(user=request.user, data=request.POST)
if passform.is_valid():
if passform.cleaned_data['change_root']:
root = models.bsdUsers.objects.get(bsdusr_username='root')
new_password = passform.cleaned_data.get('new_password1')
bsdpasswdform = forms.bsdUserPasswordForm(instance=root)
bsdpasswdform.cleaned_data = {}
bsdpasswdform.cleaned_data['bsdusr_password1'] = new_password
bsdpasswdform.cleaned_data['bsdusr_password2'] = new_password
bsdpasswdform.save()
usable = request.user.has_usable_password()
passform.save()
events = []
if not usable:
from freenasUI.tools.alert import Alert
alert = Alert()
alert.perform()
alert.write()
del alert
events.append("loadalert()")
return JsonResponse(message=_("Password successfully updated."), events=events)
extra_context.update({ 'passform' : passform, })
return render(request, 'account/passform.html', extra_context)
def user_change(request):
extra_context = {}
changeform = forms.UserChangeForm(instance=request.user)
if request.method == 'POST':
changeform = forms.UserChangeForm(instance=request.user, data=request.POST)
if changeform.is_valid():
changeform.save()
return JsonResponse(message=_("Admin user successfully updated."))
extra_context.update({ 'changeform' : changeform, })
return render(request, 'account/changeform.html', extra_context)
def group2user_update(request, object_id):
if request.method == 'POST':
f = forms.bsdGroupToUserForm(object_id, request.POST)
if f.is_valid():
f.save()
return JsonResponse(message=_("Users successfully updated."))
else:
f = forms.bsdGroupToUserForm(groupid=object_id)
return render(request, 'account/bsdgroup2user_form.html', {
'url': reverse('account_bsdgroup_members', kwargs={'object_id':object_id}),
'form' : f,
})
def user2group_update(request, object_id):
if request.method == 'POST':
f = forms.bsdUserToGroupForm(object_id, request.POST)
if f.is_valid():
f.save()
return JsonResponse(message=_("Groups successfully updated."))
else:
f = forms.bsdUserToGroupForm(userid=object_id)
return render(request, 'account/bsdgroup2user_form.html', {
'url': reverse('account_bsduser_groups', kwargs={'object_id':object_id}),
'form' : f,
})
def json_users(request, exclude=None):
from common.freenasldap import FLAGS_DBINIT, FLAGS_CACHE_READ_USER, FLAGS_CACHE_WRITE_USER
from common.freenasusers import FreeNAS_Users
query = request.GET.get("q", None)
json = {
'identifier': 'id',
'label': 'name',
'items': [],
}
if exclude:
exclude = exclude.split(',')
else:
exclude = []
idx = 1
for user in FreeNAS_Users(flags=FLAGS_DBINIT|FLAGS_CACHE_READ_USER|FLAGS_CACHE_WRITE_USER):
if idx > 50:
break
if (query == None or user.pw_name.startswith(query)) and \
user.pw_name not in exclude:
json['items'].append({
'id': user.pw_name,
'name': user.pw_name,
'label': user.pw_name,
})
idx += 1
return HttpResponse(simplejson.dumps(json, indent=3))
def json_groups(request):
from common.freenasldap import FLAGS_DBINIT, FLAGS_CACHE_READ_GROUP, FLAGS_CACHE_WRITE_GROUP
from common.freenasusers import FreeNAS_Groups
query = request.GET.get("q", None)
json = {
'identifier': 'id',
'label': 'name',
'items': [],
}
idx = 1
for grp in FreeNAS_Groups(flags=FLAGS_DBINIT|FLAGS_CACHE_READ_GROUP|FLAGS_CACHE_WRITE_GROUP):
if idx > 50:
break
if query == None or grp.gr_name.startswith(query):
json['items'].append({
'id': grp.gr_name,
'name': grp.gr_name,
'label': grp.gr_name,
})
idx += 1
return HttpResponse(simplejson.dumps(json, indent=3))
"""
Wrapper to login to do not allow login and redirect to
shutdown, reboot or logout pages,
instead redirect to /
"""
def login_wrapper(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context={}):
extra_context.update({
'sw_login_version': get_sw_login_version(),
'sw_name': get_sw_name(),
})
response = login(request, template_name='registration/login.html',
redirect_field_name=redirect_field_name,
authentication_form=authentication_form,
current_app=current_app, extra_context=extra_context)
if response.status_code in (301, 302) and response._headers.get('location', ('',''))[1] in (
reverse('system_reboot'), reverse('system_shutdown'), reverse('account_logout'),
):
response._headers['location'] = ('Location', '/')
elif request.user.is_authenticated():
return HttpResponseRedirect('/')
return response
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.