Moved card stuff from users model to new card model

This commit is contained in:
Will Bradley 2012-10-14 05:46:19 -07:00
parent ccd432a67d
commit 0254f9aa2c
27 changed files with 384 additions and 332 deletions

View File

@ -0,0 +1,109 @@
class CardsController < ApplicationController
load_and_authorize_resource
before_filter :authenticate_user!
# GET /cards
# GET /cards.json
def index
#@cards = Card.all
#authorize! :read, @cards
@cards = @cards.sort_by{|e| e[:id]}
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @cards }
end
end
# GET /cards/1
# GET /cards/1.json
def show
#@card = Card.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @card }
end
end
# PUT /cards/1/upload
def upload
#@card = Card.find(params[:id])
@upload_result = @card.upload_to_door
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @upload_result }
end
end
# PUT /cards/upload_all
def upload_all
@upload_result = Card.upload_all_to_door
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @upload_result }
end
end
# GET /cards/new
# GET /cards/new.json
def new
#@card = Card.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @card }
end
end
# GET /cards/1/edit
def edit
#@card = Card.find(params[:id])
end
# POST /cards
# POST /cards.json
def create
#@card = Card.new(params[:card])
respond_to do |format|
if @card.save
format.html { redirect_to cards_url, :notice => 'Card was successfully created.' }
format.json { render :json => @card, :status => :created, :location => @card }
else
format.html { render :action => "new" }
format.json { render :json => @card.errors, :status => :unprocessable_entity }
end
end
end
# PUT /cards/1
# PUT /cards/1.json
def update
#@card = Card.find(params[:id])
respond_to do |format|
if @card.update_attributes(params[:card])
format.html { redirect_to cards_url, :notice => 'Card was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @card.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /cards/1
# DELETE /cards/1.json
def destroy
#@card = Card.find(params[:id])
@card.destroy
respond_to do |format|
format.html { redirect_to cards_url, :notice => 'Card successfully deleted.' }
format.json { head :no_content }
end
end
end

View File

@ -1,109 +0,0 @@
class UsersController < ApplicationController
load_and_authorize_resource
before_filter :authenticate_user!
# GET /users
# GET /users.json
def index
#@users = User.all
#authorize! :read, @users
@users = @users.sort_by{|e| e[:card_id]}
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @users }
end
end
# GET /users/1
# GET /users/1.json
def show
#@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @user }
end
end
# PUT /users/1/upload
def upload
#@user = User.find(params[:id])
@upload_result = @user.upload_to_door
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @upload_result }
end
end
# PUT /users/upload_all
def upload_all
@upload_result = User.upload_all_to_door
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @upload_result }
end
end
# GET /users/new
# GET /users/new.json
def new
#@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @user }
end
end
# GET /users/1/edit
def edit
#@user = User.find(params[:id])
end
# POST /users
# POST /users.json
def create
#@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to users_url, :notice => 'User was successfully created.' }
format.json { render :json => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.json
def update
#@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to users_url, :notice => 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, :notice => 'User successfully deleted.' }
format.json { head :no_content }
end
end
end

77
app/models/card.rb Normal file
View File

@ -0,0 +1,77 @@
class Card < ActiveRecord::Base
require 'open-uri'
attr_accessible :id, :user_id, :name, :card_number, :card_permissions
validates_uniqueness_of :card_number
belongs_to :user
def upload_to_door
# load config values
door_access_url = APP_CONFIG['door_access_url']
door_access_password = APP_CONFIG['door_access_password']
# connect to door access system
source = open("#{door_access_url}?e=#{door_access_password}").read
results = source.scan(/authok/)
if(results.size > 0) then
#only continue if we've got an OK login
cardid = self.id.to_s.rjust(3, '0') #TODO: provide ability for
cardperm = self.card_permissions.to_s.rjust(3, '0')
cardnum = self.card_number.rjust(8, '0')
source = open("#{door_access_url}?m#{cardid}&p#{cardperm}&t#{cardnum}").read
results = source.scan(/cur/)
#logout
open("#{door_access_url}?e=0000")
if(results.size > 0) then
#only return true if we got some kind of decent response
return true
else
# We didn't get a decent response.
return false
end
else
# We didn't get an OK login.
return false
end
end
def self.upload_all_to_door
@cards = Card.all
@end_results = Array.new
# load config values
door_access_url = APP_CONFIG['door_access_url']
door_access_password = APP_CONFIG['door_access_password']
source = open("#{door_access_url}?e=#{door_access_password}").read
results = source.scan(/authok/)
if(results.size > 0) then
@cards.each do |u|
#only continue if we've got an OK login
cardid = u.id.to_s.rjust(3, '0')
cardperm = u.card_permissions.to_s.rjust(3, '0')
cardnum = u.card_number.rjust(8, '0')
source = open("#{door_access_url}?m#{cardid}&p#{cardperm}&t#{cardnum}").read
results = source.scan(/cur/)
if(results.size > 0) then
#only return true if we got some kind of decent response
@end_results.push([cardid,"OK"])
else
@end_results.push([cardid,"FAIL"])
end
end
#logout
open("#{door_access_url}?e=0000")
else
@end_results.push([cardid,"FAIL"])
end
return @end_results
end
end

View File

@ -2,84 +2,11 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, # :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable # :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, # :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model # Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :admin
require 'open-uri'
attr_accessible :card_id, :card_number, :card_permissions, :name, :admin has_many :cards
validates_uniqueness_of :card_id, :card_number
def upload_to_door
# load config values
door_access_url = APP_CONFIG['door_access_url']
door_access_password = APP_CONFIG['door_access_password']
# connect to door access system
source = open("#{door_access_url}?e=#{door_access_password}").read
results = source.scan(/authok/)
if(results.size > 0) then
#only continue if we've got an OK login
usernum = self.card_id.to_s.rjust(3, '0')
userperm = self.card_permissions.to_s.rjust(3, '0')
cardnum = self.card_number.rjust(8, '0')
source = open("#{door_access_url}?m#{usernum}&p#{userperm}&t#{cardnum}").read
results = source.scan(/cur/)
#logout
open("#{door_access_url}?e=0000")
if(results.size > 0) then
#only return true if we got some kind of decent response
return true
else
# We didn't get a decent response.
return false
end
else
# We didn't get an OK login.
return false
end
end
def self.upload_all_to_door
@users = User.all
@end_results = Array.new
# load config values
door_access_url = APP_CONFIG['door_access_url']
door_access_password = APP_CONFIG['door_access_password']
source = open("#{door_access_url}?e=#{door_access_password}").read
results = source.scan(/authok/)
if(results.size > 0) then
@users.each do |u|
#only continue if we've got an OK login
usernum = u.card_id.to_s.rjust(3, '0')
userperm = u.card_permissions.to_s.rjust(3, '0')
cardnum = u.card_number.rjust(8, '0')
source = open("#{door_access_url}?m#{usernum}&p#{userperm}&t#{cardnum}").read
results = source.scan(/cur/)
if(results.size > 0) then
#only return true if we got some kind of decent response
@end_results.push([usernum,"OK"])
else
@end_results.push([usernum,"FAIL"])
end
end
#logout
open("#{door_access_url}?e=0000")
else
@end_results.push([usernum,"FAIL"])
end
return @end_results
end
end end

View File

@ -0,0 +1,37 @@
<%= form_for(@card) do |f| %>
<% if @card.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@card.errors.count, "error") %> prohibited this card from being saved:</h2>
<ul>
<% @card.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :user %><br />
<%= collection_select(:card, :user_id, User.all.sort_by(&:name), :id, :name) %>
</div>
<div class="field">
<%= f.label :name, "Card Note" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :id, "Card DB ID" %><br />
<%= f.number_field :id, :in => 10...201 %>
</div>
<div class="field">
<%= f.label :card_number, "Card Number" %><br />
<%= f.text_field :card_number %>
</div>
<div class="field">
<%= f.label :card_permissions %><br />
<%= f.select :card_permissions, [["Enabled",1],["Disabled",255]] %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing card</h1>
<%= render 'form' %>
<%= link_to 'Show', @card %> |
<%= link_to 'Back', cards_path %>

