Added dump to yml and load from yml abilities for roles.
This commit is contained in:
parent
f15f2ce98d
commit
02a8a62f36
|
@ -52,6 +52,36 @@ class RolesController < ApplicationController
|
|||
redirect_to roles_path
|
||||
end
|
||||
|
||||
def get_dump
|
||||
file = Role.save_dump
|
||||
send_file file, :type => 'text/plain'
|
||||
end
|
||||
|
||||
def load_from_dump
|
||||
flag = true
|
||||
puts params.inspect
|
||||
puts File.extname(params[:file].original_filename)
|
||||
unless ['.yml', '.yaml'].include? File.extname(params[:file].original_filename)
|
||||
flash[:error] = t("layout.roles.wrong_file_type")
|
||||
flag = false
|
||||
end
|
||||
if flag
|
||||
t = YAML.load params[:file].tempfile
|
||||
unless t.is_a? Hash and t[:Roles]
|
||||
flash[:error] = t("layout.roles.wrong_file_format")
|
||||
flag = false
|
||||
else
|
||||
begin
|
||||
Role.all_from_dump! t
|
||||
flash[:notice] = t("layout.roles.successful_load")
|
||||
rescue
|
||||
flash[:error] = t("layout.roles.seeding_fail")
|
||||
end
|
||||
end
|
||||
end
|
||||
redirect_to :back
|
||||
end
|
||||
|
||||
protected
|
||||
def find_role
|
||||
@role = Role.find(params[:id])
|
||||
|
|
|
@ -6,4 +6,67 @@ class Role < ActiveRecord::Base
|
|||
serialize :can_see, Hash
|
||||
|
||||
validate :name, :presence => true
|
||||
|
||||
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
|
||||
File.open (fn, 'w'){|f| f.puts dump_roles}
|
||||
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
|
||||
end
|
||||
|
|
|
@ -7,6 +7,14 @@
|
|||
%h2.title
|
||||
= t("layout.roles.list_header")
|
||||
.inner
|
||||
.group
|
||||
= button_to t("layout.roles.get_dump"), {:controller => :roles, :action => :get_dump},
|
||||
{:style => 'float: left; margin-right: 30px'}
|
||||
= form_tag url_for(:controller => :roles, :action => :load_from_dump), :multipart => true do
|
||||
= label_tag :file, t("layout.roles.from_file") + ':'
|
||||
= file_field_tag :file
|
||||
= submit_tag t("layout.upload")
|
||||
%br
|
||||
-unless @roles.empty?
|
||||
%table.table
|
||||
%tr
|
||||
|
|
|
@ -21,6 +21,7 @@ ru:
|
|||
false_: Нет
|
||||
publish: Опубликовать
|
||||
add: Добавить
|
||||
upload: Загрузить
|
||||
|
||||
downloads:
|
||||
title: Статистика закачек пакетов
|
||||
|
@ -95,6 +96,15 @@ ru:
|
|||
|
||||
roles:
|
||||
confirm_delete: Вы уверены, что хотите удалить эту роль?
|
||||
list_header: Роли
|
||||
list: Список
|
||||
new: Создать
|
||||
get_dump: Скачать в YML
|
||||
from_file: Загрузить из файлa
|
||||
wrong_file_type: Неверный тип файла
|
||||
wrong_file_format: Неверный формат файла
|
||||
successful_load: Все роли успешно загружены
|
||||
seeding_fail: Ошибка при записи в базу
|
||||
|
||||
event_logs:
|
||||
list: Список
|
||||
|
|
|
@ -9,7 +9,13 @@ Rosa::Application.routes.draw do
|
|||
|
||||
resources :users
|
||||
|
||||
resources :roles
|
||||
resources :roles do
|
||||
collection do
|
||||
get 'get_dump'
|
||||
post 'get_dump'
|
||||
post 'load_from_dump'
|
||||
end
|
||||
end
|
||||
|
||||
resources :event_logs, :only => :index
|
||||
|
||||
|
|
Loading…
Reference in New Issue