数据库模型和关联解决方案

时间:2011-07-09 18:36:12

标签: ruby-on-rails ruby ruby-on-rails-3

我有那些模特

User, Developer, App, Permission
  1. 用户可以使用默认Permision
  2. 用户可以拥有不同应用程序的权限
  3. 用户可以安装多个应用程序
  4. 用户也可以是应用程序的开发者
  5. 开发人员仍然是用户,可以拥有所有用户的权限(安装applcation,默认权限,每个应用程序的权限)
  6. 直到现在我有:

    user.rb

    class User < ActiveRecord::Base
      has_many :apps
    end
    

    app.rb

    class App < ActiveRecord::Base
      has_many :permissions, :through => :app_permissions
    end
    

    permission.rb

    class Permission < ActiveRecord::Base
      belongs_to :app
    end
    

    app_permission.rb

    class AppPermission < ActiveRecord::Base
    end
    

    问题

    1. 如何区分用户? (常规,开发人员)使用CanCan或Rails STI或简单角色类是否更好?请证明为什么最好使用这三种解决方案中的任何一种。
    2. 创建Default_Permission模型以将应用程序权限与默认权限分开是否更好?
    3. 修改

      如果我遗漏任何信息,请询问。我希望看到一些不同的解决方案以及每种解决方案的工作原理。感谢

2 个答案:

答案 0 :(得分:1)

我会推荐以下内容:

Developer是User对象。在架构中使用is_developer布尔值区分开发人员与用户。这样可以更容易地保持用户/开发人员的集成(没有switch语句)。您可以添加命名范围以明确地查找开发人员:

class User < ActiveRecord::Base
  named_scope :regular_users, :conditions => { :is_developer => false }
  named_scope :developers, :conditios => { :is_developer => true }
  #you can then call User.regular_users or User.developers
end

或者,您可以将User / Developer作为多态关联工作。 E.g。

class Role < ActiveRecord::Base
  belongs_to :entity, :polymorphic => true #with entity_id / entity_type in your schema
end

这种方法的缺点是它会使你的代码更复杂,只需要很少的语义或零语义。


我并不真正理解默认权限的含义,但它似乎是一个逻辑问题而不是数据库。每个人都有默认权限吗?然后你可以在* after_create *上添加它,或者在编写你的逻辑时,假设它是真的(或由布尔标志控制)。以下代码将为每个用户创建一个权限,在创建后默认为true(对于现有用户,您可以通过hand / rake任务添加权限)。

class User < ActiveRecord::Base
  after_create :add_default_permission

  def add_default_permission
    Permission.default_permissions.each do |permission|
      self.app_permissions.create(:permission_id => permission.id)
    end
  end
end

对于default_permissions,我建议在权限表上使用* is_default * boolean。这样,您可以拥有多个默认权限(或稍后删除默认权限)。默认权限权限,无需区分对象模型。即。

class Permission < ActiveRecord::Base
  named_scope :default_permissions, :conditions => { :is_default => true }
end

最后,确保完整拼写出所有的ActiveRecord关联,即

class User < ActiveRecord::Base
  has_many :apps
  has_many :permissions, :through => :app_permissions, :as => :permissible #edited
end

class App < ActiveRecord::Base
  belongs_to :app_permission
  has_many :permissions, :through => :app_permissions, :as => :permissible #edited
end

class Permission < ActiveRecord::Base
  belongs_to :app_permissions
  belongs_to :permissible, :through => :app_permissions, :polymorphic => true #edited
end

class AppPermission < ActiveRecord::Base
  belongs_to :permissible, :polymorphic => true #edited
  belongs_to :app
end

当用户安装应用时:多态下编辑

Class User < ActiveRecord::Base
  def get_required_app(app)
    required_permissions = []
    app.permissions.each do |p|
      if self.permissions.find(:first, conditions => { :permission_id => p.id } ).nil?
        required_permissions.push p
      end
    end
    required_permissions
  end

  def install_app(app)
    req = required_permissions app
    return req if req.count > 0
    #add user app
  end
end

希望这可以帮助您解决问题,如果您需要任何其他信息,请告诉我。

答案 1 :(得分:0)

我无法从您的问题中了解您的具体业务需求是什么,但大多数人使用基于角色的模式进行权限管理。有关讨论和建议,请参阅this question