def adduser(request): # A user needs to be authenticated and authorized to be able to administer the DataFinder # Test if the user is now a university authenticated user if 'DF_USER_SSO_ID' not in request.session: return redirect("/login?redirectPath=admin") # Test if the user is Data Finder authorised user if request.session['DF_USER_ROLE'] != "admin" : return redirect("/") context = {} if request.GET.has_key('message'): context["message"]=request.GET['message'] if request.GET.has_key('status'): context["status"]=request.GET['status'] http_method = request.environ['REQUEST_METHOD'] if http_method == "GET": if request.GET.has_key('user_sso_id'): context["user_sso_id"] = request.GET["user_sso_id"] try: user= Users.objects.get(sso_id=context["user_sso_id"]) context['message']="Sorry, the user " + context["user_sso_id"] +" already exists." context['status']="error" return redirect("/admin?message="+context['message']+"&status="+context['status']) except Users.DoesNotExist,e: cud_authenticator = settings.get('main:cud_proxy.host') cudReq = CUDRequest(cud_proxy_host=cud_authenticator,filter={'sso_username':context["user_sso_id"]}) context["user_sso_name"] = str(cudReq.get_fullName()) context["user_sso_email"] = str(cudReq.get_email()) if cudReq.get_fullName() == None or cudReq.get_email() == None: context['message']=" Please enter a valid Oxford SSO ID" context['status']="error" return redirect("/admin?"+"message="+context['message']+"&status="+context['status']) # Set the role to default to 'user' context["user_role"] = "user" return render_to_response('add_user.html',context, context_instance=RequestContext(request)) except Exception,e: logger.error("Oops, an error occurred, sorry...") context['message']="Oops, an error occurred, sorry..." context['status']="error" return redirect("/admin?"+"message="+context['message']+"&status="+context['status'])
def __init__(self, request=None): # Only do webauth and issue cud-requests if the session variable not found if not request.session.has_key('DF_USER_SSO_ID'): user_logged_in_name = None df_user_sso_id = None if os.environ.has_key('DF_REMOTE_USER'): df_user_sso_id = os.environ.get('DF_REMOTE_USER') cud_authenticator = settings.get('main:cud_proxy.host') cudReq = CUDRequest(cud_proxy_host=cud_authenticator,filter= {'sso_username':df_user_sso_id}) user_logged_in_name = str(cudReq.get_fullName()) user_email = str(cudReq.get_email()) if not request.session.exists(request.session.session_key): request.session.create() #set up the session variables request.session['DF_USER_SSO_ID'] = df_user_sso_id request.session['DF_USER_FULL_NAME'] = user_logged_in_name request.session['DF_USER_EMAIL'] = user_email #Add the user to the database if already not registered try: users = Users.objects.filter(sso_id=df_user_sso_id) if len(users) == 0: request.session['DF_USER_ROLE'] = "user" newuser = Users() newuser.sso_id = request.session['DF_USER_SSO_ID'] newuser.username = request.session['DF_USER_FULL_NAME'] newuser.role = request.session['DF_USER_ROLE'] newuser.email = request.session['DF_USER_EMAIL'] newuser.save() else: for user in users: request.session['DF_USER_ROLE'] = user.role except Exception: self.authenticated=False request.session.modified = True # Save the session key in DFSessions try: usersession= DFSessions.objects.get(session_id=request.session.session_key) #usersession.session_id = request.session.session_key #usersession.save() except DFSessions.DoesNotExist,e: usersession = DFSessions() usersession.sso_id= request.session['DF_USER_SSO_ID'] usersession.session_id = request.session.session_key usersession.save() except Exception,e: logger.error("User session could not be saved in DF.")
context['message']="Oops, an error occurred, sorry..." context['status']="error" return redirect("/admin?"+"message="+context['message']+"&status="+context['status']) elif http_method == "POST": try: user = Users.objects.get(sso_id=request.POST.get("user_sso_id")) context['message']="Sorry, the user " + request.POST.get("user_sso_id") +" already exists." context['status']="error" return redirect("/admin?message="+context['message']+"&status="+context['status']) except Users.DoesNotExist,e: cud_authenticator = settings.get('main:cud_proxy.host') context["user_sso_id"] = request.POST.get("user_sso_id") context["user_role"] = request.POST.get("user_role") cudReq = CUDRequest(cud_proxy_host=cud_authenticator, filter={'sso_username':context["user_sso_id"]}) context["user_sso_name"] = str(cudReq.get_fullName()) context["user_sso_email"] = str(cudReq.get_email()) # Set the role to default to 'user' newuser = Users() newuser.sso_id = context["user_sso_id"] newuser.username = context["user_sso_name"] newuser.role = context["user_role"] newuser.email = context["user_sso_email"] newuser.save() context['message']="Thanks, "+ context["user_sso_id"] +" has been successfully added." context['status']="success" return redirect("/admin/users/edit?user_sso_id="+ request.POST.get("user_sso_id")+"&message="+context['message']+"&status="+context['status']) except Exception,e: