Skip to content

Commit

Permalink
Merge pull request #98 from reindexio/graphql/wildcard-permissions
Browse files Browse the repository at this point in the history
Add wildcard permission (permission for all types)
  • Loading branch information
freiksenet committed Sep 7, 2015
2 parents 3e3b327 + 4658b15 commit 282305a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
54 changes: 54 additions & 0 deletions __tests__/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,60 @@ 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.deepProperty(
permission,
'data.createReindexPermission.ReindexPermission.id'
);

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

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

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

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) => chain(typePermissions)
.concat(allTypesPermissions)
.groupBy((permission) => (
permission.user ? permission.user.value : 'anonymous'
))
.mapValues((userPermissions) => (
userPermissions.reduce(combinePermissions, {})
))
.value())
.value();
}

function extractConnectionPermissions(types) {
Expand Down

0 comments on commit 282305a

Please sign in to comment.