Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
0
def forget_pass(data, db_session):
    logger.info(LogMsg.START, data)

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

    reset_password_interval = value('reset_password_interval', 120)
    username = data.get('username')
    cell_no = data.get('cell_no')

    user = None
    if username:
        user = check_by_username(username, db_session)
    elif cell_no:
        user = check_by_cell_no(cell_no, db_session)
    else:
        logger.error(LogMsg.INVALID_USER, data)
        raise Http_error(400, Message.USERNAME_CELLNO_REQUIRED)

    if user:
        person = validate_person(user.person_id, db_session)
        logger.debug(LogMsg.PERSON_EXISTS, username)
        password = str(random.randint(1000, 9999))

        sending_data = {
            'receptor': person.cell_no,
            'token': user.username,
            'token2': password,
            'type': 'sms',
            'template': 'fajrresetPassword'
        }
        send_message(sending_data)
        logger.debug(LogMsg.SMS_SENT, sending_data)

        redis_key = 'PASS_{}'.format(person.cell_no)
        redis.set(redis_key, password, ex=reset_password_interval)
        logger.debug(LogMsg.REDIS_SET, redis_key)
        logger.info(LogMsg.END)

        return data
    logger.error(LogMsg.INVALID_USER, data)
    raise Http_error(404, Message.INVALID_USER)
Esempio n. 4
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
Esempio n. 5
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()