170
"""Handler for the Console Service AJAX backend application."""
171
if req.user is None and req.path != 'activate_me':
167
class UserServiceView(BaseView):
168
def __init__(self, req, path):
169
if len(path) > 0 and path[-1] == os.sep:
170
self.path = path[:-1]
174
def authorize(self, req):
173
175
# XXX: activate_me isn't called by a valid user, so is special for now.
174
req.throw_error(req.HTTP_FORBIDDEN,
175
"You are not logged in to IVLE.")
176
if len(req.path) > 0 and req.path[-1] == os.sep:
180
# The path determines which "command" we are receiving
181
fields = req.get_fieldstorage()
183
func = actions_map[req.path]
185
req.throw_error(req.HTTP_BAD_REQUEST,
186
"%s is not a valid userservice action." % repr(req.path))
176
if req.path == 'activate_me' and get_user_details(req) is not None:
178
return req.user is not None
180
def render(self, req):
181
# The path determines which "command" we are receiving
182
fields = req.get_fieldstorage()
184
func = actions_map[self.path]
189
class Plugin(ViewPlugin):
191
('userservice/*path', UserServiceView)
189
194
@require_method('POST')
190
195
def handle_activate_me(req, fields):
206
211
their acceptance). It must only be called through a POST request.
209
# XXX: Very special because we don't have a valid user, so req.user is
211
214
user = get_user_details(req)
214
req.throw_error(req.HTTP_FORBIDDEN,
215
"You are not logged in to IVLE.")
219
declaration = fields.getfirst('declaration')
220
except AttributeError:
221
declaration = None # Will fail next test
222
if declaration != USER_DECLARATION:
223
req.throw_error(req.HTTP_BAD_REQUEST,
224
"Please use the Terms of Service form instead of talking to "
225
"this service directly.")
227
# Make sure the user's status is "no_agreement", and set status to
228
# pending, within the one transaction. This ensures we only do this
231
# Check that the user's status is "no_agreement".
232
# (Both to avoid redundant calls, and to stop disabled users from
233
# re-enabling their accounts).
234
if user.state != "no_agreement":
235
req.throw_error(req.HTTP_BAD_REQUEST,
236
"You have already agreed to the terms.")
237
# Write state "pending" to ensure we don't try this again
238
user.state = u"pending"
244
# Get the arguments for usermgt.activate_user from the session
245
# (The user must have already logged in to use this app)
249
msg = {'activate_user': args}
251
# Release our lock on the db so usrmgt can write
254
# Try and contact the usrmgt server
256
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
257
except cjson.DecodeError:
258
# Gave back rubbish - set the response to failure
259
response = {'response': 'usrmgt-failure'}
261
# Get the staus of the users request
263
status = response['response']
268
user.state = u"enabled"
270
# Reset the user back to no agreement
271
user.state = u"no_agreement"
275
req.content_type = "text/plain"
276
req.write(cjson.encode(response))
217
declaration = fields.getfirst('declaration')
218
except AttributeError:
219
declaration = None # Will fail next test
220
if declaration != USER_DECLARATION:
223
# Make sure the user's status is "no_agreement", and set status to
224
# pending, within the one transaction. This ensures we only do this
227
# Check that the user's status is "no_agreement".
228
# (Both to avoid redundant calls, and to stop disabled users from
229
# re-enabling their accounts).
230
if user.state != "no_agreement":
231
raise BadRequest("You have already agreed to the terms.")
232
# Write state "pending" to ensure we don't try this again
233
user.state = u"pending"
278
235
req.store.rollback()
239
# Get the arguments for usermgt.activate_user from the session
240
# (The user must have already logged in to use this app)
244
msg = {'activate_user': args}
246
# Release our lock on the db so usrmgt can write
249
# Try and contact the usrmgt server
251
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
252
except cjson.DecodeError:
253
# Gave back rubbish - set the response to failure
254
response = {'response': 'usrmgt-failure'}
256
# Get the staus of the users request
258
status = response['response']
263
user.state = u"enabled"
265
# Reset the user back to no agreement
266
user.state = u"no_agreement"
269
req.content_type = "text/plain"
270
req.write(cjson.encode(response))
281
272
create_user_fields_required = [
282
273
'login', 'fullname', 'rolenm'
516
494
offeringid = fields.getfirst('offeringid')
517
495
if offeringid is None:
518
req.throw_error(req.HTTP_BAD_REQUEST,
519
"Required: offeringid")
496
raise BadRequest("Required: offeringid")
521
498
offeringid = int(offeringid)
523
req.throw_error(req.HTTP_BAD_REQUEST,
524
"offeringid must be a integer")
500
raise BadRequest("offeringid must be an integer")
526
502
offering = req.store.get(ivle.database.Offering, offeringid)
528
504
dict_projectsets = []
530
for p in offering.project_sets:
531
dict_projectsets.append({
532
'projectsetid': p.id,
533
'max_students_per_group': p.max_students_per_group,
534
'groups': [{'groupid': g.id,
536
'nick': g.nick} for g in p.project_groups]
539
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
505
for p in offering.project_sets:
506
dict_projectsets.append({
507
'projectsetid': p.id,
508
'max_students_per_group': p.max_students_per_group,
509
'groups': [{'groupid': g.id,
511
'nick': g.nick} for g in p.project_groups]
541
514
response = cjson.encode(dict_projectsets)
542
515
req.write(response)
557
530
projectsetid = fields.getfirst('projectsetid').value
558
531
groupnm = fields.getfirst('groupnm').value
559
532
if projectsetid is None or groupnm is None:
560
req.throw_error(req.HTTP_BAD_REQUEST,
561
"Required: projectsetid, groupnm")
533
raise BadRequest("Required: projectsetid, groupnm")
562
534
groupnm = unicode(groupnm)
564
536
projectsetid = int(projectsetid)
566
req.throw_error(req.HTTP_BAD_REQUEST,
567
"projectsetid must be an int")
538
raise BadRequest("projectsetid must be an integer")
568
540
# Get optional fields
569
541
nick = fields.getfirst('nick').value
570
542
if nick is not None:
571
543
nick = unicode(nick)
573
# Begin transaction since things can go wrong
545
group = ivle.database.ProjectGroup(name=groupnm,
546
project_set_id=projectsetid,
549
epoch=datetime.datetime.now())
552
# Create the group repository
553
# Yes, this is ugly, and it would be nice to just pass in the groupid,
554
# but the object isn't visible to the extra transaction in
555
# usrmgt-server until we commit, which we only do once the repo is
557
offering = group.project_set.offering
560
"subj_short_name": offering.subject.short_name,
561
"year": offering.semester.year,
562
"semester": offering.semester.semester,
563
"groupnm": group.name,
565
msg = {'create_group_repository': args}
567
# Contact the usrmgt server
575
group = ivle.database.ProjectGroup(name=groupnm,
576
project_set_id=projectsetid,
579
epoch=datetime.datetime.now())
582
# Create the group repository
583
# Yes, this is ugly, and it would be nice to just pass in the groupid,
584
# but the object isn't visible to the extra transaction in
585
# usrmgt-server until we commit, which we only do once the repo is
587
offering = group.project_set.offering
590
"subj_short_name": offering.subject.short_name,
591
"year": offering.semester.year,
592
"semester": offering.semester.semester,
593
"groupnm": group.name,
595
msg = {'create_group_repository': args}
597
# Contact the usrmgt server
599
usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
600
except cjson.DecodeError, e:
601
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
602
"Could not understand usrmgt server response: %s"%e.message)
604
if 'response' not in usrmgt or usrmgt['response']=='failure':
605
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
606
"Failure creating repository: %s"%str(usrmgt))
608
# Everything went OK. Lock it in
612
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
569
usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
570
except cjson.DecodeError, e:
571
raise Exception("Could not understand usrmgt server response:" +
574
if 'response' not in usrmgt or usrmgt['response']=='failure':
575
raise Exception("Failure creating repository: " + str(usrmgt))
614
577
req.content_type = "text/plain"
625
588
groupid = fields.getfirst('groupid')
626
589
offeringid = fields.getfirst('offeringid')
627
590
if groupid is None or offeringid is None:
628
req.throw_error(req.HTTP_BAD_REQUEST,
629
"Required: groupid, offeringid")
591
raise BadRequest("Required: groupid, offeringid")
631
593
groupid = int(groupid)
633
req.throw_error(req.HTTP_BAD_REQUEST,
634
"groupid must be an int")
595
raise BadRequest("groupid must be an integer")
635
596
group = req.store.get(ivle.database.ProjectGroup, groupid)
638
599
offeringid = int(offeringid)
640
req.throw_error(req.HTTP_BAD_REQUEST,
641
"offeringid must be an int")
601
raise BadRequest("offeringid must be an integer")
642
602
offering = req.store.get(ivle.database.Offering, offeringid)
685
644
# Add membership to database
686
645
# We can't keep a transaction open until the end here, as usrmgt-server
687
646
# needs to see the changes!
647
group.members.add(user)
650
# Rebuild the svn config file
651
# Contact the usrmgt server
652
msg = {'rebuild_svn_group_config': {}}
689
group.members.add(user)
692
# Rebuild the svn config file
693
# Contact the usrmgt server
694
msg = {'rebuild_svn_group_config': {}}
696
usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
697
except cjson.DecodeError, e:
698
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
699
"Could not understand usrmgt server response: %s"%e.message)
701
if 'response' not in usrmgt or usrmgt['response']=='failure':
702
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
703
"Failure creating repository: %s"%str(usrmgt))
705
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
654
usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
655
except cjson.DecodeError, e:
656
raise Exception("Could not understand usrmgt server response: %s" +
659
if 'response' not in usrmgt or usrmgt['response']=='failure':
660
raise Exception("Failure creating repository: " + str(usrmgt))
707
662
return(cjson.encode({'response': 'okay'}))