Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hub-discussions): user can read channel if group is non-discussable #1201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/discussions/src/utils/channel-permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ export class ChannelPermission {
return groupAccessControls.some((permission) => {
const group = userGroupsById[permission.key];

if (!group || !isGroupDiscussable(group)) {
return false;
if (action === ChannelAction.READ_POSTS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super minor, but the nested if on line 140 doesn't add a lot of value. I believe this could be simplified to just:

      if (action === ChannelAction.READ_POSTS) {
        if (!group) {
          return false;
        }
      } else if (!group || !isGroupDiscussable(group)) {
        return false;
      }

if (!group) {
return false;
}
} else {
if (!group || !isGroupDiscussable(group)) {
return false;
}
}

return (
Expand Down
27 changes: 25 additions & 2 deletions packages/discussions/test/utils/channel-permission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,29 @@ describe("ChannelPermission class", () => {

expect(channelPermission.canReadChannel(user)).toBe(false);
});

it("returns true if user logged in and in non-discussable group", async () => {
const user = buildUser({
groups: [buildGroup("groupND", "member", [CANNOT_DISCUSS])],
});
const channelAcl = [
{
category: AclCategory.GROUP,
subCategory: AclSubCategory.MEMBER,
key: "groupND",
role: Role.READ, // members write
},
{
category: AclCategory.GROUP,
subCategory: AclSubCategory.ADMIN,
key: "groupND",
role: Role.READ, // members write
},
] as IChannelAclPermission[];
const channelPermission = new ChannelPermission(channelAcl, "foo");

expect(channelPermission.canReadChannel(user)).toBe(true);
});
});

describe("Anonymous User Permissions", () => {
Expand Down Expand Up @@ -1766,7 +1789,7 @@ describe("ChannelPermission class", () => {
expect(channelPermission.canReadChannel(user)).toBe(true);
});

it("returns false if user is group member in permissions list but the group is not discussable", async () => {
it("returns true if user is group member in permissions list but the group is not discussable", async () => {
const user = buildUser({
orgId: orgId1,
groups: [
Expand All @@ -1790,7 +1813,7 @@ describe("ChannelPermission class", () => {

const channelPermission = new ChannelPermission(channelAcl, "foo");

expect(channelPermission.canReadChannel(user)).toBe(false);
expect(channelPermission.canReadChannel(user)).toBe(true);
});

it("returns false if user is group admin but group is not in permissions list", async () => {
Expand Down