Lockdown¶
What¶
Lockdown is a authentication/authorization system for RubyOnRails (ver 2.x). While Merb functionality is in place, it is not complete. There will be a release solely focused on getting the Merb functionality up to par with Rails.
Please use either memcache or the database session store instead of the default cookie session store. Due to the current implementation of access_right storage, the size limitations for the cookie may become an issue for larger projects.
I've been cleaning up references that imply Merb functionality is working. I have not completed support for Merb and will probably do so to coincide with the 1.0 release of Merb.
Installing¶
$ sudo gem install lockdown $ cd <your_project_directory> $ lockdown .
This will create a "lockdown" directory in the lib dir add two files: init.rb and session.rb.
Modify init.rb to set configuration options and define the permissions and user groups that apply to your system.
Please keep the following in mind:
- All Permissions are defined in init.rb, they cannot be defined via the administration screens.
- All User Groups should be defined in init.rb. The administration screens can be used to create user groups, but doing so should be reserved for the unexpected. Creating User Groups via the administration screens will only add more work for you if you want to run tests using those groups.
- Lockdown will sync up the rules (Permissions and User Groups) defined in init.rb with your database. You can turn off this feature.
To help you with your new application, Lockdown comes with a generator called lockdown that has various options for you to pick which templates you desire.
$ cd <your_project_directory> $ ./script/generate lockdown
This will install resources such as:
- Models
- Controllers
- Views
- Helpers
- Migrations
- Routes
Please refer to the lockdown generator page for more detail.
How it works¶
When Lockdown is installed, it adds the following line to your environment.rb (init.rb for Merb):1 require "lockdown/init"
This is the default init.rb included with Lockdown:
1 require "lockdown"
2 require File.join(File.dirname(__FILE__), "session")
3
4 Lockdown::System.configure do
5
6 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 # Configuration Options
8 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 # Options with defaults:
10 #
11 # Set timeout to 1 hour:
12 # options[:session_timeout] = (60 * 60)
13 #
14 # Call method when timeout occurs (method must be callable by controller):
15 # options[:session_timeout_method] = :clear_session_values
16 #
17 # Set system to logout if unauthorized access is attempted:
18 # options[:logout_on_access_violation] = false
19 #
20 # Set redirect to path on unauthorized access attempt:
21 # options[:access_denied_path] = "/"
22 #
23 # Set redirect to path on successful login:
24 # options[:successful_login_path] = "/"
25 #
26 # Set the system to sync the Permissions and UserGroups defined here
27 # with the database.
28 # options[:sync_init_rb_with_db] = true
29 #
30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 # Define permissions
32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 #
34 # set_permission(:product_management, all_methods(:products))
35 #
36 # :product_management is the name of the permission which is later
37 # referenced by the set_user_group method
38 #
39 # :all_methods(:products) will return an array of all controller actions
40 # for the products controller
41 #
42 # if products is your standard RESTful resource you'll get:
43 # ["products/index , "products/show",
44 # "products/new", "products/edit",
45 # "products/create", "products/update",
46 # "products/destroy"]
47 #
48 # You can pass multiple parameters to concat permissions such as:
49 #
50 # set_permission(:security_management,all_methods(:users),
51 # all_methods(:user_groups),
52 # all_methods(:permissions) )
53
54 #
55 # In addition to all_methods(:controller) there are:
56 #
57 # only_methods(:controller, :only_method_1, :only_method_2)
58 #
59 # all_except_methods(:controller, :except_method_1, :except_method_2)
60 #
61 # Some other sample permissions:
62 #
63 # set_permission(:sessions, all_methods(:sessions))
64 # set_permission(:my_account, only_methods(:users, :edit, :update, :show))
65 #
66
67 # Define your permissions here:
68
69 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 # Built-in user groups
71 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72 # You can assign the above permission to one of the built-in user groups
73 # by using the following:
74 #
75 # To allow public access on the permissions :sessions and :home:
76 # set_public_access :sessions, :home
77 #
78 # Restrict :my_account access to only authenticated users:
79 # set_protected_access :my_account
80 #
81 # Define the built-in user groups here:
82
83 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84 # Define user groups
85 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86 #
87 # set_user_group(:catalog_management, :category_management,
88 # :product_management)
89 #
90 # :catalog_management is the name of the user group
91 # :category_management and :product_management refer to permission names
92 #
93 #
94 # Define your user groups here:
95
96 end
As you can see, the first line requires lockdown. This will load the Lockdown system which consists of various parts:
- Controller
- The controller functionality will add before filters to test each request agains the defined access_rights for the current user. If the current request is not in the access_rights list, access right is denied.
- Model
- The model functionality will automatically set the updated_by/created_by fields of your model to the current_profile_id.
- View
- The view functionality intercepts the link_to method (aliases it). If the current user does not have rights to the link, the link will not show.<br/>There is also a link_to_or_show method (same params as link_to) that will print out just the name of the link (no anchor tag) if the current user does not have access.
users/index users/show users/edit users/update users/new users/create users/destroy
The internals¶
All configuration of Lockdown (Permissions and User Groups) are done in lib/lockdown/init.rb. The database functionality is merely an extension of the definitions to allow for the dynamic creation of User Groups. Permissions can not be created via the administration screens.
Lockdown doesn't have a concept of Roles. Instead, Lockdown users can be associated to one or many User Groups to allow for flexibility. In addition, you can use the admin screens to add new User Groups to the database. User groups are nothing more than a grouping mechanism for Permissions to ease management.
Here are the parts to Lockdown:
- Profiles
- The profile model contains all non-user information related to person. Lockdown uses the profile record as the reference for updated_by and created_by. This allows you to remove the user record completely when you want to revoke access, but you still retain the foreign key for history.<br/>Here are the fields you have to start with:</p>
- first_name : string
- last_name : string
- email : string
- The profile model contains all non-user information related to person. Lockdown uses the profile record as the reference for updated_by and created_by. This allows you to remove the user record completely when you want to revoke access, but you still retain the foreign key for history.<br/>Here are the fields you have to start with:</p>
- Users
- The user model contains all user information related to person.<br/>Here are the fields you have to start with:
- login : string
- crypted_password : string
- salt : string
- profile_id : integer
- The user model contains all user information related to person.<br/>Here are the fields you have to start with:
- User Groups
- User Groups exist only to group Permissions. All functionality for your site should be covered by the user groups you define in init.rb. You can use the admin screen to create new user groups if the need arises. The database model only has one field:
- name : string
- User Groups exist only to group Permissions. All functionality for your site should be covered by the user groups you define in init.rb. You can use the admin screen to create new user groups if the need arises. The database model only has one field:
- Permissions
- Permissions are the security building blocks of your system and are defined in init.rb. A permission maps to controller(s)/action(s) in your system. Please refer back to the documentation in init.rb on how to create permissions.
As permissions relate to system functionality, they cannot be created via the admin screen. The database model only has one field:- name : string
- Permissions are the security building blocks of your system and are defined in init.rb. A permission maps to controller(s)/action(s) in your system. Please refer back to the documentation in init.rb on how to create permissions.
Roadmap to 1.0¶
**this is tentative and the feature order may change
- Completed: 0.5.0: More generators to ease installation into existing projects
- 0.6.0: Password reset/reminder, Registration page template generators
- 0.7.0: OpenId support
- 0.8.0: RSpec tests and helper methods for your application
- 0.9.0: Merb Support
- 1.0.0: Model level security
Github¶
http://github.com/stonean/lockdown/tree/master
clone url: git://github.com/stonean/lockdown.git
Contact¶
Please use the forum to ask questions and the issue tracker to report problems or submit a pull request.
License¶
This code is free to use under the terms of the MIT license.
Copyright (c) 2008 Andrew Stone