Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40453
  • 博文数量: 27
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-24 00:44
文章分类
文章存档

2011年(1)

2009年(26)

我的朋友
最近访客

分类: Java

2009-05-07 18:32:05

假设你已经有了一个website,但是这个website还没有管理的功能,你只能通过console或者直接操作数据库来添加一些记录,而不能通过网站的界面来做数据管理。

过去的处理方式是生成一个admin的scaffold框架。
script/generate scaffold episode "admin/episodes"

这种做法可行但是不好,因为我们已经有了一个很好的页面来显示我们的episode list, 不需要再去生成一个很丑的list页面,

因此,我们决定自己写管理模块,我们已经有index页面了,我们只需要在其中加入edit, delete, create三个功能的链接就可以了。


    <%=link_to "edit", edit_episode_path(episode)%>
    <%=link_to "delete", episode_path(episode), :confirm=>"are you sure", :method=>:delete%>



<%=link_to "new", new_episode_path%>

到此为止,还存在两个问题,1,还没有实现这些action, 2. 现在所有用户都可以看到和执行这些action
第一个问题很容易解决,下面我们来解决第二个问题。

我们修改我们的view使这些action的部分只有在admin? 返回true的时候才显示出来,
<%if admin?%>


.....


<%end%>
把这个admin?方法放在哪里呢?因为我们要在view中调用,那么我们可以把他放在application_helper这个文件中,但是如果我们也要在controller中用到他,那么就不能放在helper中,而要放在applicationcontroller中。

class ApplicationController < ActionController
    helper_method admin?    #这一行的作用是使这个方法在view中也可以调用,因为view不能使用#controller中的方法,只能使用helper中的方法。

    def admin?
       false
    end
end

这里,我们只是简单的返回false,后面我们会具体的去实现这个方法。现在我们先来看另外一个问题,现在我们的确已经成功的隐藏了这些链接,但是用户还是可以通过直接链接的方式来执行这些动作,这样也是危险的,因此我们要在controller中加一个before_filter.
class EpisodeController < ApplicationController
    before_filter :autorize, :except=>[:index, :show]
    def index
       @episodes = Episode.find(:all)
    end
end
然后我们就去实现这个authorize方法,我们决定把他放在applicationcontroller中,这样所有的controller都可以调用了。
def autohrize
    unless admin?
       flash[:error] = "unauthorized access"
       redirect_to home_path
       false
    end
end
这样,如果没有授权的用户要执行这些action,就会被重定向到home页面,然后返回false,从而不再继续执行下面的动作,还有另外一种处理方式,如果我们不希望告诉用户这个page存在,那么我们可以返回一个404错误

好了,下面我们该实现admin? 这个方法了,除此之外,我们还有提供admin登陆的途径。

有许多方式来实现这个功能,一种方式就是安装restful_authentication这个plugin。which is an updated version of acts_as_authenticated,Once installed, the User model and a controller called Sessions can be generated like this.
$script/plugin install git://github.com/technoweenie/restful-authentication.git restful_authentication
$script/generate authenticated User sessions
$rake db:migrate
这里我们不使用这种方法,我们会在第67讲讨论这种方法。因为我们的admin不需要这么复杂,我们甚至不想建立一个User模型。
例如我们可以通过限制ip来实现admin? 方法
def admin?
request.remote_ip == '127.0.0.1'
end
这种做法显然不好,那么我们就通过一个简单的login来解决这个问题,只需要输入密码就可以了看到这些action
了。
也就是用户通过这个url: 来访问我们的login页面。

内部我们有一个SessionController, 因为controller和路径名字不对应,就需要做一下map。

class SessionController < ApplicationController
def new
end
def create
session[:password] = params[:password]
flash[:notice] = "successfully logged in!"
redirect_to home_path
end
def destroy
reset_session
flash[:notice] = "successfully logged out!"
redirect_to login_path
end
end

views中当然也要有sessions文件夹。
index.html.erb:
<%form_tag :session_path%>
Password<%=password_field_tag :password%>
<%=submit_tag "Login"%>
<%end%>
def admin?
    session[:password] == 'secret'
end

最后,别忘了在route中加入如下两行:
  1. map.connect ’login’, :controller => ’sessions’, :action => ’create’  
  2. map.connect ’logout’, :controller => ’sessions’, :action => ’destroy’ 

阅读(400) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~