From 5f17928423b0ab7378ea7e60bb4f5ed5d80e51e8 Mon Sep 17 00:00:00 2001 From: santaux Date: Tue, 18 Oct 2011 17:47:17 +0400 Subject: [PATCH] [refs #1923] Add private repositories to project --- app/controllers/private_users_controller.rb | 17 ++++++++ app/controllers/privates_controller.rb | 32 ++++++++++++++ app/helpers/private_users_helper.rb | 2 + app/helpers/privates_helper.rb | 2 + app/models/private_user.rb | 21 +++++++++ app/views/platforms/show.html.haml | 1 + app/views/private_users/index.html.haml | 43 +++++++++++++++++++ config/locales/ru.yml | 11 +++++ config/routes.rb | 5 +++ .../20111017112255_create_private_users.rb | 14 ++++++ db/schema.rb | 20 ++++++++- .../private_users_controller_spec.rb | 5 +++ spec/controllers/privates_controller_spec.rb | 5 +++ spec/helpers/private_users_helper_spec.rb | 15 +++++++ spec/helpers/privates_helper_spec.rb | 15 +++++++ spec/models/private_user_spec.rb | 5 +++ 16 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 app/controllers/private_users_controller.rb create mode 100644 app/controllers/privates_controller.rb create mode 100644 app/helpers/private_users_helper.rb create mode 100644 app/helpers/privates_helper.rb create mode 100644 app/models/private_user.rb create mode 100644 app/views/private_users/index.html.haml create mode 100644 db/migrate/20111017112255_create_private_users.rb create mode 100644 spec/controllers/private_users_controller_spec.rb create mode 100644 spec/controllers/privates_controller_spec.rb create mode 100644 spec/helpers/private_users_helper_spec.rb create mode 100644 spec/helpers/privates_helper_spec.rb create mode 100644 spec/models/private_user_spec.rb diff --git a/app/controllers/private_users_controller.rb b/app/controllers/private_users_controller.rb new file mode 100644 index 000000000..0c8df627b --- /dev/null +++ b/app/controllers/private_users_controller.rb @@ -0,0 +1,17 @@ +class PrivateUsersController < ApplicationController + def index + @private_users = PrivateUser.where(:platform_id => params[:platform_id]).paginate :page => params[:page] + @platform = Platform.find(params[:platform_id]) + end + + def create + pair = PrivateUser.generate_pair(params[:platform_id]) + redirect_to platform_private_users_path(params[:platform_id]), + :notice => "Логин: #{ pair[:login] } Пароль: #{ pair[:pass] }" + end + + def destroy + PrivateUser.find(params[:id]).destroy + redirect_to platform_private_users_path(params[:platform_id]) + end +end \ No newline at end of file diff --git a/app/controllers/privates_controller.rb b/app/controllers/privates_controller.rb new file mode 100644 index 000000000..b014f491f --- /dev/null +++ b/app/controllers/privates_controller.rb @@ -0,0 +1,32 @@ +class PrivatesController < ApplicationController + require 'digest/sha2' + + before_filter :find_platform + before_filter :authenticate + + def show + file_name = "#{APP_CONFIG['private_repo_path']}/#{params[:platform_name]}/#{params[:file_path]}" + + if File.directory?(file_name) || !File.exists?(file_name) + render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404 + else + send_file file_name + end + end + + protected + + def authenticate + authenticate_or_request_with_http_basic do |username, password| + PrivateUser.exists?( + :login => username, + :password => Digest::SHA2.new.hexdigest(password), + :platform_id => @platform.try(:id) + ) + end + end + + def find_platform + @platform = Platform.find_by_name(params[:platform_name]) + end +end diff --git a/app/helpers/private_users_helper.rb b/app/helpers/private_users_helper.rb new file mode 100644 index 000000000..c8c63083d --- /dev/null +++ b/app/helpers/private_users_helper.rb @@ -0,0 +1,2 @@ +module PrivateUsersHelper +end diff --git a/app/helpers/privates_helper.rb b/app/helpers/privates_helper.rb new file mode 100644 index 000000000..d4e240ef7 --- /dev/null +++ b/app/helpers/privates_helper.rb @@ -0,0 +1,2 @@ +module PrivatesHelper +end diff --git a/app/models/private_user.rb b/app/models/private_user.rb new file mode 100644 index 000000000..dccdc96db --- /dev/null +++ b/app/models/private_user.rb @@ -0,0 +1,21 @@ +class PrivateUser < ActiveRecord::Base + require 'digest/sha2' + require 'active_support/secure_random' + + validate :login, :uniqueness => true + + class << self + def generate_pair(platform_id) + login = "login_#{ActiveSupport::SecureRandom.hex(16)}" + pass = "pass_#{ActiveSupport::SecureRandom.hex(16)}" + + PrivateUser.create( + :login => login, + :password => Digest::SHA2.new.hexdigest(pass), + :platform_id => platform_id + ) + + {:login => login, :pass => pass} + end + end +end \ No newline at end of file diff --git a/app/views/platforms/show.html.haml b/app/views/platforms/show.html.haml index c6b5c770c..6f440f32b 100644 --- a/app/views/platforms/show.html.haml +++ b/app/views/platforms/show.html.haml @@ -4,6 +4,7 @@ %li.first= link_to t("layout.platforms.list"), platforms_path %li= link_to t("layout.platforms.new"), new_platform_path %li.active= link_to t("layout.platforms.show"), platform_path + %li= link_to t("layout.platforms.private_users"), platform_private_users_path(@platform) .content .inner %p diff --git a/app/views/private_users/index.html.haml b/app/views/private_users/index.html.haml new file mode 100644 index 000000000..d43dd15d2 --- /dev/null +++ b/app/views/private_users/index.html.haml @@ -0,0 +1,43 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first=# link_to t("layout.repositories.list"), platform_path(@platform) + "#repositories" + %li=# link_to t("layout.repositories.new"), new_platform_repository_path(@platform) + %li.active=# link_to t("layout.repositories.show"), platform_repository_path(@platform, @repository) + .content + .inner + %p + %b + = t("activerecord.attributes.platform.name") + \: + = @platform.name + %p + %b + = t("activerecord.attributes.repository.platform") + \: + = link_to @platform.name, platform_path(@platform) + .wat-cf + =# link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), platform_repository_path(@platform, @repository), :method => "delete", :class => "button", :confirm => t("layout.repositories.confirm_delete") + +.block + .secondary-navigation + %ul.wat-cf + %li.first.active= link_to t("layout.private_users.list"), platform_private_users_path(@platform) + %li= link_to t("layout.private_users.new"), platform_private_users_path(@platform), :method => :post + .content + %h2.title + = t("layout.private_users.list_header") + .inner + %table.table + %tr + %th.first= t("activerecord.attributes.private_user.login") + %th.last   + - @private_users.each do |private_user| + %tr{:class => cycle("odd", "even")} + %td + = link_to private_user.login, platform_private_user_path(@platform, private_user) + %td.last + #{link_to t("layout.delete"), platform_private_user_path(@platform, private_user), :method => :delete, :confirm => t("layout.private_users.confirm_delete")} + .actions-bar.wat-cf + .actions + = will_paginate @private_users \ No newline at end of file diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 6711ed015..1caa01f33 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -37,6 +37,12 @@ ru: sessions: sign_in_header: Вход в систему + private_users: + list: Список + new: Новая пара + list_header: Пары логин/пароль + confirm_delete: Вы уверены, что хотите удалить эту пару логин/пароль? + platforms: list: Список new: Создать @@ -57,6 +63,7 @@ ru: released_suffix: (выпущена) confirm_delete: Вы уверены, что хотите удалить эту платформу? current_platform_header: Текущая платформа + private_users: Пользователи приватного репозитория repositories: list: Список @@ -230,6 +237,10 @@ ru: build_list_item: Элемент сборочного листа attributes: + private_user: + login: Логин + password: Пароль + repository: name: Название platform_id: Платформа diff --git a/config/routes.rb b/config/routes.rb index fe44ff666..44e8fb4b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,12 @@ Rosa::Application.routes.draw do end resources :users + #resources :privates + match '/private/:platform_name/*file_path' => 'privates#show' + resources :platforms do + resources :private_users + member do get 'freeze' get 'unfreeze' diff --git a/db/migrate/20111017112255_create_private_users.rb b/db/migrate/20111017112255_create_private_users.rb new file mode 100644 index 000000000..cb7eb9700 --- /dev/null +++ b/db/migrate/20111017112255_create_private_users.rb @@ -0,0 +1,14 @@ +class CreatePrivateUsers < ActiveRecord::Migration + def self.up + create_table :private_users do |t| + t.integer :platform_id + t.string :login + t.string :password + t.timestamps + end + end + + def self.down + drop_table :private_users + end +end diff --git a/db/schema.rb b/db/schema.rb index 0e1886e2d..2d61dbcb4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111011200645) do +ActiveRecord::Schema.define(:version => 20111017112255) do create_table "arches", :force => true do |t| t.string "name", :null => false @@ -84,6 +84,16 @@ ActiveRecord::Schema.define(:version => 20111011200645) do add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + create_table "downloads", :force => true do |t| + t.string "name", :null => false + t.string "version" + t.string "distro" + t.string "platform" + t.integer "counter", :default => 0 + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "platforms", :force => true do |t| t.string "name" t.string "unixname" @@ -93,6 +103,14 @@ ActiveRecord::Schema.define(:version => 20111011200645) do t.boolean "released", :default => false end + create_table "private_users", :force => true do |t| + t.integer "platform_id" + t.string "login" + t.string "password" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "products", :force => true do |t| t.string "name", :null => false t.integer "platform_id", :null => false diff --git a/spec/controllers/private_users_controller_spec.rb b/spec/controllers/private_users_controller_spec.rb new file mode 100644 index 000000000..54140eff5 --- /dev/null +++ b/spec/controllers/private_users_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe PrivateUsersController do + +end diff --git a/spec/controllers/privates_controller_spec.rb b/spec/controllers/privates_controller_spec.rb new file mode 100644 index 000000000..fe2affa24 --- /dev/null +++ b/spec/controllers/privates_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe PrivatesController do + +end diff --git a/spec/helpers/private_users_helper_spec.rb b/spec/helpers/private_users_helper_spec.rb new file mode 100644 index 000000000..2ac72aba5 --- /dev/null +++ b/spec/helpers/private_users_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the PrivateUsersHelper. For example: +# +# describe PrivateUsersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe PrivateUsersHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/privates_helper_spec.rb b/spec/helpers/privates_helper_spec.rb new file mode 100644 index 000000000..3a17aad73 --- /dev/null +++ b/spec/helpers/privates_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the PrivatesHelper. For example: +# +# describe PrivatesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe PrivatesHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/private_user_spec.rb b/spec/models/private_user_spec.rb new file mode 100644 index 000000000..afbac1006 --- /dev/null +++ b/spec/models/private_user_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe PrivateUser do + pending "add some examples to (or delete) #{__FILE__}" +end