def user_reset(request, user_id): """Reset password for user.""" try: user = User.objects.get(id=int(user_id)) if isinstance(INIT_PASSWD, FunctionType): new_password = INIT_PASSWD() else: new_password = INIT_PASSWD user.set_password(new_password) user.save() if IS_EMAIL_CONFIGURED: if SEND_EMAIL_ON_RESETTING_USER_PASSWD: try: send_user_reset_email(request, user.email, new_password) msg = _('Successfully reset password to %(passwd)s, an email has been sent to %(user)s.') % \ {'passwd': new_password, 'user': user.email} messages.success(request, msg) except Exception, e: logger.error(str(e)) msg = _('Successfully reset password to %(passwd)s, but failed to send email to %(user)s, please check your email configuration.') % \ {'passwd':new_password, 'user': user.email} messages.success(request, msg) else: messages.success(request, _(u'Successfully reset password to %(passwd)s for user %(user)s.') % \ {'passwd':new_password,'user': user.email}) else:
def put(self, request, email): """Reset password for user Permission checking: 1. only admin can perform this action. """ if not request.user.admin_permissions.can_manage_user(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') if not is_valid_username2(email): error_msg = 'email invalid' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: user = User.objects.get(email=email) except User.DoesNotExist as e: logger.error(e) error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if isinstance(INIT_PASSWD, FunctionType): new_password = INIT_PASSWD() else: new_password = INIT_PASSWD user.set_password(new_password) user.save() if config.FORCE_PASSWORD_CHANGE: UserOptions.objects.set_force_passwd_change(user.username) if IS_EMAIL_CONFIGURED: if SEND_EMAIL_ON_RESETTING_USER_PASSWD: c = {'email': email, 'password': new_password} contact_email = Profile.objects.get_contact_email_by_user( email) try: send_html_email( _(u'Password has been reset on %s') % get_site_name(), 'sysadmin/user_reset_email.html', c, None, [contact_email]) reset_tip = _('Successfully reset password to %(passwd)s, an email has been sent to %(user)s.') % \ {'passwd': new_password, 'user': contact_email} except Exception as e: logger.warning(e) reset_tip = _('Successfully reset password to %(passwd)s, but failed to send email to %(user)s, please check your email configuration.') % \ {'passwd': new_password, 'user': email} else: reset_tip = _('Successfully reset password to %(passwd)s for user %(user)s.') % \ {'passwd': new_password, 'user': email} else: reset_tip = _('Successfully reset password to %(passwd)s for user %(user)s. But email notification can not be sent, because Email service is not properly configured.') % \ {'passwd': new_password, 'user': email} return Response({'new_password': new_password, 'reset_tip': reset_tip})
def org_user_reset(request, user_id): """Reset an organization user's password. """ referer = request.META.get('HTTP_REFERER', None) next = reverse('org_user_admin') if referer is None else referer try: user = User.objects.get(id=int(user_id)) except User.DoesNotExist: messages.error(request, 'Failed to reset password: the user does not exist') return HttpResponseRedirect(next) org = request.user.org if not org_user_exists(org.org_id, user.username): messages.error( request, 'Failed to reset password: the user does not belong to the organization.' ) return HttpResponseRedirect(next) if isinstance(INIT_PASSWD, FunctionType): new_password = INIT_PASSWD() else: new_password = INIT_PASSWD user.set_password(new_password) user.save() # send password reset email if IS_EMAIL_CONFIGURED: if SEND_EMAIL_ON_RESETTING_USER_PASSWD: try: send_user_reset_email(request, user.email, new_password) msg = _('Successfully reset password to %(passwd)s, an email has been sent to %(user)s.') % \ {'passwd': new_password, 'user': user.contact_email} messages.success(request, msg) except Exception as e: logger.error(str(e)) msg = _('Successfully reset password to %(passwd)s, but failed to send email to %(user)s, please check your email configuration.') % \ {'passwd': new_password, 'user': user.contact_email} messages.success(request, msg) else: messages.success(request, _(u'Successfully reset password to %(passwd)s for user %(user)s.') % \ {'passwd': new_password,'user': user.contact_email}) else: messages.success(request, _(u'Successfully reset password to %(passwd)s for user %(user)s. But email notification can not be sent, because Email service is not properly configured.') % \ {'passwd': new_password,'user': user.contact_email}) return HttpResponseRedirect(next)
def user_reset(request, email): """Reset password for user.""" try: user = User.objects.get(email=email) if isinstance(INIT_PASSWD, FunctionType): new_password = INIT_PASSWD() else: new_password = INIT_PASSWD user.set_password(new_password) user.save() if config.FORCE_PASSWORD_CHANGE: UserOptions.objects.set_force_passwd_change(user.username) if IS_EMAIL_CONFIGURED: if SEND_EMAIL_ON_RESETTING_USER_PASSWD: try: contact_email = Profile.objects.get_contact_email_by_user( user.email) send_user_reset_email(request, contact_email, new_password) msg = _('Successfully reset password to %(passwd)s, an email has been sent to %(user)s.') % \ {'passwd': new_password, 'user': contact_email} messages.success(request, msg) except Exception as e: logger.error(str(e)) msg = _('Successfully reset password to %(passwd)s, but failed to send email to %(user)s, please check your email configuration.') % \ {'passwd':new_password, 'user': user.email} messages.success(request, msg) else: messages.success(request, _('Successfully reset password to %(passwd)s for user %(user)s.') % \ {'passwd':new_password,'user': user.email}) else: messages.success(request, _('Successfully reset password to %(passwd)s for user %(user)s. But email notification can not be sent, because Email service is not properly configured.') % \ {'passwd':new_password,'user': user.email}) except User.DoesNotExist: msg = _('Failed to reset password: user does not exist') messages.error(request, msg) referer = request.META.get('HTTP_REFERER', None) next_page = reverse('sys_info') if referer is None else referer return HttpResponseRedirect(next_page)
def put(self, request, org_id, email): """ Reset an organization user's password. """ # resource check org_id = int(org_id) if not ccnet_api.get_org_by_id(org_id): error_msg = 'Organization %s not found.' % org_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) try: user = User.objects.get(email=email) except User.DoesNotExist: error_msg = 'User %s not found.' % email return api_error(status.HTTP_404_NOT_FOUND, error_msg) if not org_user_exists(org_id, user.username): err_msg = 'User %s does not exist in the organization.' % user.username return api_error(status.HTTP_404_NOT_FOUND, err_msg) # Reset an organization user's password. if isinstance(INIT_PASSWD, FunctionType): new_password = INIT_PASSWD() else: new_password = INIT_PASSWD user.set_password(new_password) user.save() # send password reset email if IS_EMAIL_CONFIGURED: if SEND_EMAIL_ON_RESETTING_USER_PASSWD: send_to = user.username profile = Profile.objects.get_profile_by_user(user.username) if profile and profile.contact_email: send_to = profile.contact_email try: send_user_reset_email(request, send_to, new_password) except Exception as e: logger.error(str(e)) return Response({'new_password': new_password})
def put(self, request, email): """Reset password for user Permission checking: 1. only admin can perform this action. """ if not is_valid_username(email): error_msg = 'email invalid' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: user = User.objects.get(email=email) except User.DoesNotExist as e: logger.error(e) error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if isinstance(INIT_PASSWD, FunctionType): new_password = INIT_PASSWD() else: new_password = INIT_PASSWD user.set_password(new_password) user.save() if config.FORCE_PASSWORD_CHANGE: UserOptions.objects.set_force_passwd_change(user.username) if IS_EMAIL_CONFIGURED and SEND_EMAIL_ON_RESETTING_USER_PASSWD: c = {'email': email, 'password': new_password} try: send_html_email(_(u'Password has been reset on %s') % get_site_name(), 'sysadmin/user_reset_email.html', c, None, [email]) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'new_password': new_password})