Esempio n. 1
0
def db_user_update_user(store, user_id, request):
    """
    Updates the specified user.
    This version of the function is specific for users that with comparison with
    admins can change only few things:
      - preferred language
      - the password (with old password check)
      - pgp key
    raises: globaleaks.errors.ReceiverIdNotFound` if the receiver does not exist.
    """
    user = models.db_get(store, models.User, id=user_id)

    user.language = request.get('language',
                                State.tenant_cache[1].default_language)

    new_password = request['password']
    old_password = request['old_password']

    if new_password and old_password:
        user.password = change_password(user.password, old_password,
                                        new_password, user.salt)

        if user.password_change_needed:
            user.password_change_needed = False

        user.password_change_date = datetime_now()

    # The various options related in manage PGP keys are used here.
    parse_pgp_options(user, request)

    return user
Esempio n. 2
0
def wizard(store, request, language):
    try:
        request['node']['default_language'] = language
        request['node']['languages_enabled'] = [language]

        # Header title of the homepage is initially set with the node title
        request['node']['header_title_homepage'] = request['node']['name']

        db_update_node(store, request['node'], True, language)
        context = db_create_context(store, request['context'], language)

        # associate the new context to the receiver
        request['receiver']['contexts'] = [context.id]

        db_create_receiver(store, request['receiver'], language)

        admin = store.find(models.User,
                           (models.User.username == unicode('admin'))).one()

        admin.mail_address = request['admin']['mail_address']

        password = request['admin']['password']
        old_password = request['admin']['old_password']

        if password and old_password and len(password) and len(old_password):
            admin.password = security.change_password(admin.password,
                                                      old_password, password,
                                                      admin.salt)
    except Exception as excep:
        log.err("Failed wizard initialization %s" % excep)
        raise excep
Esempio n. 3
0
def update_receiver_settings(store,
                             receiver_id,
                             request,
                             language=GLSetting.memory_copy.default_language):
    receiver = store.find(Receiver, Receiver.id == unicode(receiver_id)).one()
    receiver.description[language] = request['description']

    if not receiver:
        raise errors.ReceiverIdNotFound

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password,
                                                 old_password, new_password,
                                                 receiver.user.salt)

    mail_address = request['mail_address']

    if mail_address != receiver.mail_address:
        log.info("Email change %s => %s" %
                 (receiver.mail_address, mail_address))
        receiver.mail_address = mail_address

    receiver.tip_notification = acquire_bool(request['tip_notification'])
    receiver.message_notification = acquire_bool(
        request['message_notification'])
    receiver.comment_notification = acquire_bool(
        request['comment_notification'])
    receiver.file_notification = acquire_bool(request['file_notification'])

    gpg_options_parse(receiver, request)

    return receiver_serialize_receiver(receiver, language)
Esempio n. 4
0
def update_receiver_settings(store, receiver_id, request, language=GLSetting.memory_copy.default_language):
    receiver = store.find(Receiver, Receiver.id == unicode(receiver_id)).one()
    receiver.description[language] = request['description']

    if not receiver:
        raise errors.ReceiverIdNotFound

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password,
                                                 old_password,
                                                 new_password,
                                                 receiver.user.salt)

    mail_address = request['mail_address']

    if mail_address != receiver.mail_address:
        log.info("Email change %s => %s" % (receiver.mail_address, mail_address))
        receiver.mail_address = mail_address

    receiver.tip_notification = acquire_bool(request['tip_notification'])
    receiver.message_notification = acquire_bool(request['message_notification'])
    receiver.comment_notification = acquire_bool(request['comment_notification'])
    receiver.file_notification = acquire_bool(request['file_notification'])

    gpg_options_parse(receiver, request)

    return receiver_serialize_receiver(receiver, language)
Esempio n. 5
0
def wizard(store, request, language):
    try:
        request['node']['default_language'] = language
        request['node']['languages_enabled'] = [language]

        # Header title of the homepage is initially set with the node title
        request['node']['header_title_homepage'] = request['node']['name']

        db_update_node(store, request['node'], True, language)
        context = db_create_context(store, request['context'], language)

        # associate the new context to the receiver
        request['receiver']['contexts'] = [context.id]

        db_create_receiver(store, request['receiver'], language)

        admin = store.find(models.User, (models.User.username == unicode('admin'))).one()

        admin.mail_address = request['admin']['mail_address']

        password = request['admin']['password']
        old_password = request['admin']['old_password']

        if password and old_password and len(password) and len(old_password):
            admin.password = security.change_password(admin.password,
                                                      old_password,
                                                      password,
                                                      admin.salt)
    except Exception as excep:
        log.err("Failed wizard initialization %s" % excep)
        raise excep
Esempio n. 6
0
def update_receiver_settings(store, user_id, request, language=GLSetting.memory_copy.default_language):
    receiver = store.find(Receiver, Receiver.id == unicode(user_id)).one()
    receiver.description[language] = request.get('description')

    if not receiver:
        raise errors.ReceiverGusNotFound

    new_password = request.get('password')
    old_password = request.get('old_password')

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password,
                                                 old_password,
                                                 new_password,
                                                 receiver.user.salt)

    mail_address = acquire_mail_address(request)
    if not mail_address:
        raise errors.NoEmailSpecified

    # receiver.notification_fields is not update until GLClient supports them
    receiver.tip_notification = acquire_bool(request['tip_notification'])
    receiver.comment_notification = acquire_bool(request['comment_notification'])
    receiver.file_notification = acquire_bool(request['file_notification'])

    gpg_options_parse(receiver, request)

    return receiver_serialize_receiver(receiver, language)
Esempio n. 7
0
def db_user_update_user(store, user_id, request, language):
    """
    Updates the specified user.
    This version of the function is specific for users that with comparison with
    admins can change only few things:
      - preferred language
      - the password (with old password check)
      - pgp key
    raises: globaleaks.errors.ReceiverIdNotFound` if the receiver does not exist.
    """
    user = models.User.get(store, user_id)

    if not user:
        raise errors.UserIdNotFound

    user.language = request.get('language', GLSettings.memory_copy.default_language)

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        user.password = change_password(user.password,
                                        old_password,
                                        new_password,
                                        user.salt)

        if user.password_change_needed:
            user.password_change_needed = False

        user.password_change_date = datetime_now()

    # The various options related in manage PGP keys are used here.
    parse_pgp_options(user, request)

    return user
Esempio n. 8
0
def db_update_node(store, request, wizard_done, language):
    """
    Update and serialize the node infos

    :param store: the store on which perform queries.
    :param language: the language in which to localize data
    :return: a dictionary representing the serialization of the node
    """
    node = store.find(models.Node).one()

    fill_localized_keys(request, models.Node.localized_strings, language)

    admin = store.find(models.User, (models.User.username == unicode('admin'))).one()

    admin.mail_address = request['email']
    admin.language = request['admin_language']
    admin.timezone = request['admin_timezone']

    password = request.get('password', None)
    old_password = request.get('old_password', None)

    if password and old_password and len(password) and len(old_password):
        admin.password = security.change_password(admin.password,
                                    old_password, password, admin.salt)

    # verify that the languages enabled are valid 'code' in the languages supported
    node.languages_enabled = []
    for lang_code in request['languages_enabled']:
        if lang_code in LANGUAGES_SUPPORTED_CODES:
            node.languages_enabled.append(lang_code)
        else:
            raise errors.InvalidInputFormat("Invalid lang code enabled: %s" % lang_code)

    if not len(node.languages_enabled):
        raise errors.InvalidInputFormat("Missing enabled languages")

    # enforcing of default_language usage (need to be set, need to be _enabled)
    if request['default_language']:
        if request['default_language'] not in node.languages_enabled:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        node.default_language = request['default_language']

    else:
        node.default_language = node.languages_enabled[0]
        log.err("Default language not set!? fallback on %s" % node.default_language)

    if wizard_done:
        node.wizard_done = True

    try:
        node.update(request)
    except DatabaseError as dberror:
        log.err("Unable to update node: %s" % dberror)
        raise errors.InvalidInputFormat(dberror)

    db_update_memory_variables(store)

    return db_admin_serialize_node(store, language)
Esempio n. 9
0
def update_node(store, request, language=GLSetting.memory_copy.default_language):
    """
    Update the node, setting the last update time on it.

    Password:
        If old_password and password are present, password update is performed

    URLs:
        If one url is present, is properly validated

    Returns:
        the last update time of the node as a :class:`datetime.datetime`
        instance
    """
    node = store.find(Node).one()

    for attr in getattr(node, "localized_strings"):
        new_value = unicode(request[attr])
        request[attr] = getattr(node, attr)
        request[attr][language] = new_value

    password = request.get('password', None)
    old_password = request.get('old_password', None)

    if password and old_password and len(password) and len(old_password):
        admin = store.find(User, (User.username == unicode('admin'))).one()
        admin.password = security.change_password(admin.password,
                                    old_password, password, admin.salt)

    if len(request['public_site']) > 1:
        if not utility.acquire_url_address(request['public_site'], hidden_service=True, http=True):
            log.err("Invalid public page regexp in [%s]" % request['public_site'])
            raise errors.InvalidInputFormat("Invalid public site")

    if len(request['hidden_service']) > 1:
        if not utility.acquire_url_address(request['hidden_service'], hidden_service=True, http=False):
            log.err("Invalid hidden service regexp in [%s]" % request['hidden_service'])
            raise errors.InvalidInputFormat("Invalid hidden service")

    # verify that the languages enabled are valid 'code' in the languages supported
    node.languages_enabled = []
    for lang_code in request['languages_enabled']:
        if lang_code in LANGUAGES_SUPPORTED_CODES:
            node.languages_enabled.append(lang_code)
        else:
            raise errors.InvalidInputFormat("Invalid lang code enabled: %s" % lang_code)

    if not request['default_language'] in LANGUAGES_SUPPORTED_CODES:
        raise errors.InvalidInputFormat("Invalid lang code as default")

    # name, description tor2web boolean value are acquired here
    node.update(request)

    node.last_update = utility.datetime_now()

    return admin_serialize_node(node, language)
Esempio n. 10
0
def update_receiver_settings(store, receiver_id, request, language):
    """
    TODO: remind that 'description' is imported, but is not permitted
        by UI to be modified right now.
    """
    receiver = store.find(Receiver, Receiver.id == receiver_id).one()
    receiver.description[language] = request['description']

    if not receiver:
        raise errors.ReceiverIdNotFound

    receiver.user.language = request.get('language', GLSetting.memory_copy.language)
    receiver.user.timezone = request.get('timezone', GLSetting.memory_copy.default_timezone)

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password,
                                                 old_password,
                                                 new_password,
                                                 receiver.user.salt)

        if receiver.user.password_change_needed:
            receiver.user.password_change_needed = False

        receiver.user.password_change_date = datetime_now()

    mail_address = request['mail_address']
    ping_mail_address = request['ping_mail_address']

    if mail_address != receiver.mail_address:
        log.err("Email cannot be change by receiver, only by admin " \
                "%s rejected. Kept %s" % (receiver.mail_address, mail_address))

    if ping_mail_address != receiver.ping_mail_address:
        log.info("Ping email going to be update, %s => %s" % (
            receiver.ping_mail_address, ping_mail_address))
        receiver.ping_mail_address = ping_mail_address

    receiver.tip_notification = acquire_bool(request['tip_notification'])
    receiver.message_notification = acquire_bool(request['message_notification'])
    receiver.comment_notification = acquire_bool(request['comment_notification'])
    receiver.file_notification = acquire_bool(request['file_notification'])
    receiver.ping_notification = acquire_bool(request['ping_notification'])

    gpg_options_parse(receiver, request)

    return receiver_serialize_receiver(receiver, language)
