Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
2061 Vistas

I am trying to add user when they try to log in with SSO. However, they are always getting error that  duplicate key value violates unique constraint "mail_channel_partner_partner_unique"

DETAIL:  Key (channel_id, partner_id)=(1, 6973) already exists


partner_id is already taken by other user, so Odoo is not creating new one properly. 

Even when trying to create partner first, then add the partner to user creation, it is not taking it properly. Sample code:

if not user:

            user_vals = {

                'name': profile["displayName"],

                'login': profile["mail"]

            }

            # Check if there is a partner with the same email, to avoid duplication

            existing_partner = request.env['res.partner'].sudo().search([('email', '=', profile["mail"])], limit=1)

            if existing_partner:

                _logger.debug("Partner exists: %s", existing_partner)

                user_vals['partner_id'] = existing_partner.id

                user = request.env['res.users'].sudo().create(user_vals)

                _logger.debug("User created and linked to existing partner: %s", user)

            # Create the user if they do not exist

            else:

                _logger.debug("No partner, creating one")

                partner_vals={'name': profile["displayName"], 'email': profile["mail"]}

                new_partner = request.env['res.partner'].sudo().create(partner_vals)

                user_vals["partner_id"]=new_partner.id

                user = request.env['res.users'].sudo().create(user_vals)

                _logger.debug("Creating user with vals: %s", user_vals)

Avatar
Descartar
Autor

We discovered that users log in as someone else, when they login in before, and after they try to open the link and that bypasses SSO, so they log in as another user. Is there a way to reauthenticate when any link is open? 

Mejor respuesta

Hi,

Please refer to the code below:


if not user:

    user_vals = {

        'name': profile["displayName"],

        'login': profile["mail"],

    }


    # Check if there is a partner with the same email

    existing_partner = request.env['res.partner'].sudo().search([('email', '=', profile["mail"])], limit=1)


    if existing_partner and not existing_partner.user_ids:

        # Safe to reuse this partner

        _logger.debug("Partner exists and not linked to a user: %s", existing_partner)

        user_vals['partner_id'] = existing_partner.id

    else:

        # Either no partner or already linked → create a new one

        _logger.debug("Creating new partner for user")

        partner_vals = {

            'name': profile["displayName"],

            'email': profile["mail"],

        }

        new_partner = request.env['res.partner'].sudo().create(partner_vals)

        user_vals['partner_id'] = new_partner.id


    # Create the user linked to the partner

    user = request.env['res.users'].with_context(no_reset_password=True).sudo().create(user_vals)

    _logger.debug("User created and linked to partner: %s", user)


Hope it helps.



Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
jun 25
2097
3
jul 25
3688
1
may 25
1804
4
may 25
3112
1
feb 25
7171