新手问题 管理系统用户和子用户的设计

stephen · December 05, 2013 · Last by mimosa replied at December 08, 2013 · 3391 hits

类似淘宝的,一间店铺有一个主账号,多个管理子账号, 这种有什么好实现方法吗? 是不是账号表自关联?

场景: 一个用户建立多件店铺,列如建立 A,B,C 三间,他可以设置另外 5 个用户 1,2,3,4,5 帮忙管理,例如: 1,2 用户管理 A 店铺,3 用户管理 B 店铺,4,5 用户管理 C 店铺。

参考下 devise 中的这篇文章,感觉应用场景很像。 https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain

@chunlea 好像很复杂,很恐惧!

# -*- encoding: utf-8 -*-

class User
  include Mongoid::Document
  include Store

  store_in collection: 'users'

  # Referenced
  belongs_to :ownable,   polymorphic: true, index: true # 店长
  has_many   :shops,     foreign_key: 'nick' # 店铺
  has_many   :employees, foreign_key: 'seller_nick' # 店铺
# -*- encoding: utf-8 -*-

class Employee # 伙计
  include Mongoid::Document
  # Referenced
  belongs_to :account
  belongs_to :employee, class_name: 'Account', foreign_key: 'employee_id'
  belongs_to :seller,   class_name: 'User',    foreign_key: 'seller_nick'

  attr_accessor :email

  # Fields
  field :employee_id,   type: String
  field :employee_name, type: String
  field :seller_nick,   type: String
  field :role,          type: String
  field :_id,           type: String, default: -> { "#{seller_nick}:#{employee_name}" }

  # Validations
  validates_presence_of :seller_nick, :role

角色:

def title
    case role
      when 'admin'
       '店长'
      when 'op'
       '运营'
      when 'cs'
       '客服'
    end
  end
You need to Sign in before reply, if you don't have an account, please Sign up first.