def delete_user(id): """ Delete the user: must be admin or logged in user """ if admin_or_me_permission(id).can(): user = user_service.delete_user(id) if user is None: raise NotFound(f"user {id} not found") return jsonify(user_schema.dump(user)) else: raise Unauthorized("Unauthorized")
def get_user(id): """ Get the user: must be admin or logged in user """ if admin_or_me_permission(id).can(): user = user_service.get_user_by_id(id) if (user == None): raise NotFound(f"user {id} not found") return jsonify(user_schema.dump(user)) else: raise Unauthorized("Unauthorized")
def criarUsuario(cls): try: db.session.execute('SELECT 1') except: db.session.rollback() return jsonify({ 'message': 'Failed to connect to database', 'data': {} }), 401 msg = jsonify({'message': 'An error ocurred', 'data': {}}), 401 hashed = '' if request.json: if request.json.get('senha'): salt = bcrypt.gensalt() hashed = bcrypt.hashpw( request.json.get('senha').encode('utf8'), salt) usuario = Usuario(nome=request.json.get('nome'), dtNasc=request.json.get('dtNasc'), cpf=request.json.get('cpf'), celular=request.json.get('celular'), email=request.json.get('email'), senha=hashed, admin=False, status=True) endereco = Endereco( rua=request.json.get('endereco').get('rua'), numero=request.json.get('endereco').get('numero'), complemento=request.json.get('endereco').get('complemento'), bairro=request.json.get('endereco').get('bairro'), cep=request.json.get('endereco').get('cep'), cidade=request.json.get('endereco').get('cidade'), estado=request.json.get('endereco').get('estado')) if endereco.rua and endereco.numero and endereco.bairro and endereco.cep and endereco.cidade and endereco.estado: try: db.session.add(usuario) db.session.flush() endereco.user_id = usuario.id db.session.add(endereco) db.session.commit() result = user_schema.dump(usuario) result['endereco'] = address_schema.dump(endereco) msg = jsonify({ 'message': 'User created successfully', 'data': result }) except: db.session.rollback() return jsonify({ 'message': 'An error occured', 'data': {} }), 401 return msg
def register_user(): """ Register the user. assign investor role by default """ email = request.form.get('email') password = request.form.get('password') new_user = investor_service.create_investor(email, password) if new_user == None: raise AlreadyExists( "An account with that email address already exists") return jsonify(user_schema.dump(user_service.get_user_by_id(new_user.id)))
def login(): """ Login route: 1. authenticate the user 2. login_user """ email = request.form.get('email') password = request.form.get('password') remember = request.form.get('remember', False) user = authenticate_user(email, password) login_user(user, remember) identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) return jsonify(user_schema.dump(user))