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

Add expiration for space members and apply new permission structure #8320

Merged
merged 11 commits into from
Jan 31, 2023
1 change: 1 addition & 0 deletions packages/web-client/src/helpers/resource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { User } from '../user'
export interface SpaceRole {
id: string
displayName: string
expirationDate: string
kind: 'user' | 'group'
isMember(u: User): boolean
}
Expand Down
4 changes: 3 additions & 1 deletion packages/web-client/src/helpers/share/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export function buildSpaceShare(s, storageId): Share {
additionalInfo: null
},
permissions,
role
role,
expiration: s.expirationDate,
expires: s.expirationDate
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
}
}
47 changes: 23 additions & 24 deletions packages/web-client/src/helpers/space/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,32 @@ export function buildSpace(data): SpaceResource {

if (data.root?.permissions) {
for (const permission of data.root.permissions) {
for (const role of SpacePeopleShareRoles.list()) {
if (permission.roles.includes(role.name)) {
spaceRoles[role.name] = permission.grantedToIdentities.reduce((acc, info) => {
const kind = info.hasOwnProperty('group') ? 'group' : 'user'
const spaceRole: SpaceRole = {
kind,
id: info[kind].id,
displayName: info[kind].displayName,
isMember(u?: any): boolean {
if (!u) {
return false
}
spaceRoles[permission.roles[0]].push(
...permission.grantedToIdentities.reduce((acc, info) => {
const kind = info.hasOwnProperty('group') ? 'group' : 'user'
const spaceRole: SpaceRole = {
kind,
id: info[kind].id,
displayName: info[kind].displayName,
expirationDate: permission.expirationDateTime,
isMember(u?: any): boolean {
if (!u) {
return false
}

switch (this.kind) {
case 'user':
return this.id == u.uuid
case 'group':
return u.groups.map((g) => g.id).includes(this.id)
default:
return false
}
switch (this.kind) {
case 'user':
return this.id == u.uuid
case 'group':
return u.groups.map((g) => g.id).includes(this.id)
default:
return false
}
}
return [...acc, spaceRole]
}, [])
}
}
}
return [...acc, spaceRole]
}, [])
)
}

if (data.root?.deleted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('buildSpace', () => {
])('returns true for a viewer of the space', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
permissions: [{ roles: [data.role], grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.isViewer(mock<User>({ uuid }))).toBe(data.expectedResult)
Expand All @@ -29,7 +29,7 @@ describe('buildSpace', () => {
])('returns true for a editor of the space', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
permissions: [{ roles: [data.role], grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.isEditor(mock<User>({ uuid }))).toBe(data.expectedResult)
Expand All @@ -44,7 +44,7 @@ describe('buildSpace', () => {
])('returns true for a manager of the space', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
permissions: [{ roles: [data.role], grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.isManager(mock<User>({ uuid }))).toBe(data.expectedResult)
Expand Down
27 changes: 21 additions & 6 deletions packages/web-runtime/src/store/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const actions = {
const spaceShares = []

for (const role of Object.keys(space.spaceRoles)) {
for (const { kind, id } of space.spaceRoles[role]) {
for (const { kind, id, expirationDate } of space.spaceRoles[role]) {
const client = unref(graphClient)
let prom: Promise<AxiosResponse>
switch (kind) {
Expand All @@ -161,7 +161,7 @@ const actions = {
}

prom.then((resolved) => {
spaceShares.push(buildSpaceShare({ ...resolved.data, role }, space.id))
spaceShares.push(buildSpaceShare({ ...resolved.data, role, expirationDate }, space.id))
})

promises.push(prom)
Expand All @@ -174,25 +174,40 @@ const actions = {
},
async addSpaceMember(
context,
{ client, graphClient, path, shareWith, permissions, role, storageId, displayName }
{
client,
graphClient,
path,
shareWith,
permissions,
role,
storageId,
displayName,
expirationDate
}
) {
await client.shares.shareSpaceWithUser(path, shareWith, storageId, {
permissions,
role: role.name
role: role.name,
expirationDate
})
const graphResponse = await graphClient.drives.getDrive(storageId)
context.commit('UPSERT_SPACE', buildSpace(graphResponse.data))
const shareObj = { role: role.name, onPremisesSamAccountName: shareWith, displayName }
context.commit('UPSERT_SPACE_MEMBERS', buildSpaceShare(shareObj, storageId))
},
async changeSpaceMember(context, { client, graphClient, share, permissions, role }) {
async changeSpaceMember(
context,
{ client, graphClient, share, permissions, expirationDate, role }
) {
await client.shares.shareSpaceWithUser(
'',
share.collaborator.name || share.collaborator.displayName,
share.id,
{
permissions,
role: role.name
role: role.name,
expirationDate
}
)

Expand Down