Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Creates better way or returning data in a familiar format (#234)
Browse files Browse the repository at this point in the history
* Adds ability to verifyOrRestrict, populateOrRestrict, or hasRoleOrRestrict to either add a query restriction to the query params when the user is not authenticated or authorized or else leave the query unrestricted.

* Now filters direct id routes.

* Adds restriction hooks missing from git.

* Fixes jshint errors.

* Adds check to make sure restriction methods are only used for find and get methods.

* Fixes jshint errors.

* Fixes pagination in restriction hooks.

* Fixes pagination issue.
  • Loading branch information
codingfriend1 authored and ekryski committed Jul 15, 2016
1 parent c971985 commit b5a2c74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
24 changes: 10 additions & 14 deletions src/hooks/has-role-or-restrict.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ export default function(options = {}){
}

return this.find({ query }, params).then(results => {
if(results.length >= 1) {
if(hook.id !== undefined && hook.id !== null) {
hook.result = results[0];
} else {
hook.result = results;
}
if(hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
} else {
hook.result = results;
return hook;
}
throw new errors.NotFound(`No record found`);
}).catch(() => {
throw new errors.NotFound(`No record found`);
});
Expand Down Expand Up @@ -133,15 +131,13 @@ export default function(options = {}){
}

return this.find({ query }, params).then(results => {
if(results.length >= 1) {
if(hook.id !== undefined && hook.id !== null) {
hook.result = results[0];
} else {
hook.result = results;
}
if(hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
} else {
hook.result = results;
return hook;
}
throw new errors.NotFound(`No record found`);
}).catch(() => {
throw new errors.NotFound(`No record found`);
});
Expand Down
25 changes: 10 additions & 15 deletions src/hooks/populate-or-restrict.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ export default function(options = {}){
}

return this.find({ query }, params).then(results => {
if(results.length >= 1) {
if(hook.id !== undefined && hook.id !== null) {
hook.result = results[0];
} else {
hook.result = results;
}
if(hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
} else {
hook.result = results;
return hook;
}
throw new errors.NotFound(`No record found`);
}).catch(() => {
throw new errors.NotFound(`No record found`);
});
Expand Down Expand Up @@ -90,16 +88,13 @@ export default function(options = {}){
}

return this.find({ query }, params).then(results => {
if(results.length >= 1) {
if(hook.id !== undefined && hook.id !== null) {
hook.result = results[0];
} else {
hook.result = results;
}
if(hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
} else {
hook.result = results;
return hook;
}

throw new errors.NotFound(`No record found`);
}).catch(() => {
throw new errors.NotFound(`No record found`);
});
Expand Down
14 changes: 6 additions & 8 deletions src/hooks/verify-or-restrict.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function(options = {}){
if (hook.method !== 'find' && hook.method !== 'get') {
throw new Error(`'verifyOrRestrict' should only be used in a find or get hook.`);
}

// If it was an internal call then skip this hook
if (!hook.params.provider) {
return hook;
Expand Down Expand Up @@ -39,16 +40,13 @@ export default function(options = {}){
}

return this.find({ query }, params).then(results => {
if(results.length >= 1) {
if(hook.id !== undefined && hook.id !== null) {
hook.result = results[0];
} else {
hook.result = results;
}
if(hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
} else {
hook.result = results;
return hook;
}

throw new errors.NotFound(`No record found`);
}).catch(() => {
throw new errors.NotFound(`No record found`);
});
Expand Down

0 comments on commit b5a2c74

Please sign in to comment.