Esempio n. 11
0
    def test_change_password(self):
        first_pass = helpers.VALID_PASSWORD1
        second_pass = helpers.VALID_PASSWORD2
        dummy_salt = generateRandomSalt()

        # as first we hash a "first_password" like has to be:
        hashed1 = binascii.b2a_hex(scrypt.hash(str(first_pass), dummy_salt))

        # now emulate the change unsing the globaleaks.security module
        hashed2 = change_password(hashed1, first_pass, second_pass, dummy_salt)

        # verify that second stored pass is the same
        self.assertEqual(
            hashed2, binascii.b2a_hex(scrypt.hash(str(second_pass),
                                                  dummy_salt)))
Esempio n. 12
0
    def test_change_password(self):
        first_pass = helpers.VALID_PASSWORD1
        second_pass = helpers.VALID_PASSWORD2
        dummy_salt = generateRandomSalt()

        # as first we hash a "first_password" like has to be:
        hashed1 = binascii.b2a_hex(scrypt.hash(str(first_pass), dummy_salt))

        # now emulate the change unsing the globaleaks.security module
        hashed2 = change_password(hashed1, first_pass, second_pass, dummy_salt)

        # verify that second stored pass is the same
        self.assertEqual(
            hashed2,
            binascii.b2a_hex(scrypt.hash(str(second_pass), dummy_salt))
        )
Esempio n. 13
0
def update_receiver_settings(store, receiver_id, request, language):
    """
    TODO: remind that 'description' is imported, but is not permitted
        by UI to be modified right now.
    """
    receiver = store.find(Receiver, Receiver.id == receiver_id).one()
    receiver.description[language] = request['description']

    if not receiver:
        raise errors.ReceiverIdNotFound

    receiver.user.language = request.get('language', GLSetting.memory_copy.language)
    receiver.user.timezone = request.get('timezone', GLSetting.memory_copy.default_timezone)

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password,
                                                 old_password,
                                                 new_password,
                                                 receiver.user.salt)

        if receiver.user.password_change_needed:
            receiver.user.password_change_needed = False

        receiver.user.password_change_date = datetime_now()

    mail_address = request['mail_address']
    ping_mail_address = request['ping_mail_address']

    if mail_address != receiver.mail_address:
        log.err("Email cannot be change by receiver, only by admin " \
                "%s rejected. Kept %s" % (receiver.mail_address, mail_address))

    if ping_mail_address != receiver.ping_mail_address:
        log.info("Ping email going to be update, %s => %s" % (
            receiver.ping_mail_address, ping_mail_address))
        receiver.ping_mail_address = ping_mail_address

    receiver.tip_notification = acquire_bool(request['tip_notification'])

    pgp_options_parse(receiver, request)

    return receiver_serialize_receiver(receiver, language)
Esempio n. 14
0
def update_receiver_settings(store, receiver_id, request, language):
    """
    TODO: remind that 'description' is imported, but is not permitted
        by UI to be modified right now.
    """
    receiver = store.find(Receiver, Receiver.id == receiver_id).one()
    receiver.description[language] = request['description']

    if not receiver:
        raise errors.ReceiverIdNotFound

    receiver.user.language = request.get(
        'language', GLSettings.memory_copy.default_language)
    receiver.user.timezone = request.get(
        'timezone', GLSettings.memory_copy.default_timezone)

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password,
                                                 old_password, new_password,
                                                 receiver.user.salt)

        if receiver.user.password_change_needed:
            receiver.user.password_change_needed = False

        receiver.user.password_change_date = datetime_now()

    ping_mail_address = request['ping_mail_address']

    if ping_mail_address != receiver.ping_mail_address:
        log.info("Ping email going to be updated, %s => %s" %
                 (receiver.ping_mail_address, ping_mail_address))
        receiver.ping_mail_address = ping_mail_address

    receiver.tip_notification = request['tip_notification']
    receiver.ping_notification = request['ping_notification']

    pgp_options_parse(receiver, request)

    node = store.find(Node).one()

    return receiver_serialize_receiver(receiver, node, language)
Esempio n. 15
0
def update_receiver_settings(store, receiver_id, request, language):
    """
    TODO: remind that 'description' is imported, but is not permitted
        by UI to be modified right now.
    """
    receiver = store.find(Receiver, Receiver.id == receiver_id).one()
    receiver.description[language] = request["description"]

    if not receiver:
        raise errors.ReceiverIdNotFound

    receiver.user.language = request.get("language", GLSettings.memory_copy.default_language)
    receiver.user.timezone = request.get("timezone", GLSettings.memory_copy.default_timezone)

    new_password = request["password"]
    old_password = request["old_password"]

    if len(new_password) and len(old_password):
        receiver.user.password = change_password(receiver.user.password, old_password, new_password, receiver.user.salt)

        if receiver.user.password_change_needed:
            receiver.user.password_change_needed = False

        receiver.user.password_change_date = datetime_now()

    ping_mail_address = request["ping_mail_address"]

    if ping_mail_address != receiver.ping_mail_address:
        log.info("Ping email going to be updated, %s => %s" % (receiver.ping_mail_address, ping_mail_address))
        receiver.ping_mail_address = ping_mail_address

    receiver.tip_notification = request["tip_notification"]
    receiver.ping_notification = request["ping_notification"]

    pgp_options_parse(receiver, request)

    node = store.find(Node).one()

    return receiver_serialize_receiver(receiver, node, language)
Esempio n. 16
0
def db_user_update_user(store, user_id, request, language):
    """
    Updates the specified user.
    This version of the function is specific for users that with comparison with
    admins can change only few things:
      - preferred language
      - preferred timezone
      - the password (with old password check)
      - pgp key
    raises: globaleaks.errors.ReceiverIdNotFound` if the receiver does not exist.
    """
    user = models.User.get(store, user_id)

    if not user:
        raise errors.UserIdNotFound

    user.language = request.get('language',
                                GLSettings.memory_copy.default_language)
    user.timezone = request.get('timezone',
                                GLSettings.memory_copy.default_timezone)

    new_password = request['password']
    old_password = request['old_password']

    if len(new_password) and len(old_password):
        user.password = change_password(user.password, old_password,
                                        new_password, user.salt)

        if user.password_change_needed:
            user.password_change_needed = False

        user.password_change_date = datetime_now()

    # The various options related in manage PGP keys are used here.
    parse_pgp_options(user, request)

    return user
