authentasaurus 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +173 -0
- data/TODO +4 -0
- data/app/controllers/areas_controller.rb +4 -0
- data/app/controllers/groups_controller.rb +4 -0
- data/app/controllers/permissions_controller.rb +4 -0
- data/app/controllers/recoveries_controller.rb +2 -0
- data/app/controllers/registrations_controller.rb +2 -0
- data/app/controllers/sessions_controller.rb +2 -0
- data/app/controllers/user_invitations_controller.rb +4 -0
- data/app/controllers/users_controller.rb +4 -0
- data/app/controllers/validations_controller.rb +2 -0
- data/app/models/area.rb +2 -0
- data/app/models/authentasaurus_emailer.rb +2 -0
- data/app/models/group.rb +2 -0
- data/app/models/permission.rb +2 -0
- data/app/models/recovery.rb +2 -0
- data/app/models/session.rb +2 -0
- data/app/models/user.rb +3 -0
- data/app/models/user_invitation.rb +2 -0
- data/app/models/user_sync.rb +3 -0
- data/app/models/validation.rb +2 -0
- data/app/views/areas/edit.html.erb +7 -0
- data/app/views/areas/index.html.erb +11 -0
- data/app/views/areas/new.html.erb +7 -0
- data/app/views/areas/show.html.erb +7 -0
- data/app/views/authentasaurus_emailer/invitation_mail.html.erb +4 -0
- data/app/views/authentasaurus_emailer/recovery_mail.html.erb +7 -0
- data/app/views/authentasaurus_emailer/validation_mail.html.erb +6 -0
- data/app/views/groups/edit.html.erb +7 -0
- data/app/views/groups/index.html.erb +11 -0
- data/app/views/groups/new.html.erb +7 -0
- data/app/views/groups/show.html.erb +7 -0
- data/app/views/permissions/edit.html.erb +19 -0
- data/app/views/permissions/index.html.erb +17 -0
- data/app/views/permissions/new.html.erb +19 -0
- data/app/views/permissions/show.html.erb +7 -0
- data/app/views/recoveries/edit.html.erb +24 -0
- data/app/views/recoveries/new.html.erb +15 -0
- data/app/views/registrations/new.html.erb +21 -0
- data/app/views/sessions/new.html.erb +41 -0
- data/app/views/sessions/no_access.html.erb +1 -0
- data/app/views/user_invitations/index.html.erb +14 -0
- data/app/views/user_invitations/new.html.erb +9 -0
- data/app/views/users/edit.html.erb +27 -0
- data/app/views/users/index.html.erb +19 -0
- data/app/views/users/new.html.erb +27 -0
- data/app/views/users/show.html.erb +11 -0
- data/app/views/validations/activate.html.erb +6 -0
- data/app/views/validations/resend_validation_email.html.erb +6 -0
- data/config/locales/en.yml +34 -0
- data/generators/authentasaurus/USAGE +2 -0
- data/generators/authentasaurus/authentasaurus_generator.rb +9 -0
- data/generators/authentasaurus/templates/authentasaurus_tasks.rake +48 -0
- data/generators/authentasaurus/templates/defaults.yml +51 -0
- data/generators/authentasaurus/templates/initializer.rb +3 -0
- data/generators/authentasaurus_views/USAGE +12 -0
- data/generators/authentasaurus_views/authentasaurus_views_generator.rb +87 -0
- data/generators/authentasaurus_views/templates/views/areas/edit.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/areas/index.html.erb +11 -0
- data/generators/authentasaurus_views/templates/views/areas/new.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/areas/show.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/authentasaurus_emailer/invitation_mail.html.erb +4 -0
- data/generators/authentasaurus_views/templates/views/authentasaurus_emailer/recovery_mail.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/authentasaurus_emailer/validation_mail.html.erb +6 -0
- data/generators/authentasaurus_views/templates/views/groups/edit.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/groups/index.html.erb +11 -0
- data/generators/authentasaurus_views/templates/views/groups/new.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/groups/show.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/permissions/edit.html.erb +19 -0
- data/generators/authentasaurus_views/templates/views/permissions/index.html.erb +17 -0
- data/generators/authentasaurus_views/templates/views/permissions/new.html.erb +19 -0
- data/generators/authentasaurus_views/templates/views/permissions/show.html.erb +7 -0
- data/generators/authentasaurus_views/templates/views/recoveries/edit.html.erb +24 -0
- data/generators/authentasaurus_views/templates/views/recoveries/new.html.erb +15 -0
- data/generators/authentasaurus_views/templates/views/registrations/new.html.erb +21 -0
- data/generators/authentasaurus_views/templates/views/sessions/new.html.erb +41 -0
- data/generators/authentasaurus_views/templates/views/sessions/no_access.html.erb +1 -0
- data/generators/authentasaurus_views/templates/views/user_invitations/index.html.erb +14 -0
- data/generators/authentasaurus_views/templates/views/user_invitations/new.html.erb +9 -0
- data/generators/authentasaurus_views/templates/views/users/edit.html.erb +27 -0
- data/generators/authentasaurus_views/templates/views/users/index.html.erb +19 -0
- data/generators/authentasaurus_views/templates/views/users/new.html.erb +27 -0
- data/generators/authentasaurus_views/templates/views/users/show.html.erb +11 -0
- data/generators/authentasaurus_views/templates/views/validations/activate.html.erb +6 -0
- data/generators/authentasaurus_views/templates/views/validations/resend_validation_email.html.erb +6 -0
- data/lib/action_controller/authorization.rb +215 -0
- data/lib/action_view/authorization.rb +123 -0
- data/lib/active_record/acts_as_authenticatable.rb +80 -0
- data/lib/active_record/acts_as_authenticatable_validatable.rb +32 -0
- data/lib/active_record/authenticatable.rb +56 -0
- data/lib/active_resource/acts_as_authenticatable.rb +42 -0
- data/lib/active_resource/authenticatable.rb +22 -0
- data/lib/authentasaurus.rb +21 -0
- data/lib/authentasaurus/areas_controller.rb +71 -0
- data/lib/authentasaurus/authentasaurus_controller.rb +2 -0
- data/lib/authentasaurus/groups_controller.rb +73 -0
- data/lib/authentasaurus/models/area.rb +8 -0
- data/lib/authentasaurus/models/authentasaurus_emailer.rb +31 -0
- data/lib/authentasaurus/models/group.rb +8 -0
- data/lib/authentasaurus/models/permission.rb +9 -0
- data/lib/authentasaurus/models/recovery.rb +23 -0
- data/lib/authentasaurus/models/session.rb +63 -0
- data/lib/authentasaurus/models/user_invitation.rb +21 -0
- data/lib/authentasaurus/models/validation.rb +18 -0
- data/lib/authentasaurus/permissions_controller.rb +71 -0
- data/lib/authentasaurus/recoveries_controller.rb +59 -0
- data/lib/authentasaurus/registrations_controller.rb +32 -0
- data/lib/authentasaurus/sessions_controller.rb +45 -0
- data/lib/authentasaurus/user_invitations_controller.rb +39 -0
- data/lib/authentasaurus/users_controller.rb +71 -0
- data/lib/authentasaurus/validations_controller.rb +34 -0
- data/lib/helpers/migrations.rb +171 -0
- data/lib/helpers/routing.rb +90 -0
- data/rails/init.rb +37 -0
- metadata +188 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
<h1>Editing <%= @permission.area.target %> permissions for <%= @permission.group.name %></h1>
|
2
|
+
<% form_for @permission do |f| %>
|
3
|
+
<%= f.label :group_id %>
|
4
|
+
<%= f.collection_select(:group_id, Group.find(:all), :id, :name, {:prompt => true}) %>
|
5
|
+
<br>
|
6
|
+
<%= f.label :area_id %>
|
7
|
+
<%= f.collection_select(:area_id, Area.find(:all), :id, :name, {:prompt => true}) %>
|
8
|
+
<br>
|
9
|
+
<%= f.label :group_id %>
|
10
|
+
<%= f.collection_select(:group_id, Group.find(:all), :id, :name, {:prompt => true}) %>
|
11
|
+
<br>
|
12
|
+
<%= f.label :read %>
|
13
|
+
<%= f.check_box :read %>
|
14
|
+
<br>
|
15
|
+
<%= f.label :write %>
|
16
|
+
<%= f.check_box :write %>
|
17
|
+
<br>
|
18
|
+
<%= f.submit %>
|
19
|
+
<% end %>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<h1>Permissions</h1>
|
2
|
+
<table>
|
3
|
+
<tr>
|
4
|
+
<th>Group</th>
|
5
|
+
<th>Area</th>
|
6
|
+
<th>Read</th>
|
7
|
+
<th>Write</th>
|
8
|
+
</tr>
|
9
|
+
<% @permissions.each do |permission| %>
|
10
|
+
<tr>
|
11
|
+
<td><%= link_to permission.group.name, permission.group %></td>
|
12
|
+
<td><%= link_to permission.area.name, permission.area %></td>
|
13
|
+
<td><%= check_box_tag :read, "read", permission.read, :disabled => true %></td>
|
14
|
+
<td><%= check_box_tag :write, "write", permission.write, :disabled => true %></td>
|
15
|
+
</tr>
|
16
|
+
<% end %>
|
17
|
+
</table>
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<h1>Create Permission</h1>
|
2
|
+
<% form_for @permission do |f| %>
|
3
|
+
<%= f.label :group_id %>
|
4
|
+
<%= f.collection_select(:group_id, Group.find(:all), :id, :name, {:prompt => true}) %>
|
5
|
+
<br>
|
6
|
+
<%= f.label :area_id %>
|
7
|
+
<%= f.collection_select(:area_id, Area.find(:all), :id, :name, {:prompt => true}) %>
|
8
|
+
<br>
|
9
|
+
<%= f.label :group_id %>
|
10
|
+
<%= f.collection_select(:group_id, Group.find(:all), :id, :name, {:prompt => true}) %>
|
11
|
+
<br>
|
12
|
+
<%= f.label :read %>
|
13
|
+
<%= f.check_box :read %>
|
14
|
+
<br>
|
15
|
+
<%= f.label :write %>
|
16
|
+
<%= f.check_box :write %>
|
17
|
+
<br>
|
18
|
+
<%= f.submit %>
|
19
|
+
<% end %>
|
@@ -0,0 +1,7 @@
|
|
1
|
+
<h1><%= @permission.area.name %> permissions for <%= @permission.group.name %></h1>
|
2
|
+
<% if @permission.read %>
|
3
|
+
<h3>Members of <%= @permission.group.name %> have read permission for <%= @permission.area.name %></h3>
|
4
|
+
<% end %>
|
5
|
+
<% if @permission.write %>
|
6
|
+
<h3>Members of <%= @permission.group.name %> have write permission for <%= @permission.area.name %></h3>
|
7
|
+
<% end %>
|
@@ -0,0 +1,24 @@
|
|
1
|
+
<h1>Fill in your new password</h1>
|
2
|
+
|
3
|
+
<% form_tag do_recover_password_path, :method => :delete do %>
|
4
|
+
<%= error_messages_for :user %>
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<%= label_tag :name %><br />
|
8
|
+
<%= text_field_tag :name, @user.name, :disabled => true %>
|
9
|
+
</p>
|
10
|
+
|
11
|
+
<p>
|
12
|
+
<%= label_tag "user[new_password]", "Password" %><br />
|
13
|
+
<%= password_field_tag "user[new_password]" %>
|
14
|
+
</p>
|
15
|
+
<p>
|
16
|
+
<%= label_tag "user[new_password_confirmation]", "Password confirmation" %><br />
|
17
|
+
<%= password_field_tag "user[new_password_confirmation]" %>
|
18
|
+
</p>
|
19
|
+
<p>
|
20
|
+
<%= submit_tag "Set" %>
|
21
|
+
</p>
|
22
|
+
<% end %>
|
23
|
+
|
24
|
+
<%= link_to "Cancel", url_for(:back) %>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<h1>Recover your password</h1>
|
2
|
+
|
3
|
+
<% form_tag do_forgot_password_path, :method => :post do %>
|
4
|
+
<%= error_messages_for :recovery %>
|
5
|
+
<p>
|
6
|
+
<%= label_tag :email %><br />
|
7
|
+
<%= text_field_tag :email, params[:email] %>
|
8
|
+
</p>
|
9
|
+
|
10
|
+
<p>
|
11
|
+
<%= submit_tag "Send Instructions" %>
|
12
|
+
</p>
|
13
|
+
<% end %>
|
14
|
+
|
15
|
+
<%= link_to "Cancel", url_for(:back) %>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<h1>Enter your data</h1>
|
2
|
+
<% form_for @user, :url => registrations_path(:token => params[:token]) do |f| %>
|
3
|
+
<%= f.error_messages %>
|
4
|
+
<br>
|
5
|
+
<%= f.label :username %>
|
6
|
+
<%= f.text_field :username, :size => 30 %>
|
7
|
+
<br>
|
8
|
+
<%= f.label :password %>
|
9
|
+
<%= f.password_field :password, :size => 30 %>
|
10
|
+
<br>
|
11
|
+
<%= f.label :password_confirmation %>
|
12
|
+
<%= f.password_field :password_confirmation, :size => 30 %>
|
13
|
+
<br>
|
14
|
+
<%= f.label :name %>
|
15
|
+
<%= f.text_field :name, :size => 30 %>
|
16
|
+
<br>
|
17
|
+
<%= f.label :email %>
|
18
|
+
<%= f.text_field :email, :size => 30 %>
|
19
|
+
<br>
|
20
|
+
<%= f.submit "Create" %>
|
21
|
+
<% end %>
|
@@ -0,0 +1,41 @@
|
|
1
|
+
<div class="form-stroke">
|
2
|
+
<%= flash[:alert] if flash[:alert] %>
|
3
|
+
<% form_for @session do |f| %>
|
4
|
+
<h2>Sign in</h2>
|
5
|
+
<br>
|
6
|
+
<%= f.error_messages %>
|
7
|
+
<p>
|
8
|
+
<%= f.label :username %>
|
9
|
+
<%= f.text_field :username, :style => "width: 98%;" %>
|
10
|
+
</p>
|
11
|
+
|
12
|
+
<p>
|
13
|
+
<%= f.label :password %>
|
14
|
+
<%= f.password_field :password, :style => "width: 98%;" %>
|
15
|
+
</p>
|
16
|
+
|
17
|
+
<label>
|
18
|
+
<%= f.check_box :remember %> Remember me
|
19
|
+
</label>
|
20
|
+
|
21
|
+
<div>
|
22
|
+
<div style="float:left; padding-top: 9px;">
|
23
|
+
<% unless defined?(forgot_password_path).nil? %>
|
24
|
+
<p>
|
25
|
+
<%= link_to(t(:forgot_password_link, :scope => [:authentasaurus, :views, :sessions]), forgot_password_path) %>
|
26
|
+
</p>
|
27
|
+
<% end %>
|
28
|
+
|
29
|
+
<% unless defined?(resend_validation_email_path).nil? %>
|
30
|
+
<p>
|
31
|
+
<%= link_to(t(:resend_validation_email_link, :scope => [:authentasaurus, :views, :sessions]), resend_validation_email_path) %>
|
32
|
+
</p>
|
33
|
+
<% end %>
|
34
|
+
</div>
|
35
|
+
<div style="float:right;">
|
36
|
+
<%= submit_tag "Sign in" %>
|
37
|
+
</div>
|
38
|
+
<br style="clear: both;">
|
39
|
+
</div>
|
40
|
+
<% end %>
|
41
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>You are not allowed to access this page.</h1>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<h1>User Invitations</h1>
|
2
|
+
<table width="100%">
|
3
|
+
<tr>
|
4
|
+
<th>Email</th>
|
5
|
+
</tr>
|
6
|
+
<% @invitations.each do |invitation| %>
|
7
|
+
<tr>
|
8
|
+
<td><%= invitation.email %></td>
|
9
|
+
<td><%= link_to "Delete", invitation, :method => :delete, :confirm => "Are you sure?" %></td>
|
10
|
+
</tr>
|
11
|
+
<% end %>
|
12
|
+
</table>
|
13
|
+
<br>
|
14
|
+
<%= link_to "New", :action => :new %>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
<h1>Editing <%= @user.username %></h1>
|
2
|
+
<% form_for @user do |f| %>
|
3
|
+
<%= f.error_messages %>
|
4
|
+
<br>
|
5
|
+
<%= f.label :username %>
|
6
|
+
<%= f.text_field :username, :size => 30 %>
|
7
|
+
<br>
|
8
|
+
<%= f.label :new_password %>
|
9
|
+
<%= f.password_field :new_password, :size => 30 %>
|
10
|
+
<br>
|
11
|
+
<%= f.label :new_password_confirmation %>
|
12
|
+
<%= f.password_field :new_password_confirmation, :size => 30 %>
|
13
|
+
<br>
|
14
|
+
<%= f.label :name %>
|
15
|
+
<%= f.text_field :name, :size => 30 %>
|
16
|
+
<br>
|
17
|
+
<%= f.label :email %>
|
18
|
+
<%= f.text_field :email, :size => 30 %>
|
19
|
+
<br>
|
20
|
+
<%= f.label :active %>
|
21
|
+
<%= f.check_box :active %>
|
22
|
+
<br>
|
23
|
+
<%= f.label :group_id %>
|
24
|
+
<%= f.collection_select(:group_id, Group.find(:all), :id, :name, {:prompt => true}) %>
|
25
|
+
<br>
|
26
|
+
<%= f.submit "Create" %>
|
27
|
+
<% end %>
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<h1>Users</h1>
|
2
|
+
<table width="100%">
|
3
|
+
<tr>
|
4
|
+
<th>Username</th>
|
5
|
+
<th>Name</th>
|
6
|
+
<th>Email</th>
|
7
|
+
<th>Active</th>
|
8
|
+
<th>Group</th>
|
9
|
+
</tr>
|
10
|
+
<% @users.each do |user| %>
|
11
|
+
<tr>
|
12
|
+
<td><%= link_to user.username, user %></td>
|
13
|
+
<td><%= user.name %></td>
|
14
|
+
<td><%= user.email %></td>
|
15
|
+
<td><%= check_box_tag user.id, "active", user.active, :disabled => true %></td>
|
16
|
+
<td><%= link_to user.group.name, user.group %></td>
|
17
|
+
</tr>
|
18
|
+
<% end %>
|
19
|
+
</table>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
<h1>Create new user</h1>
|
2
|
+
<% form_for @user do |f| %>
|
3
|
+
<%= f.error_messages %>
|
4
|
+
<br>
|
5
|
+
<%= f.label :username %>
|
6
|
+
<%= f.text_field :username, :size => 30 %>
|
7
|
+
<br>
|
8
|
+
<%= f.label :password %>
|
9
|
+
<%= f.password_field :password, :size => 30 %>
|
10
|
+
<br>
|
11
|
+
<%= f.label :password_confirmation %>
|
12
|
+
<%= f.password_field :password_confirmation, :size => 30 %>
|
13
|
+
<br>
|
14
|
+
<%= f.label :name %>
|
15
|
+
<%= f.text_field :name, :size => 30 %>
|
16
|
+
<br>
|
17
|
+
<%= f.label :email %>
|
18
|
+
<%= f.text_field :email, :size => 30 %>
|
19
|
+
<br>
|
20
|
+
<%= f.label :active %>
|
21
|
+
<%= f.check_box :active %>
|
22
|
+
<br>
|
23
|
+
<%= f.label :group_id %>
|
24
|
+
<%= f.collection_select(:group_id, Group.find(:all), :id, :name, {:prompt => true}) %>
|
25
|
+
<br>
|
26
|
+
<%= f.submit "Create" %>
|
27
|
+
<% end %>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<h1><%= @user.username %></h1>
|
2
|
+
<dl>
|
3
|
+
<dt>Name : </dt>
|
4
|
+
<dd><%= @user.name %></dd>
|
5
|
+
<dt>Email : </dt>
|
6
|
+
<dd><%= @user.email %></dd>
|
7
|
+
<dt>Active : </dt>
|
8
|
+
<dd><%= check_box_tag @user.id, "active", @user.active, :disabled => true %></dd>
|
9
|
+
<dt>Group : </dt>
|
10
|
+
<dd><%= link_to @user.group.name, @user.group %></dd>
|
11
|
+
</dl>
|
@@ -0,0 +1,215 @@
|
|
1
|
+
# Defines authorization helpers for ActionController
|
2
|
+
module ActionController::Authorization
|
3
|
+
|
4
|
+
def self.included(base) # :nodoc:
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
base.send :include, InstanceMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# <tt>require_login</tt>::
|
13
|
+
# requires the user to login before accessing the actions specified
|
14
|
+
#
|
15
|
+
# <b>ex:</b> Tells Authentasaurus that the action destroy requires login and that
|
16
|
+
# Authentasaurus shouldn't store the request in the session
|
17
|
+
# (typically for logout actions)
|
18
|
+
#
|
19
|
+
# * :skip_request - skips saving the original request (to redirect to after login)
|
20
|
+
# * :user_model - if defined, authentasaurus will use that model instead of the default "User"
|
21
|
+
# * :if - specifies a method, proc or string to call to determine if the authorization should occur
|
22
|
+
# * :unless - specifies a method, proc or string to call to determine if the authorization should not occur
|
23
|
+
#
|
24
|
+
# require_login :destroy, :skip_request => true
|
25
|
+
def require_login (*attrs)
|
26
|
+
options = attrs.extract_options!.symbolize_keys
|
27
|
+
attrs = attrs.flatten
|
28
|
+
|
29
|
+
unless attrs.empty?
|
30
|
+
before_filter :only => attrs, :if => options[:if], :unless => options[:unless] do |controller|
|
31
|
+
controller.instance_eval {check_logged_in !options[:skip_request].nil?, options[:user_model]}
|
32
|
+
end
|
33
|
+
else
|
34
|
+
before_filter :if => options[:if], :unless => options[:unless] do |c|
|
35
|
+
c.instance_eval {check_logged_in !options[:skip_request].nil?, options[:user_model]}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# <tt>require_write</tt>::
|
41
|
+
# requires the user to have a write permission to that area to access the actions specified
|
42
|
+
#
|
43
|
+
# <b>ex:</b> Tells Authentasaurus that the actions create_user and delete_user
|
44
|
+
# requires login and write permission.
|
45
|
+
#
|
46
|
+
# * :skip_request - skips saving the original request (to redirect to after login)
|
47
|
+
# * :user_model - if defined, authentasaurus will use that model instead of the default "User"
|
48
|
+
# * :if - specifies a method, proc or string to call to determine if the authorization should occur
|
49
|
+
# * :unless - specifies a method, proc or string to call to determine if the authorization should not occur
|
50
|
+
#
|
51
|
+
# require_write :create_user, :delete_user
|
52
|
+
def require_write(*attrs)
|
53
|
+
options = attrs.extract_options!.symbolize_keys
|
54
|
+
attrs = attrs.flatten
|
55
|
+
|
56
|
+
|
57
|
+
unless attrs.empty?
|
58
|
+
before_filter :only => attrs, :if => options[:if], :unless => options[:unless] do |controller|
|
59
|
+
controller.instance_eval { check_write_permissions !options[:skip_request].nil?, options[:user_model] }
|
60
|
+
end
|
61
|
+
else
|
62
|
+
before_filter :if => options[:if], :unless => options[:unless] do |c|
|
63
|
+
c.instance_eval {check_write_permissions !options[:skip_request].nil?, options[:user_model]}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# <tt>require_read</tt>::
|
69
|
+
# requires the user to have a read permission to that area to access the actions specified
|
70
|
+
#
|
71
|
+
# <b>ex:</b> Tells Authentasaurus that the action show_user requires login and read
|
72
|
+
# permission.
|
73
|
+
#
|
74
|
+
# * :skip_request - skips saving the original request (to redirect to after login)
|
75
|
+
# * :user_model - if defined, authentasaurus will use that model instead of the default "User"
|
76
|
+
# * :if - specifies a method, proc or string to call to determine if the authorization should occur
|
77
|
+
# * :unless - specifies a method, proc or string to call to determine if the authorization should not occur
|
78
|
+
#
|
79
|
+
# require_read :show_user
|
80
|
+
def require_read(*attrs)
|
81
|
+
options = attrs.extract_options!.symbolize_keys
|
82
|
+
attrs = attrs.flatten
|
83
|
+
|
84
|
+
unless attrs.empty?
|
85
|
+
before_filter :only => attrs, :if => options[:if], :unless => options[:unless] do |controller|
|
86
|
+
controller.instance_eval { check_read_permissions !options[:skip_request].nil?, options[:user_model] }
|
87
|
+
end
|
88
|
+
else
|
89
|
+
before_filter :if => options[:if], :unless => options[:unless] do |c|
|
90
|
+
c.instance_eval { check_read_permissions !options[:skip_request].nil?, options[:user_model] }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
module InstanceMethods
|
97
|
+
private
|
98
|
+
# Returns an object of the current user
|
99
|
+
def current_user(user_model = nil)
|
100
|
+
user_model = User if user_model.nil?
|
101
|
+
return user_model.find session[:user_id] if session[:user_id]
|
102
|
+
end
|
103
|
+
|
104
|
+
# Checks if the current user is logged in and redirects to the login path if the user is not logged in.
|
105
|
+
#
|
106
|
+
# If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
|
107
|
+
def check_logged_in(skip_request = false, user_model = nil)
|
108
|
+
unless is_logged_in?(user_model)
|
109
|
+
login_required skip_request
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Checks if the current user is logged in and has write permission over the current controller, redirects to no access
|
114
|
+
# page if the user hasn't the permissions and redirects to the login path if the user is not logged in
|
115
|
+
#
|
116
|
+
# If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
|
117
|
+
def check_write_permissions(skip_request = false, user_model = nil)
|
118
|
+
if is_logged_in?(user_model)
|
119
|
+
user_permissions = session[:user_permissions]
|
120
|
+
check = user_permissions[:write].find { |perm| perm==self.controller_name || perm=="all" }
|
121
|
+
unless check
|
122
|
+
redirect_to no_access_sessions_path
|
123
|
+
end
|
124
|
+
else
|
125
|
+
login_required skip_request
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# Checks if the current user is logged in and has read permission over the current controller, redirects to no access
|
130
|
+
# page if the user hasn't the permissions and redirects to the login path if the user is not logged in
|
131
|
+
#
|
132
|
+
# If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
|
133
|
+
def check_read_permissions(skip_request = false, user_model = nil)
|
134
|
+
if is_logged_in?(user_model)
|
135
|
+
user_permissions = session[:user_permissions]
|
136
|
+
check = user_permissions[:read].find { |perm| perm==self.controller_name || perm=="all" }
|
137
|
+
unless check
|
138
|
+
redirect_to no_access_sessions_path
|
139
|
+
end
|
140
|
+
else
|
141
|
+
login_required skip_request
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Checks if the current user is logged in but takes no further action
|
146
|
+
def is_logged_in?(user_model)
|
147
|
+
user_model = User if user_model.nil?
|
148
|
+
unless user_model.find_by_id(session[:user_id])
|
149
|
+
return cookie_login?(user_model)
|
150
|
+
end
|
151
|
+
return true
|
152
|
+
end
|
153
|
+
|
154
|
+
# Logs in the user through a remember me cookie
|
155
|
+
def cookie_login?(user_model)
|
156
|
+
user_model = User if user_model.nil?
|
157
|
+
|
158
|
+
if cookies[:remember_me_token]
|
159
|
+
user = user_model.find_by_remember_me_token cookies[:remember_me_token]
|
160
|
+
if user.nil?
|
161
|
+
cookies.delete :remember_me_token
|
162
|
+
return false
|
163
|
+
else
|
164
|
+
session[:user_id] = user.id
|
165
|
+
session[:user_permissions] = {:read => user.permissions.collect{|per| per.area.name if per.read}, :write => user.permissions.collect{|per| per.area.name if per.write}}
|
166
|
+
return true
|
167
|
+
end
|
168
|
+
else
|
169
|
+
return false
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Redirects the user to the login page
|
174
|
+
#
|
175
|
+
# If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
|
176
|
+
def login_required(skip_request)
|
177
|
+
unless(skip_request)
|
178
|
+
session[:original_url]=request.url
|
179
|
+
end
|
180
|
+
flash[:notice] = t(:login_required, :scope => [:authentasaurus, :action_controller, :errors, :messages])
|
181
|
+
redirect_to new_session_path
|
182
|
+
end
|
183
|
+
|
184
|
+
# Checks if the current user has the appropriate permission
|
185
|
+
#
|
186
|
+
# *ex*: has?(:write) or has?(:read, :users)
|
187
|
+
def has?(permission, area = nil)
|
188
|
+
return false unless is_logged_in?
|
189
|
+
check = false
|
190
|
+
case permission
|
191
|
+
when :write
|
192
|
+
unless area
|
193
|
+
user_permissions = session[:user_permissions]
|
194
|
+
check = user_permissions[:write].find { |perm| perm==self.controller_name || perm=="all" }
|
195
|
+
else
|
196
|
+
user_permissions = session[:user_permissions]
|
197
|
+
check = user_permissions[:write].find { |perm| perm==area.to_s || perm=="all" }
|
198
|
+
end
|
199
|
+
when :read
|
200
|
+
unless area
|
201
|
+
user_permissions = session[:user_permissions]
|
202
|
+
check = user_permissions[:read].find { |perm| perm==self.controller_name || perm=="all" }
|
203
|
+
else
|
204
|
+
user_permissions = session[:user_permissions]
|
205
|
+
check = user_permissions[:read].find { |perm| perm==area.to_s || perm=="all" }
|
206
|
+
end
|
207
|
+
end
|
208
|
+
return check
|
209
|
+
end
|
210
|
+
|
211
|
+
def controller_instance
|
212
|
+
self
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|