"Fossies" - the Fresh Open Source Software Archive

Member "horizon-16.0.0/openstack_dashboard/dashboards/identity/users/tabs.py" (16 Oct 2019, 5383 Bytes) of package /linux/misc/openstack/horizon-16.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "tabs.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 15.1.0_vs_16.0.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #      http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 import logging
   14 
   15 from django.conf import settings
   16 from django.utils.translation import ugettext_lazy as _
   17 
   18 from horizon import exceptions
   19 from horizon import tabs
   20 
   21 from openstack_dashboard import api
   22 from openstack_dashboard.dashboards.identity.users.groups \
   23     import tables as groups_tables
   24 from openstack_dashboard.dashboards.identity.users.role_assignments \
   25     import tables as role_assignments_tables
   26 from openstack_dashboard import policy
   27 
   28 
   29 LOG = logging.getLogger(__name__)
   30 
   31 
   32 class OverviewTab(tabs.Tab):
   33     """Overview of the user.
   34 
   35     Global user informations such as user name, domain ID, email...
   36     """
   37     name = _("Overview")
   38     slug = "overview"
   39     template_name = 'identity/users/_detail_overview.html'
   40 
   41     def _get_domain_name(self, user):
   42         domain_name = ''
   43         if api.keystone.VERSIONS.active >= 3:
   44             try:
   45                 if policy.check((("identity", "identity:get_domain"),),
   46                                 self.request):
   47                     domain = api.keystone.domain_get(
   48                         self.request, user.domain_id)
   49                     domain_name = domain.name
   50                 else:
   51                     domain = api.keystone.get_default_domain(self.request)
   52                     domain_name = domain.get('name')
   53             except Exception:
   54                 exceptions.handle(self.request,
   55                                   _('Unable to retrieve user domain.'))
   56         return domain_name
   57 
   58     def _get_project_name(self, user):
   59         project_id = user.project_id
   60         if not project_id:
   61             return
   62         try:
   63             tenant = api.keystone.tenant_get(self.request, project_id)
   64             return tenant.name
   65         except Exception as e:
   66             LOG.error('Failed to get tenant %(project_id)s: %(reason)s',
   67                       {'project_id': project_id, 'reason': e})
   68 
   69     def _get_extras(self, user):
   70         if api.keystone.VERSIONS.active >= 3:
   71             extra_info = settings.USER_TABLE_EXTRA_INFO
   72             return dict((display_key, getattr(user, key, ''))
   73                         for key, display_key in extra_info.items())
   74         else:
   75             return {}
   76 
   77     def get_context_data(self, request):
   78         user = self.tab_group.kwargs['user']
   79         return {
   80             "user": user,
   81             "domain_name": self._get_domain_name(user),
   82             'extras': self._get_extras(user),
   83             'project_name': self._get_project_name(user),
   84         }
   85 
   86 
   87 class RoleAssignmentsTab(tabs.TableTab):
   88     """Role assignment of the user to domain/project."""
   89     table_classes = (role_assignments_tables.RoleAssignmentsTable,)
   90     name = _("Role assignments")
   91     slug = "roleassignments"
   92     template_name = "horizon/common/_detail_table.html"
   93     preload = False
   94 
   95     def get_roleassignmentstable_data(self):
   96         user = self.tab_group.kwargs['user']
   97 
   98         role_assignments = []
   99 
  100         try:
  101             # Get all the roles of the user
  102             role_assignments = api.keystone.role_assignments_list(
  103                 self.request, user=user, include_subtree=False,
  104                 include_names=True)
  105 
  106         except Exception:
  107             exceptions.handle(
  108                 self.request,
  109                 _("Unable to display the role assignments of this user."))
  110         else:
  111             # Find all the role assignments through the groups of the user
  112             try:
  113                 user_groups = api.keystone.group_list(
  114                     self.request, user=user.id)
  115 
  116                 # Get the role for each group of the user:
  117                 for group in user_groups:
  118                     group_role_assignments = api.keystone. \
  119                         role_assignments_list(
  120                             self.request, group=group, include_subtree=False,
  121                             include_names=True)
  122 
  123                     role_assignments.extend(group_role_assignments)
  124 
  125             except Exception:
  126                 exceptions.handle(
  127                     self.request,
  128                     _("Unable to display role assignment through groups."))
  129 
  130         return role_assignments
  131 
  132 
  133 class GroupsTab(tabs.TableTab):
  134     """Groups of the user."""
  135     table_classes = (groups_tables.GroupsTable,)
  136     name = _("Groups")
  137     slug = "groups"
  138     template_name = "horizon/common/_detail_table.html"
  139     preload = False
  140 
  141     def get_groupstable_data(self):
  142         user_groups = []
  143         user = self.tab_group.kwargs['user']
  144 
  145         try:
  146             user_groups = api.keystone.group_list(self.request, user=user.id)
  147         except Exception:
  148             exceptions.handle(self.request,
  149                               _("Unable to display the groups of this user."))
  150 
  151         return user_groups
  152 
  153 
  154 class UserDetailTabs(tabs.DetailTabsGroup):
  155     slug = "user_details"
  156     tabs = (OverviewTab, RoleAssignmentsTab, GroupsTab,)