Steps to reproduce:
1. Make sure the 'teacher' role has permission 'moodle/user:viewdetails'
2. Assign a user the role of 'teacher' on a course
3. Click on 'participants'
4. Click on the name of one of the students
5. Click on 'Full Profile'
Able to see student's profile
Message 'The details of this user are not available to you'.
I understand that this is because the context in which you are viewing the user is outside of the course (and hence the 'teacher' no longer has the 'teacher' role at that point), but it would be helpful if the user:viewdetails permission was atomatically extended to cover viewing the profiles of anyone enrolled on a course as a student.