Skip to content

Mentors: no direct messages, and only the groups they're added to - #303

Closed
svaka2000 wants to merge 10 commits into
Open-Coding-Society:masterfrom
svaka2000:feat/mentor-chat
Closed

svaka2000 wants to merge 10 commits into
Open-Coding-Society:masterfrom
svaka2000:feat/mentor-chat

Conversation

@svaka2000

@svaka2000 svaka2000 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Mentors (accounts with ROLE_MENTOR and no teacher or admin role) can't reach students privately through Spring's chat routes anymore. Spring checks this itself, so no page can get around it.

  • No direct message that includes a mentor can be started, sent or edited, whoever sends it. Reading old messages and deleting your own still work, so the navbar bell and /dm keep loading.
  • A mentor reads, posts and shares files only in groups they've been added to. Never a class group (CSA, CSP, CSH, CSSE), and only once the group has at least 2 members who aren't mentors, so a mentor can't use a group that's just them and one student.
  • Only a teacher or admin, or a group member who isn't a mentor, can add or remove a mentor. A mentor can't add anyone and can only leave.
  • Mentors can't list, search, make, rename or delete groups, change group grades, join class groups, or look at another person's groups. Their own group list and unread count only have the groups they can use.
  • The older /api/chat/{groupId}, the UI Builder's "message the owner", and group events from a signed-in chat socket follow the same rules.
  • The older /api/chat/{groupId} also checks membership again, for everyone: only a group's members, teachers and admins read it. Its check had been commented out, and no page reads this route.
  • Group responses mark each member "mentor": true or false, so a page can show a team's mentors.
  • Apart from that older route, requests that don't involve a mentor get the same answers as before. A refusal is a 403 with a reason (like mentor_no_direct_messages) so a page can say why in its own words. Bulk group creation lists a refused group under errors instead.

How it fits with Darshan's work:

Other notes:

How to try it:

  1. Make two students and a mentor. For the mentor, set roles_json to ["ROLE_USER","ROLE_MENTOR"] in the person table, and sign in with POST /authenticate (signing in through the site asks Flask, which can take a hand-set role away).
  2. As the mentor, POST /api/dm/conversations with {"otherPersonIds": [<a student's id>]}: 403 mentor_no_direct_messages.
  3. As one student, make a group with both students, then add the mentor with POST /api/groups/{id}/members/{mentorId}. As the mentor, GET /api/groups/chat/{id}/messages works. Have the other student leave, and the mentor gets 403 mentor_team_too_small.

A direct message that includes a mentor (ROLE_MENTOR and no teacher or
admin role) can't be started, sent or edited, whoever is sending it.
Reading old messages and deleting your own still work, so the navbar
bell and /dm keep answering. Refusals answer 403 with an error and a
reason (mentor_no_direct_messages) a page can show its own words for.

The rules live in MentorRules, a class of static methods the controllers
ask with the people they already loaded.
A mentor (ROLE_MENTOR and no teacher or admin role) reads, posts and
shares files only in a group they are a member of, that isn't a class
group and has at least 2 members who aren't mentors. Only a member who
isn't a mentor, or a teacher, adds or removes a mentor; a mentor adds
no one and may only leave. A new group may start with a mentor only if
its maker could have added them. Mentors can't list, search, make,
rename or delete groups, change group grades, join class groups or
read another person's groups.

Everyone else gets the answers they had before. Refusals answer 403
with a reason. Group responses mark each member as a mentor or not.

MentorAccess loads a group's members (one query, since open-in-view is
off) and whether it is a class group, and asks MentorRules.
after the mentor rules were written, so a mentor could list and
download any group's shared files. Both now ask MentorAccess like the
other group chat routes: a mentor gets them only for a group they
belong to, that isn't a class group and has at least 2 members who
aren't mentors. Everyone else gets the answers they had before.
…w the rules too

A review found three more ways around the mentor rules, and one rule
that was too strict.

- The older /api/chat/{groupId} keeps its own chat file per group. A
  mentor reads and posts there only where they may use the group's chat.
- The UI Builder's "message the owner" opened a direct message without
  the check, so it now refuses one that would include a mentor
  (mentor_no_direct_messages, 403).
- A mentor's own group list holds only the groups they may use, so no
  page offers one Spring would refuse, and they don't see its members.
- A mentor may now send an empty class list, to leave every class
  group. Any class is still refused.

The docs also no longer say a socket sendFile needs no sign-in: the
socket drops events without a signed-in identity, but it doesn't check
the mentor rules for one that has it.
A socket can carry a signed-in identity (a Spring /login session gives
the handshake its Principal), and GroupChatWebSocketController then ran
sendMessage, sendFile, joinGroup, typing and heartbeat for any group.
It now drops those events when the mentor rules refuse the signed-in
mentor that group, as the REST routes do. Leaving always works, and
everyone else's events work as before.
GET /api/chat/{groupId} had its membership check commented out, so any
signed-in account could read any group's older chat. It now answers 403
to anyone who isn't in the group, unless they're a teacher or admin.
Posting still needs membership, as before. No page reads this route.
svaka2000 added a commit to svaka2000/Spring-JJ that referenced this pull request Nov 26, 2026
…or's types

GroupChatSendTest built GroupChatApiController, GroupChatRealtimeService and
GroupChatReadService by naming their constructors. Open PRs add constructor
dependencies to the controller and the realtime service (Open-Coding-Society#302 a
MessageReactionService, Open-Coding-Society#303 a MentorAccess), so once one of them and this
test were both on master, the tests would stop compiling, with no merge
conflict to show it.

TestBeans builds a class through its biggest constructor: each parameter gets
the first object the test passes that fits it, and a Mockito mock otherwise.
The test then keeps compiling and running whatever dependencies those classes
gain.
POST /api/chat/{groupId} checked membership before the mentor rule, so a
mentor posting to a group they aren't in got a 403 with no reason. The
mentor rule now goes first and answers mentor_not_in_group; anyone else
outside the group still gets the plain 403.
A mentor's own group list already leaves out a group Spring refuses them
(a class group, or a team that dropped below 2 members who aren't
mentors). The unread count still counted those groups, and marking one
read is refused, so the bell kept saying "New messages in 1 group" with
no way to open or clear it.

MentorAccess.refusesGroup answers for one group id, and getUnread drops
every group it refuses. Everyone else's count is unchanged.
The mentor check loaded the person for every group event, typing and
heartbeats included, for everyone with a signed-in socket. A socket from
Spring's own pages carries the roles of its sign-in, so an event from
anyone that sign-in doesn't call a mentor now skips the lookup. A
mentor's events still load the person and the group, through the same
MentorAccess.refusesGroup the unread count uses.
…ommand

The Mentors section says the unread count leaves out a group the mentor
may not use, and that the socket reads the sign-in's roles first (a role
given since counts from the next sign-in). The test command in the doc
and AGENTS.md missed the socket and older-chat tests; *Mentor*Test runs
them all.
@svaka2000

Copy link
Copy Markdown
Contributor Author

Combined into #315 with the same commits.

@svaka2000 svaka2000 closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant