Skip to content

Groups and mentors: mentor chat rules, group member checks and group grade checks - #315

Open
svaka2000 wants to merge 63 commits into
Open-Coding-Society:masterfrom
svaka2000:combined/groups-and-mentors
Open

svaka2000 wants to merge 63 commits into
Open-Coding-Society:masterfrom
svaka2000:combined/groups-and-mentors

Conversation

@svaka2000

Copy link
Copy Markdown
Contributor

This puts four PRs in one, with the same commits: #303 (mentors), #309 (group member checks), #310 (who changes group grades) and #312 (who reads group grades). It merges after #314, the group chat PR, because it builds on that PR's reactions (#302). Until #314 merges, GitHub shows its commits here too.

Mentors (was #303)

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

  • No direct message that includes a mentor can be started, sent or edited. Reading old messages and deleting your own still work.
  • 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.
  • Only a teacher or admin, or a group member who isn't a mentor, adds or removes a mentor. A mentor can't add anyone and can only leave.
  • Mentors can't list, search, make, rename or delete groups, or read or change group grades.
  • The older /api/chat/{groupId}, the UI Builder's "message the owner" and group events from the chat socket follow the same rules.
  • A refusal is a 403 that names the rule, like mentor_no_direct_messages. Bulk group creation lists a refused group under errors instead.
  • Group responses mark each member "mentor": true or false, so a page can show a team's mentors.
  • Apart from that field and reading the older /api/chat/{groupId} (now only for the group's members, teachers and admins), this part changes nothing for requests that don't involve a mentor.

Group member checks (was #309)

  • A group's members, and any teacher or admin, can add and remove people, as the Members dialog on the Messages page already lets members do.
  • Anyone signed in can still join or leave a group that isn't a class group, except that a mentor can only leave.
  • Through POST and DELETE /api/groups/{id}/members/{personId}, only teachers and admins add or remove people in a class group. Students join and leave theirs through the classes on their Profile.
  • Changing a group's name, period or course (PUT /api/groups/{id}) is for teachers and admins. A class group keeps its name, a taken name gets 409, and no group name can contain "/".
  • Deleting a group is for admins only and never for a class group, the same rule the Messages page uses to show Delete.

Who changes group grades (was #310)

A group's grades are a list on the group (gradesJson), with one entry per assignment.

  • Replacing (PUT) or clearing (DELETE) the whole list is for teachers and admins.
  • Adding or replacing one assignment's entry (POST), which already copies it into each member's own grades, is for teachers, admins and whoever grades the group on that assignment: the assignment's creators, its assigned graders, and anyone assigned to grade the group's hand-in.

Who reads group grades (was #312)

  • GET /api/groups/{id}/grades returns the list only to the group's members, teachers, admins and anyone who grades one of the group's hand-ins. They see the whole list. Anyone else gets 403.
  • A mentor gets the mentor 403, even as a member.

On top of the group chat PR

Other notes:

How to try it:
Run ./mvnw test -Dtest='*Mentor*Test,GroupMembershipAccessTest,GroupGradesAccessTest'.

A send reads the group's whole messages file, adds the message and writes
the file back. decodeFile answers null for any failure, not only a missing
file, so when S3 timed out, was throttled or answered a 5xx, the send took
the chat for empty and wrote the one new message over the whole history. It
still answered 200 and broadcast the message.

The send now reads with a strict decodeFileOrThrow, like fileExistsOrThrow:
only S3's 404 means no messages yet. A timeout, no connection or a 5xx
throws MessagesReadFailedException, nothing is saved or broadcast, and
POST /api/groups/chat/{id}/messages answers 503 with Retry-After, the same
as a shared-file check that fails. S3 refusing the read (a 4xx) or a bug
here also writes nothing and gets the route's usual 400, since waiting
won't fix it. S3 failures to read are logged with their cause. Without S3
settings a send still broadcasts live, as before. decodeFile, edits and
deletes are unchanged.

Tests send through the controller to a real S3FileHandler over a stand-in
S3 client: a 503 and a timeout keep the stored file byte for byte and
answer 503, a 403 and a bug write nothing and get no retry, a missing file
starts the history, and a server without S3 still broadcasts. The two older
tests that stub S3 now stub the strict read a send makes.
Sending, editing and deleting a message each read the group's whole messages
file from S3, change it and write the file back, with no lock. Two at once
both read the same old copy, so the later write dropped the other's change:
two people sending at the same moment lost a message, and an edit or delete
during a send lost the edit or the new message. They now take one lock per
group, the same pattern as the shared-files metadata lock. Reads don't wait,
and other groups don't wait on each other.

The lock only works within one server. Several servers writing the same file
would need S3's conditional writes instead.

Tests run two sends, an edit and a send, and a delete and a send at once
against an S3 stub that holds each read until the other one has started.
The send route and the storage notes now say a send reads the messages
file with decodeFileOrThrow: S3 being unavailable answers 503 with
Retry-After and nothing is saved or broadcast, S3 refusing the read or a
bug gets the route's usual 400, and edits and deletes still read with
decodeFile. The testing list gets a bullet
for GroupChatSendTest and S3FileHandlerDecodeTest, with the command that
runs the S3 handler tests.
The storage notes said every send rewrites the file with no lock, and Known
issues listed two sends at once losing one. The note now says sending,
editing and deleting take one lock per group, within one server only. The
testing list names GroupChatServiceTest, and the known issue is gone.
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.
…HTML

The push text was the stored body with its whitespace collapsed, so a phone
showed what the page reads: [[reply:…]], [[files:…]], [[code:…]] and
[[pin:…]] markers, and the rich composer's HTML (<a href=…>, &nbsp;). Now
the markers are left out, the HTML becomes the text a reader sees, and a
message that was only a marker says what it did ("pinned a message",
"sent code", "shared files"). A marker's payload can't hold a bracket, so a
body full of unclosed "[[" is still matched in linear time.

notifyPerson runs inside the message's transaction and never throws, so if
the text can't be made from a body, the notification goes out without text
and the DM is still sent.
PermissionsPolicyTest checks permissions.json against every request the
controllers answer. The reaction routes for direct messages and group
chats go with the other /api/dm and /api/groups/chat requests: any
signed-in admin, student, teacher or user, as the filter chain answers
today.
DirectMessageTopicGuard already dropped a client's SEND to a DM topic.
It now drops a client's SEND to any destination the simple broker
handles (WebSocketBrokerConfig.BROKER_PREFIX, "/topic"), so group chat
topics get the same rule. Only the server publishes to topics: pages
send group messages over REST, and Spring's group page sends to
/app/groups.chat.

The tests cover SENDs to DM, group and other topic destinations being
dropped, and the server's own group broadcast still reaching its
subscribers.
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.
…se group ids

A remove read the row and then deleted it, so a second remove at the same
moment could fail. Removing one reaction and clearing a deleted message's
reactions are now single delete statements. The names of the people who
reacted come from one query for id, uid and name, instead of whole Person
rows that also loaded each person's eager links. A group message id has to
be a lowercase UUID, like the ids Spring saves, so an uppercase copy can't
match some lookups and miss others on MySQL.
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.
…in then

The frame was sent inside the change's transaction with the reactions read
there, so two people reacting at the same moment could each send a list
missing the other's reaction, and everyone in the chat would keep one of
them. It is now sent once the transaction commits, with the reactions read
in a transaction of their own. A group's reactions were already read after
their change had committed.
Spring still starts, as it does when the calls or calendar tables can't
be made, but the log now says reactions won't work instead of a warning
that is easy to miss.
The group chat fixes (Open-Coding-Society#296 to Open-Coding-Society#299) each add a line after the chat
regression tests, so the reactions line goes above it and the two
change different places.
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.
…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.
After the path check, the upload wrote the file with
S3FileHandler.uploadFile, which catches every failure and answers null. So
when S3 throttled the write, timed out or couldn't be reached, the upload got
a 500 "Upload failed", which reads as a bug in this server. Shared files now
write with uploadFileOrThrow, a strict version beside it, the way
fileExistsOrThrow sits beside fileExists. uploadFile is unchanged for the
other S3 features.

A timeout, no connection or a 5xx on the write now answers 503 with a
try-again message and Retry-After, like a failed check, and the file isn't
shared: no "shared by" record and no socket frame. S3 refusing the write (a
4xx such as no permission) or a bug here is still a 500 "Upload failed", and
every failed write is still logged.

S3's RequestTimeout counts as a timeout too, for the check and the write:
S3 sends it as a 400, but it means the connection was too slow, and the AWS
SDK retries it like a 5xx.

SharedFileCheckFailedException is now SharedFileStorageUnavailableException,
since a failed write throws it too, so the 503 is still answered in one
place. One check (isUnavailable) decides what counts as S3 being unavailable
for both the check and the write. The architecture doc says the same.

Tests cover a throttle, a timeout and a RequestTimeout on the write (503), an
S3 403, an S3 400 and a bug (500), sharing again once S3 is back, and
uploadFileOrThrow itself.
At every start, StartupService checks each group's messages file and
creates an empty one for a group that has none. The check used
fileExists, which reads any failure (a throttle, a 5xx, a timeout) as a
missing file, so a check that failed at boot wrote an empty file over
that group's whole history.

ensureGroupStorageExists now checks with fileExistsOrThrow: only S3's
404 creates the file, and any other failure throws before anything is
written. StartupService logs that group with the cause and goes on to
the next one, so the server still starts. The check holds the group's
message lock, so a message sent while the server is starting isn't
written over, and a server without S3 settings checks nothing
(S3FileHandler.isConfigured). The shared-files/ placeholder keeps the
lenient check: it is empty, so writing it again loses nothing.
S3's RequestTimeout comes as a 400, but it means the connection was too
slow, and the AWS SDK retries it like a 5xx. The shared-file check and write
already count it as S3 being unavailable (isUnavailable), but the send's
strict read had its own rule that only counted a 5xx, so a timed-out read got
the route's 400 instead of a 503 with Retry-After. The read now uses
isUnavailable too, so one check decides it for the whole group chat. The
history was kept either way: only the status was wrong.

The send test adds a RequestTimeout to the failures that answer 503.
The storage notes now say StartupService creates a group's empty
messages file and shared-files/ placeholder only when S3 answers 404
(fileExistsOrThrow). Any other failure to check is logged and leaves
that group as it is, the other groups are still checked, the check
holds the group's message lock, and a server without S3 settings
checks nothing. The testing list gets a bullet
for StartupServiceTest, with its command.
S3 can store a shared file and then fail to answer, and the AWS SDK's own
retries can fail too. The upload then answers 503 without a "shared by"
record or a sendFileServer frame, and sharing it again finds the path taken
(409), which the page handles like any other taken name. Nothing is lost,
and the old 500 ended the same way. Recognizing "this stored file is mine"
on a retry would need a way to match the stored copy to the upload, so for now
the doc and a comment at the write say what happens.
Sending, editing and deleting a message each read the group's messages file
and save it back. The save used S3FileHandler.uploadFile, which catches every
failure and answers null, so when S3 throttled, timed out or couldn't be
reached on the upload, the route still answered 200: a send showed live and
was gone after a reload, and an edit or a delete came back after one. Edits
and deletes also read with the lenient decodeFile, so a failed read answered
an edit with 404 and a delete with 200 (and told the group it was deleted).

All three now read with readMessagesOrThrow and save with a strict
writeMessagesOrThrow (uploadFileOrThrow). S3 being unavailable on either
(isUnavailable) throws MessagesStorageUnavailableException, the renamed
MessagesReadFailedException, and the send, edit and delete routes answer 503
with Retry-After. Nothing is broadcast. S3 refusing the request or a bug here
still gets the route's usual 400, and the strict read now logs every failure,
a bug included, the way the save does. Without S3 settings nothing is saved,
and the chat still works live.

A save that timed out may still be stored, so a send tried again can show up
twice. That's easier to see than a message lost quietly.

Tests: GroupChatSendTest adds an upload that fails after a good read, and
GroupChatEditDeleteTest covers edits and deletes whose read or upload fails,
that both still save when S3 is up, and that every failed read or upload is
logged. The S3 failure lists move to
S3Failures. GroupChatServiceTest, GroupChatMessageJsonTest and
GroupChatRegressionTest now stand in for a configured S3 through the strict
read and write; the regression test saves each upload back into its list, so
its checks on what's stored can still fail.
The send, edit and delete routes now list their 503 with Retry-After, and
that a save which timed out may still be stored. The
storage note says all three read and save the messages file strictly
(MessagesStorageUnavailableException), a new note says a save that timed out
may still be stored, so a send tried again can show up twice, and the testing
list names GroupChatEditDeleteTest.
…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.
Adding or removing a member of an existing group now needs the caller
to be one of the group's members, a teacher or an admin. Outside the
mentor rules, anyone may still join or leave a group that isn't a
class group. Through the member routes only teachers and admins add or
remove people in a class group; students join and leave theirs through
the classes on their Profile. Changing a group's name, period or
course is for teachers and admins. A class group keeps its name, and a
new name can't be another group's or a class group's name. No group
name, made or renamed, can contain "/". Deleting is admin-only and
never a class group, the same rule the Messages page uses to show
Delete.
Replacing or clearing a group's grades (PUT and DELETE
/api/groups/{id}/grades) is for teachers and admins, since that
changes every assignment's entry at once. Adding one assignment's
entry (POST), which also copies it into each member's own grades, is
for them and for whoever grades the group on that assignment: its
creators and graders, and the graders of the group's submission to
it. Mentors still get the mentor refusal first.
Reading a group's grades (GET /api/groups/{id}/grades) is now for its
members, teachers and admins, and whoever grades one of its
submissions, the same idea as the submission listings. Mentors still
get the mentor refusal first.
… of wiping the history when S3 can't read it
…a chat when S3 fails during the start-up check
… message with an emoji

Where it meets the group chat fixes merged above:
- GroupChatRealtimeService: a delete removes the message's reactions only after the chat
  says it deleted the message, so deleting an id that isn't saved (404) changes nothing.
- GroupChatApiController: keeps both the 503 helper and the reaction routes.
- docs: keeps the send, edit and delete rows with their 503 and 404 notes, plus the three
  reaction rows.
- GroupChatReactionTest: the delete test now stubs the chat's delete, which reads S3 itself
  instead of calling getMessages, and a new test checks that a 404 delete keeps the reactions.
…y /topic destination

docs: Open-Coding-Society#295's rewrite of the WebSocket testing bullet, with Open-Coding-Society#302's reactions bullet kept
after it.
…the groups they're added to

Open-Coding-Society#302 (reactions, already here) and Open-Coding-Society#303 each add one constructor
argument to GroupChatApiController. Both stay, and the tests that build
the controller, the group chat one and the direct message one pass both.
A group's reactions (reading them, adding one, taking one back) use the
same mentor check as its messages. In a direct message that includes a
mentor, adding or taking back a reaction is refused like sending, and
reading reactions still works like reading messages.
…e removed

By the time the reactions are removed, the message is already gone from S3. A database error
there answered an error and told no one, and a retry found nothing to delete (404). Now the
error is logged, the group still gets deleteMessageServer, and the leftover reactions are never
shown.
…be removed

DirectMessageService.deleteMessage removed the message's reactions inside its own transaction,
so any error from the reactions table, such as a missing message_reaction table under
ddl-auto=none, rolled back the delete and sent no "deleted" frame. The reactions are now removed
after the delete commits, and a failure there is logged. removeAllFor runs in a transaction of
its own (REQUIRES_NEW), since Spring runs a transactional call made from afterCommit inside the
finished transaction, where it would never commit. MessageReactionTableInitializer's comment now
says what breaks without the table.
…no longer rolls back when its reactions can't be removed
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