from odoo import http
from odoo.http import request


class XenoMyAvatar(http.Controller):

    @http.route("/xeno/my_avatar/<int:width>x<int:height>", type="http", auth="user")
    def my_avatar(self, width=128, height=128):
        """The logged-in user's own avatar_128, sudo()'d.

        core's own /web/image/hr.employee/<id>/avatar_128 route
        (web/controllers/binary.py's content_image) resolves the record via
        ir.binary._find_record(), which calls record.check_access("read")
        directly rather than going through a plain search/read -- and on
        this deployment, a regular employee's own hr.employee record fails
        that check (confirmed live: ir.model.access.check('hr.employee',
        'read') is False for base.group_user, even though ordinary
        search_read/read calls on the SAME record still succeed, some
        internal ORM distinction between those and an explicit
        check_access() call). _find_record() catches that AccessError as a
        UserError and silently falls back to Odoo's placeholder image --
        so a regular employee's own profile photo never rendered on the My
        Profile landing page, no error visible anywhere.

        Deliberately narrow: always resolves to request.env.user's own
        employee_id server-side, never a client-supplied id -- this can
        only ever serve the caller's own photo, unlike the core route this
        replaces (which -- for a viewer who DOES pass its check_access,
        e.g. an Officer -- can serve any employee's photo by id).
        """
        employee = request.env.user.employee_id
        stream = request.env["ir.binary"].sudo()._get_image_stream_from(
            employee.sudo(), "avatar_128", width=width, height=height,
        )
        return stream.get_response()
