Introduction
Preallowed is a restful service implemented in Ruby on Rails. Any resource that is accessible via a browser is also available via programmatic interface in XML format. Ruby on Rails offers a simple elegant way for accessing restful resources called Active Resource (see examples below). Accessing it from Java or any other language capable of accessing resources via http is just as simple (see a Java example).
Initial setup
Although the service is extremely easy to use, it requires some basic configuration.
Registering profile
You have to Register a Profile. It takes only few seconds and is absolutely free.
During the registration process all fields are required. Here is an explanation of the fields meaning:
Client Name -- this is how we keep the users separate from each other. The client name should describe your software and can be something like mycompany.com, or dev.mycompany.com, or mywebsite etc... All the service requests are made within a clients scope. You will need to know the client id to access the service.
User name, password, password confirmation -- you will use this user name and password combination to access resources for your client name. Currently there is only one Client per User, but it might eventually change based on requests we get from you.
Email -- we only need it to communicate back to you about service alerts and notifications, so please make sure to provide existing email address.
Once the profile is registered, you can login and create your subjects, roles, resources. We expect that the subject most of the times will be created programmatically, while the roles and resources most likely will be configured through preallowed.com interface (this is just a suggested use of our service based on our assessment and common sense, but we leave it totally up to the user -- you can create subjects, roles and resources programmatically from your application or through the preallowed.com admin interface).
environment.rb
After registering a profile, you need to configure your application to be able to access the service through your profile. The key configuration file is environment.rb# Include your application configuration below
PREALLOWED_LOGIN = 'your_user_login' # this is the same login/password combination you are using to log in to the preallowed.com site. You configured it during registering a profile.
PREALLOWED_PASSWORD = 'your_password'
CLIENT_ID = "2" # to find client_id login and click on a client from a list. The url will look like this http://www.preallowed.com/clients/2 . The number 2 is your client id.
USER_ROLE_ID = "3" # we assuming that the number of roles for your application is fixed and is known ahead of time. Each role has id in preallowed system. You will access roles by there id.
TEAM_CAPTAIN_ROLE_ID = "4"
ADMIN_ROLE_ID = "5"
PREALLOWED_HOST = "http://www.preallowed.com"
CLIENTS_URI = PREALLOWED_HOST + "/clients/" + CLIENT_ID
Accessing service from Ruby on Rails client (Active Resource)
We need to create few "Active Resource" model classes.Client.rb
self.site = PREALLOWED_HOST
self.user = PREALLOWED_LOGIN
self.password = PREALLOWED_PASSWORD
end
Subject.rb
self.site = CLIENTS_URI
self.user = PREALLOWED_LOGIN
self.password = PREALLOWED_PASSWORD
end
Role.rb
self.site = CLIENTS_URI
self.user = PREALLOWED_LOGIN
self.password = PREALLOWED_PASSWORD
end
preallowed.com routes.rb
We thought it would be beneficial for Rails developers to see our routes file. We will be opensourcing all of the source code soon -- stay tuned.1 ActionController::Routing::Routes.draw do |map| 2 map.resources :clients, :member => {:subject_id_from_name => :get}#TODO: write functional test 3 map.resources :clients do |client| 4 client.resources :subjects, 5 :member => { 6 :has_access => :get, # the url should look like this /clients/:client_id/subjects/:id/has_access params[:resource]=(resoruce string described by REG EXP) 7 :is_subject_in_role => :get, 8 :add_role => :put, 9 :remove_role => :put 10 } 11 client.resources :roles, 12 :member => { 13 :add_subject => :put, 14 :remove_subject => :put, 15 :add_resource => :put, 16 :remove_resource => :put 17 } 18 client.resources :resources, 19 :member => { 20 :add_role => :put, 21 :remove_role => :put 22 } 23 24 end 25 26 map.resources :profiles 27 28 end
Accessing subject after login
Once your user loges into your application, you can programmatically access corresponding subject from preallowed service, if the subject does not yet exist, you can create it right from your rails app. As an example, we use restful authentication plugin, and once current_user is initialized, we call following line:
implementation of add_to_preallowed in user.rb
add_user_to_role, remove_user_from_role
is_user_in_role
Example of use of has_access