def register(): account_name = request.json['name'] account = register_rest.ioc.new_account_service().find_by_name(account_name) lang = detect_language() messages = resource_bundle.ResourceBundle() if account is None: username = request.json['username'] register_rest.logger.info('['+get_customer_ip()+'] Processing registration request') user = register_rest.ioc.new_user_service().find_by_username(username) if user is None: a = model.Account() a.name = account_name a.lang = detect_language() u = model.User(username, generate_password_hash(request.json['password'])) u.activation_hash = str(uuid.uuid4()) a.users.append(u) register_rest.ioc.new_account_service().save(a) success_message = messages.get_text(lang, 'register_success') registration_email = register_rest.ioc.get_config()['registration_email'] customer_email = model.Email() customer_email.type = "REGISTRATION" customer_email.from_address = registration_email customer_email.ref_id = u.id customer_email.to_address = u.username customer_email.language = lang messages = resource_bundle.ResourceBundle() customer_email.subject = messages.get_text(lang, 'registration_email_subject') register_rest.ioc.new_email_service().save(customer_email) admin_email = model.Email() admin_email.type = 'REGISTRATION_ADMIN' admin_email.from_address = registration_email admin_email.to_address = registration_email admin_email.subject = "New customer: "+u.username register_rest.ioc.new_email_service().save(admin_email) if register_rest.ioc.get_config()['SKIP_ACCOUNT_ACTIVATION']: u.active = True session['username'] = u.username model.base.db.session.commit() register_rest.logger.info('['+get_customer_ip()+'] Registration successful') return Response(json.dumps({"success_message": success_message, 'skip_activation':register_rest.ioc.get_config()['SKIP_ACCOUNT_ACTIVATION']}), status=200, mimetype='application/json') else: register_rest.logger.info('['+get_customer_ip()+'] Registration failed, user already exist') error_message = messages.get_text(lang, 'register_user_already_exist') return Response(json.dumps({"error_message": error_message}), status=409, mimetype='application/json') else: register_rest.logger.info('['+get_customer_ip()+'] Registration failed, account already exist') error_message = messages.get_text(lang, 'register_account_already_exist') return Response(json.dumps({"error_message": error_message}), status=409, mimetype='application/json')
def new(): messages = resource_bundle.ResourceBundle() password_blueprint.logger.info('['+get_customer_ip()+'] Processing new password request') password = request.json['password'] if password==request.json['confirmPassword']: reset_hash = request.json['h'] email_hash = request.json['e'] user = password_blueprint.ioc.new_user_service().find_by_reset_hash(reset_hash) if user is not None: if hashlib.sha224(user.username.encode('utf-8')).hexdigest()==email_hash: user.password = generate_password_hash(password) user.active = True model.base.db.session.commit() password_blueprint.logger.info('['+get_customer_ip()+'] Authenticating after password reset') session['username'] = user.username return Response(status=200, mimetype='application/json') else: password_blueprint.logger.info('['+get_customer_ip()+'] Failed to process new password request, email hash invalid') else: password_blueprint.logger.info('['+get_customer_ip()+'] Failed to process new password request, no such user') else: password_blueprint.logger.info('['+get_customer_ip()+'] Failed to process new password request, password mismatch confirm password') lang = detect_language() error_message = messages.get_text(lang, 'new_password_mismatch') return Response(json.dumps({"error_message": error_message}), status=400, mimetype='application/json')
def new_message(): contact_blueprint.logger.info('['+get_customer_ip()+'] Send contact message') oi = contact_blueprint.ioc.new_contact_service().save_conctact(model.Contact(get_customer_ip(), request.json['email'], request.json['message'])) messages = resource_bundle.ResourceBundle() lang = detect_language() admin_email = model.Email() admin_email.type = 'ADMIN_CONTACT_EMAIL' admin_email.ref_id = oi.id admin_email.from_address = contact_blueprint.ioc.get_config()['contact_email'] admin_email.to_address = contact_blueprint.ioc.get_config()['contact_email'] admin_email.subject = "Cusomer message" contact_blueprint.ioc.new_email_service().save(admin_email) return jsonify({'id':oi.id, 'success_message':messages.get_text(lang, 'contact_message_success')})
def login(): username = request.json['username'] messages = resource_bundle.ResourceBundle() lang = detect_language() login_blueprint.logger.info('['+get_customer_ip()+'] Processing authentication request') user = login_blueprint.ioc.new_user_service().find_by_username(username) if user is not None and user.active: if check_password_hash(user.password, request.json['password']): login_blueprint.logger.info('['+get_customer_ip()+'] Authentication successful') session['username'] = username return Response(status=200, mimetype='application/json') else: error_message = messages.get_text(lang, 'login_wrong_username_or_password') login_blueprint.logger.info('['+get_customer_ip()+'] Authentication failed password mismatch') else: if user is None: error_message = messages.get_text(lang, 'login_wrong_username_or_password') else: error_message = messages.get_text(lang, 'login_user_inactive') login_blueprint.logger.info('['+get_customer_ip()+'] Authentication failed no user for username:'******'application/json')
def reset(): password_blueprint.logger.info('['+get_customer_ip()+'] Processing reset password request') user = password_blueprint.ioc.new_user_service().find_by_username(request.json['username']) messages = resource_bundle.ResourceBundle() if user is not None: user.reset_hash = str(uuid.uuid4()) model.base.db.session.commit() reset_password_subject = resource_bundle.ResourceBundle().get_text(user.account.lang, 'reset_password') reset_password_email = model.Email() reset_password_email.language = user.account.lang reset_password_email.ref_id = user.id reset_password_email.type = 'RESET_PASSWORD' reset_password_email.from_address = password_blueprint.ioc.get_config()['no_reply'] reset_password_email.to_address = user.username reset_password_email.subject = reset_password_subject password_blueprint.ioc.new_email_service().save(reset_password_email) success_message = messages.get_text(user.account.lang, 'reset_password_link_info') return Response(json.dumps({"success_message": success_message}), status=200, mimetype='application/json') else: password_blueprint.logger.info('['+get_customer_ip()+'] Failed to request reset password, no such user') lang = detect_language() error_message = messages.get_text(lang, 'reset_password_no_email') return Response(json.dumps({"error_message": error_message}), status=204, mimetype='application/json')