Rubyonrails开发从头来(windows)(十七)-控制访问权限

上次我们实现了登录的页面,现在我们区别管理员和普通用户,根据用户的不同身份,转入到不同的页面

在rails中提供了filter来截获访问某个action的请求,可以用filter在action被调用前或调用后添加我们自己的处理。在这里,我们在admin的controller中的action被调用前添加拦截器。如果是管理员,就进入管理页面,如果是普通用户,就进入登录页面。

1.在application.rb中给ApplicationController类添加authorize方法,代码如下:

def authorizeunless session[:user_id]flash[:notice] = "Please log in"redirect_to(:controller => "login", :action => "login")endend

然后在admin.rb文件里AdminController类开始的地方添加一行代码:

class AdminController < ApplicationControllerbefore_filter :authorize……

这样,我们就添加了一个拦截器,现在,AdminController中所有的action被调用前都会先调用authorize方法。

我们还要修改login_controller.rb文件,给LoginController里添加一句代码:

class LoginController < ApplicationControllerbefore_filter :authorize, :except => :login……

2.现在,我们如果直接访问http://localhost:3003/admin/ship,将会直接定位到login页面,并且提示先请登录。如图:

如果之前已经登录过,因为session还存在,所以不会看到效果,只要把浏览器关掉才重新定位admin/ship页面就可以了。

3.下面我们先完成一个用户列表的功能。代码很简单:

在login_controller.rb文件中,修改list_users方法:

def list_users@all_users = User.find(:all)end

修改views/login目录下的list_users.rhtml文件:

<% @page_title = "User List" -%><table><% for user in @all_users -%>  <tr>      <td><%= user.name %></td>      <td><%= link_to("(delete)", :action => :delete_user, :id => user.id) %></td>  </tr><% end -%></table>

完成后,如果你已经登录的话,直接打开http://localhost:3003/login/list_users页面,会看到一个简单的用户列表页面,如图:

4.在用户列表页面上可以看到有delete链接,当然,我们不能把所有的管理员都删除掉,至少要保留一个,所以我们添加一些代码,来确保某个管理员不会被删除。在users.rb文件里添加代码:

before_destroy :dont_destroy_dahuzizyddef dont_destroy_dahuzizydraise "Can't destroy dahuzizyd" if self.name == 'dahuzizyd'end

并且修改login_controller.rb文件中的delete_user方法,如下:

def delete_userid = params[:id]if id && user = User.find(id)beginuser.destroyflash[:notice] = "User #{user.name} deleted"rescueflash[:notice] = "Can't delete that user"end  end  redirect_to(:action => :list_users)end

这样,在用户列表页面上,当删除名为dahuzizyd的用户时会提示不能删除,如图:

5.接下来,我们来修改views/layout目录下的admin.rhtml文件 ,这样我们就可以在管理员登录以后显示不同的功能链接,代码如下:

<html>  <head>  <title>ADMINISTER Pragprog Books Online STore</title>  <%= stylesheet_link_tag "scaffold", "depot", "admin", :media => "all" %>  </head>  <body>      <div id="banner">          <%= @page_title || "Administer Bookshelf" %>      </div>      <div id="columns">          <div id="side">              <% if session[:user_id] -%>                  <%= link_to("Products", :controller => "admin",                  :action => "list") %><br />                  <%= link_to("Shipping", :controller => "admin",                  :action => "ship") %><br />                  <hr/>                  <%= link_to("Add user", :controller => "login",                  :action => "add_user") %><br />                  <%= link_to("List users", :controller => "login",                  :action => "list_users") %><br />                  <hr/>                  <%= link_to("Log out", :controller => "login",                  :action => "logout") %>                  <% end -%>                  </div>                  <div id="main">                  <% if flash[:notice] -%>                  <div id="notice"><%= flash[:notice] %></div>              <% end -%>              <%= @content_for_layout %>          </div>      </div>  </body></html>

完成后,我们可以点击Add user链接,出现下面的页面:

6.好了,现在在页面左侧的链接里有一个logout,我们还没有实现它的功能,现在就开始,修改login_controller.rb文件中的log_out方法,代码如下:

def logoutsession[:user_id] = nilflash[:notice] = "Logged out"redirect_to(:action => "login")end

OK,现在我们点击logout按钮,会重新定位到login页面,如图:

7.最后我们再整理下代码,在sTore_controller.rb文件中,@cart = find_cart这句代码在除了empty_cart以外的所有方法中都是第一句,所以,我们现在修改方法find_cart方法:

def find_cart@cart = (session[:cart] ||= Cart.new)end

并且,给sTore_controller.rb添加一个拦截器:

before_filter :find_cart, :except => :index

这样我们就可以在sTore_controller.rb中所有的@cart = find_cart代码都注释掉了。

旅行,重复一个承诺和梦想,听他第二十八次提起童年往事,

Rubyonrails开发从头来(windows)(十七)-控制访问权限

相关文章:

你感兴趣的文章:

标签云: