'POST /api/users/{id}/information
Save a user\'s email, username, addresses etc.
:param id: the encoded id of the user
:type  id: str
:param payload: data with new settings
:type  payload: dict'

@expose_api
def set_information(self, trans, id, payload={}, **kwd):

    user = self._get_user(trans, id)
    email = payload.get('email')
    username = payload.get('username')
    if (email or username):
        message = (self._validate_email_publicname(email, username) or validate_email(trans, email, user))
        if ((not message) and username):
            message = validate_publicname(trans, username, user)
        if message:
            raise MessageException(message)
        if (user.email != email):
            private_role = trans.app.security_agent.get_private_user_role(user)
            private_role.name = email
            private_role.description = ('Private    role    for    ' + email)
            user.email = email
            trans.sa_session.add(user)
            trans.sa_session.add(private_role)
            trans.sa_session.flush()
            if trans.app.config.user_activation_on:
                user.active = False
                if self.send_verification_email(trans, user.email, user.username):
                    message = 'The    login    information    has    been    updated    with    the    changes.<br>Verification    email    has    been    sent    to    your    new    email    address.    Please    verify    it    by    clicking    the    activation    link    in    the    email.<br>Please    check    your    spam/trash    folder    in    case    you    cannot    find    the    message.'
                else:
                    message = 'Unable    to    send    activation    email,    please    contact    your    local    Galaxy    administrator.'
                    if (trans.app.config.error_email_to is not None):
                        message += ('    Contact:    %s' % trans.app.config.error_email_to)
                    raise MessageException(message)
        if (user.username != username):
            user.username = username
    user_info_form_id = payload.get('info|form_id')
    if user_info_form_id:
        prefix = 'info|'
        user_info_form = trans.sa_session.query(trans.app.model.FormDefinition).get(trans.security.decode_id(user_info_form_id))
        user_info_values = {}
        for item in payload:
            if item.startswith(prefix):
                user_info_values[item[len(prefix):]] = payload[item]
        form_values = trans.model.FormValues(user_info_form, user_info_values)
        trans.sa_session.add(form_values)
        user.values = form_values
    extra_user_pref_data = dict()
    get_extra_pref_keys = self._get_extra_user_preferences(trans)
    if (get_extra_pref_keys is not None):
        for key in get_extra_pref_keys:
            key_prefix = (key + '|')
            for item in payload:
                if item.startswith(key_prefix):
                    if (payload[item] == ''):
                        keys = item.split('|')
                        section = get_extra_pref_keys[keys[0]]
                        for input in section['inputs']:
                            if ((input['name'] == keys[1]) and input['required']):
                                raise MessageException('Please    fill    the    required    field')
                    extra_user_pref_data[item] = payload[item]
        user.preferences['extra_user_preferences'] = json.dumps(extra_user_pref_data)
    address_dicts = {}
    address_count = 0
    for item in payload:
        match = re.match('^address_(?P<index>\\d+)\\|(?P<attribute>\\S+)', item)
        if match:
            groups = match.groupdict()
            index = int(groups['index'])
            attribute = groups['attribute']
            address_dicts[index] = (address_dicts.get(index) or {})
            address_dicts[index][attribute] = payload[item]
            address_count = max(address_count, (index + 1))
    user.addresses = []
    for index in range(0, address_count):
        d = address_dicts[index]
        if d.get('id'):
            try:
                user_address = trans.sa_session.query(trans.app.model.UserAddress).get(trans.security.decode_id(d['id']))
            except Exception as e:
                raise MessageException(('Failed    to    access    user    address    (%s).    %s' % (d['id'], e)))
        else:
            user_address = trans.model.UserAddress()
            trans.log_event('User    address    added')
        for field in AddressField.fields():
            if ((str(field[2]).lower() == 'required') and (not d.get(field[0]))):
                raise MessageException(('Address    %s:    %s    (%s)    required.' % ((index + 1), field[1], field[0])))
            setattr(user_address, field[0], str(d.get(field[0], '')))
        user_address.user = user
        user.addresses.append(user_address)
        trans.sa_session.add(user_address)
    trans.sa_session.add(user)
    trans.sa_session.flush()
    trans.log_event('User    information    added')
    return {'message': 'User    information    has    been    saved.'}
