35
35
app = Flask (__name__ )
36
36
37
37
def validate_login (form ):
38
- print "vlcero"
39
- print form
38
+
39
+ print "validate_login: %s" % form
40
+
40
41
try :
41
- print "vluno"
42
- print form
43
42
username = form ['username' ]
44
- print "vldos"
45
43
password = form ['password' ]
46
44
if len (username )== 0 :
47
45
return response ({'error' : 'Invalid credentials' } ,status = 401 )
48
46
except KeyError , error :
49
47
print "KeyError %s" % form
50
48
return Response ('Wrong request' ,status = 400 )
51
49
except :
52
- print "vlcuatro"
53
- print form
50
+
54
51
raise
55
- print "vltres"
52
+
56
53
57
54
user_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % username
58
55
l = ldap_bind (user_dn , password )
59
56
return (user_dn , l , username , password )
57
+
60
58
def add_record (l ,dn , new ):
61
59
try :
62
60
ldif = modlist .addModlist (new )
@@ -69,6 +67,20 @@ def add_record(l,dn, new):
69
67
raise
70
68
return False
71
69
70
+ def increase_current_uidnumber (l ):
71
+ update_dn = "uid=template.user,ou=Users,ou=auth,dc=guifibages,dc=net"
72
+ original_record = l .search_s (update_dn , ldap .SCOPE_BASE )[0 ][1 ]
73
+ modified_record = copy .deepcopy (original_record )
74
+ modified_record ['uidNumber' ][0 ] = str (int (modified_record ['uidNumber' ][0 ])+ 1 )
75
+ modified , error = modify_ldap_property (l , update_dn , original_record , modified_record )
76
+ if modified :
77
+ print "increase_current_uidnumber correct"
78
+ return True
79
+ else :
80
+ print "couldn't increase_current_uidnumber"
81
+ return False
82
+
83
+
72
84
def modify_ldap_property (l , modified_dn , old , new ):
73
85
try :
74
86
ldif = modlist .modifyModlist (old , new )
@@ -121,8 +133,7 @@ def read_pac(view="internet"):
121
133
122
134
def user_access (l , user_dn ):
123
135
result = l .search_s (user_dn , ldap .SCOPE_SUBTREE , 'objectClass=*' , ['memberOf' ])[0 ][1 ]
124
- print user_dn
125
- print result
136
+ print "user_access:\n \t %s\n \t %s" % (user_dn , result )
126
137
if 'memberOf' in result and "cn=ldapAdmin,ou=Groups,ou=auth,dc=guifibages,dc=net" in result ['memberOf' ]:
127
138
return "admin"
128
139
else :
@@ -184,7 +195,7 @@ def list_users():
184
195
@app .route ('/api/user/<target_user>/get' , methods = ['POST' ])
185
196
@crossdomain ('*' )
186
197
def get_user (target_user ):
187
- print "Uno"
198
+
188
199
try :
189
200
user_dn , l , username , password = validate_login (request .form )
190
201
except ldap .INVALID_CREDENTIALS :
@@ -194,22 +205,75 @@ def get_user(target_user):
194
205
except :
195
206
return response ({'error' : "shit happened" }, 500 )
196
207
197
- print "Dos"
208
+
198
209
target_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % target_user
199
210
200
211
try :
201
212
result = l .search_s (target_dn , ldap .SCOPE_SUBTREE , 'objectClass=*' )[0 ][1 ]
202
213
except ldap .NO_SUCH_OBJECT :
203
214
return response ({'error' : 'Not found' } ,status = 404 )
204
- print "tres"
215
+
205
216
206
217
result ['access_level' ] = user_access (l , user_dn )
207
- print "cuatro"
218
+
208
219
return response (result )
209
220
221
+ @app .route ('/api/user/new' , methods = ['POST' ])
222
+ @crossdomain ('*' )
223
+ def add_user ():
224
+ result = {}
225
+ try :
226
+ user_dn , l , username , password = validate_login (request .form )
227
+ except ldap .INVALID_CREDENTIALS :
228
+ return response ({'error' : 'Invalid credentials' } ,status = 401 )
229
+ except ldap .SERVER_DOWN :
230
+ return response ({'error' : "Can't connect to server" } ,status = 500 )
231
+ except :
232
+ return response ({'error' : "shit happened" }, 500 )
233
+ update_dn = "uid=template.user,ou=Users,ou=auth,dc=guifibages,dc=net"
234
+ original_record = l .search_s (update_dn , ldap .SCOPE_BASE )[0 ][1 ]
235
+ modified_record = copy .deepcopy (original_record )
236
+ print request .form
237
+ check_search = '(uid=%s)' % request .form ['newuser_uid' ]
238
+ print "add_user -1"
239
+ check = l .search_s ("ou=Users,ou=auth,dc=guifibages,dc=net" , ldap .SCOPE_SUBTREE , check_search , ['uid' , 'cn' , 'uidNumber' ])
240
+ print "add_user"
241
+ print "add_user: %s (%d)" % (check , len (check ))
242
+ if len (check ) > 0 :
243
+
244
+ return response ("User already exists %s" % request .form ['newuser_uid' ], 409 )
245
+
246
+
247
+ for field in request .form :
248
+ if field in ['username' , 'password' ]:
249
+ continue
250
+ new_value = request .form [field ]
251
+ new_value = new_value .encode ('utf-8' )
252
+ if field == 'userPassword' :
253
+ new_value = ssha (request .form [field ])
254
+ if field [0 :8 ] == 'newuser_' :
255
+ modified_record [field [8 :]] = new_value
256
+ continue
257
+ if field not in original_record or original_record [field ] != new_value and new_value not in original_record [field ]:
258
+ modified_record [field ] = new_value
259
+ modified_record ['homeDirectory' ] = "/guifibages/users/%s" % modified_record ['uid' ]
260
+
261
+ print "Minga longa %s" % modified_record
262
+
263
+ new_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % modified_record ['uid' ]
264
+ modified , error = add_record (l , new_dn , modified_record )
265
+ print "Result:\n modified: %s\n error: %s" % (modified , error )
266
+ if modified :
267
+ increase_current_uidnumber (l )
268
+ return response (modified_record )
269
+ else :
270
+ return response (error , error ['code' ])
271
+
272
+
210
273
@app .route ('/api/user/<target_user>/update' , methods = ['POST' ])
211
274
@crossdomain ('*' )
212
275
def update_user (target_user ):
276
+ print "update_user:\n \t %s" % request .form
213
277
result = {}
214
278
try :
215
279
user_dn , l , username , password = validate_login (request .form )
@@ -219,6 +283,7 @@ def update_user(target_user):
219
283
return response ({'error' : "Can't connect to server" } ,status = 500 )
220
284
except :
221
285
return response ({'error' : "shit happened" }, 500 )
286
+
222
287
update_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % target_user
223
288
original_record = l .search_s (update_dn , ldap .SCOPE_BASE )[0 ][1 ]
224
289
modified_record = copy .deepcopy (original_record )
@@ -228,6 +293,8 @@ def update_user(target_user):
228
293
continue
229
294
new_value = request .form [field ]
230
295
new_value = new_value .encode ('utf-8' )
296
+ if field == 'userPassword' :
297
+ new_value = ssha (request .form [field ])
231
298
if field not in original_record or original_record [field ] != new_value and new_value not in original_record [field ]:
232
299
modified_record [field ] = new_value
233
300
@@ -272,4 +339,4 @@ def login():
272
339
if __name__ == "__main__" :
273
340
global sessions
274
341
sessions = dict ()
275
- app .run (debug = True ,port = 8050 )
342
+ app .run (debug = True ,port = 8050 )
0 commit comments