【问题标题】:Rails dynamic authorization interfaceRails 动态授权接口
【发布时间】:2018-10-20 09:12:42
【问题描述】:

我刚开始使用 Ruby on Rails 进行开发,我正在寻找一个动态授权插件,它使管理员能够将权限与角色相关联,并将角色与用户相关联。

我在 stackoverflow 和一些论坛上找到了一些帖子,在 railscasts.org 也找到了一些针对这个主题的播客,但它们都提到 acl9、declarative_authorization、Aegis、restful acl 甚至 Authlogic,这不是授权而是身份验证插入。其他的不提供所需的功能。

那么有人可以告诉我是否可以使用 ruby​​ on rails 设置 Web 界面来管理用户角色?

所以现在我使用以下迁移迁移了我的数据库。

class AddRolesAndRightsTables < ActiveRecord::Migration
  def self.up
    create_table :roles_users do |t|
      t.integer :role_id
      t.integer :user_id 
    end

    create_table :roles do |t|
      t.string :name
    end

    create_table :rights_roles do |t|
      t.integer :right_id 
      t.integer :role_id
    end

    create_table :rights do |t|
      t.string :name
      t.string :controller
      t.string :action
    end
  end

  def self.down
    drop_table :roles_users
    drop_table :roles
    drop_table :rights_roles
    drop_table :rights
  end
end

除了一些视图和控制器动作之外,我还向 ApplicationController 添加了以下动作。

def check_authorization
  user = User.find(session[:user])
  unless user.roles.detect do |role|
    role.rights.select do |right|
      right.action == action_name && right.controller == self.class.controller_path
    end
  end

  redirect_back_or user
  flash[:notice] = "You are not authorized to view the page you requested."
  return false
end

运行 Right.synchronize_with_controllers(请参阅 Wolfman-Blog 的博客帖子),我收到以下错误。

syntax error, unexpected $end, expecting kEND (line 17 in application_controller)

【问题讨论】:

    标签: ruby-on-rails dynamic authorization


    【解决方案1】:

    我会推荐 Ryan Bate 的 cancan。我花了一段时间才理解它是如何工作的,但它真的值得努力学习。

    在我的完整程序中,我可以为我的所有控制器使用一个简单的load_and_authorize_resource,并在这里和那里添加额外的位来完成它。

    希望authenticationauthorzation 之间也没有混淆。如果你想快速搭建一个管理界面,我建议Active Admin

    否则,使用 cancannamespace 管理控制器可能更灵活 =)

    更新

    要获得一个基于 cancan 的简单的基于角色的权限界面,您可能会在其中创建角色并为其分配权限。

    User belongs_to Role
    
    Role has_many Users
    Role has_and_belongs_to_many Permissions
    
    Permissions has_and_belongs_to_many Roles
    

    每个Permission 记录都定义了一个模型以及您可以对其执行的 RESTful 操作。

    如果你只需要定义一个Role,但它的权限是静态的,我建议你可以把它们写到cancan的Ability.rb中,去掉Permission这个模型。

    抱歉,我无法为您提供确切的代码,因为我不需要实现基于权限的模型。

    Cancan Railscast http://railscasts.com/episodes/192-authorization-with-cancan

    又一个很棒的康康舞教程 http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

    DB 中的技能:康康舞 https://github.com/ryanb/cancan/wiki/Abilities-in-Database

    【讨论】:

    • 因此可以对界面进行编程,以便管理员能够在不更改代码的情况下创建新角色并分配权限?
    • Yes.Cancan 和大多数其他授权 gem 可能不会干扰您实现权限的方式。我会用简短的​​ sn-p n 链接更新我的答案。
    • 还没有,因为我在实现方面还有一些问题。我决定不使用康康酱,并采用了与你的类似的 Rails 食谱书中的方法。我还在blog.wolfman.com/articles/2006/5/20/… 找到了有关如何实现用户界面来管理角色的教程。问题是这个接口是使用 Rails 2.1 编写的,而我使用的是 Rails 3.2.2,我在调整代码时遇到了很多问题。目前我在使用 check_authorization 方法时遇到错误。我将代码添加到我的帖子中。
    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多