2011-10-13 16:55:03 +01:00
|
|
|
class Role < ActiveRecord::Base
|
|
|
|
has_many :permissions
|
2011-10-23 22:39:44 +01:00
|
|
|
has_many :rights, :through => :permissions
|
2011-10-19 21:19:45 +01:00
|
|
|
has_many :relations, :through => :role_lines
|
2011-10-23 22:39:44 +01:00
|
|
|
|
|
|
|
serialize :can_see, Hash
|
|
|
|
|
|
|
|
validate :name, :presence => true
|
2011-10-26 01:33:25 +01:00
|
|
|
|
|
|
|
def to_dump
|
|
|
|
tmp = attributes.reject {|k,v| ['created_at', 'updated_at'].include? k}
|
|
|
|
tmp['rights'] = rights.inject({}) do |h, right|
|
|
|
|
h[right.controller] ||= []
|
|
|
|
h[right.controller] << right.action
|
|
|
|
h[right.controller].uniq!
|
|
|
|
h
|
|
|
|
end
|
|
|
|
return tmp
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
def save_dump filename = 'config/roles.yml'
|
|
|
|
fn = File.expand_path filename
|
2011-10-26 10:31:58 +01:00
|
|
|
File.open(fn, 'w'){|f| f.puts dump_roles}
|
2011-10-26 01:33:25 +01:00
|
|
|
return filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def dump_roles
|
|
|
|
roles = Role.find(:all, :include => :rights)
|
|
|
|
roles = roles.map(&:to_dump)
|
|
|
|
return {:Roles => roles}.to_yaml
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def all_from_dump! dump_hash
|
|
|
|
arr = []
|
|
|
|
dump_hash[:Roles].each do |role|
|
|
|
|
arr << from_dump!(role)
|
|
|
|
end
|
|
|
|
arr
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_from_dump dump_hash
|
|
|
|
arr = []
|
|
|
|
dump_hash[:Roles].each do |role|
|
|
|
|
arr << from_dump(role)
|
|
|
|
end
|
|
|
|
arr
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_dump! fields
|
|
|
|
from_dump(fields).save
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_dump fields
|
|
|
|
rights = fields.delete('rights')
|
|
|
|
a = find(fields['id']) || new
|
|
|
|
a.rights = []
|
|
|
|
a.attributes = fields
|
|
|
|
rights.each do |con, acts|
|
|
|
|
acts.each do |act|
|
|
|
|
unless r = Right.where(:controller => con, :action => act)
|
|
|
|
r = Right.create(:controller => con, :action => act)
|
|
|
|
end
|
|
|
|
a.rights << r
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
end
|
2011-10-13 16:55:03 +01:00
|
|
|
end
|