繁体   English   中英

添加多个级别的用户访问

[英]adding multiple levels of user access

我试图在我的应用程序中添加多个级别的用户,这就是我可以想到的想法:

class ApplicationController < ActionController::Base

 before_filter :setup , :authorize
protect_from_forgery

 def setup
 @minimum_permission = "admin"
end  

def authorize
  perm = { admin: 3 , moderator: 2 ,  reader: 1 }
  perm.default = 0
  if    User.find_by_id(session[:user_id])
        unless perm[session[:user_permission].to_s.to_sym] >= perm[@minimum_permission.to_sym]
            redirect_to global_login_url, notice: "you need to have "+@minimum_permission+" privileges to access this page"
        end
  else
        redirect_to global_login_url, notice: "Please log in"
  end
end
end

在我看来,我会做类似的事情:

class FieldsController < ApplicationController
skip_before_filter :authorize
# GET /fields
# GET /fields.json
def index
@minimum_permission = "moderator"
authorize
  @fields = Field.all   
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @fields }
  end
  end
....
end

这是我的会话控制器:

 class SessionsController < ApplicationController
 skip_before_filter :authorize
  def new
  end

 def create

user = User.find_by_cpf_no(params[:cpf_no])
if user and user.authenticate(params[:password])
  session[:user_id] = user.id
  session[:user_permission] = user.permission
  session[:user_name] = user.name
  redirect_to fields_url, alert: "successfully logged in"
else
  redirect_to fields_path, alert: "Invalid user/password combination"
end
 end

 def destroy
session[:user_id] = nil
session[:user_permission] = nil
session[:user_name] = nil
redirect_to root_path, notice: "Logged out"
 end
end

这对于登录的人来说很好用! 但是当我尝试不登录而访问索引时,出现此错误:

“在此操作中多次调用了渲染和/或重定向。请注意,您只能调用渲染或重定向,每个操作最多只能调用一次。还要注意,重定向和渲染都不会终止该操作的执行,因此,如果要重定向后退出操作,您需要执行“ redirect_to(...)并返回”之类的操作。

我怎样才能解决这个问题? 还有没有更好的方法来处理多个级别的用户? 谢谢

那里有很棒的库,这些库应该可以立即满足您的需求。 看一下康康舞 在cancan的Wiki中,您还可以找到基于角色的授权条目。

因此,在某个地方,您有一个前置过滤器或其他重定向功能可以将您发送到周围。 日志应该至少给了您尝试重定向的行之一。 首先,我要检查global_login_url(不是路径?)正在跳过所有过滤器,并且func在渲染后退出,并且在以后没有击中其他函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM