summaryrefslogtreecommitdiffstats
path: root/ipa-server/ipa-gui/ipagui/subcontrollers/group.py
blob: c3048f0de0c5c534c00ff91e0a565a542b6f7157 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from pickle import dumps, loads
from base64 import b64encode, b64decode
import logging

import cherrypy
import turbogears
from turbogears import controllers, expose, flash
from turbogears import validators, validate
from turbogears import widgets, paginate
from turbogears import error_handler
from turbogears import identity

from ipacontroller import IPAController
import ipa.config
import ipa.group
from ipa.entity import utf8_encode_values
from ipa import ipaerror
import ipagui.forms.group

log = logging.getLogger(__name__)

group_new_form = ipagui.forms.group.GroupNewForm()
group_edit_form = ipagui.forms.group.GroupEditForm()

group_fields = ['*']

class GroupController(IPAController):


    #########
    # Group #
    #########

    @expose()
    @identity.require(identity.not_anonymous())
    def index(self, tg_errors=None):
        raise turbogears.redirect("/group/list")

    @expose("ipagui.templates.groupnew")
    @identity.require(identity.not_anonymous())
    def new(self, tg_errors=None):
        """Displays the new group form"""
        if tg_errors:
            turbogears.flash("There were validation errors.<br/>" +
                             "Please see the messages below for details.")

        client = self.get_ipaclient()

        return dict(form=group_new_form, group={})

    @expose()
    @identity.require(identity.not_anonymous())
    def create(self, **kw):
        """Creates a new group"""
        self.restrict_post()
        client = self.get_ipaclient()

        if kw.get('submit') == 'Cancel':
            turbogears.flash("Add group cancelled")
            raise turbogears.redirect('/')

        tg_errors, kw = self.groupcreatevalidate(**kw)
        if tg_errors:
            turbogears.flash("There were validation errors.<br/>" +
                             "Please see the messages below for details.")
            return dict(form=group_new_form, group=kw,
                    tg_template='ipagui.templates.groupnew')

        #
        # Create the group itself
        #
        try:
            new_group = ipa.group.Group()
            new_group.setValue('cn', kw.get('cn'))
            new_group.setValue('description', kw.get('description'))

            rv = client.add_group(new_group)
        except ipaerror.exception_for(ipaerror.LDAP_DUPLICATE):
            turbogears.flash("Group with name '%s' already exists" %
                    kw.get('cn'))
            return dict(form=group_new_form, group=kw,
                    tg_template='ipagui.templates.groupnew')
        except ipaerror.IPAError, e:
            turbogears.flash("Group add failed: " + str(e) + "<br/>" + str(e.detail))
            return dict(form=group_new_form, group=kw,
                    tg_template='ipagui.templates.groupnew')

        #
        # NOTE: from here on, the group now exists.
        #       on any error, we redirect to the _edit_ group page.
        #       this code does data setup, similar to groupedit()
        #
        group = client.get_entry_by_cn(kw['cn'], group_fields)
        group_dict = group.toDict()
        member_dicts = []

        # store a copy of the original group for the update later
        group_data = b64encode(dumps(group_dict))
        member_data = b64encode(dumps(member_dicts))
        group_dict['group_orig'] = group_data
        group_dict['member_data'] = member_data

        # preserve group add info in case of errors
        group_dict['dnadd'] = kw.get('dnadd')
        group_dict['dn_to_info_json'] = kw.get('dn_to_info_json')

        #
        # Add members
        #
        failed_adds = []
        try:
            dnadds = kw.get('dnadd')
            if dnadds != None:
                if not(isinstance(dnadds,list) or isinstance(dnadds,tuple)):
                    dnadds = [dnadds]
                failed_adds = client.add_members_to_group(
                        utf8_encode_values(dnadds), group.dn)
                kw['dnadd'] = failed_adds
        except ipaerror.IPAError, e:
            failed_adds = dnadds

        if len(failed_adds) > 0:
            message = "Group successfully created.<br />"
            message += "There was an error adding group members.<br />"
            message += "Failures have been preserved in the add/remove lists."
            turbogears.flash(message)
            return dict(form=group_edit_form, group=group_dict,
                        members=member_dicts,
                        tg_template='ipagui.templates.groupedit')

        turbogears.flash("%s added!" % kw.get('cn'))
        raise turbogears.redirect('/group/show', cn=kw.get('cn'))

    @expose("ipagui.templates.dynamiceditsearch")
    @identity.require(identity.not_anonymous())
    def edit_search(self, **kw):
        """Searches for users+groups and displays list of results in a table.
           This method is used for the ajax search on the group edit page."""
        client = self.get_ipaclient()

        users = []
        groups = []
        counter = 0
        searchlimit = 100
        criteria = kw.get('criteria')
        if criteria != None and len(criteria) > 0:
            try:
                users = client.find_users(criteria.encode('utf-8'), None, searchlimit)
                users_counter = users[0]
                users = users[1:]

                groups = client.find_groups(criteria.encode('utf-8'), None,
                        searchlimit)
                groups_counter = groups[0]
                groups = groups[1:]

                if users_counter < 0 or groups_counter < 0:
                    counter = -1
                else:
                    counter = users_counter + groups_counter
            except ipaerror.IPAError, e:
                turbogears.flash("search failed: " + str(e))

        return dict(users=users, groups=groups, criteria=criteria,
                counter=counter)


    @expose("ipagui.templates.groupedit")
    @identity.require(identity.not_anonymous())
    def edit(self, cn, tg_errors=None):
        """Displays the edit group form"""
        if tg_errors:
            turbogears.flash("There were validation errors.<br/>" +
                             "Please see the messages below for details.")

        client = self.get_ipaclient()

        try:
            group = client.get_entry_by_cn(cn, group_fields)

            group_dict = group.toDict()

            #
            # convert members to users, for easier manipulation on the page
            #

            members = client.memberOf(group.dn, ['dn', 'givenname', 'sn', 'uid', 'cn'])
            members.sort(self.sort_group_member)

            # Map users into an array of dicts, which can be serialized
            # (so we don't have to do this on each round trip)
            member_dicts = map(lambda member: member.toDict(), members)

            # store a copy of the original group for the update later
            group_data = b64encode(dumps(group_dict))
            member_data = b64encode(dumps(member_dicts))
            group_dict['group_orig'] = group_data
            group_dict['member_data'] = member_data

            return dict(form=group_edit_form, group=group_dict, members=member_dicts)
        except ipaerror.IPAError, e:
            turbogears.flash("Group edit failed: " + str(e))
            raise turbogears.redirect('/group/show', uid=cn)

    @expose()
    @identity.require(identity.not_anonymous())
    def update(self, **kw):
        """Updates an existing group"""
        self.restrict_post()
        client = self.get_ipaclient()

        if kw.get('submit') == 'Cancel Edit':
            turbogears.flash("Edit group cancelled")
            raise turbogears.redirect('/group/show', cn=kw.get('cn'))

        # Decode the member data, in case we need to round trip
        member_dicts = loads(b64decode(kw.get('member_data')))


        tg_errors, kw = self.groupupdatevalidate(**kw)
        if tg_errors:
            turbogears.flash("There were validation errors.<br/>" +
                             "Please see the messages below for details.")
            return dict(form=group_edit_form, group=kw, members=member_dicts,
                        tg_template='ipagui.templates.groupedit')

        group_modified = False

        #
        # Update group itself
        #
        try:
            orig_group_dict = loads(b64decode(kw.get('group_orig')))

            new_group = ipa.group.Group(orig_group_dict)
            if new_group.description != kw.get('description'):
                group_modified = True
                new_group.setValue('description', kw.get('description'))
            if kw.get('editprotected') == 'true':
                new_gid = str(kw.get('gidnumber'))
                if new_group.gidnumber != new_gid:
                    group_modified = True
                    new_group.setValue('gidnumber', new_gid)

            if group_modified:
                rv = client.update_group(new_group)
                #
                # If the group update succeeds, but below operations fail, we
                # need to make sure a subsequent submit doesn't try to update
                # the group again.
                #
                kw['group_orig'] = b64encode(dumps(new_group.toDict()))
        except ipaerror.IPAError, e:
            turbogears.flash("Group update failed: " + str(e))
            return dict(form=group_edit_form, group=kw, members=member_dicts,
                        tg_template='ipagui.templates.groupedit')

        #
        # Add members
        #
        failed_adds = []
        try:
            dnadds = kw.get('dnadd')
            if dnadds != None:
                if not(isinstance(dnadds,list) or isinstance(dnadds,tuple)):
                    dnadds = [dnadds]
                failed_adds = client.add_members_to_group(
                        utf8_encode_values(dnadds), new_group.dn)
                kw['dnadd'] = failed_adds
        except ipaerror.IPAError, e:
            turbogears.flash("Group update failed: " + str(e))
            return dict(form=group_edit_form, group=kw, members=member_dicts,
                        tg_template='ipagui.templates.groupedit')

        #
        # Remove members
        #
        failed_dels = []
        try:
            dndels = kw.get('dndel')
            if dndels != None:
                if not(isinstance(dndels,list) or isinstance(dndels,tuple)):
                    dndels = [dndels]
                failed_dels = client.remove_members_from_group(
                        utf8_encode_values(dndels), new_group.dn)
                kw['dndel'] = failed_dels
        except ipaerror.IPAError, e:
            turbogears.flash("Group update failed: " + str(e))
            return dict(form=group_edit_form, group=kw, members=member_dicts,
                        tg_template='ipagui.templates.groupedit')

        #
        # TODO - check failed ops to see if it's because of another update.
        #        handle "someone else already did it" errors better - perhaps
        #        not even as an error
        # TODO - update the Group Members list.
        #        (note that we have to handle the above todo first, or else
        #         there will be an error message, but the add/del lists will
        #         be empty)
        #
        if (len(failed_adds) > 0) or (len(failed_dels) > 0):
            message = "There was an error updating group members.<br />"
            message += "Failures have been preserved in the add/remove lists."
            if group_modified:
                message = "Group Details successfully updated.<br />" + message
            turbogears.flash(message)
            return dict(form=group_edit_form, group=kw, members=member_dicts,
                        tg_template='ipagui.templates.groupedit')

        turbogears.flash("%s updated!" % kw['cn'])
        raise turbogears.redirect('/group/show', cn=kw['cn'])


    @expose("ipagui.templates.grouplist")
    @identity.require(identity.not_anonymous())
    def list(self, **kw):
        """Search for groups and display results"""
        client = self.get_ipaclient()

        groups = None
        # counter = 0
        criteria = kw.get('criteria')
        if criteria != None and len(criteria) > 0:
            try:
                groups = client.find_groups(criteria.encode('utf-8'), None, 0, 2)
                counter = groups[0]
                groups = groups[1:]
                if counter == -1:
                    turbogears.flash("These results are truncated.<br />" +
                                    "Please refine your search and try again.")
            except ipaerror.IPAError, e:
                turbogears.flash("Find groups failed: " + str(e))
                raise turbogears.redirect("/group/list")

        return dict(groups=groups, criteria=criteria,
                    fields=ipagui.forms.group.GroupFields())

    @expose("ipagui.templates.groupshow")
    @identity.require(identity.not_anonymous())
    def show(self, cn):
        """Retrieve a single group for display"""
        client = self.get_ipaclient()

        try:
            group = client.get_entry_by_cn(cn, group_fields)
            group_dict = group.toDict()

            #
            # convert members to users, for display on the page
            #

            members = client.memberOf(group.dn, ['dn', 'givenname', 'sn', 'uid', 'cn'])
            members = members[1:]
            members.sort(self.sort_group_member)
            member_dicts = map(lambda member: member.toDict(), members)

            return dict(group=group_dict, fields=ipagui.forms.group.GroupFields(),
                    members = member_dicts)
        except ipaerror.IPAError, e:
            turbogears.flash("Group show failed: " + str(e))
            raise turbogears.redirect("/")

    @expose()
    @identity.require(identity.not_anonymous())
    def delete(self, dn):
        """Delete group."""
        self.restrict_post()
        client = self.get_ipaclient()

        try:
            client.delete_group(dn)

            turbogears.flash("group deleted")
            raise turbogears.redirect('/group/list')
        except (SyntaxError, ipaerror.IPAError), e:
            turbogears.flash("Group deletion failed: " + str(e) + "<br/>" + str(e.detail))
            raise turbogears.redirect('/group/list')

    @validate(form=group_new_form)
    @identity.require(identity.not_anonymous())
    def groupcreatevalidate(self, tg_errors=None, **kw):
        return tg_errors, kw

    @validate(form=group_edit_form)
    @identity.require(identity.not_anonymous())
    def groupupdatevalidate(self, tg_errors=None, **kw):
        return tg_errors, kw