-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hub-discussions): add post util functions canCreatePost and canC…
…reateReply affects: @esri/hub-discussions
- Loading branch information
1 parent
f0bba80
commit 7df0ada
Showing
9 changed files
with
1,074 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { IGroup, IUser } from "@esri/arcgis-rest-types"; | ||
import { IChannel, IDiscussionsUser, SharingAccess } from "../../types"; | ||
import { ChannelPermission } from "../channel-permission"; | ||
import { CANNOT_DISCUSS } from "../constants"; | ||
import { hasOrgAdminUpdateRights } from "../portal-privilege"; | ||
|
||
const ALLOWED_GROUP_ROLES = Object.freeze(["owner", "admin", "member"]); | ||
|
||
type ILegacyChannelPermissions = Pick< | ||
IChannel, | ||
"groups" | "orgs" | "access" | "allowAnonymous" | ||
>; | ||
|
||
/** | ||
* Utility to determine if User has privileges to create a post in a channel | ||
* @param channel | ||
* @param user | ||
* @returns {boolean} | ||
*/ | ||
export function canCreatePost( | ||
channel: IChannel, | ||
user: IUser | IDiscussionsUser = {} | ||
): boolean { | ||
const { access, groups, orgs, allowAnonymous, allowPost } = channel; | ||
|
||
if (hasOrgAdminUpdateRights(user, channel.orgId)) { | ||
return true; | ||
} | ||
|
||
if (!allowPost) { | ||
return false; | ||
} | ||
|
||
if (channel.channelAcl) { | ||
const channelPermission = new ChannelPermission(channel); | ||
return channelPermission.canPostToChannel(user as IDiscussionsUser); | ||
} | ||
|
||
// Once channelAcl usage is enforced, we will remove authorization by legacy permissions | ||
return isAuthorizedToPostByLegacyPermissions(user, { | ||
access, | ||
groups, | ||
orgs, | ||
allowAnonymous, | ||
}); | ||
} | ||
|
||
function isAuthorizedToPostByLegacyPermissions( | ||
user: IUser | IDiscussionsUser, | ||
channelParams: ILegacyChannelPermissions | ||
): boolean { | ||
const { username, groups: userGroups, orgId: userOrgId } = user; | ||
const { allowAnonymous, access, groups, orgs } = channelParams; | ||
|
||
// order is important here | ||
if (allowAnonymous === true) { | ||
return true; | ||
} | ||
|
||
if (!username) { | ||
return false; | ||
} | ||
|
||
if (access === SharingAccess.PUBLIC) { | ||
return true; | ||
} | ||
|
||
if (access === SharingAccess.PRIVATE) { | ||
return isAuthorizedToPostByLegacyGroup(groups, userGroups); | ||
} | ||
|
||
if (access === SharingAccess.ORG) { | ||
return orgs.includes(userOrgId); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isAuthorizedToPostByLegacyGroup( | ||
channelGroups: string[] = [], | ||
userGroups: IGroup[] = [] | ||
) { | ||
return channelGroups.some((channelGroupId: string) => { | ||
return userGroups.some((group: IGroup) => { | ||
const { | ||
id: userGroupId, | ||
userMembership: { memberType: userMemberType }, | ||
typeKeywords, | ||
} = group; | ||
|
||
return ( | ||
channelGroupId === userGroupId && | ||
ALLOWED_GROUP_ROLES.includes(userMemberType) && | ||
!typeKeywords.includes(CANNOT_DISCUSS) | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { IGroup, IUser } from "@esri/arcgis-rest-types"; | ||
import { IChannel, IDiscussionsUser, SharingAccess } from "../../types"; | ||
import { ChannelPermission } from "../channel-permission"; | ||
import { CANNOT_DISCUSS } from "../constants"; | ||
import { hasOrgAdminUpdateRights } from "../portal-privilege"; | ||
|
||
const ALLOWED_GROUP_ROLES = Object.freeze(["owner", "admin", "member"]); | ||
|
||
type ILegacyChannelPermissions = Pick< | ||
IChannel, | ||
"groups" | "orgs" | "access" | "allowAnonymous" | ||
>; | ||
|
||
/** | ||
* Utility to determine if User has privileges to create a post in a channel | ||
* @param channel | ||
* @param user | ||
* @returns {boolean} | ||
*/ | ||
export function canCreateReply( | ||
channel: IChannel, | ||
user: IUser | IDiscussionsUser = {} | ||
): boolean { | ||
const { access, groups, orgs, allowAnonymous, allowReply } = channel; | ||
|
||
if (hasOrgAdminUpdateRights(user, channel.orgId)) { | ||
return true; | ||
} | ||
|
||
if (!allowReply) { | ||
return false; | ||
} | ||
|
||
if (channel.channelAcl) { | ||
const channelPermission = new ChannelPermission(channel); | ||
return channelPermission.canPostToChannel(user as IDiscussionsUser); | ||
} | ||
|
||
// Once channelAcl usage is enforced, we will remove authorization by legacy permissions | ||
return isAuthorizedToPostByLegacyPermissions(user, { | ||
access, | ||
groups, | ||
orgs, | ||
allowAnonymous, | ||
}); | ||
} | ||
|
||
function isAuthorizedToPostByLegacyPermissions( | ||
user: IUser | IDiscussionsUser, | ||
channelParams: ILegacyChannelPermissions | ||
): boolean { | ||
const { username, groups: userGroups, orgId: userOrgId } = user; | ||
const { allowAnonymous, access, groups, orgs } = channelParams; | ||
|
||
// order is important here | ||
if (allowAnonymous === true) { | ||
return true; | ||
} | ||
|
||
if (!username) { | ||
return false; | ||
} | ||
|
||
if (access === SharingAccess.PUBLIC) { | ||
return true; | ||
} | ||
|
||
if (access === SharingAccess.PRIVATE) { | ||
return isAuthorizedToPostByLegacyGroup(groups, userGroups); | ||
} | ||
|
||
if (access === SharingAccess.ORG) { | ||
return orgs.includes(userOrgId); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isAuthorizedToPostByLegacyGroup( | ||
channelGroups: string[] = [], | ||
userGroups: IGroup[] = [] | ||
) { | ||
return channelGroups.some((channelGroupId: string) => { | ||
return userGroups.some((group: IGroup) => { | ||
const { | ||
id: userGroupId, | ||
userMembership: { memberType: userMemberType }, | ||
typeKeywords, | ||
} = group; | ||
|
||
return ( | ||
channelGroupId === userGroupId && | ||
ALLOWED_GROUP_ROLES.includes(userMemberType) && | ||
!typeKeywords.includes(CANNOT_DISCUSS) | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.