summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/identity/domains/tables.py
blob: 7fb879f96f2e8666804e7e8420ac3f94c10aa98c (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging

from django.conf import settings
from django.template import defaultfilters as filters
from django.urls import reverse
from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ungettext_lazy

from keystoneclient import exceptions as keystoneclient_exceptions

from horizon import exceptions
from horizon import messages
from horizon import tables

from openstack_dashboard import api

from openstack_dashboard.dashboards.identity.domains import constants


LOG = logging.getLogger(__name__)


class UpdateUsersLink(tables.LinkAction):
    name = "users"
    verbose_name = _("Manage Members")
    url = "horizon:identity:domains:update"
    classes = ("ajax-modal",)
    policy_rules = (("identity", "identity:update_domain"),)

    def get_link_url(self, domain):
        step = 'update_user_members'
        base_url = reverse(self.url, args=[domain.id])
        param = urlencode({"step": step})
        return "?".join([base_url, param])


class UpdateGroupsLink(tables.LinkAction):
    name = "groups"
    verbose_name = _("Modify Groups")
    url = "horizon:identity:domains:update"
    classes = ("ajax-modal",)
    icon = "pencil"
    policy_rules = (("identity", "identity:update_domain"),)

    def get_link_url(self, domain):
        step = 'update_group_members'
        base_url = reverse(self.url, args=[domain.id])
        param = urlencode({"step": step})
        return "?".join([base_url, param])


class CreateDomainLink(tables.LinkAction):
    name = "create"
    verbose_name = _("Create Domain")
    url = constants.DOMAINS_CREATE_URL
    classes = ("ajax-modal",)
    icon = "plus"
    policy_rules = (('identity', 'identity:create_domain'),)

    def allowed(self, request, domain):
        return api.keystone.keystone_can_edit_domain()


class EditDomainLink(tables.LinkAction):
    name = "edit"
    verbose_name = _("Edit")
    url = constants.DOMAINS_UPDATE_URL
    classes = ("ajax-modal",)
    icon = "pencil"
    policy_rules = (('identity', 'identity:update_domain'),)

    def allowed(self, request, domain):
        return api.keystone.keystone_can_edit_domain()


class DeleteDomainsAction(tables.DeleteAction):
    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Delete Domain",
            u"Delete Domains",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Deleted Domain",
            u"Deleted Domains",
            count
        )

    policy_rules = (('identity', 'identity:delete_domain'),)

    def allowed(self, request, datum):
        return api.keystone.keystone_can_edit_domain()

    def delete(self, request, obj_id):
        domain = self.table.get_object_by_id(obj_id)
        if domain.enabled:
            msg = _('Domain "%s" must be disabled before it can be deleted.') \
                % domain.name
            messages.error(request, msg)
            raise keystoneclient_exceptions.ClientException(msg)
        else:
            LOG.info('Deleting domain "%s".', obj_id)
            api.keystone.domain_delete(request, obj_id)


class DisableDomainsAction(tables.BatchAction):
    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Disable Domain",
            u"Disable Domains",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Disabled Domain",
            u"Disabled Domains",
            count
        )

    name = "disable"
    policy_rules = (('identity', 'identity:update_domain'),)
    verbose_name = _("Disable Domains")

    def allowed(self, request, datum):
        return api.keystone.keystone_can_edit_domain() \
            and (datum is None or datum.enabled)

    def action(self, request, obj_id):
        domain = self.table.get_object_by_id(obj_id)
        if domain.enabled:
            LOG.info('Disabling domain "%s".', obj_id)
            try:
                api.keystone.domain_update(request,
                                           domain_id=domain.id,
                                           name=domain.name,
                                           description=domain.description,
                                           enabled=False)
            except Exception:
                exceptions.handle(request, ignore=True)
                return False


class EnableDomainsAction(tables.BatchAction):
    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Enable Domain",
            u"Enable Domains",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Enabled Domain",
            u"Enabled Domains",
            count
        )

    name = "enable"
    policy_rules = (('identity', 'identity:update_domain'),)
    verbose_name = _("Enable Domains")

    def allowed(self, request, datum):
        return api.keystone.keystone_can_edit_domain() \
            and (datum is None or not datum.enabled)

    def action(self, request, obj_id):
        domain = self.table.get_object_by_id(obj_id)
        if not domain.enabled:
            LOG.info('Enabling domain "%s".', obj_id)
            try:
                api.keystone.domain_update(request,
                                           domain_id=domain.id,
                                           name=domain.name,
                                           description=domain.description,
                                           enabled=True)
            except Exception:
                exceptions.handle(request, ignore=True)
                return False


class DomainFilterAction(tables.FilterAction):
    def allowed(self, request, datum):
        multidomain_support = settings.OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT
        return multidomain_support

    def filter(self, table, domains, filter_string):
        """Naive case-insensitive search."""
        q = filter_string.lower()

        def comp(domain):
            if q in domain.name.lower():
                return True
            return False

        return filter(comp, domains)


class SetDomainContext(tables.Action):
    name = "set_domain_context"
    verbose_name = _("Set Domain Context")
    url = constants.DOMAINS_INDEX_URL
    preempt = True
    policy_rules = (('identity', 'identity:update_domain'),)

    def allowed(self, request, datum):
        multidomain_support = settings.OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT
        if not multidomain_support:
            return False

        ctx = request.session.get("domain_context", None)
        if ctx and datum.id == ctx:
            return False
        return True

    def single(self, table, request, obj_id):
        if ('domain_context' not in request.session or
                request.session['domain_context'] != obj_id):
            try:
                domain = api.keystone.domain_get(request, obj_id)
                request.session['domain_context'] = obj_id
                request.session['domain_context_name'] = domain.name
                messages.success(request,
                                 _('Domain Context updated to Domain %s.') %
                                 domain.name)
            except Exception:
                messages.error(request,
                               _('Unable to set Domain Context.'))


class UnsetDomainContext(tables.Action):
    name = "clear_domain_context"
    verbose_name = _("Clear Domain Context")
    url = constants.DOMAINS_INDEX_URL
    preempt = True
    requires_input = False
    policy_rules = (('identity', 'identity:update_domain'),)

    def allowed(self, request, datum):
        ctx = request.session.get("domain_context", None)
        return ctx is not None

    def single(self, table, request, obj_id):
        if 'domain_context' in request.session:
            request.session.pop("domain_context")
            request.session.pop("domain_context_name")
            messages.success(request, _('Domain Context cleared.'))


class DomainsTable(tables.DataTable):
    name = tables.WrappingColumn('name', verbose_name=_('Name'))
    description = tables.Column(lambda obj: getattr(obj, 'description', None),
                                verbose_name=_('Description'))
    id = tables.Column('id', verbose_name=_('Domain ID'))
    enabled = tables.Column('enabled', verbose_name=_('Enabled'), status=True,
                            filters=(filters.yesno, filters.capfirst))

    class Meta(object):
        name = "domains"
        verbose_name = _("Domains")
        table_actions_menu = (EnableDomainsAction, DisableDomainsAction)
        row_actions = (SetDomainContext, UpdateUsersLink, UpdateGroupsLink,
                       EditDomainLink, EnableDomainsAction,
                       DisableDomainsAction, DeleteDomainsAction)
        table_actions = (DomainFilterAction, CreateDomainLink,
                         DeleteDomainsAction, UnsetDomainContext)