View File

@ -0,0 +1,34 @@
<h1>Listing cards</h1>
<%= link_to 'New Card', new_card_path %>
<%= link_to 'Upload all cards', upload_all_path %>
<table>
<tr>
<th>User</th>
<th>Note</th>
<th>DB ID</th>
<th>Card #</th>
<th>Access?</th>
<th></th>
<th></th>
<th></th>
</tr>
<% if !@cards.blank? %>
<% @cards.each do |card| %>
<tr>
<td><%= card.user.name %></td>
<td><%= card.name %></td>
<td><%= card.id %></td>
<td><%= card.card_number %></td>
<td><%= if card.card_permissions == 1 then "Access" end %></td>
<td><%= link_to 'Upload', upload_path(card) %></td>
<td><%= link_to 'Edit', edit_card_path(card) %></td>
<td><%= link_to 'Destroy', card, :confirm => 'Are you sure? WARNING: THIS DOES NOT REMOVE THE CARD FROM THE DOOR SYSTEM! DISABLE AND UPLOAD IT FIRST.', :method => :delete %></td>
</tr>
<% end %>
<% end %>
</table>
<br />

View File

@ -0,0 +1,5 @@
<h1>New card</h1>
<%= render 'form' %>
<%= link_to 'Back', cards_path %>

View File

@ -0,0 +1,24 @@
<p>
<b>Name:</b>
<%= @card.name %>
</p>
<p>
<b>Card DB ID:</b>
<%= @card.id %>
</p>
<p>
<b>Card Number:</b>
<%= @card.card_number %>
</p>
<p>
<b>Card Permissions:</b>
<%= @card.card_permissions %>
</p>
<%= link_to 'Upload to Door', upload_path(@card) %>
<%= link_to 'Edit', edit_card_path(@card) %> |
<%= link_to 'Back', cards_path %>

View File

@ -0,0 +1,14 @@
<% if @upload_result %>
<p>
<b>Upload result:</b>
<%= @card.name %> uploaded successfully.
</p>
<% else %>
<p>
<b>Upload result:</b>
Error uploading <%= @card.name %>.
</p>
<% end %>
<%= link_to 'Back', cards_path %>

View File

@ -9,4 +9,4 @@
</p> </p>
<%= link_to 'Back', users_path %> <%= link_to 'Back', cards_path %>

View File

@ -8,7 +8,7 @@
</head> </head>
<body> <body>
<div id="header"> <div id="header">
<%= link_to 'Users', users_path if user_signed_in? %> <%= link_to 'Cards', cards_path if user_signed_in? %>
<%= link_to 'Logs', door_logs_path if user_signed_in? %> <%= link_to 'Logs', door_logs_path if user_signed_in? %>
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete if user_signed_in? %> <%= link_to 'Sign out', destroy_user_session_path, :method => :delete if user_signed_in? %>
<%= link_to 'Sign in', new_user_session_path unless user_signed_in? %> <%= link_to 'Sign in', new_user_session_path unless user_signed_in? %>

View File

@ -1,55 +0,0 @@
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<% if @user.id.blank? || !params[:password].nil? %>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, :autocomplete => "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<% else %>
<div class="field">
<a href="?password=edit">Change Password</a>
</div>
<% end %>
<div class="field">
<%= f.label :card_id, "Card DB ID" %><br />
<%= f.number_field :card_id, :in => 10...201 %>
</div>
<div class="field">
<%= f.label :card_number, "Card Number" %><br />
<%= f.text_field :card_number %>
</div>
<div class="field">
<%= f.label :card_permissions %><br />
<%= f.select :card_permissions, [["Enabled",1],["Disabled",255]] %>
</div>
<div class="field">
<%= f.label :admin, "Admin?" %><br />
<%= f.check_box :admin %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -1,6 +0,0 @@
<h1>Editing user</h1>
<%= render 'form' %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>

View File

@ -1,34 +0,0 @@
<h1>Listing users</h1>
<%= link_to 'New User', new_user_path %>
<%= link_to 'Upload all users', upload_all_path %>
<table>
<tr>
<th>Name</th>
<th>Card DB ID</th>
<th>Card #</th>
<th>Access?</th>
<th>Admin?</th>
<th></th>
<th></th>
<th></th>
</tr>
<% if !@users.blank? %>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.card_id %></td>
<td><%= user.card_number %></td>
<td><%= if user.card_permissions == 1 then "Access" end %></td>
<td><%= if user.admin? then "Admin" end %></td>
<td><%= link_to 'Upload', upload_path(user) %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure? WARNING: THIS DOES NOT REMOVE THE USER FROM THE DOOR SYSTEM! DISABLE THEM FIRST.', :method => :delete %></td>
</tr>
<% end %>
<% end %>
</table>
<br />

View File

@ -1,5 +0,0 @@
<h1>New user</h1>
<%= render 'form' %>
<%= link_to 'Back', users_path %>

View File

@ -1,24 +0,0 @@
<p>
<b>Name:</b>
<%= @user.name %>
</p>
<p>
<b>Card DB ID:</b>
<%= @user.card_id %>
</p>
<p>
<b>Card Number:</b>
<%= @user.card_number %>
</p>
<p>
<b>Card Permissions:</b>
<%= @user.card_permissions %>
</p>
<%= link_to 'Upload to Door', upload_path(@user) %>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>

View File

@ -1,14 +0,0 @@
<% if @upload_result %>
<p>
<b>Upload result:</b>
<%= @user.name %> uploaded successfully.
</p>
<% else %>
<p>
<b>Upload result:</b>
Error uploading <%= @user.name %>.
</p>
<% end %>
<%= link_to 'Back', users_path %>

View File

@ -2,9 +2,9 @@ Dooraccess::Application.routes.draw do
devise_for :users devise_for :users
match 'users/upload_all' => 'users#upload_all', :as => :upload_all match 'cards/upload_all' => 'cards#upload_all', :as => :upload_all
resources :users resources :cards
match 'users/:id/upload' => 'users#upload', :as => :upload match 'cards/:id/upload' => 'cards#upload', :as => :upload
match 'door_logs' => 'door_logs#index', :as => :door_logs match 'door_logs' => 'door_logs#index', :as => :door_logs
match 'door_logs/download' => 'door_logs#download', :as => :download match 'door_logs/download' => 'door_logs#download', :as => :download

View File

@ -0,0 +1,10 @@
class CreateCards < ActiveRecord::Migration
def change
create_table :cards do |t|
t.string :card_number
t.integer :card_permissions
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
class AddUserIdToCards < ActiveRecord::Migration
def change
add_column :cards, :user_id, :integer
end
end

View File

@ -0,0 +1,5 @@
class AddNameToCards < ActiveRecord::Migration
def change
add_column :cards, :name, :string
end
end

View File

@ -0,0 +1,11 @@
class MoveCardDataToCards < ActiveRecord::Migration
def up
User.all.each do |u|
u.cards.create(:id => u.card_id, :name => u.name, :card_number => u.card_number, :card_permissions => u.card_permissions)
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -0,0 +1,13 @@
class RemoveCardFromUsers < ActiveRecord::Migration
def up
remove_column :users, :card_id
remove_column :users, :card_number
remove_column :users, :card_permissions
end
def down
add_column :users, :card_id, :integer
add_column :users, :card_number, :string
add_column :users, :card_permissions, :integer
end
end

View File

@ -10,7 +10,16 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120904024426) do ActiveRecord::Schema.define(:version => 20121014122548) do
create_table "cards", :force => true do |t|
t.string "card_number"
t.integer "card_permissions"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
t.string "name"
end
create_table "door_logs", :force => true do |t| create_table "door_logs", :force => true do |t|
t.string "key" t.string "key"
@ -21,9 +30,6 @@ ActiveRecord::Schema.define(:version => 20120904024426) do
create_table "users", :force => true do |t| create_table "users", :force => true do |t|
t.string "name" t.string "name"
t.integer "card_id"
t.string "card_number"
t.integer "card_permissions"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.string "email", :default => "", :null => false t.string "email", :default => "", :null => false

9
test/fixtures/cards.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
card_number: MyString
card_permissions: 1
two:
card_number: MyString
card_permissions: 1

7
test/unit/card_test.rb Normal file
View File

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