Browse Source

Tried to add model "account"

Dmitry Yu Okunev 3 years ago
parent
commit
8156d69e06

+ 6 - 0
app/assets/stylesheets/application.css

@@ -88,6 +88,12 @@ ul li {
 	padding:	15px;
 }
 
+#content a {
+	color: #06c;
+	text-decoration: none;
+	border-bottom: 1px dotted #06c;
+}
+
 #header {
 	color:		#fff;
 	top:		0;

+ 7 - 2
app/controllers/application_controller.rb

@@ -6,7 +6,6 @@ class ApplicationController < ActionController::Base
 	before_action :determine_user
 
 	def determine_user
-
 		if session[:cas].respond_to?(:with_indifferent_access)
 			session[:cas] = session[:cas].with_indifferent_access
 		end
@@ -17,8 +16,14 @@ class ApplicationController < ActionController::Base
 		end
 
 		@user = session[:cas]
+		return if @user.nil?
+
+		@user["user"].downcase unless @user.nil?
 
-		@user["user"] = @user["user"].downcase unless @user.nil?
+		@primary_account = Account.find_by(:login => @user["user"])
+		if @primary_account.nil? 
+			@primary_account = Account.create(:pulled => 0, :isused => false, :login => @user["user"], :password => nil, :issue => nil, :comment => 'Auto-created by the website', :owner => @user["user"], :ipaddr => Account.GetFreeIPAddr)
+		end
 	end
 
 	def common

+ 5 - 0
app/controllers/vpn_controller.rb

@@ -1,2 +1,7 @@
 class VpnController < ApplicationController
+	def openvpn
+		return if @user.nil?
+		@accounts = Account.find_by(:owner => @user["user"])
+		@accounts = Array.new if @accounts.nil?
+	end
 end

+ 22 - 0
app/models/account.rb

@@ -0,0 +1,22 @@
+class Account < ApplicationRecord
+	def self.GetFreeIPAddr
+		ipaddr_busy = Hash.new
+
+		Account.all.each do |account|
+			ipaddr_busy[account.ipaddr] = true
+		end
+
+		require 'ipaddr'
+
+		first = IPAddr.new("192.168.127.0").to_i
+		last  = IPAddr.new("192.168.255.254").to_i
+
+		cur = first
+		while not ipaddr_busy[cur].nil? do
+			cur += 1
+			throw :noFreeIPAddresses
+		end
+
+		return cur
+	end
+end

+ 5 - 4
app/views/layouts/application.html.erb

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 	<html>
 	<head>
-		<title>Соответствие: ОК &lt;-&gt; ОФЗ</title>
+		<title>VPN-сервис НИЯУ МИФИ</title>
 		<meta name="viewport" content="width=device-width">
 		<%= csrf_meta_tags %>
 
@@ -32,11 +32,12 @@
 
 		<div id='navigation' class='transition'>
 			<ul class='transition'>
-					<li><a href='/vpn/about'>О сервисе</a></li>
+					<li><a href='/'>О сервисе</a></li>
 					<li><a href='/vpn/linux'>Инструкция для Linux</a></li>
-					<li><a href='/vpn/windows'>Инструкция для Windows</a></li>
+					<li><a href='http://ut.mephi.ru/services/vpn-mephi-ru/freebsd'>Инструкция для FreeBSD</a></li>
+					<li><a href='http://ut.mephi.ru/services/vpn-mephi-ru/win7'>Инструкция для Windows</a></li>
 					<li><a href='/vpn/macos'>Инструкция для MacOS</a></li>
-					<li><a href='/vpn/openvpn'>Активировать OpenVPN</a></li>
+					<li><a href='/vpn/openvpn'>Настройка OpenVPN</a></li>
 			</ul>
 		</div>
 

File diff suppressed because it is too large
+ 9 - 0
app/views/vpn/about.html.erb


+ 6 - 0
app/views/vpn/linux.html.erb

@@ -0,0 +1,6 @@
+<h1>Инструкция для Linux</h1>
+<ul>
+	<li><a href='http://ut.mephi.ru/services/vpn-mephi-ru/debian'>Debian/Wheezy</a></li>
+	<li><a href='http://ut.mephi.ru/services/vpn-mephi-ru/fedora'>Fedora</a></li>
+	<li><a href='http://ut.mephi.ru/services/vpn-mephi-ru/gentoo'>Gentoo</a></li>
+</ul>

+ 2 - 0
app/views/vpn/macos.html.erb

@@ -0,0 +1,2 @@
+<h1>Инструкция для MacOS</h1>
+Инструкция отсутствует.

+ 10 - 0
app/views/vpn/openvpn.html.erb

@@ -0,0 +1,10 @@
+<h1>Настройка OpenVPN</h1>
+<% if @accounts.nil? %>
+	Вам необходимо авторизоваться для управления учётными записями.
+<% else %>
+	<table>
+		<% @accounts.each do |account| %>
+			<tr><td></td></tr>
+		<% end %>
+	</table>
+<% end %>

app/views/vpn/help.html.erb → app/views/vpn/windows.html.erb


+ 6 - 1
config/routes.rb

@@ -1,4 +1,9 @@
 Rails.application.routes.draw do
-	root 'vpn#help'
+	get 'vpn/linux'
+	get 'vpn/windows'
+	get 'vpn/macos'
+	get 'vpn/openvpn'
+
+	root 'vpn#about'
 	# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
 end

+ 25 - 0
db/migrate/20161111073417_create_accounts.rb

@@ -0,0 +1,25 @@
+class CreateAccounts < ActiveRecord::Migration[5.0]
+	def change
+		create_table :accounts do |t|
+			t.integer :ipaddr, :unsigned => true, :limit => 8
+			t.integer :pulled
+			t.boolean :isused
+			t.string :login
+			t.string :password
+			t.integer :issue
+			t.string :comment
+			t.string :owner
+
+			t.timestamps
+
+			t.index :ipaddr
+			t.index :login
+			t.index :password
+			t.index :issue
+			t.index :pulled
+			t.index :isused
+			t.index :created_at
+			t.index :updated_at
+		end
+	end
+end

+ 23 - 1
db/schema.rb

@@ -10,7 +10,28 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20161012183128) do
+ActiveRecord::Schema.define(version: 20161111073417) do
+
+  create_table "accounts", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
+    t.bigint   "ipaddr",                  unsigned: true
+    t.integer  "pulled"
+    t.boolean  "isused"
+    t.string   "login"
+    t.string   "password"
+    t.integer  "issue"
+    t.string   "comment"
+    t.string   "owner"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["created_at"], name: "index_accounts_on_created_at", using: :btree
+    t.index ["ipaddr"], name: "index_accounts_on_ipaddr", using: :btree
+    t.index ["issue"], name: "index_accounts_on_issue", using: :btree
+    t.index ["isused"], name: "index_accounts_on_isused", using: :btree
+    t.index ["login"], name: "index_accounts_on_login", using: :btree
+    t.index ["password"], name: "index_accounts_on_password", using: :btree
+    t.index ["pulled"], name: "index_accounts_on_pulled", using: :btree
+    t.index ["updated_at"], name: "index_accounts_on_updated_at", using: :btree
+  end
 
   create_table "ipaddrs", primary_key: "ipaddr", unsigned: true, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
     t.datetime "doc",    default: -> { "CURRENT_TIMESTAMP" }, null: false
@@ -74,6 +95,7 @@ ActiveRecord::Schema.define(version: 20161012183128) do
     t.string   "password"
     t.bigint   "issue"
     t.text     "comment",  limit: 65535
+    t.string   "owner"
     t.index ["password"], name: "password", using: :btree
     t.index ["username"], name: "username", unique: true, using: :btree
   end

+ 21 - 0
test/fixtures/accounts.yml

@@ -0,0 +1,21 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  ipaddr: 1
+  pulled: 1
+  isused: false
+  login: MyString
+  password: MyString
+  issue: 1
+  comment: MyString
+  owner: MyString
+
+two:
+  ipaddr: 1
+  pulled: 1
+  isused: false
+  login: MyString
+  password: MyString
+  issue: 1
+  comment: MyString
+  owner: MyString

+ 7 - 0
test/models/account_test.rb

@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class AccountTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end