def login(request): modal_auto_open = "false" if request.method == 'POST': form = LoginForm(request.POST) form_pass = PassForm() if form.is_valid(): try: client = ClientFactory( NETWORK_API_URL, NETWORK_API_USERNAME, NETWORK_API_PASSWORD) user = client.create_usuario().authenticate(form.cleaned_data[ 'username'], form.cleaned_data['password'], form.cleaned_data['is_ldap_user']) if user is None: messages.add_message( request, messages.ERROR, auth_messages.get("user_invalid")) else: request.session.set_expiry(SESSION_EXPIRY_AGE) auth = AuthSession(request.session) user = user.get('user') if user.get('permission') is None: messages.add_message( request, messages.ERROR, auth_messages.get("nogroup_error")) return render_to_response(templates.LOGIN, {'form': form, 'form_pass': form_pass, 'modal': modal_auto_open}, context_instance=RequestContext(request)) auth.login(User(user.get('id'), user.get('user'), user.get('nome'), user.get( 'email'), user.get('pwd'), user.get('permission'), user.get('ativo'), user.get('user_ldap'))) if form.cleaned_data['redirect'] != "": return HttpResponseRedirect(form.cleaned_data['redirect']) return HttpResponseRedirect(URL_HOME) except InvalidParameterError, e: logger.error(e) messages.add_message( request, messages.ERROR, auth_messages.get("user_invalid")) except NetworkAPIClientError, e: logger.error(e) messages.add_message(request, messages.ERROR, e) except LDAPNotFoundError, e: logger.error(e) messages.add_message( request, messages.ERROR, auth_messages.get("user_ldap_not_found"))
def lost_pass(request): form = LoginForm() modal_auto_open = "true" form_pass = None try: if request.method == 'POST': form_pass = PassForm(request.POST) if form_pass.is_valid(): client = ClientFactory(NETWORK_API_URL, NETWORK_API_USERNAME, NETWORK_API_PASSWORD) users = client.create_usuario().listar() users = users.get("usuario") username = form_pass.cleaned_data['username'] email = form_pass.cleaned_data['email'] for user in users: if user.get("user").upper() == username.upper(): if user.get("email") == email: if not user.get('user_ldap'): pass_open = make_random_password() password = hashlib.md5(pass_open).hexdigest() ativo = '1' if user.get('ativo') else '0' client.create_usuario().alterar( user.get('id'), user.get('user'), password, user.get('nome'), ativo, user.get('email'), user.get('user_ldap')) lists = dict() lists['user'] = user.get('user') lists['new_pass'] = pass_open # Montar Email com nova senha connection = EmailBackend( username=EMAIL_HOST_USER, password=EMAIL_HOST_PASSWORD) send_email = EmailMessage( 'Solicitação de Nova Senha', loader.render_to_string( MAIL_NEW_PASS, lists), EMAIL_FROM, [email], connection=connection) send_email.content_subtype = "html" send_email.send() messages.add_message( request, messages.SUCCESS, auth_messages.get("email_success")) modal_auto_open = 'false' return render_to_response( templates.LOGIN, { 'form': form, 'form_pass': form_pass, 'modal': modal_auto_open }, context_instance=RequestContext(request)) else: messages.add_message( request, messages.ERROR, auth_messages.get( "user_ldap_cant_recover_pass")) modal_auto_open = 'false' return render_to_response( templates.LOGIN, { 'form': form, 'form_pass': form_pass, 'modal': modal_auto_open }, context_instance=RequestContext(request)) for user in users: if user.get("user_ldap") is not None and user.get( "user_ldap").upper() == username.upper(): messages.add_message( request, messages.ERROR, auth_messages.get("user_ldap_cant_recover_pass")) modal_auto_open = 'false' return render_to_response( templates.LOGIN, { 'form': form, 'form_pass': form_pass, 'modal': modal_auto_open }, context_instance=RequestContext(request)) messages.add_message(request, messages.ERROR, auth_messages.get("user_email_invalid")) modal_auto_open = 'false' except NetworkAPIClientError as e: logger.error(e) messages.add_message(request, messages.ERROR, e) modal_auto_open = 'false' except BaseException as e: logger.exception(e) logger.error("URLError Invalid EMAIL_HOST in settings.py") messages.add_message(request, messages.ERROR, "Invalid EMAIL_HOST in settings.py") modal_auto_open = 'false' return render_to_response(templates.LOGIN, { 'form': form, 'form_pass': form_pass, 'modal': modal_auto_open }, context_instance=RequestContext(request))
def lost_pass(request): form = LoginForm() modal_auto_open = "true" try: if request.method == 'POST': form_pass = PassForm(request.POST) if form_pass.is_valid(): client = ClientFactory( NETWORK_API_URL, NETWORK_API_USERNAME, NETWORK_API_PASSWORD) users = client.create_usuario().listar() users = users.get("usuario") username = form_pass.cleaned_data['username'] email = form_pass.cleaned_data['email'] for user in users: if user.get("user").upper() == username.upper(): if user.get("email") == email: if not user.get('user_ldap'): pass_open = make_random_password() password = hashlib.md5(pass_open).hexdigest() ativo = None if user.get('ativo'): ativo = '1' else: ativo = '0' client.create_usuario().alterar(user.get('id'), user.get('user'), password, user.get('nome'), ativo, user.get('email'), user.get('user_ldap')) lists = dict() lists['user'] = user.get('user') lists['new_pass'] = pass_open # Montar Email com nova senha connection = EmailBackend( username=EMAIL_HOST_USER, password=EMAIL_HOST_PASSWORD) send_email = EmailMessage('Solicitação de Nova Senha', loader.render_to_string( MAIL_NEW_PASS, lists), EMAIL_FROM, [email], connection=connection) send_email.content_subtype = "html" send_email.send() messages.add_message( request, messages.SUCCESS, auth_messages.get("email_success")) modal_auto_open = 'false' return render_to_response(templates.LOGIN, {'form': form, 'form_pass': form_pass, 'modal': modal_auto_open}, context_instance=RequestContext(request)) else: messages.add_message( request, messages.ERROR, auth_messages.get("user_ldap_cant_recover_pass")) modal_auto_open = 'false' return render_to_response(templates.LOGIN, {'form': form, 'form_pass': form_pass, 'modal': modal_auto_open}, context_instance=RequestContext(request)) for user in users: if user.get("user_ldap") is not None and user.get("user_ldap").upper() == username.upper(): messages.add_message( request, messages.ERROR, auth_messages.get("user_ldap_cant_recover_pass")) modal_auto_open = 'false' return render_to_response(templates.LOGIN, {'form': form, 'form_pass': form_pass, 'modal': modal_auto_open}, context_instance=RequestContext(request)) messages.add_message( request, messages.ERROR, auth_messages.get("user_email_invalid")) modal_auto_open = 'false' except NetworkAPIClientError, e: logger.error(e) messages.add_message(request, messages.ERROR, e) modal_auto_open = 'false'