auth-assistant 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. data/.DS_Store +0 -0
  2. data/.document +5 -0
  3. data/.gitignore +39 -0
  4. data/Changelog.txt +30 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +308 -0
  7. data/Rakefile +20 -0
  8. data/VERSION +1 -0
  9. data/app/.DS_Store +0 -0
  10. data/app/views/.DS_Store +0 -0
  11. data/app/views/auth_assist/menu/_admin_login_items.html.erb +11 -0
  12. data/app/views/auth_assist/menu/_login_items.html.erb +11 -0
  13. data/app/views/auth_assist/menu/_registration_items.html.erb +10 -0
  14. data/auth-assistant.gemspec +115 -0
  15. data/config/locales/en.yml +14 -0
  16. data/init.rb +1 -0
  17. data/lib/.DS_Store +0 -0
  18. data/lib/auth-assistant.rb +24 -0
  19. data/lib/auth_assistant/configure.rb +16 -0
  20. data/lib/auth_assistant/helpers/admin_role.rb +59 -0
  21. data/lib/auth_assistant/helpers/all.rb +4 -0
  22. data/lib/auth_assistant/helpers/localhost.rb +22 -0
  23. data/lib/auth_assistant/helpers/roles.rb +52 -0
  24. data/lib/auth_assistant/helpers/user_role.rb +47 -0
  25. data/lib/auth_assistant/model/user_config.rb +42 -0
  26. data/lib/auth_assistant/role_strategies/admin_field.rb +37 -0
  27. data/lib/auth_assistant/role_strategies/all.rb +7 -0
  28. data/lib/auth_assistant/role_strategies/multi_role_assignment.rb +34 -0
  29. data/lib/auth_assistant/role_strategies/role_assignment.rb +41 -0
  30. data/lib/auth_assistant/role_strategies/role_field.rb +32 -0
  31. data/lib/auth_assistant/role_strategies/roles_field.rb +31 -0
  32. data/lib/auth_assistant/role_strategies/roles_mask.rb +35 -0
  33. data/lib/auth_assistant/role_strategies/shared.rb +25 -0
  34. data/lib/auth_assistant/translate/authlabels.rb +23 -0
  35. data/lib/auth_assistant/view/all.rb +4 -0
  36. data/lib/auth_assistant/view/auth_menu_item.rb +27 -0
  37. data/lib/auth_assistant/view/registration_link.rb +30 -0
  38. data/lib/auth_assistant/view/rest_link.rb +70 -0
  39. data/lib/auth_assistant/view/session_link.rb +31 -0
  40. data/lib/generators/.DS_Store +0 -0
  41. data/lib/generators/auth_assist/.DS_Store +0 -0
  42. data/lib/generators/auth_assist/clear/clear_generator.rb +30 -0
  43. data/lib/generators/auth_assist/config/.DS_Store +0 -0
  44. data/lib/generators/auth_assist/config/config_generator.rb +72 -0
  45. data/lib/generators/auth_assist/templates/ability.rb +22 -0
  46. data/lib/generators/auth_assist/templates/auth_assistant.rb +6 -0
  47. data/lib/generators/auth_assist/templates/permits.rb +91 -0
  48. data/lib/generators/auth_assist/templates/remove_multi_role_assignments_migration.rb +24 -0
  49. data/lib/generators/auth_assist/templates/remove_role_assignments_migration.rb +17 -0
  50. data/lib/generators/auth_assist/templates/role_assignments_migration.rb +14 -0
  51. data/lib/generators/auth_assist/templates/roles_migration.rb +13 -0
  52. data/lib/generators/auth_assist/test.rb +40 -0
  53. data/lib/generators/auth_assist/views/views_generator.rb +66 -0
  54. data/lib/generators/auth_code_refactor.rb +71 -0
  55. data/lib/generators/migration_helper.rb +81 -0
  56. data/lib/generators/reverse_migrations.rb +48 -0
  57. data/lib/generators/role_migrations.rb +167 -0
  58. data/lib/permits.rb +92 -0
  59. data/spec/auth-assistant_spec.rb +7 -0
  60. data/spec/generators/ability_gen_spec.rb +9 -0
  61. data/spec/sandbox.rb +9 -0
  62. data/spec/spec.opts +1 -0
  63. data/spec/spec_helper.rb +11 -0
  64. metadata +167 -0
