2017-01-17 08:34:47 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2017-01-25 13:19:16 +00:00
|
|
|
|
|
|
|
|
2017-12-18 10:38:30 +00:00
|
|
|
from django.contrib import messages
|
2017-01-25 13:19:16 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2017-01-17 08:34:47 +00:00
|
|
|
from django.core.cache import cache
|
2017-01-25 13:19:16 +00:00
|
|
|
from django.shortcuts import redirect
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.urls import reverse_lazy
|
2017-01-17 08:34:47 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.views.generic.base import TemplateView
|
2017-07-10 02:26:17 +00:00
|
|
|
from django.views.generic.edit import (
|
2019-12-05 07:09:25 +00:00
|
|
|
CreateView, UpdateView
|
2017-07-10 02:26:17 +00:00
|
|
|
)
|
2018-08-13 07:01:56 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
from common.const import (
|
|
|
|
create_success_msg, update_success_msg, KEY_CACHE_RESOURCES_ID
|
|
|
|
)
|
2019-12-05 07:09:25 +00:00
|
|
|
from common.utils import get_logger
|
2019-07-24 04:50:39 +00:00
|
|
|
from common.permissions import (
|
2019-12-05 07:09:25 +00:00
|
|
|
PermissionsMixin, IsOrgAdmin,
|
2019-09-12 10:56:26 +00:00
|
|
|
CanUpdateDeleteUser,
|
2019-07-24 04:50:39 +00:00
|
|
|
)
|
2018-09-03 03:24:25 +00:00
|
|
|
from orgs.utils import current_org
|
2017-01-25 13:19:16 +00:00
|
|
|
from .. import forms
|
|
|
|
from ..models import User, UserGroup
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..utils import get_password_check_rules, is_need_unblock
|
2018-02-27 04:18:36 +00:00
|
|
|
from ..signals import post_user_create
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2017-07-10 02:26:17 +00:00
|
|
|
__all__ = [
|
|
|
|
'UserListView', 'UserCreateView', 'UserDetailView',
|
2019-12-20 07:55:59 +00:00
|
|
|
'UserUpdateView', 'UserBulkUpdateView',
|
|
|
|
'UserGrantedAssetView', 'UserAssetPermissionListView',
|
|
|
|
'UserGrantedRemoteAppView', 'UserRemoteAppPermissionListView',
|
|
|
|
'UserGrantedDatabasesAppView', 'UserDatabaseAppPermissionListView',
|
2017-07-10 02:26:17 +00:00
|
|
|
]
|
2017-01-25 13:19:16 +00:00
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserListView(PermissionsMixin, TemplateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/user_list.html'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-12-18 10:38:30 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
context.update({
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User list'),
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserCreateView(PermissionsMixin, SuccessMessageMixin, CreateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = User
|
2019-05-28 04:11:55 +00:00
|
|
|
form_class = forms.UserCreateForm
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/user_create.html'
|
|
|
|
success_url = reverse_lazy('users:user-list')
|
2018-01-09 15:07:53 +00:00
|
|
|
success_message = create_success_msg
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-05-24 11:41:07 +00:00
|
|
|
check_rules = get_password_check_rules()
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('Create user'),
|
|
|
|
'password_check_rules': check_rules,
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
user = form.save(commit=False)
|
|
|
|
user.created_by = self.request.user.username or 'System'
|
|
|
|
user.save()
|
2018-12-11 12:32:55 +00:00
|
|
|
if current_org and current_org.is_real():
|
2019-09-12 10:56:26 +00:00
|
|
|
user.related_user_orgs.add(current_org.id)
|
2018-04-03 08:28:58 +00:00
|
|
|
post_user_create.send(self.__class__, user=user)
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().form_valid(form)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2018-08-01 02:44:43 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(UserCreateView, self).get_form_kwargs()
|
|
|
|
data = {'request': self.request}
|
|
|
|
kwargs.update(data)
|
|
|
|
return kwargs
|
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserUpdateView(PermissionsMixin, SuccessMessageMixin, UpdateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = User
|
2019-05-28 04:11:55 +00:00
|
|
|
form_class = forms.UserUpdateForm
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/user_update.html'
|
|
|
|
context_object_name = 'user_object'
|
|
|
|
success_url = reverse_lazy('users:user-list')
|
2018-01-09 15:07:53 +00:00
|
|
|
success_message = update_success_msg
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
def _deny_permission(self):
|
|
|
|
obj = self.get_object()
|
|
|
|
return not self.request.user.is_superuser and obj.is_superuser
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if self._deny_permission():
|
|
|
|
return redirect(self.success_url)
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2018-11-22 04:27:27 +00:00
|
|
|
check_rules = get_password_check_rules()
|
2018-06-05 09:26:31 +00:00
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('Update user'),
|
|
|
|
'password_check_rules': check_rules,
|
|
|
|
}
|
2017-12-18 10:38:30 +00:00
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2018-08-01 02:44:43 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(UserUpdateView, self).get_form_kwargs()
|
|
|
|
data = {'request': self.request}
|
|
|
|
kwargs.update(data)
|
|
|
|
return kwargs
|
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserBulkUpdateView(PermissionsMixin, TemplateView):
|
2017-04-12 03:50:15 +00:00
|
|
|
model = User
|
|
|
|
form_class = forms.UserBulkUpdateForm
|
|
|
|
template_name = 'users/user_bulk_update.html'
|
|
|
|
success_url = reverse_lazy('users:user-list')
|
2017-12-18 10:38:30 +00:00
|
|
|
success_message = _("Bulk update user success")
|
|
|
|
form = None
|
|
|
|
id_list = None
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-04-12 03:50:15 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-05-21 08:24:01 +00:00
|
|
|
spm = request.GET.get('spm', '')
|
|
|
|
users_id = cache.get(KEY_CACHE_RESOURCES_ID.format(spm))
|
2017-04-12 03:50:15 +00:00
|
|
|
if kwargs.get('form'):
|
|
|
|
self.form = kwargs['form']
|
|
|
|
elif users_id:
|
2019-05-21 08:24:01 +00:00
|
|
|
self.form = self.form_class(initial={'users': users_id})
|
2017-04-12 03:50:15 +00:00
|
|
|
else:
|
|
|
|
self.form = self.form_class()
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
2017-04-12 03:50:15 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2017-12-18 10:38:30 +00:00
|
|
|
messages.success(request, self.success_message)
|
2017-04-12 03:50:15 +00:00
|
|
|
return redirect(self.success_url)
|
|
|
|
else:
|
|
|
|
return self.get(request, form=form, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': 'Assets',
|
2018-08-13 07:01:56 +00:00
|
|
|
'action': _('Bulk update user'),
|
2017-04-12 03:50:15 +00:00
|
|
|
'form': self.form,
|
|
|
|
'users_selected': self.id_list,
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-12 03:50:15 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserDetailView(PermissionsMixin, DetailView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = User
|
|
|
|
template_name = 'users/user_detail.html'
|
2019-12-20 07:55:59 +00:00
|
|
|
context_object_name = "object"
|
2018-07-16 04:13:13 +00:00
|
|
|
key_prefix_block = "_LOGIN_BLOCK_{}"
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2018-07-16 04:13:13 +00:00
|
|
|
user = self.get_object()
|
|
|
|
key_block = self.key_prefix_block.format(user.username)
|
2017-01-17 08:34:47 +00:00
|
|
|
groups = UserGroup.objects.exclude(id__in=self.object.groups.all())
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User detail'),
|
2018-07-16 04:13:13 +00:00
|
|
|
'groups': groups,
|
|
|
|
'unblock': is_need_unblock(key_block),
|
2019-09-12 10:56:26 +00:00
|
|
|
'can_update': CanUpdateDeleteUser.has_update_object_permission(
|
|
|
|
self.request, self, user
|
|
|
|
),
|
|
|
|
'can_delete': CanUpdateDeleteUser.has_delete_object_permission(
|
|
|
|
self.request, self, user
|
|
|
|
),
|
2017-01-17 08:34:47 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-04-08 16:45:28 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2019-09-12 10:56:26 +00:00
|
|
|
org_users = current_org.get_org_members().values_list('id', flat=True)
|
2018-09-03 03:24:25 +00:00
|
|
|
queryset = queryset.filter(id__in=org_users)
|
|
|
|
return queryset
|
|
|
|
|
2017-04-08 16:45:28 +00:00
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserGrantedAssetView(PermissionsMixin, DetailView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = User
|
|
|
|
template_name = 'users/user_granted_asset.html'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
2017-12-31 04:20:08 +00:00
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User granted assets'),
|
2017-01-17 08:34:47 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-25 13:19:16 +00:00
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
class UserAssetPermissionListView(PermissionsMixin, DetailView):
|
2017-03-30 08:28:00 +00:00
|
|
|
model = User
|
2019-12-05 07:09:25 +00:00
|
|
|
template_name = 'users/user_asset_permission.html'
|
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-03-30 08:28:00 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
2017-12-31 04:20:08 +00:00
|
|
|
'app': _('Users'),
|
2019-12-05 07:09:25 +00:00
|
|
|
'action': _('Asset permission'),
|
2018-04-18 04:48:07 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
2019-12-20 07:55:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedRemoteAppView(PermissionsMixin, DetailView):
|
|
|
|
model = User
|
|
|
|
template_name = 'users/user_granted_remote_app.html'
|
|
|
|
permission_classes = [IsOrgAdmin]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User granted RemoteApp'),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class UserRemoteAppPermissionListView(PermissionsMixin, DetailView):
|
|
|
|
model = User
|
|
|
|
template_name = 'users/user_remote_app_permission.html'
|
|
|
|
permission_classes = [IsOrgAdmin]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('RemoteApp permission'),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedDatabasesAppView(PermissionsMixin, DetailView):
|
|
|
|
model = User
|
|
|
|
template_name = 'users/user_granted_database_app.html'
|
|
|
|
permission_classes = [IsOrgAdmin]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User granted DatabaseApp'),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class UserDatabaseAppPermissionListView(PermissionsMixin, DetailView):
|
|
|
|
model = User
|
|
|
|
template_name = 'users/user_database_app_permission.html'
|
|
|
|
permission_classes = [IsOrgAdmin]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('DatabaseApp permission'),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|