Example #1
0
def update_profile():
    """Update an user"""
    logging.info('[ROUTER]: Updating profile')
    body = request.get_json()
    identity = current_identity
    try:
        password = body.get('password', None)
        repeat_password = body.get('repeatPassword', None)
        if password is not None and repeat_password is not None and password == repeat_password:
            user = UserService.update_profile_password(body, identity)
        else:
            if 'role' in body:
                del body['role']
            name = body.get('name', None)
            country = body.get('country', None)
            institution = body.get('institution', None)
            if name is not None or country is not None or institution is not None:
                user = UserService.update_user(body, str(identity.id))
            else:
                return error(status=400, detail='Not updated')
    except UserNotFound as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=404, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize()), 200
Example #2
0
def create_user():
    """Create an user"""
    logging.info('[ROUTER]: Creating user')
    body = request.get_json()
    if request.headers.get('Authorization', None) is not None:
        @jwt_required()
        def identity():
            pass
        identity()
    identity = current_identity
    if identity:
        user_role = body.get('role', 'USER')
        if identity.role == 'USER' and user_role == 'ADMIN':
            return error(status=403, detail='Forbidden')
    else:
        body['role'] = 'USER'
    try:
        user = UserService.create_user(body)
    except UserDuplicated as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=400, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize()), 200
 def update_execution(execution, execution_id):
     logging.info('[SERVICE]: Updating execution')
     status = execution.get('status', None)
     progress = execution.get('progress', None)
     results = execution.get('results', None)
     if status is None and progress is None and results is None:
         raise Exception
     execution = ExecutionService.get_execution(execution_id=execution_id)
     if not execution:
         raise ExecutionNotFound(message='Execution with id '+execution_id+' does not exist')
     if status is not None:
         execution.status = status
         if status == 'FINISHED' or status == 'FAILED':
             execution.end_date = datetime.datetime.utcnow()
             execution.progress = 100
             user = UserService.get_user(str(execution.user_id))
             script = ScriptService.get_script(str(execution.script_id))
             email = EmailService.send_html_email(
                 recipients=[user.email],
                 html=EXECUTION_FINISHED_MAIL_CONTENT.format(status, execution.params.get('task_name'), script.name, str(execution.id), execution.start_date, execution.end_date, status),
                 subject='[MISLAND] Execution finished'
             )
     if progress is not None:
         execution.progress = progress
     if results is not None:
         execution.results = results
     try:
         logging.info('[DB]: ADD')
         db.session.add(execution)
         db.session.commit()
     except Exception as error:
         raise error
     return execution
Example #4
0
def authenticate(email, password):
    logging.info('[JWT]: Auth user '+email)
    user = None
    try:
        user = UserService.authenticate_user(user_id=str(email), password=str(password))
    except Exception as e:
        logging.error('[JWT]: Error')
    return user
Example #5
0
def identity(payload):
    user_id = str(payload['identity'])
    user = None
    try:
        user = UserService.get_user(user_id=user_id)
    except Exception as e:
        logging.error(str(e))
        logging.error('[JWT]: Error')
    return user
Example #6
0
def delete_profile():
    """Delete Me"""
    logging.info('[ROUTER]: Delete me')
    identity = current_identity
    try:
        user = UserService.delete_user(str(identity.id))
    except UserNotFound as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=404, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize()), 200
Example #7
0
def get_users():
    """Get users"""
    logging.info('[ROUTER]: Getting all users')
    include = request.args.get('include')
    include = include.split(',') if include else []
    identity = current_identity
    if identity.role != 'ADMIN' and identity.email != '*****@*****.**':
        return error(status=403, detail='Forbidden')
    try:
        users = UserService.get_users()
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=[user.serialize(include) for user in users]), 200
Example #8
0
def recover_password(user):
    """Revover password"""
    logging.info('[ROUTER]: Recovering password')
    try:
        user = UserService.recover_password(user)
    except UserNotFound as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=404, detail=e.message)
    except EmailError as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=500, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize()), 200
Example #9
0
def update_user(user):
    """Update an user"""
    logging.info('[ROUTER]: Updating user'+user)
    body = request.get_json()
    identity = current_identity
    if identity.role != 'ADMIN' and identity.email != '*****@*****.**':
        return error(status=403, detail='Forbidden')
    try:
        user = UserService.update_user(body, user)
    except UserNotFound as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=404, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize()), 200
Example #10
0
def get_user(user):
    """Get an user"""
    logging.info('[ROUTER]: Getting user'+user)
    include = request.args.get('include')
    include = include.split(',') if include else []
    identity = current_identity
    if identity.role != 'ADMIN' and identity.email != '*****@*****.**':
        return error(status=403, detail='Forbidden')
    try:
        user = UserService.get_user(user)
    except UserNotFound as e:
        logging.error('[ROUTER]: '+e.message)
        return error(status=404, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: '+str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize(include)), 200
Example #11
0
def delete_user(user):
    """Delete an user"""
    logging.info('[ROUTER]: Deleting user' + user)
    identity = current_identity
    if user == '*****@*****.**':
        return error(status=403, detail='Forbidden')
    if identity.role != 'ADMIN' and identity.email != '*****@*****.**':
        return error(status=403, detail='Forbidden')
    try:
        user = UserService.delete_user(user)
    except UserNotFound as e:
        logging.error('[ROUTER]: ' + e.message)
        return error(status=404, detail=e.message)
    except Exception as e:
        logging.error('[ROUTER]: ' + str(e))
        return error(status=500, detail='Generic Error')
    return jsonify(data=user.serialize()), 200