• Ruby on rails开发从头来(windows)(十七)控制访问权限


     

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

     

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

     

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

    def authorize

    unless session[:user_id]

    flash[:notice] = "Please log in"

    redirect_to(:controller => "login", :action => "login")

    end

    end

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

    class AdminController < ApplicationController

    before_filter :authorize

    ……

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

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

    class LoginController < ApplicationController

    before_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_dahuzizyd

    def dont_destroy_dahuzizyd

    raise "Can't destroy dahuzizyd" if self.name == 'dahuzizyd'

    end

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

    def delete_user

    id = params[:id]

    if id && user = User.find(id)

    begin

    user.destroy

    flash[:notice] = "User #{user.name} deleted"

    rescue

    flash[: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 logout

    session[:user_id] = nil

    flash[: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代码都注释掉了。
  • 相关阅读:
    Solaris 11 让 ls 的输出 带上颜色
    Solaris11 Vsphere client tool 的鼠标问题
    VNC Viewer连接Cent OS 时的 复制粘帖 功能
    vSphere Client的拷贝 粘帖 功能
    更改CentOS7登录画面的分辨率
    一个故意消耗内存的java程序MemoryEater
    安装CentOS7文字界面版后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法
    windows 安装 Redis
    centos 安装postgresql 完整版
    centos 7 官网安装 PostgreSQL
  • 原文地址:https://www.cnblogs.com/dahuzizyd/p/rails_ruby_dotnet_windows_study_17.html
Copyright © 2020-2023  润新知