diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 22c3b0ebb..1d8405888 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -51,7 +51,37 @@ class RolesController < ApplicationController flash[:notice] = t("flash.role.destroyed") 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]) diff --git a/app/models/role.rb b/app/models/role.rb index 25ad40798..b051b3b12 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -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 diff --git a/app/views/roles/index.html.haml b/app/views/roles/index.html.haml index 3462e5071..02a937873 100644 --- a/app/views/roles/index.html.haml +++ b/app/views/roles/index.html.haml @@ -7,24 +7,32 @@ %h2.title = t("layout.roles.list_header") .inner - -unless @roles.empty? - %table.table - %tr - %th.first ID - %th= t("activerecord.attributes.role.name") - %th.last   - - @roles.each do |role| - %tr{:class => cycle("odd", "even")} - %td - = role.id - %td - = link_to role.name, role_path(role) - %td.last - #{link_to t("layout.show"), role_path(role)} | #{link_to t("layout.edit"), edit_role_path(role)} | #{link_to t("layout.delete"), role_path(role), :method => :delete, :confirm => t("layout.roles.confirm_delete")} - .actions-bar.wat-cf - .actions - -else - .inner - %label.label Роли отсутствуют, - = link_to "создать новую роль", new_role_path + .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 + %th.first ID + %th= t("activerecord.attributes.role.name") + %th.last   + - @roles.each do |role| + %tr{:class => cycle("odd", "even")} + %td + = role.id + %td + = link_to role.name, role_path(role) + %td.last + #{link_to t("layout.show"), role_path(role)} | #{link_to t("layout.edit"), edit_role_path(role)} | #{link_to t("layout.delete"), role_path(role), :method => :delete, :confirm => t("layout.roles.confirm_delete")} + .actions-bar.wat-cf + .actions + -else + .inner + %label.label Роли отсутствуют, + = link_to "создать новую роль", new_role_path - content_for :sidebar, render(:partial => 'sidebar') diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 1e482863a..1e4011581 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -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: Список diff --git a/config/routes.rb b/config/routes.rb index ef2c9ba03..604ee67f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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