Skip to content

Commit

Permalink
Немного поиграл с Ability
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov committed Feb 2, 2012
1 parent 3f5f0ac commit d11caa4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
5 changes: 4 additions & 1 deletion app/controllers/users_controller.rb
@@ -1,6 +1,8 @@
class UsersController < ApplicationController
skip_before_filter :require_login, :only => [:index, :new, :create, :activate]
skip_before_filter :require_login, :only => [ :new, :create, :activate]

load_and_authorize_resource
skip_authorize_resource :only => [ :new, :create, :activate ]

# GET /users
# GET /users.xml
Expand Down Expand Up @@ -45,6 +47,7 @@ def edit
# POST /users.xml
def create
@user = User.new(params[:user])
@user.roles = 'user'

respond_to do |format|
if @user.save
Expand Down
5 changes: 4 additions & 1 deletion app/models/ability.rb
Expand Up @@ -8,8 +8,11 @@ def initialize(user)

if user.role? :admin
can :manage, :all
else
elsif user.role? :user
can :manage, User, :id => user.id
can :read, :all
else
can :read, :all
end

# The first argument to `can` is the action you are giving the user permission to do.
Expand Down
2 changes: 1 addition & 1 deletion app/views/user_sessions/edit.html.erb
Expand Up @@ -3,4 +3,4 @@
<%= render 'form' %>
<%= link_to 'Show', @user_session %> |
<%= link_to 'Back', user_sessions_path %>
<%= link_to 'Back', root_path %>
2 changes: 1 addition & 1 deletion app/views/user_sessions/new.html.erb
Expand Up @@ -6,4 +6,4 @@
<h1>Forgot Password?</h1>
<%= render 'forgot_password_form' %>
<%= link_to 'Back', user_sessions_path %>
<%= link_to 'Back', root_path %>
14 changes: 9 additions & 5 deletions app/views/users/index.html.erb
Expand Up @@ -12,13 +12,17 @@
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
<td><% if can? :read, User %>
<%= link_to 'Show', user %>
<% end %></td>
<td><% if can? :update, User %>
<%= link_to 'Edit', edit_user_path(user) %>
<% end %></td>
<td><% if can? :destroy, User %>
<%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %>
<% end %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New User', new_user_path %>
4 changes: 4 additions & 0 deletions db/seeds.rb
Expand Up @@ -13,3 +13,7 @@
user = User.create :email => 'user@test.com', :password => 'user', :password_confirmation => 'user'
user.roles = 'user'
user.save

test = User.create :email => 'test@test.com', :password => 'test', :password_confirmation => 'test'
test.roles = 'user'
test.save

0 comments on commit d11caa4

Please sign in to comment.