Exemplo n.º 1
0
def reset_pass(data, db_session):
    logger.info(LogMsg.START, data)
    schema_validate(data, RESET_PASS_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    cell_no = data.get('cell_no')
    redis_key = 'PASS_{}'.format(cell_no)
    code = redis.get(redis_key)
    if code is None:
        logger.error(LogMsg.REGISTER_KEY_DOESNT_EXIST)
        raise Http_error(404, Message.INVALID_CODE)

    code = code.decode("utf-8")
    if (code is None) or (code != data.get('code')):
        logger.error(LogMsg.REGISTER_KEY_INVALID)
        raise Http_error(409, Message.INVALID_CODE)

    user = check_by_cell_no(cell_no, db_session)

    if user:
        user.password = data.get('password')

        logger.debug(LogMsg.USER_PASSWORD_RESET, user_to_dict(user))
        logger.info(LogMsg.END)

        return data

    logger.error(LogMsg.NOT_FOUND, data)
    raise Http_error(404, Message.INVALID_USER)
Exemplo n.º 2
0
def register(data, db_session):
    valid_registering_intervall = value('valid_registering_intervall', 120)

    logging.info(Msg.START + ' data is: ' + str(data))

    cell_no = data.get('cell_no')
    if cell_no is None:
        logging.error(Msg.DATA_MISSING.format('cell_no'))
        raise Http_error(404, Msg.DATA_MISSING.format('cell_no'))

    logging.debug(Msg.CHECK_USER_EXISTANCE)

    if check_user(cell_no, db_session):
        logging.error(Msg.USER_XISTS)
        raise Http_error(409, Msg.USER_XISTS)

    logging.debug(Msg.CHECK_REDIS_FOR_EXISTANCE)

    if redis.get(cell_no):
        logging.error(Msg.REGISTER_XISTS)
        raise Http_error(403, Msg.REGISTER_XISTS)

    logging.debug(Msg.GENERATING_REGISTERY_CODE.format(cell_no))

    password = random.randint(1000, 9999)
    message = 'BELLEZZA     your activation code is : {}'.format(password)
    data = {'cell_no': cell_no, 'message': message}

    logging.debug(Msg.SEND_CODE_BY_SMS.format(cell_no))
    sent_data = send_message(data)
    logging.debug(Msg.SMS_SENT.format(cell_no))

    redis.set(cell_no, password, ex=valid_registering_intervall)

    return sent_data
Exemplo n.º 3
0
def register(data, db_session):
    logger.info(LogMsg.START, data)

    schema_validate(data, REGISTER_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    cell_no = data.get('cell_no')

    logger.debug(LogMsg.CHECK_USER_EXISTANCE)

    if check_by_cell_no(cell_no, db_session):
        logger.error(LogMsg.USER_XISTS)
        raise Http_error(409, Message.USER_ALREADY_EXISTS)

    logger.debug(LogMsg.CHECK_REDIS_FOR_EXISTANCE)

    cell_data = redis.get(cell_no)

    if cell_data:
        logger.error(LogMsg.REGISTER_XISTS)
        activation_code = (json.loads(cell_data.decode('utf-8'))).get(
            'activation_code', None)
        if activation_code:
            logger.error(LogMsg.USER_HAS_ACTIVATION_CODE)
            raise Http_error(403, {
                'msg': Message.ALREADY_HAS_VALID_KEY,
                'time': redis.ttl(cell_no)
            })
        else:
            logger.error(LogMsg.USER_HAS_SIGNUP_TOKEN)
            redis.delete(cell_no)
    logger.debug(LogMsg.GENERATING_REGISTERY_CODE, cell_no)

    password = str(random.randint(1000, 9999))

    data = {
        'receptor': cell_no,
        'token': password,
        'type': 'sms',
        'template': 'ratingregister'
    }
    logger.debug(LogMsg.SEND_CODE_BY_SMS.format(cell_no))
    sent_data = send_message(data)

    redis.set(cell_no,
              json.dumps({'activation_code': password}),
              ex=valid_registering_intervall)
    result = {
        'msg': Message.MESSAGE_SENT,
        'cell_no': cell_no,
        'time': redis.ttl(cell_no)
    }
    logger.debug(LogMsg.SMS_SENT, result)
    logger.info(LogMsg.END)

    return result
Exemplo n.º 4
0
def signup(data, db_session, *args, **kwargs):
    logger.info(LogMsg.START, data)

    schema_validate(data, SIGN_UP_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    cell_no = data.get('cell_no')
    logger.debug(LogMsg.SIGNUP_GETTING_TOKEN_FROM_REDIS, cell_no)
    signup_token = redis.get(cell_no)
    if signup_token is None:
        logger.error(LogMsg.SIGNUP_TOKEN_NOT_IN_REDIS, cell_no)
        raise Http_error(404, Message.SIGNUP_TOKEN_NOT_EXISTS)

    signup_token = json.loads(signup_token.decode("utf-8")).get(
        'signup_token', None)
    if (signup_token is None) or (signup_token != data.get('signup_token')):
        logger.error(
            LogMsg.SIGNUP_TOKEN_INVALID, {
                'redis_signup_token': signup_token,
                'data_token': data.get('signup_token')
            })
        raise Http_error(404, Message.INVALID_SIGNUP_TOKEN)

    user_data = {
        k: v
        for k, v in data.items() if k in ['username', 'password']
    }
    person_data = {k: v for k, v in data.items() if k not in user_data.keys()}
    del person_data['signup_token']

    logger.debug(LogMsg.PERSON_GENERATING, person_data)
    person = add_person(db_session, person_data, SIGNUP_USER)

    if user_data:
        user_data.update({'person_id': person.id})
    logger.debug(LogMsg.USER_GENERATING, user_data)
    user = add_user(db_session, user_data, SIGNUP_USER)

    full_name = '{} {}'.format(data.get('last_name', ''), data.get('name', ''))

    user_welcoming_data = {
        'receptor': cell_no,
        'token': full_name,
        'type': 'sms',
        'template': 'fajrwelcoming'
    }
    send_message(user_welcoming_data)
    logger.debug(LogMsg.SMS_SENT, user_welcoming_data)

    result = {'user': user_to_dict(user), 'person': model_to_dict(person)}
    logger.debug(LogMsg.SIGNUP_SUCCESS, result)
    logger.info(LogMsg.END)
    return result
Exemplo n.º 5
0
def add(db_session, data):
    logging.info(Msg.START)
    cell_no = data.get('cell_no')
    name = data.get('name')
    user = db_session.query(User).filter(User.username == cell_no).first()
    if user:
        logging.error(Msg.USER_XISTS.format(cell_no))
        raise Http_error(409, {"cell_no": Msg.USER_XISTS.format(cell_no)})

    logging.debug(Msg.CHECK_REDIS_FOR_EXISTANCE)

    activation_code = redis.get(cell_no)
    if activation_code is None:
        logging.error(Msg.REGISTER_KEY_DOESNT_EXIST)
        raise Http_error(404,
                         {"activation_code": Msg.REGISTER_KEY_DOESNT_EXIST})

    activation_code = activation_code.decode("utf-8")
    if activation_code != data.get('activation_code'):
        logging.error(Msg.REGISTER_KEY_INVALID)
        raise Http_error(400, {"activation_code": Msg.REGISTER_KEY_INVALID})

    user_by_name = db_session.query(User).filter(User.name == name).first()
    if user_by_name != None:
        logging.error(Msg.NAME_NOT_UNIQUE)
        raise Http_error(409, {"name": Msg.NAME_NOT_UNIQUE})

    logging.debug(Msg.USR_ADDING)

    model_instance = User()
    model_instance.username = cell_no
    model_instance.password = data.get('password')
    model_instance.name = name
    model_instance.id = str(uuid4())
    model_instance.creation_date = Now()
    model_instance.creator = data.get('cell_no')

    if data.get('tags') is not None:
        tags = (data.get('tags')).split(',')
        for item in tags:
            item.strip()
        model_instance.tags = tags

    logging.debug(Msg.DATA_ADDITION)

    db_session.add(model_instance)

    logging.debug(Msg.DB_ADD + json.dumps(model_to_dict(model_instance)))

    logging.info(Msg.END)
    return model_instance
Exemplo n.º 6
0
def activate_account(data, db_session):
    logger.info(LogMsg.START, data)

    cell_no = data.get('cell_no')
    if cell_no is None:
        logger.error(LogMsg.DATA_MISSING, 'cell_no')
        raise Http_error(400, Message.CELL_NO_REQUIRED)

    logger.debug(LogMsg.CHECK_USER_EXISTANCE)

    if check_by_cell_no(cell_no, db_session):
        logger.error(LogMsg.USER_XISTS, data)
        raise Http_error(409, Message.USER_ALREADY_EXISTS)

    logger.debug(LogMsg.CHECK_REDIS_FOR_EXISTANCE)

    cell_data = redis.get(cell_no)
    if cell_data is None:
        logger.error(LogMsg.REGISTER_KEY_DOESNT_EXIST)
        raise Http_error(404, Message.NO_VALID_ACTIVATION_CODE)

    activation_code = (json.loads(cell_data.decode("utf-8"))).get(
        'activation_code', None)
    print(activation_code)

    if activation_code is None:
        logger.error(LogMsg.USER_HAS_SIGNUP_TOKEN)
        raise Http_error(404, Message.NO_VALID_ACTIVATION_CODE)

    if activation_code != data.get('activation_code'):
        logger.error(LogMsg.REGISTER_KEY_INVALID)
        raise Http_error(409, Message.WRONG_ACTIVATION_CODE)

    signup_token = str(uuid4())
    redis.delete(cell_no)
    redis.set(cell_no,
              json.dumps({'signup_token': signup_token}),
              ex=valid_activating_intervall)

    data = {'cell_no': cell_no, 'signup_token': signup_token}

    return data
Exemplo n.º 7
0
def get_user_permissions(username, db_session):
    user = check_user(username, db_session)

    if user is None:
        logger.error(LogMsg.NOT_FOUND, {'username': username})
        raise Http_error(404, Message.INVALID_USERNAME)
    redis_key = 'PERMISSIONS_{}'.format(user.id)
    permission_list = app_redis.get(redis_key)
    if permission_list is not None:
        data =  json.loads(permission_list.decode("utf-8"))
        return data.get('permission_values',None),data.get('presses',None)

    group_list = get_user_group_list(user.id, db_session)
    if not bool(group_list):
        return [],[]
    permissions = get_permission_list_of_groups(group_list.keys(), db_session)
    permission_values = get_permissions_values(permissions, db_session)

    app_redis.set(redis_key, json.dumps({'permission_values':permission_values,'presses':list(group_list.values())}),
                  ex=permission_list_expiration_time)


    return permission_values,group_list.values()