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
Your rails app is like a club. Everyone wants in. Most users are fine, but every now and again someone wants to cause trouble.
5
4
@@ -8,73 +7,68 @@ You need a bouncer to keep the riff raff out.
8
7
Bouncer allows you to filter the params passed into your controller so that you can safely manage mass assignment at the controller level.
9
8
10
9
11
-
Example
12
-
=======
10
+
## Example
13
11
14
12
Take a user model with the following attributes: login, password, admin.
15
13
16
14
admin is a boolean that signifies whether the user has the ability to perform admin tasks.
17
15
18
16
You probably use the following code throughout your application in controllers.
19
17
20
-
<pre>
21
-
<code>
22
-
@user = User.new(params[:user])
23
-
</code>
24
-
</pre>
18
+
@user = User.new(params[:user])
19
+
25
20
26
21
This code is problematic. Should a user decide to fiddle with your parameters and pass in admin=true you could have a bit of a problem on your hands.
27
22
28
23
You need a bouncer with a door list.
29
24
30
-
<pre>
31
-
<code>
32
-
class UsersController < ApplicationController
33
-
allow_assignment :user => [ :login, :password ]
34
-
end
35
-
</code>
36
-
</pre>
25
+
class UsersController < ApplicationController
26
+
allow_params :user => [ :login, :password ]
27
+
end
37
28
38
-
Now you can safely use <code> User.new(params[:user])</code> in your controller to mass assign only the attributes you've said are safe.
29
+
Now you can safely use `User.new(params[:user])` in your controller to mass assign only the attributes you've said are safe.
39
30
40
-
You should use allow_assignment in every controller within your application. By default bouncer will strip everything from the params hash that isn't required by rails to operate.
31
+
You should use allow_params in every controller within your application. By default bouncer will strip everything from the params hash that isn't required by rails to operate.
41
32
42
-
Why Not Use attr_accessible?
43
-
==========================
33
+
# Why Not Use attr_accessible?
44
34
45
-
attr_accessible is great. But it's handled at the model level.
35
+
`attr_accessible` is great. But it's handled at the model level.
46
36
47
37
You might want to allow assignment of the admin attribute from an admin specific user interface.
48
38
49
39
You end up with code like this:
50
40
51
-
<pre>
52
-
<code>
53
-
class AdminUsersController < ApplicationController
41
+
class AdminUsersController < ApplicationController
54
42
55
-
...
56
-
@user = User.new(params[:user])
57
-
@user.admin = params[:user][:admin]
58
-
...
43
+
...
44
+
@user = User.new(params[:user])
45
+
@user.admin = params[:user][:admin]
46
+
...
59
47
60
-
end
61
-
</code>
62
-
</pre>
48
+
end
49
+
63
50
64
51
The following is much nicer:
65
52
66
-
<pre>
67
-
<code>
68
-
class UsersController < ApplicationController
69
-
allow_assignment :user => [ :login, :password ]
70
-
end
53
+
class UsersController < ApplicationController
54
+
allow_params :user => [ :login, :password ]
55
+
end
56
+
57
+
class AdminUsersController < ApplicationController
0 commit comments