I'm having a hard time understanding strong params. I understand it prevents mass assignment of variables you don't permit. But in Hartl's tutorial I also read that without strong params someone could change for example any user's admin status through a patch request (which I guess isn't mass assignment, because that's just one value your would change). But then how do you implement strong params for variables that:
- Should only be allowed to be set once (when creating a new user)
- Some users should be able to change but others not
For example, I have:
private
def user_params
params.require(:user).permit(:email,
:username,
#:verified,
#:admin,
#:moderator,
#:activated,
#:activated_at,
:password,
:password_confirmation)
end
Now, the ones with a dash I understand should NOT be permitted. Otherwise users could change their values through mass assignment (or otherwise).
However:
- An admin user (which is a specific user from the same table/controller) should be able to change these variables for all users.
- In the case of my app, organizations (a different table) should be able to give a user moderator rights and thus change these values for users.
- Username should only be set when a new user is created and after that should never be permitted to change. Now, by permitting username in strong_params doesn't that mean it is vulnerable to be changed through mass assignment?
How does strong params relate to these issues?