Binary file
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,39 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # jeweler generated
12
+ pkg
13
+
14
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
15
+ #
16
+ # * Create a file at ~/.gitignore
17
+ # * Include files you want ignored
18
+ # * Run: git config --global core.excludesfile ~/.gitignore
19
+ #
20
+ # After doing this, these files will be ignored in all your git projects,
21
+ # saving you from having to 'pollute' every project you touch with them
22
+ #
23
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
24
+ #
25
+ # For MacOS:
26
+ #
27
+ #.DS_Store
28
+ #
29
+ # For TextMate
30
+ #*.tmproj
31
+ #tmtags
32
+ #
33
+ # For emacs:
34
+ #*~
35
+ #\#*
36
+ #.\#*
37
+ #
38
+ # For vim:
39
+ #*.swp
@@ -0,0 +1,30 @@
1
+ May 18, 2010
2
+ ---
3
+ v.0.4.0
4
+
5
+ CanCan 1.1 support http://wiki.github.com/ryanb/cancan/upgrading-to-11
6
+
7
+ General:
8
+ * Configuration now done through AuthAssistant::Configuration, a singleton
9
+ * Fixed various bugs
10
+ * Tested that generators work for all strategies and that rails can start up after any generation!
11
+
12
+ Generators:
13
+ * Fixed various bugs
14
+ * Completed generators refactoring to ensure more elegant/generic and enhanceable generators solution
15
+ * Added generator strategy classes for each strategy with API for generation and reversal of the strategy
16
+
17
+ Permits:
18
+ * Now uses the new hash option for Permit 'ownership' (see owns function in Permit::Base)
19
+ * Added request as second argument to Ability and Permits permit? method to enable construction of more advanced permits (1)
20
+
21
+ (1) See 'request' example in http://wiki.github.com/ryanb/cancan/defining-abilities-with-blocks)
22
+
23
+ May 16, 2010
24
+ ---
25
+ Support for standard roles
26
+ * admin (all)
27
+ * user (all)
28
+ * guest (default nil user)
29
+
30
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,308 @@
1
+ # auth-assistant #
2
+
3
+ Provides assistance for setting up an auth solution using devise and cancan auth frameworks.
4
+
5
+ ## Installation and configuration ##
6
+
7
+ This gem has been designed for Rails 3 only.
8
+
9
+ Insert `gem 'auth-assistant'` in your Gemfile
10
+ Run `$ bundle install`
11
+
12
+ The gem should automatically configure itself with Rails after you run the `config` generator (see below).
13
+
14
+ `$ rails g auth_assist:config NAME` where NAME is the name of the role strategy to be used.
15
+
16
+ To make the extra authentication view helpers accessible from your views
17
+
18
+ # app/helpers/application_helper.rb
19
+
20
+ module ApplicationHelper
21
+ auth_assist_helpers
22
+ end
23
+
24
+ ## Permits ##
25
+
26
+ Authorization is setup by designing permits for each can of role to do certain actions.
27
+ The config generator generates a default permits.rb file in /lib
28
+
29
+ Please see "cancan 1.1 wiki":http://wiki.github.com/ryanb/cancan/upgrading-to-11 for more options
30
+ you can use in designing your Permits. The 'owns' convenience method provided, now uses the new hash option so it
31
+ is also available in the controller using fx:
32
+
33
+ `Book.accessible_by(current_ability)`
34
+
35
+ Example:
36
+ <pre>
37
+ module RolePermit
38
+ class Moderator
39
+ def initialize(ability)
40
+ super
41
+ end
42
+
43
+ def permit?(user)
44
+ super
45
+ return if !user.role?(:moderator)
46
+ can :read, :all
47
+ # can manage comment instance if 'user' field on instance points to this user, marking ownership
48
+ user.owns(Comment)
49
+
50
+ # override default 'user_id' field to use 'owner' as foreign key to user.id
51
+ user.owns(Book, :author)
52
+ end
53
+ end
54
+ end
55
+ </pre>
56
+
57
+ ## View helpers ##
58
+
59
+ Currently the view helpers only target use with devise and cancan.
60
+ The default labels are always loaded from the `auth_assist` locale file, which is generated by the `config` generator.
61
+
62
+ ### Rest link helpers ###
63
+
64
+ Display a link (anchor tag) for a given object only if the current user has permission to execute that action.
65
+
66
+ * show_link or read_link
67
+ * edit_link or update_link
68
+ * create_link or new_link
69
+ * destroy_link or delete_link
70
+
71
+ Each Rest helper method takes an object for which to create the link. Optionally provide a label as the second argument.
72
+
73
+ Example usage:
74
+
75
+ <%= create_link project %>
76
+ <%= create_link project, 'Create new project' %>
77
+
78
+ ### Session link helpers ###
79
+
80
+ Show links for performing user authentication and registration actions
81
+
82
+ * log_out_link or sign_out_link
83
+ * log_in_link or sign_in_link
84
+
85
+ Each of these methods take an optional options hash.
86
+ If no role option given, they default to create link for basic 'user' role.
87
+
88
+ Example usage:
89
+
90
+ <%= log_out_link %>
91
+ <%= log_out_link :label => 'Log me out' %>
92
+ <%= log_out_link :role => 'admin', :label => 'Log me out' %>
93
+
94
+
95
+ ### Registration link helpers ###
96
+
97
+ Show links for performing user authentication and registration actions
98
+
99
+ * register_link or sign_up_link
100
+ * edit_profile_link or edit_registration_link
101
+
102
+ Each of these methods take an optional options hash.
103
+ If no role option given, they default to create link for basic 'user' role.
104
+
105
+ Example usage:
106
+
107
+ <%= register_link %>
108
+ <%= register_link :label => 'Register me' %>
109
+ <%= register_link :role => 'admin', :label => 'Register me' %>
110
+
111
+ ### Registration Menu item helpers ###
112
+
113
+ Show menu links for registration conditionally
114
+
115
+ * edit_user_menu_item or edit_registration_menu_item
116
+ * register_menu_item or sign_up_menu_item
117
+
118
+ 1) only shown if user is currently logged in
119
+ 2) only shown if user is NOT currently logged in (and hence already registered)
120
+
121
+ Example usage:
122
+
123
+ ul.menu
124
+ <%= register_menu_item %>
125
+
126
+ ### Session Menu item helpers ###
127
+
128
+ Show menu links for session operations conditionally
129
+
130
+ * logout_menu_item or sign_out_menu_item
131
+ * login_menu_item or sign_in_menu_item
132
+
133
+ 1) only shown if user is currently logged in
134
+ 2) only shown if user is NOT currently logged in
135
+
136
+ ul.menu
137
+ <%= login_menu_item %>
138
+ <%= logout_menu_item %>
139
+
140
+
141
+ ## Block helpers ##
142
+
143
+ Execute block if user is logged in (or not logged in)
144
+ * user_block
145
+ * not_user_block
146
+
147
+ Execute block if user is logged and is admin (or not admin)
148
+ * admin_block
149
+ * not_admin_block
150
+
151
+ Execute block if ip is localhost (or not localhost)
152
+ * localhost_block
153
+ * not_localhost_block
154
+
155
+ Execute block if role is included in list of roles (or not)
156
+ * roles_block
157
+ * not_roles_block
158
+
159
+ ## Block area helpers ##
160
+
161
+ Create div.user 'area' and execute block if user is logged in as a user (or not)
162
+
163
+ * user_area
164
+ * not_user_area
165
+
166
+ Create div.admin 'area' and execute block if user is admin (or not admin)
167
+
168
+ * admin_area
169
+ * not_admin_area
170
+
171
+ Example:
172
+ <pre>
173
+ <% admin_area do %>
174
+ ul.admin_menu
175
+ ...
176
+
177
+ If logged in as admin, results in:
178
+
179
+ div.admin
180
+ ul.admin_menu
181
+ ...
182
+ </pre>
183
+
184
+ ## Roles block area helpers ##
185
+
186
+ Creates are if role is one included in list of roles (or not)
187
+
188
+ * roles_area
189
+ * not_roles_area
190
+
191
+ Example:
192
+ <pre>
193
+ <% roles_area 'admin, 'editor', :class => 'special' do %>
194
+ ul.admin_menu
195
+ ...
196
+
197
+ If logged in as either 'editor' or 'admin', results in:
198
+
199
+ div.special
200
+ ul.admin_menu
201
+ ...
202
+
203
+ </pre>
204
+
205
+ ## Misc helpers ##
206
+
207
+ * user? -
208
+ * admin?
209
+ * role?
210
+ * localhost?
211
+
212
+ Examples
213
+ <pre>
214
+ <%= current_user.username if user? %>
215
+ <%= "Admin: #{current_user.username}" if admin? %>
216
+ <%= "Special user!" if role?('admin', 'reviewer') %>
217
+ <%= "Running on localhost!" if localhost? %>
218
+ </pre>
219
+
220
+ ## Generators ##
221
+
222
+ The following generators are available
223
+
224
+ * config - configure with new strategy
225
+ * clear - clear existing strategy
226
+ * views - generate partials for use in views
227
+
228
+ ### Config Generator ###
229
+
230
+ The `config` generator generates a configuration initializer file for setting up `auth_assistant` to use a particular role strategy.
231
+
232
+ `$ rails g auth_assistant:config NAME`
233
+
234
+ NAME is the name of a role strategy.
235
+
236
+ Strategies with a single role for each user
237
+ * admin_field
238
+ * role_field
239
+ * role_assignment
240
+
241
+ Strategies with multiple roles for each user
242
+ * roles_field
243
+ * roles_mask
244
+ * multi_role_assignment
245
+
246
+ Currently role groups are not supported. Feel free to provide an add-on to support this or integrate with an existing 'role group' solution.
247
+
248
+ Example usage:
249
+
250
+ $ rails g auth_assist:config admin_field
251
+
252
+ Also ensure devise is setup and configured
253
+
254
+ $ rails g auth_assist:config roles_mask --devise
255
+
256
+ To also create an administrator model using STI to inherit and override the basic user strategies
257
+
258
+ $ rails g auth_assist:config roles_field --administrator
259
+
260
+ To ensure a user model migration is generated
261
+
262
+ $ rails g auth_assist:config role_field --migration
263
+
264
+
265
+ ### Clear Generator ###
266
+
267
+ The `clear` generator removes any existing strategy file and optionally generates a migration to remove any tables and fields related to the existing role strategy.
268
+ This allows you to easily change role strategy by first running the `clear` generator and then the `config` generator with a new strategy.
269
+
270
+ `$ rails g auth_assist:clear NAME`
271
+
272
+ Example usage:
273
+
274
+ `$ rails g auth_assist:clear role_field`
275
+
276
+ ### Views Generator ###
277
+
278
+ The `views` generator generates views (partials) for use with Menus.
279
+
280
+ `$ rails g auth_assistant:views`
281
+
282
+ Create HAML views
283
+
284
+ `$ rails g auth_assist:views --template_engine haml'
285
+
286
+ Example usage:
287
+ <pre>
288
+ ul.menu
289
+ render 'auth_assist/login_items'
290
+ render 'auth_assist/registration_items'
291
+
292
+ ul.admin_menu_
293
+ render 'auth_assist/admin_login_items'
294
+ </pre>
295
+
296
+ == Note on Patches/Pull Requests
297
+
298
+ * Fork the project.
299
+ * Make your feature addition or bug fix.
300
+ * Add tests for it. This is important so I don't break it in a
301
+ future version unintentionally.
302
+ * Commit, do not mess with rakefile, version, or history.
303
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
304
+ * Send me a pull request. Bonus points for topic branches.
305
+
306
+ == Copyright
307
+
308
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "auth-assistant"
5
+ gem.summary = %Q{Provides assistance for setting up an auth solution}
6
+ gem.description = %Q{Provides assistance for setting up an auth solution using devise and cancan auth frameworks}
7
+ gem.email = "kmandrup@gmail.com"
8
+ gem.homepage = "http://github.com/kristianmandrup/devise-assistant"
9
+ gem.authors = ["Kristian Mandrup"]
10
+ gem.add_development_dependency "rspec", ">= 2.0.0"
11
+ gem.add_dependency "devise", ">= 1.0"
12
+ gem.add_dependency "cancan", ">= 1.0"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+
15
+ # add more gem options here
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0
Binary file
Binary file
@@ -0,0 +1,11 @@
1
+ <% admin_block do %>
2
+ <li>
3
+ <%= link_to('Logout', destroy_admin_session_path) %>
4
+ </li>
5
+ <% end %>
6
+ <% not_admin_block do %>
7
+ <li>
8
+ <%= link_to('Login', new_admin_session_path) %>
9
+ </li>
10
+ <% end %>
11
+