From 5bca783e1236a268338c7140f2734e76ff3b15c3 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 14 Sep 2016 01:08:26 +0800 Subject: [PATCH] permission user search --- .../templates/assets/asset_group_detail.html | 8 - .../templates/assets/system_user_asset.html | 4 - .../assets/system_user_asset_group.html | 4 - .../templates/assets/system_user_detail.html | 4 - apps/common/utils.py | 45 ++++- apps/perms/models.py | 34 +++- .../perms/asset_permission_asset_list.html | 186 ++++++++++++++++++ .../perms/asset_permission_detail.html | 4 - .../perms/asset_permission_user_list.html | 20 +- apps/perms/views.py | 13 +- apps/users/templates/users/user_detail.html | 4 - 11 files changed, 288 insertions(+), 38 deletions(-) create mode 100644 apps/perms/templates/perms/asset_permission_asset_list.html diff --git a/apps/assets/templates/assets/asset_group_detail.html b/apps/assets/templates/assets/asset_group_detail.html index 4c48867b9..1b266de44 100644 --- a/apps/assets/templates/assets/asset_group_detail.html +++ b/apps/assets/templates/assets/asset_group_detail.html @@ -33,10 +33,6 @@ @@ -78,10 +74,6 @@ diff --git a/apps/assets/templates/assets/system_user_asset.html b/apps/assets/templates/assets/system_user_asset.html index 4f1fb4097..9f30c724e 100644 --- a/apps/assets/templates/assets/system_user_asset.html +++ b/apps/assets/templates/assets/system_user_asset.html @@ -39,10 +39,6 @@ diff --git a/apps/assets/templates/assets/system_user_asset_group.html b/apps/assets/templates/assets/system_user_asset_group.html index bcddd09fc..52e48815c 100644 --- a/apps/assets/templates/assets/system_user_asset_group.html +++ b/apps/assets/templates/assets/system_user_asset_group.html @@ -40,10 +40,6 @@ diff --git a/apps/assets/templates/assets/system_user_detail.html b/apps/assets/templates/assets/system_user_detail.html index 9dde0a0e1..7ca599d8f 100644 --- a/apps/assets/templates/assets/system_user_detail.html +++ b/apps/assets/templates/assets/system_user_detail.html @@ -42,10 +42,6 @@ diff --git a/apps/common/utils.py b/apps/common/utils.py index f6cb9ea9a..0d2a4284c 100644 --- a/apps/common/utils.py +++ b/apps/common/utils.py @@ -2,6 +2,8 @@ # from __future__ import unicode_literals +from itertools import chain +import string from django.shortcuts import reverse as dj_reverse from django.conf import settings @@ -39,4 +41,45 @@ def date_expired_default(): except TypeError: years = 70 - return timezone.now() + timezone.timedelta(days=365 * years) \ No newline at end of file + return timezone.now() + timezone.timedelta(days=365 * years) + + +def combine_seq(s1, s2, callback=None): + for s in (s1, s2): + if not hasattr(s, '__iter__'): + return [] + + seq = chain(s1, s2) + if callback: + seq = map(callback, seq) + + return seq + + +def search_object_attr(obj, value='', attr_list=None, ignore_case=False): + try: + object_attr = obj.__dict__ + except AttributeError: + return False + + if not isinstance(value, str): + return False + + if value == '': + return True + + if attr_list is not None: + for attr in attr_list: + object_attr.pop(attr) + + print(value) + print(object_attr) + if ignore_case: + if value.lower() in map(string.lower, filter(lambda x: isinstance(x, (str, unicode)), object_attr.values())): + return True + else: + if value in object_attr.values(): + return True + return False + + diff --git a/apps/perms/models.py b/apps/perms/models.py index 3a1cb53ce..6b3b88b3f 100644 --- a/apps/perms/models.py +++ b/apps/perms/models.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals, absolute_import +import functools from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -6,7 +7,7 @@ from django.utils import timezone from users.models import User, UserGroup from assets.models import Asset, AssetGroup, SystemUser -from common.utils import date_expired_default +from common.utils import date_expired_default, combine_seq class AssetPermission(models.Model): @@ -16,8 +17,8 @@ class AssetPermission(models.Model): ) name = models.CharField(max_length=128, verbose_name=_('Name')) - users = models.ManyToManyField(User, related_name='asset_permissions') - user_groups = models.ManyToManyField(UserGroup, related_name='asset_permissions') + users = models.ManyToManyField(User, related_name='asset_permissions', blank=True) + user_groups = models.ManyToManyField(UserGroup, related_name='asset_permissions', blank=True) assets = models.ManyToManyField(Asset, related_name='granted_by_permissions', blank=True) asset_groups = models.ManyToManyField(AssetGroup, related_name='granted_by_permissions', blank=True) system_users = models.ManyToManyField(SystemUser, related_name='granted_by_permissions') @@ -37,6 +38,33 @@ class AssetPermission(models.Model): return True return True + @staticmethod + def set_inherit(obj): + setattr(obj, 'inherited', True) + return obj + + def get_granted_users(self): + return list(set(self.users.all() or []) | set(self.get_granted_user_groups_member())) + + def get_granted_user_groups_member(self): + combine_users = functools.partial(combine_seq, callback=AssetPermission.set_inherit) + try: + return functools.reduce(combine_users, [user_group.users.all() + for user_group in self.user_groups.iterator()]) + except TypeError: + return [] + + def get_granted_assets(self): + return list(self.assets.all() or []) | set(self.get_granted_asset_groups_member()) + + def get_granted_asset_groups_member(self): + combine_assets = functools.partial(combine_seq, callback=AssetPermission.set_inherit) + try: + return functools.reduce(combine_assets, [asset_group.users.all() + for asset_group in self.asset_groups.iterator()]) + except TypeError: + return [] + class Meta: db_table = 'asset_permission' diff --git a/apps/perms/templates/perms/asset_permission_asset_list.html b/apps/perms/templates/perms/asset_permission_asset_list.html new file mode 100644 index 000000000..c60ea49a8 --- /dev/null +++ b/apps/perms/templates/perms/asset_permission_asset_list.html @@ -0,0 +1,186 @@ +{% extends 'base.html' %} +{% load common_tags %} +{% load users_tags %} +{% load static %} +{% load i18n %} + +{% block custom_head_css_js %} + + +{% endblock %} +{% block content %} +
+
+
+
+ +
+
+
+
+ {% trans 'Asset list of ' %} {{ asset_permission.name }} +
+ + + + + + + + + + +
+
+
+ + + + + + + + + + + + {% for user in page_obj %} + + + + + + + + + {% endfor %} + +
{% trans 'Hostname' %}{% trans 'IP' %}{% trans 'Port' %}{% trans 'Is valid' %}
{{ user.name }}{{ user.username }}{{ user.email }} + {% if user.is_expired and user.is_active %} + + {% else %} + + {% endif %} + + +
+
+ {% include '_pagination.html' %} +
+
+
+
+
+
+
+ {% trans 'Add user to asset permission' %} +
+
+ + + + + + + + + + + +
+ +
+ +
+
+
+ +
+
+ {% trans 'Add user group to asset permission' %} +
+
+ + + + + + + + + + + + {% for user_group in user_groups %} + + + + + {% endfor %} + +
+ +
+ +
{{ user_group.name }} + +
+
+
+
+
+
+
+
+
+ +{% endblock %} +{% block custom_foot_js %} + +{% endblock %} \ No newline at end of file diff --git a/apps/perms/templates/perms/asset_permission_detail.html b/apps/perms/templates/perms/asset_permission_detail.html index c8de5ec44..ee4d1fab3 100644 --- a/apps/perms/templates/perms/asset_permission_detail.html +++ b/apps/perms/templates/perms/asset_permission_detail.html @@ -43,10 +43,6 @@ diff --git a/apps/perms/templates/perms/asset_permission_user_list.html b/apps/perms/templates/perms/asset_permission_user_list.html index ec138c401..c9da9aedc 100644 --- a/apps/perms/templates/perms/asset_permission_user_list.html +++ b/apps/perms/templates/perms/asset_permission_user_list.html @@ -28,6 +28,16 @@ {% trans 'Assets and asset gruops' %} +
@@ -43,10 +53,6 @@ @@ -60,7 +66,8 @@ {% trans 'Name' %} {% trans 'Username' %} {% trans 'Email' %} - {% trans 'Active' %} + {% trans 'Is valid' %} + @@ -77,6 +84,9 @@ {% endif %} + + + {% endfor %} diff --git a/apps/perms/views.py b/apps/perms/views.py index 37ecca9d7..67003ea91 100644 --- a/apps/perms/views.py +++ b/apps/perms/views.py @@ -1,6 +1,7 @@ # ~*~ coding: utf-8 ~*~ from __future__ import unicode_literals, absolute_import +import functools from django.utils.translation import ugettext as _ from django.conf import settings @@ -11,6 +12,7 @@ from django.urls import reverse_lazy from django.contrib.messages.views import SuccessMessageMixin from django.views.generic.detail import DetailView, SingleObjectMixin +from common.utils import search_object_attr from .hands import AdminUserRequiredMixin, User, UserGroup, SystemUser from .models import AssetPermission from .forms import AssetPermissionForm @@ -124,10 +126,19 @@ class AssetPermissionUserListView(AdminUserRequiredMixin, SingleObjectMixin, Lis def get(self, request, *args, **kwargs): self.object = self.get_object(queryset=AssetPermission.objects.all()) + self.keyword = keyword = self.request.GET.get('keyword', '') return super(AssetPermissionUserListView, self).get(request, *args, **kwargs) def get_queryset(self): - return self.object.users.all() + print(self.keyword) + queryset = self.object.get_granted_users() + + if self.keyword: + search_func = functools.partial(search_object_attr, value=self.keyword, + attr_list=['name', 'username', 'email'], + ignore_case=True) + queryset = filter(search_func, queryset[:]) + return queryset def get_context_data(self, **kwargs): context = { diff --git a/apps/users/templates/users/user_detail.html b/apps/users/templates/users/user_detail.html index ae61ddf9a..a8500ac51 100644 --- a/apps/users/templates/users/user_detail.html +++ b/apps/users/templates/users/user_detail.html @@ -34,10 +34,6 @@