-
Notifications
You must be signed in to change notification settings - Fork 10
Fixes #9 include all attributes when no roles are specified to get #11
base: master
Are you sure you want to change the base?
Changes from 3 commits
cb79593
cbf3814
8e8c575
044a156
8e4508f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,28 +10,44 @@ init = function(target) { | |
| Object.keys(target.rawAttributes).forEach(function (attr) { | ||
| if (target.rawAttributes[attr].roles !== undefined) { | ||
| if (target.rawAttributes[attr].roles === false) { | ||
| target.rawAttributes[attr].roles = {}; | ||
| return; | ||
| return; | ||
| } | ||
|
|
||
| Object.keys(target.rawAttributes[attr].roles).forEach(function (role) { | ||
| if (typeof target.rawAttributes[attr].roles[role] === "boolean") { | ||
| target.rawAttributes[attr].roles[role] = { | ||
| set: target.rawAttributes[attr].roles[role], | ||
| get: target.rawAttributes[attr].roles[role] | ||
| }; | ||
| Object.keys(target.rawAttributes[attr].roles).forEach(function (roleName) { | ||
| var role = target.rawAttributes[attr].roles[roleName]; | ||
|
|
||
| if (roleName === 'default') { | ||
| if(typeof role === "string"){ | ||
| role = target.rawAttributes[attr].roles.default = target.rawAttributes[attr].roles[role]; | ||
| } | ||
| } | ||
|
|
||
| if (typeof role === "boolean") { | ||
| target.rawAttributes[attr].roles[roleName] = { set: role, get: role }; | ||
| return; | ||
| } | ||
|
|
||
| if (target.rawAttributes[attr].roles[role].set === undefined) { | ||
| target.rawAttributes[attr].roles[role].set = false; | ||
| if (role.set === undefined) { | ||
| role.set = true; | ||
| } | ||
| if (target.rawAttributes[attr].roles[role].get === undefined) { | ||
| target.rawAttributes[attr].roles[role].get = false; | ||
| if (role.get === undefined) { | ||
| role.get = true; | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| var accessGranted = function(attr, options){ | ||
| return !attr | ||
| // If no role is given apply default if any | ||
| || !options.role && (typeof attr.roles !== 'object' || (attr.roles.default||{}).get !== false) | ||
| // If no roles defined in attribute or set to true | ||
| || attr.roles === undefined || attr.roles === true | ||
| // If role is given but not defined in attribute apply default if any | ||
| || options.role && (typeof attr.roles === 'object' && attr.roles[options.role] === undefined && (attr.roles.default||{}).get !== false) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this case should rather throw, i don't like silently failing.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by silently failing?. Not returning boolean but rising an error instead?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @i don't think that applying the default role for a "bad" role is a good default.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean that if I try to save some attribute defaulted to set: false, I'll get an error?. In my particular case the client could send all attributes and I need just omit those protected ones. I think throwing is obtrusive (at least in my case) I would be forced to enclose every operation in try-catch block when I just need to be careful when assigning roles. Maybe this should be configurable don't you think?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid configuration on call-site at all costs in favour of reasonable convention. What i mean is that if you call |
||
| // Apply given role if defined in attribute | ||
| || (attr.roles[options.role]||{}).get === true; | ||
| }; | ||
|
|
||
| target.Instance.prototype.get = function(key, options) { | ||
| if (typeof key === "object" && !options) { | ||
|
|
@@ -43,12 +59,11 @@ init = function(target) { | |
| options = {}; | ||
| } | ||
|
|
||
| if (options.raw === true) { | ||
| return $get.call(this, key, options); | ||
| } | ||
| if (key !== undefined) { | ||
| var attr = target.rawAttributes[key]; | ||
| if (!attr || !attr.roles || attr.roles && attr.roles[options.role] && attr.roles[options.role].get) { | ||
|
|
||
| if (accessGranted(attr, options)) { | ||
|
|
||
| return $get.call(this, key, options); | ||
| } else { | ||
| return undefined; | ||
|
|
@@ -57,15 +72,16 @@ init = function(target) { | |
|
|
||
| var values = $get.call(this, options) | ||
| , response = {}; | ||
|
|
||
| Object.keys(values).forEach(function (key) { | ||
| var attr = target.rawAttributes[key]; | ||
|
|
||
| var val = values[key]; | ||
| if(!attr && val instanceof Sequelize.Instance) { | ||
| response[key] = val.get.call(val, options); | ||
| } | ||
| else if (!attr || !attr.roles || attr.roles && attr.roles[options.role] && attr.roles[options.role].get) { | ||
| else if ( accessGranted(attr, options) ) { | ||
|
|
||
| response[key] = val; | ||
| } | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accessGrantedForGetor accept amethodargument