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
|
import random
from pickle import dumps, loads
from base64 import b64encode, b64decode
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 model import *
# import logging
# log = logging.getLogger("ipagui.controllers")
import ipa.config
import ipa.ipaclient
import ipa.user
import xmlrpclib
import forms.user
from ipa import ipaerror
ipa.config.init_config()
user_new_form = forms.user.UserNewForm()
user_edit_form = forms.user.UserEditForm()
password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
client = ipa.ipaclient.IPAClient(True)
client.set_principal("test@FREEIPA.ORG")
def restrict_post():
if cherrypy.request.method != "POST":
turbogears.flash("This method only accepts posts")
raise turbogears.redirect("/")
def utf8_encode(value):
if value != None:
value = value.encode('utf-8')
return value
class Root(controllers.RootController):
@expose(template="ipagui.templates.welcome")
def index(self):
return dict()
########
# User #
########
@expose("ipagui.templates.usernew")
def usernew(self, tg_errors=None):
"""Displays the new user form"""
if tg_errors:
turbogears.flash("There was a problem with the form!")
return dict(form=user_new_form)
@expose()
def usercreate(self, **kw):
"""Creates a new user"""
restrict_post()
if kw.get('submit') == 'Cancel':
turbogears.flash("Add user cancelled")
raise turbogears.redirect('/userlist')
tg_errors, kw = self.usercreatevalidate(**kw)
if tg_errors:
return dict(form=user_new_form, tg_template='ipagui.templates.usernew')
try:
new_user = ipa.user.User()
new_user.setValue('uid', kw.get('uid'))
new_user.setValue('givenname', kw.get('givenname'))
new_user.setValue('sn', kw.get('sn'))
new_user.setValue('mail', kw.get('mail'))
new_user.setValue('telephonenumber', kw.get('telephonenumber'))
rv = client.add_user(new_user)
turbogears.flash("%s added!" % kw['uid'])
raise turbogears.redirect('/usershow', uid=kw['uid'])
except ipaerror.IPAError, e:
turbogears.flash("User add failed: " + str(e))
return dict(form=user_new_form, tg_template='ipagui.templates.usernew')
@expose("ipagui.templates.useredit")
def useredit(self, uid, tg_errors=None):
"""Displays the edit user form"""
if tg_errors:
turbogears.flash("There was a problem with the form!")
user = client.get_user_by_uid(uid)
user_dict = user.toDict()
# store a copy of the original user for the update later
user_data = b64encode(dumps(user_dict))
user_dict['user_orig'] = user_data
return dict(form=user_edit_form, user=user_dict)
@expose()
def userupdate(self, **kw):
"""Updates an existing user"""
restrict_post()
if kw.get('submit') == 'Cancel':
turbogears.flash("Edit user cancelled")
raise turbogears.redirect('/usershow', uid=kw.get('uid'))
tg_errors, kw = self.userupdatevalidate(**kw)
if tg_errors:
return dict(form=user_edit_form, user=kw,
tg_template='ipagui.templates.useredit')
try:
orig_user_dict = loads(b64decode(kw.get('user_orig')))
new_user = ipa.user.User(orig_user_dict)
new_user.setValue('givenname', kw.get('givenname'))
new_user.setValue('sn', kw.get('sn'))
new_user.setValue('mail', kw.get('mail'))
new_user.setValue('telephonenumber', kw.get('telephonenumber'))
#
# this is a hack until we decide on the policy for names/cn/sn/givenName
#
new_user.setValue('cn',
"%s %s" % (new_user.getValue('givenname'),
new_user.getValue('sn')))
rv = client.update_user(new_user)
turbogears.flash("%s updated!" % kw['uid'])
raise turbogears.redirect('/usershow', uid=kw['uid'])
except ipaerror.IPAError, e:
turbogears.flash("User update failed: " + str(e))
return dict(form=user_edit_form, user=kw,
tg_template='ipagui.templates.useredit')
@expose("ipagui.templates.userlist")
def userlist(self, **kw):
"""Retrieve a list of all users and display them in one huge list"""
users = None
counter = 0
uid = kw.get('uid')
if uid != None and len(uid) > 0:
try:
users = client.find_users(uid.encode('utf-8'))
counter = users[0]
users = users[1:]
if counter == -1:
turbogears.flash("These results are truncated.<br />" +
"Please refine your search and try again.")
except ipaerror.IPAError, e:
turbogears.flash("User list failed: " + str(e))
raise turbogears.redirect("/userlist")
return dict(users=users, uid=uid, fields=forms.user.UserFields())
@expose("ipagui.templates.usershow")
def usershow(self, uid):
"""Retrieve a single user for display"""
try:
user = client.get_user_by_uid(uid)
return dict(user=user.toDict(), fields=forms.user.UserFields())
except ipaerror.IPAError, e:
turbogears.flash("User show failed: " + str(e))
raise turbogears.redirect("/")
@validate(form=user_new_form)
def usercreatevalidate(self, tg_errors=None, **kw):
return tg_errors, kw
@validate(form=user_edit_form)
def userupdatevalidate(self, tg_errors=None, **kw):
return tg_errors, kw
@expose()
def userindex(self):
raise turbogears.redirect("/userlist")
@expose()
def generate_password(self):
password = ""
generator = random.SystemRandom()
for char in range(8):
index = generator.randint(0, len(password_chars) - 1)
password += password_chars[index]
return password
@expose()
def suggest_uid(self, givenname, sn):
if (len(givenname) == 0) or (len(sn) == 0):
return ""
uid = givenname[0] + sn[:7]
try:
client.get_user_by_uid(uid)
except ipaerror.exception_for(ipaerror.LDAP_NOT_FOUND):
return uid
uid = givenname[:7] + sn[0]
try:
client.get_user_by_uid(uid)
except ipaerror.exception_for(ipaerror.LDAP_NOT_FOUND):
return uid
uid = (givenname + sn)[:8]
try:
client.get_user_by_uid(uid)
except ipaerror.exception_for(ipaerror.LDAP_NOT_FOUND):
return uid
uid = sn[:8]
try:
client.get_user_by_uid(uid)
except ipaerror.exception_for(ipaerror.LDAP_NOT_FOUND):
return uid
suffix = 2
template = givenname[0] + sn[:7]
while suffix < 20:
uid = template[:8 - len(str(suffix))] + str(suffix)
try:
client.get_user_by_uid(uid)
except ipaerror.exception_for(ipaerror.LDAP_NOT_FOUND):
return uid
suffix += 1
return ""
@expose()
def suggest_email(self, givenname, sn):
if (len(givenname) == 0) or (len(sn) == 0):
return ""
# TODO - get from config
domain = "freeipa.org"
return "%s.%s@%s" % (givenname, sn, domain)
# TODO - mail is currently not indexed nor searchable.
# implement when it's done
# email = givenname + "." + sn + domain
# users = client.find_users(email, ['mail'])
# if len(filter(lambda u: u['mail'] == email, users[1:])) == 0:
# return email
# email = self.suggest_uid(givenname, sn) + domain
# users = client.find_users(email, ['mail'])
# if len(filter(lambda u: u['mail'] == email, users[1:])) == 0:
# return email
# suffix = 2
# template = givenname + "." + sn
# while suffix < 20:
# email = template + str(suffix) + domain
# users = client.find_users(email, ['mail'])
# if len(filter(lambda u: u['mail'] == email, users[1:])) == 0:
# return email
# suffix += 1
# return ""
#########
# Group #
#########
@expose("ipagui.templates.groupindex")
def groupindex(self, tg_errors=None):
return dict()
############
# Resource #
############
@expose("ipagui.templates.resindex")
def resindex(self, tg_errors=None):
return dict()
|