def profile_edit(request): tas = TASClient() user = request.user tas_user = tas.get_user(username=user.username) user = request.user ds_profile = DesignSafeProfile.objects.get(user_id=user.id) pro_form = forms.ProfessionalProfileForm(request.POST or None, instance=ds_profile) if request.method == 'POST': form = forms.UserProfileForm(request.POST, initial=tas_user) if form.is_valid() and pro_form.is_valid(): pro_form.save() data = form.cleaned_data # punt on PI Eligibility for now data['piEligibility'] = tas_user['piEligibility'] # retain original account source data['source'] = tas_user['source'] tas.save_user(tas_user['id'], data) messages.success(request, 'Your profile has been updated!') try: ds_profile = user.profile ds_profile.ethnicity = data['ethnicity'] ds_profile.gender = data['gender'] except ObjectDoesNotExist as e: logger.info('exception e: {} {}'.format(type(e), e )) ds_profile = DesignSafeProfile( user=user, ethnicity=data['ethnicity'], gender=data['gender'] ) ds_profile.update_required = False ds_profile.save() return HttpResponseRedirect(reverse('designsafe_accounts:manage_profile')) else: try: tas_user['ethnicity'] = user.profile.ethnicity tas_user['gender'] = user.profile.gender except ObjectDoesNotExist: pass form = forms.UserProfileForm(initial=tas_user) context = { 'title': 'Account Profile', 'form': form, 'pro_form': pro_form } return render(request, 'designsafe/apps/accounts/profile_edit.html', context)
def authenticate(self, *args, **kwargs): user = None if 'backend' in kwargs and kwargs['backend'] == 'agave': token = kwargs['token'] base_url = getattr(settings, 'AGAVE_TENANT_BASEURL') self.logger.info('Attempting login via Agave with token "%s"' % token[:8].ljust(len(token), '-')) # TODO make this into an AgavePy call response = requests.get( '%s/profiles/v2/me' % base_url, headers={'Authorization': 'Bearer %s' % token}) if response.status_code >= 200 and response.status_code <= 299: json_result = response.json() agave_user = json_result['result'] username = agave_user['username'] UserModel = get_user_model() try: user = UserModel.objects.get(username=username) user.first_name = agave_user['first_name'] user.last_name = agave_user['last_name'] user.email = agave_user['email'] user.save() except UserModel.DoesNotExist: self.logger.info('Creating local user record for "%s" ' 'from Agave Profile' % username) user = UserModel.objects.create_user( username=username, first_name=agave_user['first_name'], last_name=agave_user['last_name'], email=agave_user['email']) try: profile = DesignSafeProfile.objects.get(user=user) except DesignSafeProfile.DoesNotExist: profile = DesignSafeProfile(user=user) profile.save() try: prefs = NotificationPreferences.objects.get(user=user) except NotificationPreferences.DoesNotExist: prefs = NotificationPreferences(user=user) prefs.save() self.logger.error('Login successful for user "%s"' % username) else: self.logger.error('Agave Authentication failed: %s' % response.text) return user
def authenticate(self, username=None, password=None, request=None, **kwargs): user = None if username is not None and password is not None: tas_user = None if request is not None: self.logger.info('Attempting login via TAS for user "%s" from IP "%s"' % (username, request.META.get('REMOTE_ADDR'))) else: self.logger.info('Attempting login via TAS for user "%s" from IP "%s"' % (username, 'unknown')) try: # Check if this user is valid on the mail server if self.tas.authenticate(username, password): tas_user = self.tas.get_user(username=username) self.logger.info('Login successful for user "%s"' % username) else: raise ValidationError('Authentication Error', 'Your username or password is incorrect.') except Exception as e: self.logger.warning(e.args) if re.search(r'PendingEmailConfirmation', e.args[1]): raise ValidationError('Please confirm your email address before logging in.') else: raise ValidationError(e.args[1]) if tas_user is not None: UserModel = get_user_model() try: # Check if the user exists in Django's local database user = UserModel.objects.get(username=username) user.first_name = tas_user['firstName'] user.last_name = tas_user['lastName'] user.email = tas_user['email'] user.save() except UserModel.DoesNotExist: # Create a user in Django's local database self.logger.info('Creating local user record for "%s" from TAS Profile' % username) user = UserModel.objects.create_user( username=username, first_name=tas_user['firstName'], last_name=tas_user['lastName'], email=tas_user['email'] ) try: profile = DesignSafeProfile.objects.get(user=user) except DesignSafeProfile.DoesNotExist: profile = DesignSafeProfile(user=user) profile.save() return user
def profile_edit(request): tas = TASClient() user = request.user tas_user = tas.get_user(username=user.username) if request.method == 'POST': form = forms.UserProfileForm(request.POST, initial=tas_user) if form.is_valid(): data = form.cleaned_data # punt on PI Eligibility for now data['piEligibility'] = tas_user['piEligibility'] # retain original account source data['source'] = tas_user['source'] tas.save_user(tas_user['id'], data) messages.success(request, 'Your profile has been updated!') try: ds_profile = user.profile ds_profile.ethnicity = data['ethnicity'] ds_profile.gender = data['gender'] ds_profile.save() except ObjectDoesNotExist as e: logger.info('exception e: {} {}'.format(type(e), e )) ds_profile = DesignSafeProfile( user=user, ethnicity=data['ethnicity'], gender=data['gender'] ) ds_profile.save() return HttpResponseRedirect(reverse('designsafe_accounts:manage_profile')) else: try: tas_user['ethnicity'] = user.profile.ethnicity tas_user['gender'] = user.profile.gender except ObjectDoesNotExist: pass form = forms.UserProfileForm(initial=tas_user) context = { 'title': 'Manage Profile', 'form': form, } return render(request, 'designsafe/apps/accounts/profile_edit.html', context)
def authenticate(self, *args, **kwargs): user = None if 'backend' in kwargs and kwargs['backend'] == 'agave': token = kwargs['token'] base_url = getattr(settings, 'AGAVE_TENANT_BASEURL') self.logger.info('Attempting login via Agave with token "%s"' % token[:8].ljust(len(token), '-')) # TODO make this into an AgavePy call response = requests.get('%s/profiles/v2/me' % base_url, headers={'Authorization': 'Bearer %s' % token}) json_result = response.json() if 'status' in json_result and json_result['status'] == 'success': agave_user = json_result['result'] username = agave_user['username'] UserModel = get_user_model() try: user = UserModel.objects.get(username=username) user.first_name = agave_user['first_name'] user.last_name = agave_user['last_name'] user.email = agave_user['email'] user.save() except UserModel.DoesNotExist: self.logger.info('Creating local user record for "%s" ' 'from Agave Profile' % username) user = UserModel.objects.create_user( username=username, first_name=agave_user['first_name'], last_name=agave_user['last_name'], email=agave_user['email'] ) try: profile = DesignSafeProfile.objects.get(user=user) except DesignSafeProfile.DoesNotExist: profile = DesignSafeProfile(user=user) profile.save() self.logger.info('Login successful for user "%s"' % username) else: self.logger.info('Agave Authentication failed: %s' % json_result) return user
def authenticate(self, username=None, password=None, request=None, **kwargs): user = None if username is not None and password is not None: tas_user = None if request is not None: self.logger.info( 'Attempting login via TAS for user "%s" from IP "%s"' % (username, request.META.get('REMOTE_ADDR'))) else: self.logger.info( 'Attempting login via TAS for user "%s" from IP "%s"' % (username, 'unknown')) try: # Check if this user is valid on the mail server if self.tas.authenticate(username, password): tas_user = self.tas.get_user(username=username) self.logger.info('Login successful for user "%s"' % username) else: raise ValidationError( 'Authentication Error', 'Your username or password is incorrect.') except Exception as e: self.logger.warning(e.args) if re.search(r'PendingEmailConfirmation', e.args[1]): raise ValidationError( 'Please confirm your email address before logging in.') else: raise ValidationError(e.args[1]) if tas_user is not None: UserModel = get_user_model() try: # Check if the user exists in Django's local database user = UserModel.objects.get(username=username) user.first_name = tas_user['firstName'] user.last_name = tas_user['lastName'] user.email = tas_user['email'] user.save() except UserModel.DoesNotExist: # Create a user in Django's local database self.logger.info( 'Creating local user record for "%s" from TAS Profile' % username) user = UserModel.objects.create_user( username=username, first_name=tas_user['firstName'], last_name=tas_user['lastName'], email=tas_user['email']) try: profile = DesignSafeProfile.objects.get(user=user) except DesignSafeProfile.DoesNotExist: profile = DesignSafeProfile(user=user) profile.save() try: prefs = NotificationPreferences.objects.get(user=user) except NotificationPreferences.DoesNotExist: prefs = NotificationPreferences(user=user) prefs.save() return user