Esempio n. 17
0
def db_update_node(store,
                   request,
                   wizard_done=True,
                   language=GLSetting.memory_copy.default_language):
    """
    Update the node, setting the last update time on it.

    Password:
        If old_password and password are present, password update is performed

    URLs:
        If one url is present, is properly validated

    Returns:
        the last update time of the node as a :class:`datetime.datetime`
        instance
    """
    node = store.find(Node).one()

    mo = structures.Rosetta()
    mo.acquire_request(language, request, Node)
    for attr in mo.get_localized_attrs():
        request[attr] = mo.get_localized_dict(attr)

    password = request.get('password', None)
    old_password = request.get('old_password', None)

    if password and old_password and len(password) and len(old_password):
        admin = store.find(User, (User.username == unicode('admin'))).one()
        admin.password = security.change_password(admin.password, old_password,
                                                  password, admin.salt)
    try:
        receipt_example = generate_example_receipt(request['receipt_regexp'])

        if len(receipt_example) != 16:
            raise Exception

    except Exception as excep:
        log.err(
            "Only receipt returning 16 bytes are accepted. Sets to default: [0-9]{16}"
        )
        request['receipt_regexp'] = u"[0-9]{16}"
        receipt_example = generate_example_receipt(request['receipt_regexp'])

    # check the 'reset_css' boolean option: remove an existent custom CSS
    if request['reset_css']:
        custom_css_path = os.path.join(GLSetting.static_path,
                                       "%s.css" % GLSetting.reserved_names.css)

        if os.path.isfile(custom_css_path):
            try:
                os.remove(custom_css_path)
                log.debug("Reset on custom CSS done.")
            except Exception as excep:
                log.err("Unable to remove custom CSS: %s: %s" %
                        (custom_css_path, excep))
                raise errors.InternalServerError(excep)
        else:
            log.err("Requested CSS Reset, but custom CSS do not exists")

    # verify that the languages enabled are valid 'code' in the languages supported
    node.languages_enabled = []
    for lang_code in request['languages_enabled']:
        if lang_code in LANGUAGES_SUPPORTED_CODES:
            node.languages_enabled.append(lang_code)
        else:
            raise errors.InvalidInputFormat("Invalid lang code enabled: %s" %
                                            lang_code)

    if not len(node.languages_enabled):
        raise errors.InvalidInputFormat("Missing enabled languages")

    # enforcing of default_language usage (need to be set, need to be _enabled)
    if request['default_language']:

        if request['default_language'] not in LANGUAGES_SUPPORTED_CODES:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        if request['default_language'] not in node.languages_enabled:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        node.default_language = request['default_language']

    else:
        node.default_language = node.languages_enabled[0]
        log.err("Default language not set!? fallback on %s" %
                node.default_language)

    # default False in creation, default False in the option.
    if wizard_done:
        if node.wizard_done:
            log.err("wizard completed more than one time!?")
        else:
            log.debug("wizard completed: Node initialized")
            node.wizard_done = True

    # name, description tor2web boolean value are acquired here
    try:
        node.update(request)
    except Exception as dberror:
        log.err("Unable to update Node: %s" % dberror)
        raise errors.InvalidInputFormat(dberror)

    node.last_update = datetime_now()
    return db_admin_serialize_node(store, language)
Esempio n. 18
0
def db_update_node(store, request, wizard_done=True, language=GLSetting.memory_copy.default_language):
    """
    Update the node, setting the last update time on it.

    Password:
        If old_password and password are present, password update is performed

    URLs:
        If one url is present, is properly validated

    Returns:
        the last update time of the node as a :class:`datetime.datetime`
        instance
    """
    node = store.find(Node).one()

    mo = structures.Rosetta()
    mo.acquire_request(language, request, Node)
    for attr in mo.get_localized_attrs():
        request[attr] = mo.get_localized_dict(attr)

    password = request.get('password', None)
    old_password = request.get('old_password', None)

    if password and old_password and len(password) and len(old_password):
        admin = store.find(User, (User.username == unicode('admin'))).one()
        admin.password = security.change_password(admin.password,
                                    old_password, password, admin.salt)
    try:
        receipt_example = generate_example_receipt(request['receipt_regexp'])

        if len(receipt_example) != 16:
            raise Exception

    except Exception as excep:
        log.err("Only receipt returning 16 bytes are accepted. Sets to default: [0-9]{16}")
        request['receipt_regexp'] = u"[0-9]{16}"
        receipt_example = generate_example_receipt(request['receipt_regexp'])


    # check the 'reset_css' boolean option: remove an existent custom CSS
    if request['reset_css']:
        custom_css_path = os.path.join(GLSetting.static_path, "%s.css" % GLSetting.reserved_names.css)

        if os.path.isfile(custom_css_path):
            try:
                os.remove(custom_css_path)
                log.debug("Reset on custom CSS done.")
            except Exception as excep:
                log.err("Unable to remove custom CSS: %s: %s" % (custom_css_path, excep))
                raise errors.InternalServerError(excep)
        else:
            log.err("Requested CSS Reset, but custom CSS does not exist")

    # check the 'reset_homepage' boolean option: remove an existent custom Homepage
    if request['reset_homepage']:
        custom_homepage_path = os.path.join(GLSetting.static_path, "%s.html" % GLSetting.reserved_names.html)

        if os.path.isfile(custom_homepage_path):
            try:
                os.remove(custom_homepage_path)
                log.debug("Reset on custom Homepage done.")
            except Exception as excep:
                log.err("Unable to remove custom Homepage: %s: %s" % (custom_homepage_path, excep))
                raise errors.InternalServerError(excep)
        else:
            log.err("Requested Homepage Reset, but custom Homepage does not exist")

    # verify that the languages enabled are valid 'code' in the languages supported
    node.languages_enabled = []
    for lang_code in request['languages_enabled']:
        if lang_code in LANGUAGES_SUPPORTED_CODES:
            node.languages_enabled.append(lang_code)
        else:
            raise errors.InvalidInputFormat("Invalid lang code enabled: %s" % lang_code)

    if not len(node.languages_enabled):
        raise errors.InvalidInputFormat("Missing enabled languages")

    # enforcing of default_language usage (need to be set, need to be _enabled)
    if request['default_language']:

        if request['default_language'] not in LANGUAGES_SUPPORTED_CODES:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        if request['default_language'] not in node.languages_enabled:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        node.default_language = request['default_language']

    else:
        node.default_language = node.languages_enabled[0]
        log.err("Default language not set!? fallback on %s" % node.default_language)

    # default False in creation, default False in the option.
    if wizard_done:
        if node.wizard_done:
            log.err("wizard completed more than one time!?")
        else:
            log.debug("wizard completed: Node initialized")
            node.wizard_done = True

    # name, description tor2web boolean value are acquired here
    try:
        node.update(request)
    except Exception as dberror:
        log.err("Unable to update Node: %s" % dberror)
        raise errors.InvalidInputFormat(dberror)

    node.last_update = datetime_now()
    return db_admin_serialize_node(store, language)
Esempio n. 19
0
def db_update_node(store, request, wizard_done, language):
    """
    Update and serialize the node infos

    :param store: the store on which perform queries.
    :param language: the language in which to localize data
    :return: a dictionary representing the serialization of the node
    """
    node = store.find(models.Node).one()

    fill_localized_keys(request, models.Node.localized_strings, language)

    admin = store.find(models.User, (models.User.username == unicode('admin'))).one()

    admin.language = request.get('admin_language', GLSetting.memory_copy.language)
    admin.timezone = request.get('admin_timezone', GLSetting.memory_copy.default_timezone)

    password = request.get('password', None)
    old_password = request.get('old_password', None)

    if password and old_password and len(password) and len(old_password):
        admin.password = security.change_password(admin.password,
                                    old_password, password, admin.salt)

    # verify that the languages enabled are valid 'code' in the languages supported
    node.languages_enabled = []
    for lang_code in request['languages_enabled']:
        if lang_code in LANGUAGES_SUPPORTED_CODES:
            node.languages_enabled.append(lang_code)
        else:
            raise errors.InvalidInputFormat("Invalid lang code enabled: %s" % lang_code)

    if not len(node.languages_enabled):
        raise errors.InvalidInputFormat("Missing enabled languages")

    # enforcing of default_language usage (need to be set, need to be _enabled)
    if request['default_language']:

        if request['default_language'] not in LANGUAGES_SUPPORTED_CODES:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        if request['default_language'] not in node.languages_enabled:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        node.default_language = request['default_language']

    else:
        node.default_language = node.languages_enabled[0]
        log.err("Default language not set!? fallback on %s" % node.default_language)

    if wizard_done:
        node.wizard_done = True

    # since change of regexp format to XXXX-XXXX-XXXX-XXXX
    # we removed the possibility to customize the receipt from the GLCllient
    request['receipt_regexp'] = GLSetting.defaults.receipt_regexp

    try:
        node.update(request)
    except DatabaseError as dberror:
        log.err("Unable to update Node: %s" % dberror)
        raise errors.InvalidInputFormat(dberror)

    node.last_update = datetime_now()

    db_import_memory_variables(store)

    return db_admin_serialize_node(store, language)
Esempio n. 20
0
def db_update_node(store, request, wizard_done=True, language=GLSetting.memory_copy.default_language):
    """
    Update the node, setting the last update time on it.

    Password:
        If old_password and password are present, password update is performed

    URLs:
        If one url is present, is properly validated

    Returns:
        the last update time of the node as a :class:`datetime.datetime`
        instance
    """
    node = store.find(models.Node).one()

    fill_localized_keys(request, models.Node.localized_strings, language)

    admin = store.find(models.User, (models.User.username == unicode('admin'))).one()

    admin.language = request.get('admin_language', GLSetting.memory_copy.default_language)
    admin.timezone = request.get('admin_timezone', GLSetting.memory_copy.default_timezone)

    password = request.get('password', None)
    old_password = request.get('old_password', None)

    if password and old_password and len(password) and len(old_password):
        admin.password = security.change_password(admin.password,
                                    old_password, password, admin.salt)

    # check the 'reset_css' boolean option: remove an existent custom CSS
    if request['reset_css']:
        custom_css_path = os.path.join(GLSetting.static_path, "%s.css" % GLSetting.reserved_names.css)

        if os.path.isfile(custom_css_path):
            try:
                os.remove(custom_css_path)
                log.debug("Reset on custom CSS done.")
            except Exception as excep:
                log.err("Unable to remove custom CSS: %s: %s" % (custom_css_path, excep))
                raise errors.InternalServerError(excep)
        else:
            log.err("Requested CSS Reset, but custom CSS does not exist")

    # check the 'reset_homepage' boolean option: remove an existent custom Homepage
    if request['reset_homepage']:
        custom_homepage_path = os.path.join(GLSetting.static_path, "%s.html" % GLSetting.reserved_names.html)

        if os.path.isfile(custom_homepage_path):
            try:
                os.remove(custom_homepage_path)
                log.debug("Reset on custom Homepage done.")
            except Exception as excep:
                log.err("Unable to remove custom Homepage: %s: %s" % (custom_homepage_path, excep))
                raise errors.InternalServerError(excep)
        else:
            log.err("Requested Homepage Reset, but custom Homepage does not exist")

    # verify that the languages enabled are valid 'code' in the languages supported
    node.languages_enabled = []
    for lang_code in request['languages_enabled']:
        if lang_code in LANGUAGES_SUPPORTED_CODES:
            node.languages_enabled.append(lang_code)
        else:
            raise errors.InvalidInputFormat("Invalid lang code enabled: %s" % lang_code)

    if not len(node.languages_enabled):
        raise errors.InvalidInputFormat("Missing enabled languages")

    # enforcing of default_language usage (need to be set, need to be _enabled)
    if request['default_language']:

        if request['default_language'] not in LANGUAGES_SUPPORTED_CODES:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        if request['default_language'] not in node.languages_enabled:
            raise errors.InvalidInputFormat("Invalid lang code as default")

        node.default_language = request['default_language']

    else:
        node.default_language = node.languages_enabled[0]
        log.err("Default language not set!? fallback on %s" % node.default_language)

    # default False in creation, default False in the option.
    if wizard_done:
        if node.wizard_done:
            log.err("wizard completed more than one time!?")
        else:
            log.debug("wizard completed: Node initialized")
            node.wizard_done = True

    # since change of regexp format to XXXX-XXXX-XXXX-XXXX
    # we removed the possibility to customize the receipt from the GLCllient
    request['receipt_regexp'] = GLSetting.defaults.receipt_regexp

    try:
        node.update(request)
    except DatabaseError as dberror:
        log.err("Unable to update Node: %s" % dberror)
        raise errors.InvalidInputFormat(dberror)

    node.last_update = datetime_now()
    return db_admin_serialize_node(store, language)