You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.
A method that lets you restrict the allowed values for a given set of permitted params. If the value is not in the allowed list then the param will not make it into the strong params. Alternatively or in addition, an inverse method that only permits the param if the value is NOT in a forbidden list
API
#allow
params.require(:user).permit(:id, :role).allow(role: 'employee') # role is only permitted if the value is 'employee'
or
params.require(:user).permit(:id, :role).allow(role: ['employee', 'guest']) # also works with an array of values
#forbid
params.require(:user).permit(:id, :role).forbid(role: 'admin') # role is only permitted if the value is NOT 'employee'
params.require(:user).permit(:id, :role).forbid(role: ['admin', 'owner']) # also works with an array of values
Implementation
I think it's fairly lightweight and simple to implement. Without putting it through rigorous testing, something like below might work
class ActionController::Parameters
def allow(filters = {})
filters.each do |filter_key, filter_val|
delete_if do |params_key, params_val|
filter_key.to_s == params_key && !Array(filter_val).include?(params_val)
end
end
self
end
def forbid(filters = {})
filters.each do |filter_key, filter_val|
delete_if do |params_key, params_val|
filter_key.to_s == params_key && Array(filter_val).include?(params_val)
end
end
self
end
end
The text was updated successfully, but these errors were encountered:
The use case is more to filter params at the controller level based on the roles/permissions of the user submitting the request, not to supersede or compete with model validations. An attribute may be valid for the application, but you want to restrict the ability of a user to submit that particular value in mass assignment based on roles.
Proposal
A method that lets you restrict the allowed values for a given set of permitted params. If the value is not in the allowed list then the param will not make it into the strong params. Alternatively or in addition, an inverse method that only permits the param if the value is NOT in a forbidden list
API
#allow
or
#forbid
Implementation
I think it's fairly lightweight and simple to implement. Without putting it through rigorous testing, something like below might work
The text was updated successfully, but these errors were encountered: