RailsCasts中文版,#20 Restricting Access 为页面增加权限校验2
来源:互联网 发布:tensorflow cnn 教程 编辑:程序博客网 时间:2024/05/16 00:35
限制访问权限
在上一篇中,我们在文章列表页增加了编辑和删除操作的连接,暂时还没有进行访问控制;以至于所有访问者都能看到并进行操作。
本应是管理员看到的按钮对所有人可见了。
下面为这些按钮增加权限,在连接元素外面包一个edit
方法的调用,只有返回true
了才可见。
<li> <p class="episodeId"><%= episode.episode_id %></p> <h3><%= link_to episode.title, episode_path(episode.identifier) %></h3> <p class="summary"><%= episode.summary %></p> <p class="tagList"> Tags: <% episode.tags.each do |tag| %><%= link_to tag.title, tag_path(tag.title) %><% end %> </p> <% if admin? %> <p class="adminActions"> <%= link_to "Edit", edit_episode_path(episode) %> <%= link_to "Destroy", episode_path(episode), :confirm => "Are you sure?", :method => :delete %> </p> <% end %></li>
在编辑和删除操作连接上增加了admin?
方法的调用。
然后是实现admin?
方法,应该把这个方法写在哪里? 这里的场景是要在View中调用这个方法,理应写在application_helper.rb
中。但是我觉得关于权限判断的逻辑将来也有可能在控制器中调用,所以还是写在ApplicationController
中吧。
class ApplicationController < ActionController::Base helper_method :admin? protected def admin? false end end
在ApplicationController
类中增加admin?
方法的定义。
现在的实现很简单,直接返回false
(下一篇中会继续实现),不过已经可以使用了。别忘了设置为helper_method
以便在View中能够被调用。
即将完成
现在倒是能够通过调用admin?
方法检查对非管理员隐藏连接了,但是还有问题:如果通过直接输入网址,依然能够转向编辑和删除页面。这个问题通过使用before_filter
方法来解决。
class EpisodesController < ApplicationController before_filter :authorize, :except => [:index, :show ] def index @episodes = Episode.find(:all) end # show, new, create, edit, update and destroy methods hidden.end
增加了before_filter
的控制器类。
before_filter
方法会在这个控制器除了index
和show
方法以外的任何一个方法调用之前被调用,并执行authorize
方法。我们在ApplicationController
中增加authorize
方法,以便其他控制器也能使用。
class ApplicationController < ActionController::Base helper_method :admin? protected def admin? false end def authorize unless admin? flash[:error] = “Unauthorized access” redirect_to home_path false end endend
增加了authorize
方法的ApplicationController
。
这个方法检查当前登录的用户是不是具有管理员权限,如果没有显示一个错误并且重定向到首页。这样就实现了直接访问新建文章页面后,被自动重定向回首页。当然,也可以抛出一个404 (找不到页面)错误,让未授权用户有所感知。
#TODO:
admin?
方法还需要补充业务逻辑。下一篇介绍。
作者授权:Your welcome to post the translated text on your blog as well if the episode is free(not Pro). I just ask that you post a link back to the original episode on railscasts.com.
原文链接:http://railscasts.com/episodes/20-restricting-access
- RailsCasts中文版,#20 Restricting Access 为页面增加权限校验2
- RailsCasts中文版,#20 Restricting Access 为页面增加权限校验2
- RailsCasts中文版,#19 Where Administration Goes 为页面增加权限校验1
- RailsCasts中文版,#19 Where Administration Goes 为页面增加权限校验1
- RailsCasts20 Restricting Access 限制访问权限
- RailsCasts中文版,#7 All About Layouts 页面关联布局文件
- CXF-DOSGI为webservice增加用户名密码权限校验
- Restricting Access with Basic Authentication
- RailsCasts 2
- RailsCasts中文版,#6 Shortcut Blocks with Symbol to_proc 通过to_proc为代码块逻辑命名
- RailsCasts中文版,#11 Refactoring User Name Part 2 重构实战2
- RailsCasts中文版,#2 Dynamic find_by Methods 使用动态的find_by方法进行查找操作
- RailsCasts中文版,#16 Virtual Attributes 虚拟属性
- RailsCasts中文版,#16 Virtual Attributes 虚拟属性
- RailsCasts中文版,#15 Fun with Find Conditions 使用hash为查询条件以便生成正确语法的SQL查询
- 为mediaserver增加定制权限
- js校验页面输入是否为空
- RailsCasts中文版,#1 Caching with Instance Variables 缓存实例变量
- 常数时间内求栈的最大值
- 表格中的一些字段随着另一些字段动态改变
- Ubuntu下安装Android Eclipse开发环境
- C++string 函数:持续更新
- struts入门例子
- RailsCasts中文版,#20 Restricting Access 为页面增加权限校验2
- [sicily online]1090. Highways
- 操作系统学习笔记五CPU 调度
- Ubuntu 12.04安装Netbeans7.2
- Topic Model
- 第三章 typedef
- ADS1.2 Error:(Fatal) L6002u:could not open file C:/...
- HDU 3143 Speedy Escape(最短路+二分+搜索)
- Nulls Indicate Absence of Value【每日一译】--20121126