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 wildcard permission (permission for all types) #98

Merged
merged 3 commits into from
Sep 7, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 67 additions & 0 deletions __tests__/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,73 @@ describe('Permissions', () => {
RethinkDB.db(db).table(PERMISSION_TABLE).delete().run(conn);
});

it('wildcard permissions', async () => {
const permission = await runQuery(`mutation createPermission {
createReindexPermission(input: {
clientMutationId: "",
ReindexPermission: {
read: true,
create: true,
update: true,
delete: true,
}
}) {
ReindexPermission {
id
}
}
}`, {
isAdmin: true,
});

const permissionId = permission
.data.createReindexPermission.ReindexPermission.id;

assert.deepEqual(permission, {
data: {
createReindexPermission: {
ReindexPermission: {
id: permissionId,
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a verbose check that doesn't really test all that much. You could simply do

assert.deepProperty(
  permission,
  'data.createReindexPermission.ReindexPermission.id'
);

for the same effect and less noise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

},
},
},
});

const id = toReindexID({
type: 'Micropost',
value: 'f2f7fb49-3581-4caa-b84b-e9489eb47d84',
});

assert.deepEqual(await runQuery(`{ node(id: "${id}") { id } }`), {
data: {
node: {
id,
},
},
});

assert.deepEqual(await runQuery(`mutation deletePermission {
deleteReindexPermission(input: {
clientMutationId: "",
id: "${permissionId}"
}) {
ReindexPermission {
id
}
}
}`, {
isAdmin: true,
}), {
data: {
deleteReindexPermission: {
ReindexPermission: {
id: permissionId,
},
},
},
});
});

it('node uses permissions properly', async () => {
const id = toReindexID({
type: 'Micropost',
Expand Down
9 changes: 9 additions & 0 deletions bin/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ async function createDatabase(dbName) {
interfaces: ['Node'],
}).run(conn);

await RethinkDB.db(dbName).table('ReindexPermissions').insert({
type: null,
user: null,
read: true,
create: true,
update: true,
delete: true,
}).run(conn);

return conn;
}

Expand Down
2 changes: 1 addition & 1 deletion graphQL/builtins/createPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function createPermission(interfaces, getTypeSet) {
resolve: createNodeFieldResolve('User', 'user'),
},
type: {
type: new GraphQLNonNull(getTypeSet('ReindexType').type),
type: getTypeSet('ReindexType').type,
resolve: createNodeFieldResolve('ReindexType', 'type'),
},
read: {
Expand Down
25 changes: 15 additions & 10 deletions graphQL/getGraphQLContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ function extractPermissions(permissions, types) {
.mapValues((value) => value[0].name)
.value();

const allTypesPermissions = permissions
.filter((permission) => !permission.type);

return chain(permissions)
.groupBy((permission) => typesByID[permission.type.value])
.mapValues((typePermissions) => chain(typePermissions)
.groupBy((permission) => (
permission.user ? permission.user.value : 'anonymous'
))
.mapValues((userPermissions) => (
userPermissions.reduce(combinePermissions)
), {})
.value())
.value();
.filter((permission) => permission.type)
.groupBy((permission) => typesByID[permission.type.value])
.mapValues((typePermissions) => typePermissions.concat(allTypesPermissions))
.mapValues((typePermissions) => chain(typePermissions)
.groupBy((permission) => (
permission.user ? permission.user.value : 'anonymous'
))
.mapValues((userPermissions) => (
userPermissions.reduce(combinePermissions, {})
))
.value())
.value();
Copy link
Member

Choose a reason for hiding this comment

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

Ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OB

}

function extractConnectionPermissions(types) {
Expand Down